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_ASYNCSOCKET_H__980B05BD_A250_40D0_AD92_4F31AEC90E74__INCLUDED_)
00037 #define AFX_ASYNCSOCKET_H__980B05BD_A250_40D0_AD92_4F31AEC90E74__INCLUDED_
00038
00039 #if _MSC_VER > 1000
00040 #pragma once
00041 #endif // _MSC_VER > 1000
00042
00043 #pragma warning(disable:4786)
00044
00045 #include "SocketThreadManagerImp.h"
00046
00047 #include "SocketBase.h"
00048 #include "BlockedBuffer.h"
00049
00050
00051 #define WM_BASE WM_USER
00052 #define WM_SOCKET_GENERAL WM_BASE+1
00053 #define WM_SOCKET_ACCEPT WM_BASE+2
00054 #define WM_SOCKET_CONNECT WM_BASE+3
00055 #define WM_SOCKET_TIMEOUT WM_BASE+4
00056 #define WM_SOCKET_DELETE WM_BASE+5
00057 #define WM_SOCKET_FORCED WM_BASE+6
00058
00059
00060 #define NO_OnSocketTimeout virtual BOOL OnSocketTimeout() {return TRUE;}
00061 #define NO_OnSocketConnect virtual BOOL OnSocketConnect(int iErrorCode) {return TRUE;}
00062 #define NO_OnSocketAccept virtual BOOL OnSocketAccept(int iErrorCode) {return TRUE;}
00063 #define NO_OnSocketClose virtual BOOL OnSocketClose(int iErrorCode) {return TRUE;}
00064 #define NO_OnSocketOOB virtual BOOL OnSocketOOB(int iErrorCode) {return TRUE;}
00065 #define NO_OnSocketWrite virtual BOOL OnSocketWrite(int iErrorCode) {return TRUE;}
00066 #define NO_OnSocketReceive virtual BOOL OnSocketReceive(int iErrorCode) {return TRUE;}
00067
00068
00069 #define CAsyncSocket_Class "CAsyncSocketClass"
00070 #define CAsyncShutdown_Class "CAsyncShutdown"
00071
00072 KOMODIA_NAMESPACE_START
00073
00074 class CGenericCriticalSection;
00075 class CGenericEvent;
00076
00077 class CAsyncSocket : virtual protected CSocketBase
00078 {
00079 friend CBlockedBuffer;
00080 friend CSocketThreadManagerImp;
00081 public:
00082
00083 typedef enum _AsyncEvents
00084 {
00085 aeReceive=1,
00086 aeSend=2,
00087 aeOOB=4,
00088 aeClose=8
00089 } AsyncEvents;
00090
00091
00092 typedef BOOL (*MapProc)();
00093 typedef MapProc LPMapProc;
00094 public:
00095
00096 static void SetCleanTimeout(DWORD dwTimeout);
00097
00098
00099 void DetachedSocketThread();
00100
00101
00102 void DisableBlockingSend();
00103
00104
00105
00106 static CSocketThreadManagerImp* CloneThreadManager(unsigned long ulThreads);
00107
00108
00109 BOOL SetLocalThreadManager(CSocketThreadManagerImp* pManager);
00110
00111
00112 CSocketThreadManagerImp* GetThreadManager()const;
00113
00114
00115
00116
00117
00118
00119
00120 CSocketThreadManager::TimerID RegisterTimeout(int iMS,
00121 CSocketThreadManagerImp::LPTimeoutProc pProc,
00122 LPVOID pData,
00123 BOOL bClearTimeout=FALSE,
00124 CSocketThreadManagerImp::TimerID* pTimer=NULL);
00125
00126
00127 BOOL HasTimeout()const;
00128
00129
00130 void DisableEvents(unsigned char ucEvents);
00131
00132
00133 BOOL IsDeleting()const;
00134
00135
00136 void SetDeleting();
00137
00138
00139 BOOL IsClosing()const;
00140
00141
00142 void ForceReceiveEvent();
00143
00144
00145 virtual void DeleteSocketFromThread();
00146
00147
00148 virtual void DeleteSocketFromThread(DWORD dwTimeToWait);
00149
00150
00151 virtual void DeleteSocketFromThreadWait();
00152
00153
00154 static void SimpleMessageMap();
00155
00156
00157 static void SimpleMessageMap(DWORD dwRunTimeMS);
00158
00159
00160
00161
00162
00163
00164 static void SimpleMessageMap(LPMapProc pProc,
00165 BOOL bStopWhenTRUE,
00166 DWORD dwSleep);
00167
00168
00169 static char SimpleMessageMapKey(DWORD dwSleep);
00170
00171
00172
00173 void AllowBlockedBuffer(BOOL bAllow);
00174
00175
00176 virtual BOOL ReAsync();
00177
00178
00179 virtual BOOL ReBlock();
00180
00181
00182
00183 void FreezeThread();
00184
00185
00186 BOOL DisableAsync();
00187
00188
00189
00190 static void Initialize();
00191
00192
00193
00194 static void Shutdown();
00195
00196
00197 BOOL KillTimer();
00198
00199
00200
00201
00202 BOOL SetTimeout(int iMs);
00203
00204
00205
00206 static void SetInstance(HINSTANCE hInst);
00207
00208
00209 CAsyncSocket();
00210 virtual ~CAsyncSocket();
00211 protected:
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 virtual BOOL OnSocketTimeout()=0;
00224
00225
00226 virtual BOOL OnSocketConnect(int iErrorCode)=0;
00227
00228
00229 virtual BOOL OnSocketAccept(int iErrorCode)=0;
00230
00231
00232 virtual BOOL OnSocketClose(int iErrorCode)=0;
00233
00234
00235
00236 virtual BOOL OnSocketOOB(int iErrorCode)=0;
00237
00238
00239
00240 virtual BOOL OnSocketWrite(int iErrorCode)=0;
00241
00242
00243 virtual BOOL OnSocketReceive(int iErrorCode)=0;
00244
00245
00246 BOOL IsBlocking()const;
00247
00248
00249 int InternalWSAAsyncSelect(unsigned int wMsg,
00250 long lEvent);
00251
00252
00253 void SocketClosing();
00254
00255
00256 int GetSocketID()const;
00257
00258
00259 HWND GetWindowHandle()const;
00260
00261
00262 virtual SOCKET GetAsyncHandle()const=0;
00263
00264
00265 virtual BOOL SetAsync()=0;
00266
00267
00268 void RemoveSocketFromList();
00269
00270
00271 void AddSocketToList();
00272
00273
00274 BOOL IsTimeout()const;
00275
00276
00277 BOOL Block();
00278
00279
00280 virtual BOOL SendBlockedBuffer(const CBlockedBuffer::CBlockedData& rData);
00281
00282
00283 CBlockedBuffer* GetBlockedBuffer()const;
00284
00285
00286 static CSocketThreadManagerImp* GetSocketThreadManager();
00287
00288
00289
00290 virtual BOOL SocketConnected(int iErrorCode,
00291 BOOL bNoEvent=FALSE);
00292
00293
00294
00295 virtual BOOL SocketClosed(int iErrorCode,
00296 BOOL bNoEvent=FALSE);
00297
00298
00299
00300 virtual BOOL SocketWrite(int iErrorCode);
00301
00302
00303
00304 virtual BOOL SocketReceive(int iErrorCode,
00305 BOOL bNoEvent=FALSE);
00306
00307
00308
00309 virtual BOOL SocketAccept(int iErrorCode,
00310 BOOL bNoEvent=FALSE);
00311
00312
00313
00314 virtual void SystemTimeout();
00315
00316
00317 BOOL SetSystemTimeout(int iMS);
00318
00319
00320 BOOL KillSystemTimer();
00321
00322
00323 BOOL HasSystemTimer()const;
00324
00325
00326
00327 virtual void OnSocketDelete(CGenericEvent* pEvent=NULL);
00328
00329
00330
00331 virtual void SocketCreated();
00332
00333
00334 virtual BOOL ClearEvents();
00335
00336
00337 BOOL IsBlockSend()const;
00338
00339
00340 BOOL IsInThread()const;
00341
00342
00343 BOOL WaitForBlockEvent()const;
00344
00345
00346 void ResetEvent();
00347 private:
00348
00349 CAsyncSocket(const CAsyncSocket& rSocket);
00350
00351
00352 CAsyncSocket& operator=(const CAsyncSocket& rSocket);
00353
00354
00355 BOOL CheckAsyncSocketValid()const;
00356
00357
00358 void DeAllocateHandle();
00359
00360
00361 void AllocateHandle();
00362
00363
00364 static HINSTANCE GetInstance();
00365
00366
00367 static BOOL SetHandlers();
00368
00369
00370 static void SystemTimerProc(LPVOID lpData);
00371
00372
00373 static void DeleteTimerProc(LPVOID lpData);
00374
00375
00376 static HINSTANCE m_hInstance;
00377
00378
00379 static BOOL m_bInitialized;
00380
00381
00382 int m_iSocketID;
00383
00384
00385 BOOL m_bList;
00386
00387
00388 BOOL m_bTimeout;
00389
00390
00391 HWND m_hLocalWindowHandle;
00392
00393
00394 static BOOL m_bShuttingDown;
00395
00396
00397 BOOL m_bFreeze;
00398
00399
00400 BOOL m_bBlocking;
00401
00402
00403 unsigned int m_iMsg;
00404
00405
00406 long m_lEvent;
00407
00408
00409 static CSocketThreadManagerImp* m_pThreadManager;
00410
00411
00412 CSocketThreadManagerImp* m_pLocalThreadManager;
00413
00414
00415 CBlockedBuffer* m_pBlockedBuffer;
00416
00417
00418 CSocketThreadManagerImp::TimerID m_aTimerID;
00419
00420
00421 CSocketThreadManagerImp::TimerID m_aDeleteTimerID;
00422
00423
00424 BOOL m_bClosing;
00425
00426
00427 unsigned char m_ucEvents;
00428
00429
00430 CGenericEvent* m_pSendEvent;
00431
00432
00433 BOOL m_bBlockSend;
00434
00435
00436 const CGenericThread* m_pThread;
00437
00438
00439 BOOL m_bDeleting;
00440
00441
00442 BOOL m_bDetached;
00443 private:
00444
00445 class CAsyncShutdown : protected CSocketBase
00446 {
00447 public:
00448 CAsyncShutdown();
00449 virtual ~CAsyncShutdown();
00450 protected:
00451
00452 virtual void NotifyShutdown();
00453 };
00454 };
00455
00456 KOMODIA_NAMESPACE_END
00457
00458 #endif // !defined(AFX_ASYNCSOCKET_H__980B05BD_A250_40D0_AD92_4F31AEC90E74__INCLUDED_)