Leaked source code of windows server 2003
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.

274 lines
7.5 KiB

  1. //--------------------------------------------------------------------
  2. // Copyright (c)1998 Microsoft Corporation, All Rights Reserved.
  3. //
  4. // io.cpp
  5. //
  6. // Author:
  7. //
  8. // Edward Reus (edwardr) 02-27-98 Initial coding.
  9. //
  10. //--------------------------------------------------------------------
  11. #include "precomp.h"
  12. #ifdef DBG_MEM
  13. static LONG g_lCIoPacketCount = 0;
  14. #endif
  15. extern HINSTANCE g_hInst; // ircamera.dll instance.
  16. //--------------------------------------------------------------------
  17. // CIOPACKET::CIOPACKET()
  18. //
  19. //--------------------------------------------------------------------
  20. CIOPACKET::CIOPACKET()
  21. {
  22. m_dwKind = PACKET_KIND_LISTEN;
  23. m_ListenSocket = INVALID_SOCKET;
  24. m_Socket = INVALID_SOCKET;
  25. m_hFile = INVALID_HANDLE_VALUE;
  26. m_pLocalAddr = 0;
  27. m_pFromAddr = 0;
  28. m_pAcceptBuffer = 0;
  29. m_pReadBuffer = 0;
  30. m_pvWritePdu = 0;
  31. m_dwReadBufferSize = 0;
  32. }
  33. //--------------------------------------------------------------------
  34. // CIOPACKET::~CIOPACKET()
  35. //
  36. //--------------------------------------------------------------------
  37. CIOPACKET::~CIOPACKET()
  38. {
  39. // NOTE: Don't free m_pLocalAddr or m_pFromAddr, they just point
  40. // into m_pAcceptBuffer.
  41. if (m_pAcceptBuffer)
  42. {
  43. FreeMemory(m_pAcceptBuffer);
  44. }
  45. if (m_pReadBuffer)
  46. {
  47. FreeMemory(m_pReadBuffer);
  48. }
  49. // NOTE: Don't delete the write PDU (m_pvWritePdu), its free'd by
  50. // somebody else (when the IO completes)...
  51. }
  52. //------------------------------------------------------------------------
  53. // CIOPACKET::operator new()
  54. //
  55. //------------------------------------------------------------------------
  56. void *CIOPACKET::operator new( IN size_t Size )
  57. {
  58. void *pObj = AllocateMemory(Size);
  59. #ifdef DBG_MEM
  60. if (pObj)
  61. {
  62. InterlockedIncrement(&g_lCIoPacketCount);
  63. }
  64. WIAS_TRACE((g_hInst,"new CIOPACKET: Count: %d\n",g_lCIoPacketCount));
  65. #endif
  66. return pObj;
  67. }
  68. //------------------------------------------------------------------------
  69. // CIOPACKET::operator delete()
  70. //
  71. //------------------------------------------------------------------------
  72. void CIOPACKET::operator delete( IN void *pObj,
  73. IN size_t Size )
  74. {
  75. if (pObj)
  76. {
  77. DWORD dwStatus = FreeMemory(pObj);
  78. #ifdef DBG_MEM
  79. if (dwStatus)
  80. {
  81. WIAS_TRACE((g_hInst,"IrXfer: IrTran-P: CIOPACKET::delete: FreeMemory Failed: %d\n",dwStatus));
  82. }
  83. InterlockedDecrement(&g_lCIoPacketCount);
  84. if (g_lCIoPacketCount < 0)
  85. {
  86. WIAS_TRACE((g_hInst,"IrXfer: IrTran-P: CIOPACKET::delete Count: %d\n",g_lCIoPacketCount));
  87. }
  88. #endif
  89. }
  90. }
  91. //--------------------------------------------------------------------
  92. // CIOPACKET::Initialize()
  93. //
  94. //--------------------------------------------------------------------
  95. DWORD CIOPACKET::Initialize( IN DWORD dwKind,
  96. IN SOCKET ListenSocket,
  97. IN SOCKET Socket,
  98. IN HANDLE hIoCP )
  99. {
  100. DWORD dwStatus = NO_ERROR;
  101. m_dwKind = dwKind;
  102. if (dwKind == PACKET_KIND_LISTEN)
  103. {
  104. // The accept buffer needs to be large enough to hold
  105. // the "from" and "to" addresses:
  106. m_pAcceptBuffer = AllocateMemory(2*(16+sizeof(SOCKADDR_IRDA)));
  107. if (!m_pAcceptBuffer)
  108. {
  109. return ERROR_OUTOFMEMORY;
  110. }
  111. }
  112. m_ListenSocket = ListenSocket;
  113. m_Socket = Socket;
  114. return dwStatus;
  115. }
  116. //--------------------------------------------------------------------
  117. // CIOPACKET::PostIoRead()
  118. //
  119. //--------------------------------------------------------------------
  120. DWORD CIOPACKET::PostIoRead()
  121. {
  122. DWORD dwStatus = NO_ERROR;
  123. DWORD dwBytes;
  124. if (!m_pReadBuffer)
  125. {
  126. m_pReadBuffer = AllocateMemory(DEFAULT_READ_BUFFER_SIZE);
  127. m_dwReadBufferSize = DEFAULT_READ_BUFFER_SIZE;
  128. if (!m_pReadBuffer)
  129. {
  130. return ERROR_OUTOFMEMORY;
  131. }
  132. }
  133. memset(&m_Overlapped,0,sizeof(m_Overlapped));
  134. BOOL b = ReadFile( (HANDLE)m_Socket,
  135. m_pReadBuffer,
  136. m_dwReadBufferSize,
  137. 0, // Can be zero for overlapped IO.
  138. &m_Overlapped );
  139. if (!b)
  140. {
  141. dwStatus = GetLastError();
  142. if ((dwStatus == ERROR_HANDLE_EOF)||(dwStatus == ERROR_IO_PENDING))
  143. {
  144. dwStatus = NO_ERROR;
  145. }
  146. }
  147. #ifdef DBG_IO
  148. WIAS_TRACE((g_hInst,"CIOPACKET::PostIoRead(): ReadFile(): Socket: %d",m_Socket));
  149. #endif
  150. return dwStatus;
  151. }
  152. //--------------------------------------------------------------------
  153. // CIOPACKET::PostIoWrite()
  154. //
  155. //--------------------------------------------------------------------
  156. DWORD CIOPACKET::PostIoWrite( IN void *pvBuffer,
  157. IN DWORD dwBufferSize,
  158. IN DWORD dwOffset )
  159. {
  160. DWORD dwStatus = NO_ERROR;
  161. DWORD dwBytes;
  162. HANDLE hFile;
  163. memset(&m_Overlapped,0,sizeof(m_Overlapped));
  164. if (m_dwKind == PACKET_KIND_WRITE_SOCKET)
  165. {
  166. hFile = (HANDLE)m_Socket;
  167. int iBytes = send(m_Socket,(const char*)pvBuffer,dwBufferSize,0);
  168. if (iBytes == SOCKET_ERROR)
  169. {
  170. dwStatus = WSAGetLastError();
  171. WIAS_ERROR((g_hInst,"CIOPACKET::PostIoWrite(): send() Failed: Handle: %d Error: %d\n",m_Socket,dwStatus));
  172. return dwStatus;
  173. }
  174. }
  175. else if (m_dwKind == PACKET_KIND_WRITE_FILE)
  176. {
  177. hFile = m_hFile;
  178. m_Overlapped.Offset = dwOffset;
  179. DWORD dwBytesWritten = 0;
  180. BOOL b = WriteFile( hFile,
  181. pvBuffer,
  182. dwBufferSize,
  183. &dwBytesWritten,
  184. 0 );
  185. if (!b)
  186. {
  187. dwStatus = GetLastError();
  188. WIAS_ERROR((g_hInst,"CIOPACKET::PostIoWrite(): WriteFile() Failed: Handle: %d Error: %d\n",hFile,dwStatus));
  189. return dwStatus;
  190. }
  191. }
  192. else
  193. {
  194. WIAS_TRACE((g_hInst,"CIOPACKET::PostIoWrite(): Invalid m_dwKind: %d.\n",m_dwKind));
  195. dwStatus = ERROR_INVALID_PARAMETER;
  196. }
  197. #ifdef DBG_IO
  198. WIAS_TRACE((g_hInst,"CIOPACKET::PostIoWrite(): WriteFile(): Handle: %d Bytes: %d\n", hFile, dwBufferSize ));
  199. #endif
  200. return dwStatus;
  201. }
  202. //--------------------------------------------------------------------
  203. // CIOPACKET::PostIo()
  204. //
  205. //--------------------------------------------------------------------
  206. DWORD CIOPACKET::PostIo()
  207. {
  208. DWORD dwStatus = NO_ERROR;
  209. DWORD dwBytes;
  210. if (m_dwKind == PACKET_KIND_LISTEN)
  211. {
  212. }
  213. else if (m_dwKind == PACKET_KIND_READ)
  214. {
  215. dwStatus = PostIoRead();
  216. }
  217. else
  218. {
  219. // Packet writes back to the camera (via socket) and writes to
  220. // the image (jpeg) file are posted only when data is ready to
  221. // send...
  222. WIAS_ASSERT( g_hInst,
  223. (m_dwKind == PACKET_KIND_WRITE_SOCKET)
  224. || (m_dwKind == PACKET_KIND_WRITE_FILE) );
  225. }
  226. return dwStatus;
  227. }