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