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_UDPRELAY_H__364B884F_8339_4C12_B294_2783EB7ADF79__INCLUDED_)
00037 #define AFX_UDPRELAY_H__364B884F_8339_4C12_B294_2783EB7ADF79__INCLUDED_
00038 
00039 #if _MSC_VER > 1000
00040 #pragma once
00041 #endif // _MSC_VER > 1000
00042 
00043 #include "Relay.h"
00044 
00045 #include "UDPSocketAsync.h"
00046 
00047 KOMODIA_NAMESPACE_START
00048 
00049 class CGenericCriticalSection;
00050 
00051 class CUDPRelay : public CRelay  
00052 {
00053 public:
00054     
00055     virtual BOOL Relay(const std::string& rBindAddress,
00056                        unsigned short usBindPort,
00057                        const std::string& rDestinationAddress,
00058                        unsigned short usDestinationPort);
00059     virtual BOOL Relay(IP aBindAddress,
00060                        unsigned short usBindPort,
00061                        IP aDestinationAddress,
00062                        unsigned short usDestinationPort);
00063 
00064     
00065     virtual BOOL Stop();
00066 
00067     
00068     CUDPRelay();
00069     virtual ~CUDPRelay();
00070 private:
00071     
00072     class CListenSocket : public CUDPSocketAsync
00073     {
00074     public:
00075         
00076         CListenSocket(CUDPRelay* pFather);
00077         virtual ~CListenSocket();
00078     protected:
00079         
00080         virtual BOOL OnSocketReceive(int iErrorCode);
00081 
00082         
00083         NO_OnSocketOOB
00084         NO_OnSocketWrite
00085         NO_OnSocketTimeout
00086         NO_OnSocketConnect
00087         NO_OnSocketClose
00088         NO_OnSocketAccept
00089     private:
00090         
00091         CUDPRelay* m_pFather;
00092     };
00093 
00094     friend CListenSocket;
00095 private:
00096     
00097     class CClientSocket : public CUDPSocketAsync
00098     {
00099     public:
00100         
00101         int SendData(const char* pBuffer,
00102                      unsigned long ulBufferSize);
00103 
00104         
00105         CClientSocket(CUDPRelay* pFather,
00106                       IP aSourceAddress,
00107                       unsigned short usSourcePort);
00108         virtual ~CClientSocket();
00109     protected:
00110         
00111         virtual BOOL OnSocketReceive(int iErrorCode);
00112 
00113         
00114         
00115         virtual BOOL OnSocketTimeout();
00116 
00117         
00118         NO_OnSocketOOB
00119         NO_OnSocketWrite
00120         NO_OnSocketConnect
00121         NO_OnSocketClose
00122         NO_OnSocketAccept
00123     private:
00124         
00125         CUDPRelay* m_pFather;
00126 
00127         
00128         IP m_aSourceAddress;
00129 
00130         
00131         unsigned short m_usSourcePort;
00132 
00133         
00134         unsigned short m_usLocalPort;
00135     };
00136 
00137     friend CClientSocket;
00138 private:
00139     
00140     typedef struct _ConnectionData
00141     {
00142         IP              aSourceIP;
00143         unsigned short  usSourcePort;
00144 
00145         
00146         bool operator<(const _ConnectionData& rData)const
00147         {
00148             if (aSourceIP==rData.aSourceIP)
00149                 return usSourcePort<rData.usSourcePort;
00150             else
00151                 return aSourceIP<rData.aSourceIP;
00152         }
00153     } ConnectionData;
00154 
00155     
00156     typedef std::map<ConnectionData,CClientSocket*> SocketMap;
00157 private:
00158     
00159     CUDPRelay(const CUDPRelay& rRelay);
00160 
00161     
00162     CUDPRelay& operator=(const CUDPRelay& rRelay);
00163 
00164     
00165     IP GetTarget()const;
00166 
00167     
00168     unsigned short GetTargetPort()const;
00169 
00170     
00171     unsigned short GetBindPort()const;
00172 
00173     
00174     void SendData(const char* pBuffer,
00175                   unsigned long ulBufferSize,
00176                   IP aDestinationIP,
00177                   unsigned short usDestinationPort);
00178 
00179     
00180     unsigned short AllocateLocalPort()const;
00181 
00182     
00183     CClientSocket* CreateSocket(IP aSourceAddress,
00184                                 unsigned short usSourcePort);
00185 
00186     
00187     void RemoveSocket(IP aSourceAddress,
00188                       unsigned short usSourcePort);
00189 
00190     
00191     CListenSocket* m_pSocket;
00192 
00193     
00194     CGenericCriticalSection* m_pCSection;
00195 
00196     
00197     CGenericCriticalSection* m_pCSectionPorts;
00198 
00199     
00200     BOOL m_bCreated;
00201 
00202     
00203     IP m_aTarget;
00204 
00205     
00206     unsigned short m_usTargetPort;
00207 
00208     
00209     unsigned short m_usBindPort;
00210 
00211     
00212     SocketMap m_aSocketMap;
00213 
00214     
00215     mutable unsigned short m_usLocalPort;
00216 };
00217 
00218 KOMODIA_NAMESPACE_END
00219 
00220 #endif // !defined(AFX_UDPRELAY_H__364B884F_8339_4C12_B294_2783EB7ADF79__INCLUDED_)