Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

701 lines
18 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name :
  4. asyncio.cxx
  5. Abstract:
  6. This module implements functions for ASYNC_IO_CONNECTION Object.
  7. Author:
  8. Murali R. Krishnan ( MuraliK ) 27-March-1995
  9. Environment:
  10. User Mode -- Win32
  11. Project:
  12. Internet Services DLL
  13. Revision History:
  14. --*/
  15. /************************************************************
  16. * Include Headers
  17. ************************************************************/
  18. // need to include ftpdp.hxx here since precompiled header used.
  19. # include "ftpdp.hxx"
  20. # include "dbgutil.h"
  21. # include "asyncio.hxx"
  22. # include "..\..\infocomm\atq\atqtypes.hxx"
  23. /************************************************************
  24. * Functions
  25. ************************************************************/
  26. ASYNC_IO_CONNECTION::ASYNC_IO_CONNECTION(
  27. IN PFN_ASYNC_IO_COMPLETION pfnAioCompletion,
  28. IN SOCKET sClient OPTIONAL
  29. )
  30. : m_pAioContext ( NULL),
  31. m_pfnAioCompletion ( pfnAioCompletion),
  32. m_sClient ( sClient),
  33. m_sTimeout ( DEFAULT_CONNECTION_IO_TIMEOUT),
  34. m_pAtqContext ( NULL),
  35. m_endpointObject ( NULL)
  36. {
  37. IF_DEBUG( ASYNC_IO) {
  38. DBGPRINTF( ( DBG_CONTEXT,
  39. " Created a new ASYNC_IO_CONNECTION object ( %08x)\n",
  40. this
  41. ));
  42. }
  43. } // ASYNC_IO_CONNECTION::ASYNC_IO_CONNECTION()
  44. ASYNC_IO_CONNECTION::~ASYNC_IO_CONNECTION( VOID)
  45. /*++
  46. This function cleans up the ASYNC_IO_CONNECTION object. It also frees
  47. up sockets and ATQ context embedded in this object.
  48. THIS IS NOT MULTI_THREAD safe!
  49. --*/
  50. {
  51. IF_DEBUG( ASYNC_IO) {
  52. DBGPRINTF( ( DBG_CONTEXT,
  53. "Deleting the ASYNC_IO_CONNECTION object ( %08x) \n",
  54. this
  55. ));
  56. }
  57. if ( m_sClient != INVALID_SOCKET) {
  58. //
  59. // Shut and Close the socket. This can fail, if the socket is already
  60. // closed by some other thread before this operation completes
  61. //
  62. StopIo( NO_ERROR);
  63. }
  64. if ( m_pAtqContext != NULL) {
  65. AtqFreeContext( m_pAtqContext, TRUE );
  66. m_pAtqContext = NULL;
  67. }
  68. DBG_ASSERT( m_sClient == INVALID_SOCKET);
  69. } // ASYNC_IO_CONNECTION::~ASYN_IO_CONNECTION()
  70. BOOL
  71. ASYNC_IO_CONNECTION::ReadFile(
  72. OUT LPVOID pvBuffer,
  73. IN DWORD cbSize
  74. )
  75. /*++
  76. This starts off an Asynchronous read operation for data from client
  77. into the supplied buffer.
  78. Arguments:
  79. pvBuffer pointer to byte buffer which on successful
  80. return will contain the data read from client
  81. cbSize count of bytes of data available in the buffer.
  82. ( limits the size of data that can be read)
  83. Returns:
  84. TRUE on success and FALSE if there is a failure in setting up read.
  85. --*/
  86. {
  87. BOOL fReturn = TRUE;
  88. DBG_ASSERT( pvBuffer != NULL);
  89. IF_DEBUG( ASYNC_IO) {
  90. DBGPRINTF( ( DBG_CONTEXT,
  91. " Entering ASYNC_IO_CONNECTION( %08x)::"
  92. "ReadFile( %08x, %u)\n",
  93. this, pvBuffer, cbSize
  94. ));
  95. }
  96. if (( m_pAtqContext == NULL && !AddToAtqHandles()) ||
  97. !AtqReadFile( m_pAtqContext, pvBuffer, cbSize, NULL )) {
  98. IF_DEBUG( ASYNC_IO) {
  99. DWORD dwError = GetLastError();
  100. DBGPRINTF(( DBG_CONTEXT,
  101. "ASYNC_IO_CONNECTION(%08x)::WriteFile() failed."
  102. " Error = %u\n",
  103. this, dwError));
  104. SetLastError( dwError);
  105. }
  106. fReturn = FALSE;
  107. }
  108. return ( fReturn);
  109. } // ASYNC_IO_CONNECTION::ReadFile()
  110. BOOL
  111. ASYNC_IO_CONNECTION::WriteFile(
  112. OUT LPVOID pvBuffer,
  113. IN DWORD cbSize
  114. )
  115. /*++
  116. This starts off an Asynchronous write operation to send data to the client
  117. sending data from the supplied buffer. The buffer may not be freed
  118. until the data is sent out.
  119. Arguments:
  120. pvBuffer pointer to byte buffer which contains the data to be sent
  121. to the client.
  122. cbSize count of bytes of data to be sent.
  123. Returns:
  124. TRUE on success and FALSE if there is a failure in setting up write.
  125. --*/
  126. {
  127. BOOL fReturn = TRUE;
  128. DBG_ASSERT( pvBuffer != NULL);
  129. IF_DEBUG( ASYNC_IO) {
  130. DBGPRINTF( ( DBG_CONTEXT,
  131. " Entering ASYNC_IO_CONNECTION( %08x)::"
  132. "WriteFile( %08x, %u)\n",
  133. this, pvBuffer, cbSize
  134. ));
  135. }
  136. //
  137. // Check and add to create an atq context as well as perform
  138. // the write operation.
  139. //
  140. if ( (m_pAtqContext == NULL && !AddToAtqHandles()) ||
  141. !AtqWriteFile( m_pAtqContext, pvBuffer, cbSize, NULL )) {
  142. IF_DEBUG( ASYNC_IO) {
  143. DWORD dwError = GetLastError();
  144. DBGPRINTF(( DBG_CONTEXT,
  145. "ASYNC_IO_CONNECTION(%08x)::WriteFile() failed."
  146. " Error = %u\n",
  147. this, dwError));
  148. SetLastError( dwError);
  149. }
  150. fReturn = FALSE;
  151. }
  152. return ( fReturn);
  153. } // ASYNC_IO_CONNECTION::WriteFile()
  154. BOOL
  155. ASYNC_IO_CONNECTION::TransmitFile(
  156. IN HANDLE hFile,
  157. IN LARGE_INTEGER & liSize,
  158. IN DWORD dwOffset,
  159. IN LPTRANSMIT_FILE_BUFFERS lpTransmitBuffers OPTIONAL
  160. )
  161. /*++
  162. This starts off an Asynchronous TransmitFile operation to send file data
  163. to the client.
  164. Arguments:
  165. hFile handle for the file to be transmitted.
  166. liSize large integer containing size of file to be sent.
  167. Offset Offset within the file to begin transmitting.
  168. lpTransmitBuffers pointer to File Transmit Buffers
  169. Returns:
  170. TRUE on success and FALSE if there is a failure in setting up read.
  171. --*/
  172. {
  173. BOOL fReturn = TRUE;
  174. DBG_ASSERT( hFile != INVALID_HANDLE_VALUE);
  175. IF_DEBUG( ASYNC_IO) {
  176. DBGPRINTF( ( DBG_CONTEXT,
  177. " Entering ASYNC_IO_CONNECTION( %08x)::"
  178. "TransmitFile( %08x, %l, %ul, %08x)\n",
  179. this, hFile, liSize.HighPart, liSize.LowPart,
  180. lpTransmitBuffers
  181. ));
  182. }
  183. if ( m_pAtqContext == NULL )
  184. {
  185. if (!AddToAtqHandles())
  186. {
  187. IF_DEBUG( ASYNC_IO)
  188. {
  189. DWORD dwError = GetLastError();
  190. DBGPRINTF(( DBG_CONTEXT,
  191. "ASYNC_IO_CONNECTION(%08x)::AddToAtqHandles() failed."
  192. " Error = %u\n",
  193. this, dwError));
  194. SetLastError( dwError);
  195. }
  196. return FALSE;
  197. }
  198. }
  199. m_pAtqContext->Overlapped.Offset = dwOffset;
  200. if (!AtqTransmitFile( m_pAtqContext,
  201. hFile,
  202. ((liSize.HighPart == 0) ? liSize.LowPart : 0),
  203. lpTransmitBuffers,
  204. TF_DISCONNECT )
  205. )
  206. {
  207. IF_DEBUG( ASYNC_IO) {
  208. DWORD dwError = GetLastError();
  209. DBGPRINTF(( DBG_CONTEXT,
  210. "ASYNC_IO_CONNECTION(%08x)::TransmitFile() failed."
  211. " Error = %u\n",
  212. this, dwError));
  213. SetLastError( dwError);
  214. }
  215. fReturn = FALSE;
  216. }
  217. return ( fReturn);
  218. } // ASYNC_IO_CONNECTION::TransmitFile()
  219. BOOL
  220. ASYNC_IO_CONNECTION::TransmitFileTs(
  221. IN TS_OPEN_FILE_INFO * pOpenFile,
  222. IN LARGE_INTEGER & liSize,
  223. IN DWORD dwOffset
  224. )
  225. /*++
  226. This starts off an Asynchronous TransmitFile operation to send file data
  227. to the client.
  228. Arguments:
  229. hFile handle for the file to be transmitted.
  230. liSize large integer containing size of file to be sent.
  231. Offset Offset within the file to begin transmitting.
  232. Returns:
  233. TRUE on success and FALSE if there is a failure in setting up read.
  234. --*/
  235. {
  236. BOOL fReturn = TRUE;
  237. PBYTE pFileBuffer = NULL;
  238. HANDLE hFile = NULL;
  239. TRANSMIT_FILE_BUFFERS TransmitBuffers;
  240. DBG_ASSERT( pOpenFile );
  241. IF_DEBUG( ASYNC_IO) {
  242. DBGPRINTF( ( DBG_CONTEXT,
  243. " Entering ASYNC_IO_CONNECTION( %08x)::"
  244. "TransmitFile( %p, %p, %ul, %08x)\n",
  245. this, pOpenFile, liSize.HighPart, liSize.LowPart
  246. ));
  247. }
  248. if ( m_pAtqContext == NULL )
  249. {
  250. if (!AddToAtqHandles())
  251. {
  252. IF_DEBUG( ASYNC_IO)
  253. {
  254. DWORD dwError = GetLastError();
  255. DBGPRINTF(( DBG_CONTEXT,
  256. "ASYNC_IO_CONNECTION(%08x)::AddToAtqHandles() failed."
  257. " Error = %u\n",
  258. this, dwError));
  259. SetLastError( dwError);
  260. }
  261. return FALSE;
  262. }
  263. }
  264. pFileBuffer = pOpenFile->QueryFileBuffer();
  265. if (pFileBuffer) {
  266. //
  267. // Transmit from memory
  268. //
  269. DBG_ASSERT( liSize.HighPart == 0 );
  270. TransmitBuffers.Head = pFileBuffer + dwOffset;
  271. TransmitBuffers.HeadLength = liSize.LowPart;
  272. TransmitBuffers.Tail = NULL;
  273. TransmitBuffers.TailLength = 0;
  274. } else {
  275. //
  276. // Transmit from a file
  277. //
  278. hFile = pOpenFile->QueryFileHandle();
  279. m_pAtqContext->Overlapped.Offset = dwOffset;
  280. if (liSize.HighPart != 0)
  281. {
  282. LARGE_INTEGER liTimeOut;
  283. ULONG Remainder;
  284. liTimeOut =
  285. RtlExtendedLargeIntegerDivide(
  286. liSize,
  287. (ULONG) 1024,
  288. &Remainder);
  289. if (liTimeOut.HighPart != 0)
  290. {
  291. ((PATQ_CONT) m_pAtqContext)->TimeOut = ATQ_INFINITE;
  292. } else
  293. {
  294. ((PATQ_CONT) m_pAtqContext)->TimeOut = liTimeOut.LowPart;
  295. }
  296. }
  297. }
  298. if (!AtqTransmitFile( m_pAtqContext,
  299. hFile,
  300. ((liSize.HighPart == 0) ? liSize.LowPart : 0),
  301. pFileBuffer ? &TransmitBuffers : NULL,
  302. TF_DISCONNECT )
  303. )
  304. {
  305. IF_DEBUG( ASYNC_IO) {
  306. DWORD dwError = GetLastError();
  307. DBGPRINTF(( DBG_CONTEXT,
  308. "ASYNC_IO_CONNECTION(%08x)::TransmitFile() failed."
  309. " Error = %u\n",
  310. this, dwError));
  311. SetLastError( dwError);
  312. }
  313. fReturn = FALSE;
  314. }
  315. return ( fReturn);
  316. } // ASYNC_IO_CONNECTION::TransmitFile()
  317. BOOL
  318. ASYNC_IO_CONNECTION::StopIo( IN DWORD dwErrorCode OPTIONAL)
  319. /*++
  320. This function stops the io connection by performing a hard close on
  321. the socket that is used for IO. that is the only way one can easily kill the
  322. IO that is in progress.
  323. Arguments:
  324. dwErrorCode DWORD containing the error code for stopping IO
  325. Returns:
  326. TRUE on success and FALSE if there is a failure.
  327. --*/
  328. {
  329. INT serr = 0;
  330. IF_DEBUG( ASYNC_IO) {
  331. DBGPRINTF( ( DBG_CONTEXT,
  332. " ASYNC_IO_CONNECTION( %08x)::StopIo( %u)\n",
  333. this, dwErrorCode
  334. ));
  335. }
  336. //
  337. // NYI! dwErrorCode is not at present used.
  338. //
  339. if ( m_sClient != INVALID_SOCKET) {
  340. SOCKET sOld = m_sClient;
  341. m_sClient = INVALID_SOCKET;
  342. // MuraliK 07/25/95 Shutdown causes problems in sending last msg.
  343. # ifdef ENABLE_SHUT_DOWN
  344. // Shut the socket and close it
  345. // even if shut fails, still go ahead and close
  346. if ( shutdown( sOld, 0) == SOCKET_ERROR) {
  347. IF_DEBUG( ASYNC_IO) {
  348. DBGPRINTF((DBG_CONTEXT,
  349. " ASYNC_IO_CONNECTION( %08x)::StopIo( %u)."
  350. "shutdown(%08x,1) failed. Error = %d\n",
  351. this, dwErrorCode, sOld, WSAGetLastError()));
  352. }
  353. DBGERROR((DBG_CONTEXT, "shutdown(%u, 0) failed. Error=%u\n",
  354. sOld, WSAGetLastError()));
  355. }
  356. #endif // ENABLE_SHUT_DOWN
  357. //
  358. // patch added on 11/2/95
  359. // After AcceptEx addition, closing the ATQ'ed socket is
  360. // is to be done by ATQ module.
  361. //
  362. if ( sOld != INVALID_SOCKET) {
  363. if (m_pAtqContext != NULL) {
  364. //
  365. // per the FTP RFC, the server must close the socket when killing a data
  366. // channel.
  367. //
  368. if (!AtqCloseSocket( m_pAtqContext, TRUE)) {
  369. serr = GetLastError();
  370. }
  371. } else {
  372. // Ignore failures in Shutdown and close socket.
  373. if (closesocket( sOld) == SOCKET_ERROR) {
  374. serr = WSAGetLastError();
  375. }
  376. }
  377. if ( serr != 0 ) {
  378. SetLastError( serr);
  379. }
  380. }
  381. }
  382. return ( serr == 0);
  383. } // ASYNC_IO_CONNECTION::StopIo()
  384. BOOL
  385. ASYNC_IO_CONNECTION::SetNewSocket(IN SOCKET sNewSocket,
  386. IN PATQ_CONTEXT pNewAtqContext, // = NULL
  387. IN PVOID EndpointObject )
  388. /*++
  389. This function changes the socket maintained for given ASYNC_IO_CONNECTION
  390. object. It changes it only if the current socket in the object is already
  391. freed (by calling StopIo()).
  392. If the Atq Context in this object is a valid one corresponding to old
  393. socket, it is also freed. So any new operation will create a new AtqContext.
  394. (This is essential, since there is a one-to-one-relationship between socket
  395. and ATQ context)
  396. Arguments:
  397. sNewSocket new socket for the connection
  398. If sNewSocket == INVALID_SOCKET then this function does
  399. cleanup of old information.
  400. pNewAtqContext new ATQ Context for the socket
  401. Returns:
  402. TRUE on success and FALSE if there is any failure.
  403. --*/
  404. {
  405. BOOL fReturn = TRUE;
  406. if ( m_sClient == INVALID_SOCKET) {
  407. //
  408. // Free the Atq Context if one already exists.
  409. // ==> Reason: There is a one-to-one correspondence b/w ATQ Context
  410. // and socket. The atq context if valid was created
  411. // for old connection.
  412. //
  413. // To avoid race conditions, we exchange pointer with NULL
  414. // and later on free the object as need be.
  415. // Should we necessarily use InterlockedExchange() ???
  416. // Isn't it costly? NYI
  417. PATQ_CONTEXT pAtqContext =
  418. (PATQ_CONTEXT ) InterlockedExchangePointer( (PVOID *) &m_pAtqContext,
  419. (PVOID) pNewAtqContext);
  420. if ( pAtqContext != NULL) {
  421. AtqFreeContext( pAtqContext, TRUE );
  422. }
  423. m_sClient = sNewSocket;
  424. m_endpointObject = EndpointObject;
  425. } else {
  426. SetLastError( ERROR_INVALID_PARAMETER);
  427. fReturn = FALSE;
  428. }
  429. return ( fReturn);
  430. } // ASYNC_IO_CONNECTION::SetNewSocket()
  431. # if DBG
  432. VOID ASYNC_IO_CONNECTION::Print( VOID) const
  433. {
  434. DBGPRINTF( ( DBG_CONTEXT,
  435. " Printing ASYNC_IO_CONNECTION( %08x)\n"
  436. " CallBackFunction = %08x; Context = %08x\n"
  437. " Client Socket = %u; AtqContext = %08x;"
  438. " Timeout = %u sec; \n",
  439. this, m_pfnAioCompletion, m_pAioContext,
  440. m_sClient, m_pAtqContext, m_sTimeout));
  441. return;
  442. } // ASYNC_IO_CONNECTION::Print()
  443. # endif // DBG
  444. VOID
  445. ProcessAtqCompletion(IN LPVOID pContext,
  446. IN DWORD cbIo,
  447. IN DWORD dwCompletionStatus,
  448. IN OVERLAPPED * lpo
  449. )
  450. /*++
  451. This function processes the completion of an atq operation.
  452. It also sends a call back to the owner of this object ( ASYNC_IO_CONNECTION)
  453. object, once the operation is completed or if it is in error.
  454. ATQ module sends 2 messages whenever there is a timeout.
  455. Reason: The timeout itself is sent by a separate thread and completion port
  456. API does not support removal of a socket from the completion port. Combining
  457. these together, ATQ sends a separate timeout message and then when the
  458. application blows off the socket/handle, ATQ sends another error message
  459. implying failure of the connection.
  460. We handle this as follows:
  461. At timeout ATQ sends fIOCompletion == FALSE and
  462. dwCompletionStatus == ERROR_SEM_TIMEOUT.
  463. We send this as fTimedOut in call back.
  464. The application can check if it is fTimedOut and hence refrain from
  465. blowing the object completely away.
  466. Arguments:
  467. pContext pointer to User supplied context information
  468. ( here: pointer to ASYNC_IO_CONNECTION associated with
  469. the IO completed)
  470. cbIo count of bytes of IO performed
  471. dwCompletionStatus DWORD containing error code if any
  472. lpo - !NULL if completion from IO
  473. Returns:
  474. None
  475. --*/
  476. {
  477. LPASYNC_IO_CONNECTION pConn = (LPASYNC_IO_CONNECTION ) pContext;
  478. BOOL fTimedOut = FALSE;
  479. if ( pConn == NULL) {
  480. // This should not happen....
  481. SetLastError( ERROR_INVALID_PARAMETER);
  482. DBG_ASSERT( pConn == NULL);
  483. return ;
  484. }
  485. IF_DEBUG( ASYNC_IO) {
  486. DBGPRINTF( ( DBG_CONTEXT,
  487. "ProcessAtqCompletion(Aio=%08x, cb=%u, Status=%u,"
  488. "IO Compltion=%s).\n",
  489. pConn, cbIo, dwCompletionStatus,
  490. lpo != NULL ? "TRUE" : "FALSE" ));
  491. }
  492. if ( lpo != NULL ||
  493. (fTimedOut = (
  494. lpo == NULL &&
  495. dwCompletionStatus == ERROR_SEM_TIMEOUT))
  496. ) {
  497. //
  498. // This is the right Atq object. Process the response by passing it
  499. // to the owner of this object.
  500. //
  501. DBG_ASSERT( pConn->QueryPfnAioCompletion() != NULL);
  502. //
  503. // Invoke the call back function for completion of IO.
  504. //
  505. ( *pConn->QueryPfnAioCompletion())
  506. (pConn->QueryAioContext(), cbIo, dwCompletionStatus,
  507. pConn, fTimedOut);
  508. }
  509. return;
  510. } // ProcessAtqCompletion()
  511. /************************ End of File ***********************/