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_TCPSOCKETASYNC_H__1D78325D_C154_4B21_97E5_320394FD47EF__INCLUDED_)
00037 #define AFX_TCPSOCKETASYNC_H__1D78325D_C154_4B21_97E5_320394FD47EF__INCLUDED_
00038
00039 #if _MSC_VER > 1000
00040 #pragma once
00041 #endif // _MSC_VER > 1000
00042
00043 #include "TCPSocket.h"
00044 #include "AsyncSocket.h"
00045
00046 #include <vector>
00047
00048 KOMODIA_NAMESPACE_START
00049
00050 typedef enum _TCPSocketAsyncErrors
00051 {
00052 teConnectionTimeout=-100
00053 } TCPSocketAsyncErrors;
00054
00055 class CTCPSocketAsync : public CTCPSocket,
00056 public CAsyncSocket
00057 {
00058 friend class CTCPSocketAsyncDelegator;
00059 public:
00060 using CTCPSocket::Bind;
00061 public:
00062
00063 virtual SOCKET Detach();
00064
00065
00066 virtual BOOL Accept(CTCPSocket* pNewSocket);
00067
00068
00069 virtual BOOL Listen(unsigned long ulBackLog=5);
00070
00071
00072 virtual IP GetBindAddress()const;
00073
00074
00075 virtual unsigned short GetBindPort()const;
00076
00077
00078 virtual BOOL Bind(IP aSourceAddress,
00079 unsigned short usPort);
00080
00081
00082 BOOL SetConnectionTimeout(unsigned long ulMS);
00083 unsigned long GetConnectionTimeout()const;
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 virtual int Send(const char* pBuffer,
00094 unsigned long ulBufferLength);
00095
00096
00097 virtual BOOL Close();
00098
00099
00100
00101
00102
00103
00104
00105 virtual int Receive(char* pBuffer,
00106 unsigned long ulBufferLength);
00107
00108
00109
00110
00111
00112
00113 virtual int Peek(char* pBuffer,
00114 unsigned long ulBufferLength);
00115
00116
00117
00118
00119
00120
00121 virtual BOOL Connect(IP aDestinationAddress,
00122 unsigned short usDestinationPort,
00123 BOOL bDisableAsync=FALSE,
00124 BOOL bForceErrorEvent=FALSE);
00125 virtual BOOL Connect(const std::string& rDestinationAddress,
00126 unsigned short usDestinationPort,
00127 BOOL bDisableAsync=FALSE,
00128 BOOL bForceErrorEvent=FALSE);
00129
00130
00131 virtual BOOL Connect(unsigned short usSourcePort,
00132 IP aDestinationAddress,
00133 unsigned short usDestinationPort,
00134 BOOL bDisableAsync=FALSE,
00135 BOOL bForceErrorEvent=FALSE);
00136 virtual BOOL Connect(unsigned short usSourcePort,
00137 const std::string& rDestinationAddress,
00138 unsigned short usDestinationPort,
00139 BOOL bDisableAsync=FALSE,
00140 BOOL bForceErrorEvent=FALSE);
00141
00142
00143 virtual BOOL Create();
00144
00145
00146 CTCPSocketAsync(BOOL bRawSocket=false);
00147 virtual ~CTCPSocketAsync();
00148 public:
00149
00150 class CTCPSocketOverider
00151 {
00152 public:
00153
00154 typedef std::vector<CTCPSocketAsync*> SelectiveVector;
00155 public:
00156
00157 virtual BOOL OverideSocket(CTCPSocketAsync* pSocket)=0;
00158
00159
00160 virtual BOOL DoesSupportSelectiveOveriding()const
00161 {
00162 return FALSE;
00163 }
00164
00165
00166 virtual CTCPSocketOverider* GetSelectiveOveride(const SelectiveVector& rVector)const
00167 {
00168 return NULL;
00169 }
00170
00171
00172 CTCPSocketOverider()
00173 {
00174 }
00175 virtual ~CTCPSocketOverider()
00176 {
00177 }
00178 };
00179 public:
00180
00181 class CTCPSocketOveridable
00182 {
00183 public:
00184
00185 virtual BOOL OverideSocket(CTCPSocketOverider* pOverdier)=0;
00186
00187
00188 CTCPSocketOveridable()
00189 {
00190 }
00191 virtual ~CTCPSocketOveridable()
00192 {
00193 }
00194 };
00195 public:
00196
00197
00198 void SetLinkedSocket(CTCPSocketAsync* pSocket,
00199 CTCPSocketOverider* pOverider);
00200
00201
00202 CTCPSocketOverider* GetOverider()const;
00203 operator const CTCPSocketOverider*()const;
00204 protected:
00205
00206 virtual int SendNoAdd(const char* pBuffer,
00207 unsigned long ulBufferLength);
00208
00209
00210 virtual BOOL IsAsyncClass()const;
00211
00212
00213 virtual BOOL SetAsync();
00214
00215
00216 virtual SOCKET GetAsyncHandle()const;
00217
00218
00219 virtual BOOL SendBlockedBuffer(const CBlockedBuffer::CBlockedData& rData);
00220
00221
00222 virtual BOOL SocketConnected(int iErrorCode,
00223 BOOL bNoEvent=FALSE);
00224
00225
00226 virtual BOOL SocketReceive(int iErrorCode,
00227 BOOL bNoEvent=FALSE);
00228 protected:
00229
00230
00231 BOOL LocalListen(unsigned long ulBackLog);
00232
00233
00234 BOOL LocalConnect(unsigned short usSourcePort,
00235 IP aDestinationAddress,
00236 unsigned short usDestinationPort,
00237 BOOL bDisableAsync,
00238 BOOL bForceErrorEvent);
00239
00240
00241 BOOL LocalSocketConnect(int iErrorCode,
00242 BOOL bNoEvent);
00243
00244
00245 BOOL LocalSocketReceive(int iErrorCode,
00246 BOOL bNoEvent);
00247
00248
00249 BOOL LocalSocketAccept(int iErrorCode,
00250 BOOL bNoEvent);
00251
00252
00253 BOOL LocalSocketClose(int iErrorCode,
00254 BOOL bNoEvent);
00255
00256
00257 BOOL LocalBind(IP aSourceAddress,
00258 unsigned short usPort);
00259
00260
00261 IP LocalGetBindAddress()const;
00262
00263
00264 unsigned short LocalGetBindPort()const;
00265
00266
00267 BOOL LocalAccept(CTCPSocket* pNewSocket);
00268 private:
00269
00270 CTCPSocketAsync(const CTCPSocketAsync& rSocket);
00271
00272
00273 virtual BOOL SocketClosed(int iErrorCode,
00274 BOOL bNoEvent);
00275
00276
00277 virtual BOOL SocketWrite(int iErrorCode);
00278
00279
00280 virtual void Accepted();
00281
00282
00283 virtual void SystemTimeout();
00284
00285
00286 virtual void BeforeAccept();
00287
00288
00289 void SwitchEventsType();
00290
00291
00292 BOOL m_bDisabledConnect;
00293
00294
00295 unsigned long m_ulTimeout;
00296
00297
00298 CTCPSocketAsync* m_pLinkedSocket;
00299
00300
00301 BOOL m_bCloseEventsOnly;
00302
00303
00304 CTCPSocketOverider* m_pOverider;
00305
00306
00307 static const std::string m_sError;
00308 };
00309
00310 KOMODIA_NAMESPACE_END
00311
00312 #endif // !defined(AFX_TCPSOCKETASYNC_H__1D78325D_C154_4B21_97E5_320394FD47EF__INCLUDED_)