AsyncSocketExLayer.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. // CAsyncSocketEx by Tim Kosse ([email protected])
  2. // Version 1.1 (2002-11-01)
  3. //---------------------------------------------------------------------------
  4. // Feel free to use this class, as long as you don't claim that you wrote it
  5. // and this copyright notice stays intact in the source files.
  6. // If you use this class in commercial applications, please send a short message
  7. // to [email protected]
  8. //---------------------------------------------------------------------------
  9. #include "FileZillaPCH.h"
  10. #include "AsyncSocketExLayer.h"
  11. #include "AsyncSocketEx.h"
  12. #define WM_SOCKETEX_NOTIFY (WM_USER+3)
  13. //////////////////////////////////////////////////////////////////////
  14. // Konstruktion/Destruktion
  15. //////////////////////////////////////////////////////////////////////
  16. CAsyncSocketExLayer::CAsyncSocketExLayer()
  17. {
  18. m_pOwnerSocket = NULL;
  19. m_pNextLayer = NULL;
  20. m_pPrevLayer = NULL;
  21. m_nLayerState = notsock;
  22. m_nCriticalError=0;
  23. m_nPendingEvents = 0;
  24. m_nFamily = AF_UNSPEC;
  25. m_lEvent = 0;
  26. m_lpszSocketAddress = 0;
  27. m_nSocketPort = 0;
  28. m_nextAddr = 0;
  29. m_addrInfo = 0;
  30. }
  31. CAsyncSocketExLayer::~CAsyncSocketExLayer()
  32. {
  33. delete [] m_lpszSocketAddress;
  34. }
  35. CAsyncSocketExLayer *CAsyncSocketExLayer::AddLayer(CAsyncSocketExLayer *pLayer, CAsyncSocketEx *pOwnerSocket)
  36. {
  37. DebugAssert(pLayer);
  38. DebugAssert(pOwnerSocket);
  39. if (m_pNextLayer)
  40. {
  41. return m_pNextLayer->AddLayer(pLayer, pOwnerSocket);
  42. }
  43. else
  44. {
  45. DebugAssert(m_pOwnerSocket==pOwnerSocket);
  46. pLayer->Init(this, m_pOwnerSocket);
  47. m_pNextLayer=pLayer;
  48. }
  49. return m_pNextLayer;
  50. }
  51. int CAsyncSocketExLayer::Receive(void* lpBuf, int nBufLen, int nFlags /*=0*/)
  52. {
  53. return ReceiveNext(lpBuf, nBufLen, nFlags);
  54. }
  55. int CAsyncSocketExLayer::Send(const void* lpBuf, int nBufLen, int nFlags /*=0*/)
  56. {
  57. return SendNext(lpBuf, nBufLen, nFlags);
  58. }
  59. void CAsyncSocketExLayer::OnReceive(int nErrorCode)
  60. {
  61. if (m_pPrevLayer)
  62. {
  63. m_pPrevLayer->OnReceive(nErrorCode);
  64. }
  65. else
  66. {
  67. if (m_pOwnerSocket->m_lEvent&FD_READ)
  68. {
  69. m_pOwnerSocket->OnReceive(nErrorCode);
  70. }
  71. }
  72. }
  73. void CAsyncSocketExLayer::OnSend(int nErrorCode)
  74. {
  75. if (m_pPrevLayer)
  76. {
  77. m_pPrevLayer->OnSend(nErrorCode);
  78. }
  79. else
  80. {
  81. if (m_pOwnerSocket->m_lEvent&FD_WRITE)
  82. {
  83. m_pOwnerSocket->OnSend(nErrorCode);
  84. }
  85. }
  86. }
  87. void CAsyncSocketExLayer::OnConnect(int nErrorCode)
  88. {
  89. TriggerEvent(FD_CONNECT, nErrorCode, TRUE);
  90. }
  91. void CAsyncSocketExLayer::OnAccept(int nErrorCode)
  92. {
  93. if (m_pPrevLayer)
  94. m_pPrevLayer->OnAccept(nErrorCode);
  95. else
  96. if (m_pOwnerSocket->m_lEvent&FD_ACCEPT)
  97. m_pOwnerSocket->OnAccept(nErrorCode);
  98. }
  99. void CAsyncSocketExLayer::OnClose(int nErrorCode)
  100. {
  101. if (m_pPrevLayer)
  102. m_pPrevLayer->OnClose(nErrorCode);
  103. else
  104. if (m_pOwnerSocket->m_lEvent&FD_CLOSE)
  105. m_pOwnerSocket->OnClose(nErrorCode);
  106. }
  107. BOOL CAsyncSocketExLayer::TriggerEvent(long lEvent, int nErrorCode, BOOL bPassThrough /*=FALSE*/ )
  108. {
  109. DebugAssert(m_pOwnerSocket);
  110. if (m_pOwnerSocket->m_SocketData.hSocket==INVALID_SOCKET)
  111. {
  112. return FALSE;
  113. }
  114. if (!bPassThrough)
  115. {
  116. if (m_nPendingEvents & lEvent)
  117. {
  118. return TRUE;
  119. }
  120. m_nPendingEvents |= lEvent;
  121. }
  122. if (lEvent & FD_CONNECT)
  123. {
  124. DebugAssert(bPassThrough);
  125. if (!nErrorCode)
  126. {
  127. DebugAssert(bPassThrough && (GetLayerState()==connected || GetLayerState()==attached));
  128. }
  129. else if (nErrorCode)
  130. {
  131. SetLayerState(aborted);
  132. m_nCriticalError=nErrorCode;
  133. }
  134. }
  135. else if (lEvent & FD_CLOSE)
  136. {
  137. if (!nErrorCode)
  138. {
  139. SetLayerState(closed);
  140. }
  141. else
  142. {
  143. SetLayerState(aborted);
  144. m_nCriticalError = nErrorCode;
  145. }
  146. }
  147. DebugAssert(m_pOwnerSocket->m_pLocalAsyncSocketExThreadData);
  148. DebugAssert(m_pOwnerSocket->m_pLocalAsyncSocketExThreadData->m_pHelperWindow);
  149. DebugAssert(m_pOwnerSocket->m_SocketData.nSocketIndex!=-1);
  150. t_LayerNotifyMsg *pMsg=new t_LayerNotifyMsg;
  151. pMsg->hSocket = m_pOwnerSocket->m_SocketData.hSocket;
  152. pMsg->lEvent = ( lEvent % 0xffff ) + ( nErrorCode << 16);
  153. pMsg->pLayer=bPassThrough?m_pPrevLayer:this;
  154. BOOL res=PostMessage(m_pOwnerSocket->GetHelperWindowHandle(), WM_USER, (WPARAM)m_pOwnerSocket->m_SocketData.nSocketIndex, (LPARAM)pMsg);
  155. if (!res)
  156. {
  157. delete pMsg;
  158. }
  159. return res;
  160. }
  161. void CAsyncSocketExLayer::Close()
  162. {
  163. CloseNext();
  164. }
  165. void CAsyncSocketExLayer::CloseNext()
  166. {
  167. if (m_addrInfo)
  168. freeaddrinfo(m_addrInfo);
  169. m_nextAddr = 0;
  170. m_addrInfo = 0;
  171. m_nPendingEvents = 0;
  172. SetLayerState(notsock);
  173. if (m_pNextLayer)
  174. m_pNextLayer->Close();
  175. }
  176. BOOL CAsyncSocketExLayer::Connect(LPCTSTR lpszHostAddress, UINT nHostPort)
  177. {
  178. return ConnectNext(lpszHostAddress, nHostPort);
  179. }
  180. BOOL CAsyncSocketExLayer::Connect( const SOCKADDR* lpSockAddr, int nSockAddrLen )
  181. {
  182. return ConnectNext(lpSockAddr, nSockAddrLen);
  183. }
  184. int CAsyncSocketExLayer::SendNext(const void *lpBuf, int nBufLen, int nFlags /*=0*/)
  185. {
  186. if (m_nCriticalError)
  187. {
  188. WSASetLastError(m_nCriticalError);
  189. return SOCKET_ERROR;
  190. }
  191. else if (GetLayerState()==notsock)
  192. {
  193. WSASetLastError(WSAENOTSOCK);
  194. return SOCKET_ERROR;
  195. }
  196. else if (GetLayerState()==unconnected || GetLayerState()==connecting || GetLayerState()==listening)
  197. {
  198. WSASetLastError(WSAENOTCONN);
  199. return SOCKET_ERROR;
  200. }
  201. if (!m_pNextLayer)
  202. {
  203. DebugAssert(m_pOwnerSocket);
  204. int sent = send(m_pOwnerSocket->GetSocketHandle(), (LPSTR)lpBuf, nBufLen, nFlags);
  205. return sent;
  206. }
  207. else
  208. {
  209. return m_pNextLayer->Send(lpBuf, nBufLen, nFlags);
  210. }
  211. }
  212. int CAsyncSocketExLayer::ReceiveNext(void *lpBuf, int nBufLen, int nFlags /*=0*/)
  213. {
  214. if (m_nCriticalError)
  215. {
  216. WSASetLastError(m_nCriticalError);
  217. return SOCKET_ERROR;
  218. }
  219. else if (GetLayerState()==notsock)
  220. {
  221. WSASetLastError(WSAENOTSOCK);
  222. return SOCKET_ERROR;
  223. }
  224. else if (GetLayerState()==unconnected || GetLayerState()==connecting || GetLayerState()==listening)
  225. {
  226. WSASetLastError(WSAENOTCONN);
  227. return SOCKET_ERROR;
  228. }
  229. if (!m_pNextLayer)
  230. {
  231. DebugAssert(m_pOwnerSocket);
  232. return recv(m_pOwnerSocket->GetSocketHandle(), (LPSTR)lpBuf, nBufLen, nFlags);
  233. }
  234. else
  235. {
  236. return m_pNextLayer->Receive(lpBuf, nBufLen, nFlags);
  237. }
  238. }
  239. BOOL CAsyncSocketExLayer::ConnectNext(LPCTSTR lpszHostAddress, UINT nHostPort)
  240. {
  241. DebugAssert(GetLayerState()==unconnected);
  242. DebugAssert(m_pOwnerSocket);
  243. BOOL res = FALSE;
  244. if (m_pNextLayer)
  245. res = m_pNextLayer->Connect(lpszHostAddress, nHostPort);
  246. else if (m_nFamily == AF_INET)
  247. {
  248. USES_CONVERSION;
  249. DebugAssert(lpszHostAddress != NULL);
  250. SOCKADDR_IN sockAddr;
  251. memset(&sockAddr,0,sizeof(sockAddr));
  252. LPSTR lpszAscii = T2A((LPTSTR)lpszHostAddress);
  253. sockAddr.sin_family = AF_INET;
  254. sockAddr.sin_addr.s_addr = inet_addr(lpszAscii);
  255. if (sockAddr.sin_addr.s_addr == INADDR_NONE)
  256. {
  257. LPHOSTENT lphost;
  258. lphost = gethostbyname(lpszAscii);
  259. if (lphost != NULL)
  260. sockAddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;
  261. else
  262. {
  263. WSASetLastError(WSAEINVAL);
  264. res = FALSE;
  265. }
  266. }
  267. sockAddr.sin_port = htons((u_short)nHostPort);
  268. res = (SOCKET_ERROR != connect(m_pOwnerSocket->GetSocketHandle(), (SOCKADDR*)&sockAddr, sizeof(sockAddr)) );
  269. }
  270. else if (m_nFamily == AF_INET6 || m_nFamily == AF_UNSPEC)
  271. {
  272. USES_CONVERSION;
  273. DebugAssert(lpszHostAddress != NULL);
  274. addrinfo hints, *res0, *res1;
  275. SOCKET hSocket;
  276. int error;
  277. char port[10];
  278. freeaddrinfo(m_addrInfo);
  279. m_nextAddr = 0;
  280. m_addrInfo = 0;
  281. memset(&hints, 0, sizeof(addrinfo));
  282. hints.ai_family = m_nFamily;
  283. hints.ai_socktype = SOCK_STREAM;
  284. hints.ai_flags = 0;
  285. _snprintf(port, 9, "%u", nHostPort);
  286. error = getaddrinfo(T2CA(lpszHostAddress), port, &hints, &res0);
  287. if (error)
  288. return FALSE;
  289. for (res1 = res0; res1; res1 = res1->ai_next)
  290. {
  291. if (m_nFamily == AF_UNSPEC)
  292. hSocket = socket(res1->ai_family, res1->ai_socktype, res1->ai_protocol);
  293. else
  294. hSocket = m_pOwnerSocket->GetSocketHandle();
  295. if (INVALID_SOCKET == hSocket)
  296. {
  297. res = FALSE;
  298. continue;
  299. }
  300. if (m_nFamily == AF_UNSPEC)
  301. {
  302. m_pOwnerSocket->m_SocketData.hSocket = hSocket;
  303. m_pOwnerSocket->AttachHandle(hSocket);
  304. if (!m_pOwnerSocket->AsyncSelect(m_lEvent))
  305. {
  306. m_pOwnerSocket->Close();
  307. res = FALSE;
  308. continue ;
  309. }
  310. if (m_pOwnerSocket->m_pFirstLayer)
  311. {
  312. if (WSAAsyncSelect(m_pOwnerSocket->m_SocketData.hSocket, m_pOwnerSocket->GetHelperWindowHandle(), m_pOwnerSocket->m_SocketData.nSocketIndex+WM_SOCKETEX_NOTIFY, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE) )
  313. {
  314. m_pOwnerSocket->Close();
  315. res = FALSE;
  316. continue;
  317. }
  318. }
  319. if (m_pOwnerSocket->m_pendingCallbacks.size())
  320. PostMessage(m_pOwnerSocket->GetHelperWindowHandle(), WM_USER + 2, (WPARAM)m_pOwnerSocket->m_SocketData.nSocketIndex, 0);
  321. }
  322. if (m_nFamily == AF_UNSPEC)
  323. {
  324. m_pOwnerSocket->m_SocketData.nFamily = m_nFamily = res1->ai_family;
  325. if (!m_pOwnerSocket->Bind(m_nSocketPort, m_lpszSocketAddress))
  326. {
  327. m_pOwnerSocket->m_SocketData.nFamily = m_nFamily = AF_UNSPEC;
  328. Close();
  329. continue;
  330. }
  331. }
  332. if (!( res = ( SOCKET_ERROR != connect(m_pOwnerSocket->GetSocketHandle(), res1->ai_addr, res1->ai_addrlen) ) )
  333. && WSAGetLastError() != WSAEWOULDBLOCK)
  334. {
  335. if (hints.ai_family == AF_UNSPEC)
  336. {
  337. m_nFamily = AF_UNSPEC;
  338. Close();
  339. }
  340. continue ;
  341. }
  342. m_nFamily = res1->ai_family;
  343. m_pOwnerSocket->m_SocketData.nFamily = res1->ai_family;
  344. res = TRUE;
  345. break;
  346. }
  347. if (res1)
  348. res1 = res0->ai_next;
  349. if (res1)
  350. {
  351. m_addrInfo = res0;
  352. m_nextAddr = res1;
  353. }
  354. else
  355. freeaddrinfo(res0);
  356. if (INVALID_SOCKET == m_pOwnerSocket->GetSocketHandle())
  357. res = FALSE ;
  358. }
  359. if (res || WSAGetLastError() == WSAEWOULDBLOCK)
  360. {
  361. SetLayerState(connecting);
  362. }
  363. return res;
  364. }
  365. BOOL CAsyncSocketExLayer::ConnectNext( const SOCKADDR* lpSockAddr, int nSockAddrLen )
  366. {
  367. DebugAssert(GetLayerState()==unconnected);
  368. DebugAssert(m_pOwnerSocket);
  369. BOOL res;
  370. if (m_pNextLayer)
  371. res=m_pNextLayer->Connect(lpSockAddr, nSockAddrLen);
  372. else
  373. res = (SOCKET_ERROR!=connect(m_pOwnerSocket->GetSocketHandle(), lpSockAddr, nSockAddrLen));
  374. if (res || WSAGetLastError()==WSAEWOULDBLOCK)
  375. SetLayerState(connecting);
  376. return res;
  377. }
  378. //Gets the address of the peer socket to which the socket is connected
  379. BOOL CAsyncSocketExLayer::GetPeerName( CString& rPeerAddress, UINT& rPeerPort )
  380. {
  381. return GetPeerNameNext(rPeerAddress, rPeerPort);
  382. }
  383. BOOL CAsyncSocketExLayer::GetPeerNameNext( CString& rPeerAddress, UINT& rPeerPort )
  384. {
  385. if (m_pNextLayer)
  386. {
  387. return m_pNextLayer->GetPeerName(rPeerAddress, rPeerPort);
  388. }
  389. else
  390. {
  391. SOCKADDR* sockAddr;
  392. int nSockAddrLen;
  393. if (m_nFamily == AF_INET6)
  394. {
  395. sockAddr = (SOCKADDR*)new SOCKADDR_IN6;
  396. nSockAddrLen = sizeof(SOCKADDR_IN6);
  397. }
  398. else if (m_nFamily == AF_INET)
  399. {
  400. sockAddr = (SOCKADDR*)new SOCKADDR_IN;
  401. nSockAddrLen = sizeof(SOCKADDR_IN);
  402. }
  403. memset(sockAddr, 0, nSockAddrLen);
  404. BOOL bResult = GetPeerName(sockAddr, &nSockAddrLen);
  405. if (bResult)
  406. {
  407. if (m_nFamily == AF_INET6)
  408. {
  409. rPeerPort = ntohs(((SOCKADDR_IN6*)sockAddr)->sin6_port);
  410. LPTSTR buf = Inet6AddrToString(((SOCKADDR_IN6*)sockAddr)->sin6_addr);
  411. rPeerAddress = buf;
  412. delete [] buf;
  413. }
  414. else if (m_nFamily == AF_INET)
  415. {
  416. rPeerPort = ntohs(((SOCKADDR_IN*)sockAddr)->sin_port);
  417. rPeerAddress = inet_ntoa(((SOCKADDR_IN*)sockAddr)->sin_addr);
  418. }
  419. else
  420. {
  421. delete sockAddr;
  422. return FALSE;
  423. }
  424. }
  425. delete sockAddr;
  426. return bResult;
  427. }
  428. }
  429. BOOL CAsyncSocketExLayer::GetPeerName( SOCKADDR* lpSockAddr, int* lpSockAddrLen )
  430. {
  431. return GetPeerNameNext(lpSockAddr, lpSockAddrLen);
  432. }
  433. BOOL CAsyncSocketExLayer::GetPeerNameNext( SOCKADDR* lpSockAddr, int* lpSockAddrLen )
  434. {
  435. if (m_pNextLayer)
  436. {
  437. return m_pNextLayer->GetPeerName(lpSockAddr, lpSockAddrLen);
  438. }
  439. else
  440. {
  441. DebugAssert(m_pOwnerSocket);
  442. if ( !getpeername(m_pOwnerSocket->GetSocketHandle(), lpSockAddr, lpSockAddrLen) )
  443. {
  444. return TRUE;
  445. }
  446. else
  447. {
  448. return FALSE;
  449. }
  450. }
  451. }
  452. //Gets the address of the sock socket to which the socket is connected
  453. BOOL CAsyncSocketExLayer::GetSockName( CString& rSockAddress, UINT& rSockPort )
  454. {
  455. return GetSockNameNext(rSockAddress, rSockPort);
  456. }
  457. BOOL CAsyncSocketExLayer::GetSockNameNext( CString& rSockAddress, UINT& rSockPort )
  458. {
  459. if (m_pNextLayer)
  460. return m_pNextLayer->GetSockName(rSockAddress, rSockPort);
  461. else
  462. {
  463. SOCKADDR* sockAddr;
  464. int nSockAddrLen;
  465. if (m_nFamily == AF_INET6)
  466. {
  467. sockAddr = (SOCKADDR*)new SOCKADDR_IN6;
  468. nSockAddrLen = sizeof(SOCKADDR_IN6);
  469. }
  470. else if (m_nFamily == AF_INET)
  471. {
  472. sockAddr = (SOCKADDR*)new SOCKADDR_IN;
  473. nSockAddrLen = sizeof(SOCKADDR_IN);
  474. }
  475. memset(sockAddr, 0, nSockAddrLen);
  476. BOOL bResult = GetSockName(sockAddr, &nSockAddrLen);
  477. if (bResult)
  478. {
  479. if (m_nFamily == AF_INET6)
  480. {
  481. rSockPort = ntohs(((SOCKADDR_IN6*)sockAddr)->sin6_port);
  482. LPTSTR buf = Inet6AddrToString(((SOCKADDR_IN6*)sockAddr)->sin6_addr);
  483. rSockAddress = buf;
  484. delete [] buf;
  485. }
  486. else if (m_nFamily == AF_INET)
  487. {
  488. rSockPort = ntohs(((SOCKADDR_IN*)sockAddr)->sin_port);
  489. rSockAddress = inet_ntoa(((SOCKADDR_IN*)sockAddr)->sin_addr);
  490. }
  491. else
  492. {
  493. delete sockAddr;
  494. return FALSE;
  495. }
  496. }
  497. delete sockAddr;
  498. return bResult;
  499. }
  500. }
  501. BOOL CAsyncSocketExLayer::GetSockName( SOCKADDR* lpSockAddr, int* lpSockAddrLen )
  502. {
  503. return GetSockNameNext(lpSockAddr, lpSockAddrLen);
  504. }
  505. BOOL CAsyncSocketExLayer::GetSockNameNext( SOCKADDR* lpSockAddr, int* lpSockAddrLen )
  506. {
  507. if (m_pNextLayer)
  508. return m_pNextLayer->GetSockName(lpSockAddr, lpSockAddrLen);
  509. else
  510. {
  511. DebugAssert(m_pOwnerSocket);
  512. if ( !getsockname(m_pOwnerSocket->GetSocketHandle(), lpSockAddr, lpSockAddrLen) )
  513. return TRUE;
  514. else
  515. return FALSE;
  516. }
  517. }
  518. void CAsyncSocketExLayer::Init(CAsyncSocketExLayer *pPrevLayer, CAsyncSocketEx *pOwnerSocket)
  519. {
  520. DebugAssert(pOwnerSocket);
  521. m_pPrevLayer=pPrevLayer;
  522. m_pOwnerSocket=pOwnerSocket;
  523. m_pNextLayer=0;
  524. SetLayerState(pOwnerSocket->GetState());
  525. }
  526. int CAsyncSocketExLayer::GetLayerState()
  527. {
  528. return m_nLayerState;
  529. }
  530. void CAsyncSocketExLayer::SetLayerState(int nLayerState)
  531. {
  532. DebugAssert(m_pOwnerSocket);
  533. int nOldLayerState=GetLayerState();
  534. m_nLayerState=nLayerState;
  535. if (nOldLayerState!=nLayerState)
  536. DoLayerCallback(LAYERCALLBACK_STATECHANGE, GetLayerState(), nOldLayerState);
  537. }
  538. void CAsyncSocketExLayer::CallEvent(int nEvent, int nErrorCode)
  539. {
  540. if (m_nCriticalError)
  541. {
  542. return;
  543. }
  544. m_nCriticalError = nErrorCode;
  545. switch (nEvent)
  546. {
  547. case FD_READ:
  548. case FD_FORCEREAD:
  549. if (GetLayerState()==connecting && !nErrorCode)
  550. {
  551. m_nPendingEvents |= nEvent;
  552. break;
  553. }
  554. else if (GetLayerState()==attached)
  555. SetLayerState(connected);
  556. if (nEvent & FD_READ)
  557. m_nPendingEvents &= ~FD_READ;
  558. else
  559. m_nPendingEvents &= ~FD_FORCEREAD;
  560. if (GetLayerState()==connected || nErrorCode)
  561. {
  562. if (nErrorCode)
  563. SetLayerState(aborted);
  564. OnReceive(nErrorCode);
  565. }
  566. break;
  567. case FD_WRITE:
  568. if (GetLayerState()==connecting && !nErrorCode)
  569. {
  570. m_nPendingEvents |= nEvent;
  571. break;
  572. }
  573. else if (GetLayerState()==attached)
  574. SetLayerState(connected);
  575. m_nPendingEvents &= ~FD_WRITE;
  576. if (GetLayerState()==connected || nErrorCode)
  577. {
  578. if (nErrorCode)
  579. SetLayerState(aborted);
  580. OnSend(nErrorCode);
  581. }
  582. break;
  583. case FD_CONNECT:
  584. if (GetLayerState()==connecting || GetLayerState() == attached)
  585. {
  586. if (!nErrorCode)
  587. SetLayerState(connected);
  588. else
  589. {
  590. if (!m_pNextLayer && m_nextAddr)
  591. if (TryNextProtocol())
  592. {
  593. m_nCriticalError = 0;
  594. return;
  595. }
  596. SetLayerState(aborted);
  597. }
  598. m_nPendingEvents &= ~FD_CONNECT;
  599. OnConnect(nErrorCode);
  600. if (!nErrorCode)
  601. {
  602. if ((m_nPendingEvents & FD_READ) && GetLayerState()==connected)
  603. OnReceive(0);
  604. if ((m_nPendingEvents & FD_FORCEREAD) && GetLayerState()==connected)
  605. OnReceive(0);
  606. if ((m_nPendingEvents & FD_WRITE) && GetLayerState()==connected)
  607. OnSend(0);
  608. }
  609. m_nPendingEvents = 0;
  610. }
  611. break;
  612. case FD_ACCEPT:
  613. if (GetLayerState()==listening)
  614. {
  615. if (nErrorCode)
  616. SetLayerState(aborted);
  617. m_nPendingEvents &= ~FD_ACCEPT;
  618. OnAccept(nErrorCode);
  619. }
  620. break;
  621. case FD_CLOSE:
  622. if (GetLayerState()==connected || GetLayerState()==attached)
  623. {
  624. if (nErrorCode)
  625. SetLayerState(aborted);
  626. else
  627. SetLayerState(closed);
  628. m_nPendingEvents &= ~FD_CLOSE;
  629. OnClose(nErrorCode);
  630. }
  631. break;
  632. }
  633. }
  634. //Creates a socket
  635. BOOL CAsyncSocketExLayer::Create(UINT nSocketPort, int nSocketType,
  636. long lEvent, LPCTSTR lpszSocketAddress, int nFamily /*=AF_INET*/)
  637. {
  638. return CreateNext(nSocketPort, nSocketType, lEvent, lpszSocketAddress, nFamily);
  639. }
  640. BOOL CAsyncSocketExLayer::CreateNext(UINT nSocketPort, int nSocketType, long lEvent, LPCTSTR lpszSocketAddress, int nFamily /*=AF_INET*/)
  641. {
  642. DebugAssert((GetLayerState() == notsock) || (GetLayerState() == unconnected));
  643. BOOL res = FALSE;
  644. m_nFamily = nFamily;
  645. if (m_pNextLayer)
  646. res = m_pNextLayer->Create(nSocketPort, nSocketType, lEvent, lpszSocketAddress, nFamily);
  647. else if (m_nFamily == AF_UNSPEC)
  648. {
  649. m_lEvent = lEvent;
  650. delete [] m_lpszSocketAddress;
  651. if (lpszSocketAddress && *lpszSocketAddress)
  652. {
  653. m_lpszSocketAddress = new TCHAR[_tcslen(lpszSocketAddress) + 1];
  654. _tcscpy(m_lpszSocketAddress, lpszSocketAddress);
  655. }
  656. else
  657. m_lpszSocketAddress = 0;
  658. m_nSocketPort = nSocketPort;
  659. res = TRUE;
  660. }
  661. else
  662. {
  663. SOCKET hSocket=socket(nFamily, nSocketType, 0);
  664. if (hSocket == INVALID_SOCKET)
  665. {
  666. m_pOwnerSocket->Close();
  667. return FALSE;
  668. }
  669. m_pOwnerSocket->m_SocketData.hSocket=hSocket;
  670. m_pOwnerSocket->AttachHandle(hSocket);
  671. if (!m_pOwnerSocket->AsyncSelect(lEvent))
  672. {
  673. m_pOwnerSocket->Close();
  674. return FALSE;
  675. }
  676. if (m_pOwnerSocket->m_pFirstLayer)
  677. {
  678. if (WSAAsyncSelect(m_pOwnerSocket->m_SocketData.hSocket, m_pOwnerSocket->GetHelperWindowHandle(), m_pOwnerSocket->m_SocketData.nSocketIndex+WM_SOCKETEX_NOTIFY, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE) )
  679. {
  680. m_pOwnerSocket->Close();
  681. return FALSE;
  682. }
  683. }
  684. if (!m_pOwnerSocket->Bind(nSocketPort, lpszSocketAddress))
  685. {
  686. m_pOwnerSocket->Close();
  687. return FALSE;
  688. }
  689. res = TRUE;
  690. }
  691. if (res)
  692. SetLayerState(unconnected);
  693. return res;
  694. }
  695. int CAsyncSocketExLayer::DoLayerCallback(int nType, int nParam1, int nParam2, char* str /*=0*/)
  696. {
  697. if (!m_pOwnerSocket)
  698. return 0;
  699. int nError = WSAGetLastError();
  700. t_callbackMsg msg;
  701. msg.pLayer = this;
  702. msg.nType = nType;
  703. msg.nParam1 = nParam1;
  704. msg.nParam2 = nParam2;
  705. msg.str = str;
  706. m_pOwnerSocket->AddCallbackNotification(msg);
  707. WSASetLastError(nError);
  708. return 0;
  709. }
  710. BOOL CAsyncSocketExLayer::Listen( int nConnectionBacklog)
  711. {
  712. return ListenNext( nConnectionBacklog);
  713. }
  714. BOOL CAsyncSocketExLayer::ListenNext( int nConnectionBacklog)
  715. {
  716. DebugAssert(GetLayerState()==unconnected);
  717. BOOL res;
  718. if (m_pNextLayer)
  719. res=m_pNextLayer->Listen(nConnectionBacklog);
  720. else
  721. res=listen(m_pOwnerSocket->GetSocketHandle(), nConnectionBacklog);
  722. if (res!=SOCKET_ERROR)
  723. {
  724. SetLayerState(listening);
  725. }
  726. return res!=SOCKET_ERROR;
  727. }
  728. BOOL CAsyncSocketExLayer::Accept( CAsyncSocketEx& rConnectedSocket, SOCKADDR* lpSockAddr /*=NULL*/, int* lpSockAddrLen /*=NULL*/ )
  729. {
  730. return AcceptNext(rConnectedSocket, lpSockAddr, lpSockAddrLen);
  731. }
  732. BOOL CAsyncSocketExLayer::AcceptNext( CAsyncSocketEx& rConnectedSocket, SOCKADDR* lpSockAddr /*=NULL*/, int* lpSockAddrLen /*=NULL*/ )
  733. {
  734. DebugAssert(GetLayerState()==listening);
  735. BOOL res;
  736. if (m_pNextLayer)
  737. res=m_pNextLayer->Accept(rConnectedSocket, lpSockAddr, lpSockAddrLen);
  738. else
  739. {
  740. SOCKET hTemp = accept(m_pOwnerSocket->m_SocketData.hSocket, lpSockAddr, lpSockAddrLen);
  741. if (hTemp == INVALID_SOCKET)
  742. return FALSE;
  743. DebugCheck(rConnectedSocket.InitAsyncSocketExInstance());
  744. rConnectedSocket.m_SocketData.hSocket=hTemp;
  745. rConnectedSocket.AttachHandle(hTemp);
  746. rConnectedSocket.SetFamily(GetFamily());
  747. rConnectedSocket.SetState(connected);
  748. }
  749. return TRUE;
  750. }
  751. BOOL CAsyncSocketExLayer::ShutDown(int nHow /*=sends*/)
  752. {
  753. return ShutDownNext(nHow);
  754. }
  755. BOOL CAsyncSocketExLayer::ShutDownNext(int nHow /*=sends*/)
  756. {
  757. if (m_nCriticalError)
  758. {
  759. WSASetLastError(m_nCriticalError);
  760. return FALSE;
  761. }
  762. else if (GetLayerState()==notsock)
  763. {
  764. WSASetLastError(WSAENOTSOCK);
  765. return FALSE;
  766. }
  767. else if (GetLayerState()==unconnected || GetLayerState()==connecting || GetLayerState()==listening)
  768. {
  769. WSASetLastError(WSAENOTCONN);
  770. return FALSE;
  771. }
  772. if (!m_pNextLayer)
  773. {
  774. DebugAssert(m_pOwnerSocket);
  775. return (shutdown(m_pOwnerSocket->GetSocketHandle(), nHow) == 0);
  776. }
  777. else
  778. {
  779. return m_pNextLayer->ShutDown(nHow);
  780. }
  781. }
  782. int CAsyncSocketExLayer::GetFamily() const
  783. {
  784. return m_nFamily;
  785. }
  786. bool CAsyncSocketExLayer::SetFamily(int nFamily)
  787. {
  788. if (m_nFamily != AF_UNSPEC)
  789. return false;
  790. m_nFamily = nFamily;
  791. return true;
  792. }
  793. bool CAsyncSocketExLayer::TryNextProtocol()
  794. {
  795. m_pOwnerSocket->DetachHandle(m_pOwnerSocket->m_SocketData.hSocket);
  796. closesocket(m_pOwnerSocket->m_SocketData.hSocket);
  797. m_pOwnerSocket->m_SocketData.hSocket = INVALID_SOCKET;
  798. BOOL ret = FALSE;
  799. for (; m_nextAddr; m_nextAddr = m_nextAddr->ai_next)
  800. {
  801. m_pOwnerSocket->m_SocketData.hSocket = socket(m_nextAddr->ai_family, m_nextAddr->ai_socktype, m_nextAddr->ai_protocol);
  802. if (m_pOwnerSocket->m_SocketData.hSocket == INVALID_SOCKET)
  803. continue;
  804. m_pOwnerSocket->AttachHandle(m_pOwnerSocket->m_SocketData.hSocket);
  805. if (!m_pOwnerSocket->AsyncSelect(m_lEvent))
  806. {
  807. m_pOwnerSocket->DetachHandle(m_pOwnerSocket->m_SocketData.hSocket);
  808. closesocket(m_pOwnerSocket->m_SocketData.hSocket);
  809. m_pOwnerSocket->m_SocketData.hSocket = INVALID_SOCKET;
  810. continue;
  811. }
  812. if (m_pOwnerSocket->m_pFirstLayer)
  813. {
  814. if (WSAAsyncSelect(m_pOwnerSocket->m_SocketData.hSocket, m_pOwnerSocket->GetHelperWindowHandle(), m_pOwnerSocket->m_SocketData.nSocketIndex+WM_SOCKETEX_NOTIFY, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE))
  815. {
  816. m_pOwnerSocket->DetachHandle(m_pOwnerSocket->m_SocketData.hSocket);
  817. closesocket(m_pOwnerSocket->m_SocketData.hSocket);
  818. m_pOwnerSocket->m_SocketData.hSocket = INVALID_SOCKET;
  819. continue;
  820. }
  821. }
  822. m_pOwnerSocket->m_SocketData.nFamily = m_nextAddr->ai_family;
  823. m_nFamily = m_nextAddr->ai_family;
  824. if (!m_pOwnerSocket->Bind(m_nSocketPort, m_lpszSocketAddress))
  825. {
  826. m_pOwnerSocket->DetachHandle(m_pOwnerSocket->m_SocketData.hSocket);
  827. closesocket(m_pOwnerSocket->m_SocketData.hSocket);
  828. m_pOwnerSocket->m_SocketData.hSocket = INVALID_SOCKET;
  829. continue;
  830. }
  831. if (connect(m_pOwnerSocket->GetSocketHandle(), m_nextAddr->ai_addr, m_nextAddr->ai_addrlen) == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK)
  832. {
  833. m_pOwnerSocket->DetachHandle(m_pOwnerSocket->m_SocketData.hSocket);
  834. closesocket(m_pOwnerSocket->m_SocketData.hSocket);
  835. m_pOwnerSocket->m_SocketData.hSocket = INVALID_SOCKET;
  836. continue;
  837. }
  838. SetLayerState(connecting);
  839. ret = true;
  840. break;
  841. }
  842. if (m_nextAddr)
  843. m_nextAddr = m_nextAddr->ai_next;
  844. if (!m_nextAddr)
  845. {
  846. freeaddrinfo(m_addrInfo);
  847. m_nextAddr = 0;
  848. m_addrInfo = 0;
  849. }
  850. if (m_pOwnerSocket->m_SocketData.hSocket == INVALID_SOCKET || !ret)
  851. return FALSE;
  852. else
  853. return TRUE;
  854. }
  855. void CAsyncSocketExLayer::LogSocketMessageRaw(int nMessageType, LPCTSTR pMsg)
  856. {
  857. if (m_pPrevLayer)
  858. m_pPrevLayer->LogSocketMessageRaw(nMessageType, pMsg);
  859. else
  860. m_pOwnerSocket->LogSocketMessageRaw(nMessageType, pMsg);
  861. }
  862. bool CAsyncSocketExLayer::LoggingSocketMessage(int nMessageType)
  863. {
  864. if (m_pPrevLayer)
  865. return m_pPrevLayer->LoggingSocketMessage(nMessageType);
  866. else
  867. return m_pOwnerSocket->LoggingSocketMessage(nMessageType);
  868. }
  869. int CAsyncSocketExLayer::GetSocketOptionVal(int OptionID) const
  870. {
  871. if (m_pPrevLayer)
  872. return m_pPrevLayer->GetSocketOptionVal(OptionID);
  873. else
  874. return m_pOwnerSocket->GetSocketOptionVal(OptionID);
  875. }