00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 #if !defined(AFX_TCPRELAY_H__C59C4BC6_68A9_427C_A399_A233B48C459E__INCLUDED_)
00037 #define AFX_TCPRELAY_H__C59C4BC6_68A9_427C_A399_A233B48C459E__INCLUDED_
00038 
00039 #if _MSC_VER > 1000
00040 #pragma once
00041 #endif // _MSC_VER > 1000
00042 
00043 #include "Relay.h"
00044 
00045 #include "TCPSocketAsync.h"
00046 
00047 #include <map>
00048 
00049 KOMODIA_NAMESPACE_START
00050 
00051 class CGenericCriticalSection;
00052 
00053 class CTCPRelay : public CRelay
00054 {
00055 public:
00056     
00057     typedef unsigned short ConnectionID;
00058 public:
00059     
00060     BOOL IsRunning()const;
00061 
00062     
00063     virtual BOOL Relay(const std::string& rBindAddress,
00064                        unsigned short usBindPort,
00065                        const std::string& rDestinationAddress,
00066                        unsigned short usDestinationPort);
00067     virtual BOOL Relay(IP aBindAddress,
00068                        unsigned short usBindPort,
00069                        IP aDestinationAddress,
00070                        unsigned short usDestinationPort);
00071 
00072     
00073     virtual BOOL Stop();
00074 
00075     
00076     CTCPRelay();
00077     virtual ~CTCPRelay();
00078 protected:
00079     
00080     virtual std::string ModifyReceiveString(BOOL bIncoming,
00081                                             const char* pBuffer,
00082                                             unsigned short usBufferSize)const;
00083 
00084     
00085     IP GetBindAddress()const;
00086 
00087     
00088     IP GetTarget()const;
00089 private:
00090     
00091     class CAcceptSocket : public CTCPSocketAsync
00092     {
00093     public:
00094         
00095         CAcceptSocket(CTCPRelay* pFather);
00096         virtual ~CAcceptSocket();
00097     protected:
00098         
00099         virtual BOOL OnSocketAccept(int iErrorCode);
00100 
00101         
00102         NO_OnSocketOOB
00103         NO_OnSocketWrite
00104         NO_OnSocketTimeout
00105         NO_OnSocketConnect
00106         NO_OnSocketClose
00107         NO_OnSocketReceive
00108     private:
00109         
00110         CTCPRelay* m_pFather;
00111     };
00112 
00113     friend CAcceptSocket;
00114 private:
00115     
00116     class CClientSocket : public CTCPSocketAsync
00117     {
00118     public:
00119         
00120         virtual int Send(const char* pBuffer,
00121                          unsigned long ulBufferLength);
00122 
00123         
00124         void Stop();
00125 
00126         
00127         void SetConnectionID(ConnectionID aConnectionID);
00128 
00129         
00130         void Connect();
00131 
00132         
00133         void SetSocket(CClientSocket* pSocket);
00134 
00135         
00136         CClientSocket(CTCPRelay* pFather,
00137                       CClientSocket* pSocket);
00138         virtual ~CClientSocket();
00139     protected:
00140         
00141         virtual BOOL OnSocketConnect(int iErrorCode);
00142 
00143         
00144         virtual BOOL OnSocketClose(int iErrorCode);
00145 
00146         
00147         virtual BOOL OnSocketReceive(int iErrorCode);
00148 
00149         
00150         
00151         virtual BOOL OnSocketTimeout();
00152 
00153         
00154         NO_OnSocketAccept
00155         NO_OnSocketOOB
00156         NO_OnSocketWrite
00157     private:
00158         
00159         CTCPRelay* m_pFather;
00160 
00161         
00162         CClientSocket* m_pSocket;
00163 
00164         
00165         ConnectionID m_aConnectionID;
00166 
00167         
00168         BOOL m_bIncoming;
00169 
00170         
00171         CGenericCriticalSection* m_pCSection;
00172 
00173         
00174         BOOL m_bEvent;
00175     };
00176 
00177     friend CClientSocket;
00178 private:
00179     
00180     typedef struct _SocketData
00181     {
00182         CClientSocket*  pIncomingConnection;
00183         CClientSocket*  pOutgoingConnection;
00184     } SocketData;
00185 
00186     
00187     typedef std::map<ConnectionID,SocketData> ClientMap;
00188 private:
00189     
00190     CTCPRelay(const CTCPRelay& rRelay);
00191 
00192     
00193     CTCPRelay& operator=(const CTCPRelay& rRelay);
00194 
00195     
00196     ConnectionID NewConnection(CClientSocket* pIncomingConnection,
00197                                CClientSocket* pOutgoingConnection);
00198 
00199     
00200     void RemoveConnection(ConnectionID aConnectionID);
00201 
00202     
00203     unsigned short GetTargetPort()const;
00204 
00205     
00206     CAcceptSocket* m_pSocket;
00207 
00208     
00209     ClientMap m_aClientMap;
00210 
00211     
00212     CGenericCriticalSection* m_pCSection;
00213 
00214     
00215     IP m_aBindAddress;
00216 
00217     
00218     IP m_aTarget;
00219 
00220     
00221     unsigned short m_usTargetPort;
00222 
00223     
00224     ConnectionID m_aRunningID;
00225 
00226     
00227     BOOL m_bCreated;
00228 };
00229 
00230 KOMODIA_NAMESPACE_END
00231 
00232 #endif // !defined(AFX_TCPRELAY_H__C59C4BC6_68A9_427C_A399_A233B48C459E__INCLUDED_)