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.

373 lines
11 KiB

  1. //---------------------------------------------------------------
  2. // Copyright (C)1998 Microsoft Corporation, All Rights Reserved.
  3. //
  4. // conn.h
  5. //
  6. // Connection mapping between sockets and CCONNECTION objects.
  7. //
  8. // Author:
  9. //
  10. // Edward Reus (edwardr) 02-26-98 Initial coding.
  11. //
  12. //---------------------------------------------------------------
  13. #ifndef __CONN_HXX__
  14. #define __CONN_HXX__
  15. #define MAX_MAP_ENTRIES 16
  16. #include "irtypes.h"
  17. //---------------------------------------------------------------
  18. // Class CCONNECTION
  19. //---------------------------------------------------------------
  20. class CCONNECTION
  21. {
  22. public:
  23. // CCONNECTION();
  24. CCONNECTION( DWORD dwKind = PACKET_KIND_LISTEN,
  25. SOCKET Socket = 0,
  26. HANDLE hIoCP = INVALID_HANDLE_VALUE,
  27. CSCEP_CONNECTION *pScepConnection = 0,
  28. BOOL fSaveAsUPF = FALSE );
  29. ~CCONNECTION();
  30. void *operator new( IN size_t Size );
  31. void operator delete( IN void *pObj,
  32. IN size_t Size );
  33. DWORD InitializeForListen( IN char *pszServiceName,
  34. IN BOOL fIsIrCOMM,
  35. IN HANDLE hIoCP );
  36. char *GetServiceName();
  37. void SetKind( DWORD dwKind );
  38. DWORD GetKind();
  39. void SetSocket( SOCKET Socket );
  40. SOCKET GetSocket();
  41. DWORD ShutdownSocket();
  42. void CloseSocket();
  43. void SetListenSocket( SOCKET ListenSocket );
  44. SOCKET GetListenSocket();
  45. void CloseListenSocket();
  46. void SetIoCompletionPort( HANDLE hIoCP );
  47. HANDLE GetIoCompletionPort();
  48. void SetScepConnection( CSCEP_CONNECTION *pScepConnection );
  49. CSCEP_CONNECTION *GetScepConnection();
  50. void SetJpegOffsetAndSize( DWORD dwOffset,
  51. DWORD dwSize );
  52. LONG IncrementPendingReads();
  53. LONG DecrementPendingReads();
  54. LONG IncrementPendingWrites();
  55. LONG DecrementPendingWrites();
  56. LONG NumPendingIos();
  57. DWORD PostMoreIos( CIOPACKET *pIoPacket = NULL );
  58. DWORD SendPdu( IN SCEP_HEADER *pPdu,
  59. IN DWORD dwPduSize,
  60. OUT CIOPACKET **ppIoPacket );
  61. static WCHAR *ConstructPicturesSubDirectory( IN DWORD dwExtraChars = 0 );
  62. static BOOL SetThumbnailView( IN WCHAR *pwszParentDirectoryName,
  63. IN WCHAR *pwszDirectoryName );
  64. WCHAR *ConstructFullFileName( IN DWORD dwCopyCount );
  65. BOOL CheckSaveAsUPF();
  66. DWORD Impersonate();
  67. DWORD RevertToSelf();
  68. DWORD CreatePictureFile();
  69. DWORD SetPictureFileTime( IN FILETIME *pFileTime );
  70. DWORD WritePictureFile( IN UCHAR *pBuffer,
  71. IN DWORD dwBufferSize,
  72. OUT CIOPACKET **ppIoPacket );
  73. DWORD DeletePictureFile();
  74. DWORD ClosePictureFile();
  75. BOOL IncompleteFile();
  76. void SetReceiveComplete( IN BOOL fReceiveComplete );
  77. void SetUiCookie( COOKIE cookie );
  78. COOKIE GetUiCookie();
  79. private:
  80. static void CleanupDateString( IN OUT WCHAR *pszDateStr );
  81. DWORD m_dwKind;
  82. char *m_pszServiceName; // Service name (for Listen Sockets).
  83. HANDLE m_hIoCompletionPort;
  84. SOCKET m_ListenSocket;
  85. SOCKET m_Socket;
  86. HANDLE m_hFile;
  87. WCHAR *m_pwszPathPlusFileName;
  88. DWORD m_dwFileBytesWritten;
  89. LONG m_lMaxPendingReads;
  90. LONG m_lPendingReads;
  91. LONG m_lMaxPendingWrites;
  92. LONG m_lPendingWrites;
  93. DWORD m_dwJpegOffset; // Offset in UPF file of JPEG image.
  94. DWORD m_dwJpegSize; // Size of JPEG image in UPF file.
  95. BOOL m_fSaveAsUPF; // If TRUE, write the entire UPF file.
  96. DWORD m_dwUpfBytes; // Total UPF bytes read in from Camera.
  97. DWORD m_dwBytesWritten; // Actual number of bytes written to disk.
  98. BOOL m_fReceiveComplete; // Set to TRUE when a SCEP disconnect
  99. // packet is received from the camera.
  100. BOOL m_fImpersonating; // TRUE iff we are currently impersonating.
  101. COOKIE m_Cookie;
  102. CSCEP_CONNECTION *m_pScepConnection;
  103. EXECUTION_STATE m_ExecutionState;
  104. };
  105. //---------------------------------------------------------------
  106. // Class CCONNECTION_MAP
  107. //---------------------------------------------------------------
  108. typedef struct _CONNECTION_MAP_ENTRY
  109. {
  110. SOCKET Socket;
  111. CCONNECTION *pConnection;
  112. } CONNECTION_MAP_ENTRY;
  113. class CCONNECTION_MAP
  114. {
  115. public:
  116. CCONNECTION_MAP();
  117. ~CCONNECTION_MAP();
  118. void *operator new( IN size_t Size );
  119. void operator delete( IN void *pObj,
  120. IN size_t Size );
  121. BOOL Initialize( DWORD dwMapSize = MAX_MAP_ENTRIES );
  122. // Lookup
  123. CCONNECTION *Lookup( IN SOCKET Socket );
  124. // Lookup Connection by name:
  125. CCONNECTION *LookupByServiceName( IN char *pszServiceName );
  126. // Add a new (value,key) pair
  127. BOOL Add( IN CCONNECTION *pConnection,
  128. IN SOCKET Socket );
  129. // Remove an entry from the mapping
  130. CCONNECTION *Remove( IN SOCKET Socket );
  131. CCONNECTION *RemoveConnection( IN CCONNECTION *pConnection );
  132. // Remove the "next" entry from the mapping
  133. CCONNECTION *RemoveNext();
  134. private:
  135. RTL_CRITICAL_SECTION m_cs;
  136. DWORD m_dwMapSize;
  137. CONNECTION_MAP_ENTRY *m_pMap;
  138. };
  139. //---------------------------------------------------------------
  140. // CCONNECTION::GetServiceName()
  141. //---------------------------------------------------------------
  142. inline char *CCONNECTION::GetServiceName()
  143. {
  144. return m_pszServiceName;
  145. }
  146. //---------------------------------------------------------------
  147. // CCONNECTION::SetKind()
  148. //---------------------------------------------------------------
  149. inline void CCONNECTION::SetKind( DWORD dwKind )
  150. {
  151. m_dwKind = dwKind;
  152. if (m_dwKind == PACKET_KIND_LISTEN)
  153. {
  154. m_lMaxPendingReads = MAX_PENDING_LISTEN;
  155. }
  156. else if (m_dwKind == PACKET_KIND_READ)
  157. {
  158. m_lMaxPendingReads = MAX_PENDING_READ;
  159. }
  160. }
  161. //---------------------------------------------------------------
  162. // CCONNECTION::GetKind()
  163. //---------------------------------------------------------------
  164. inline DWORD CCONNECTION::GetKind()
  165. {
  166. return m_dwKind;
  167. }
  168. //---------------------------------------------------------------
  169. // CCONNECTION::SetSocket()
  170. //---------------------------------------------------------------
  171. inline void CCONNECTION::SetSocket( SOCKET Socket )
  172. {
  173. m_Socket = Socket;
  174. }
  175. //---------------------------------------------------------------
  176. // CCONNECTION::GetSocket()
  177. //---------------------------------------------------------------
  178. inline SOCKET CCONNECTION::GetSocket()
  179. {
  180. return m_Socket;
  181. }
  182. //---------------------------------------------------------------
  183. // CCONNECTION::SetListenSocket()
  184. //---------------------------------------------------------------
  185. inline void CCONNECTION::SetListenSocket( SOCKET ListenSocket )
  186. {
  187. m_ListenSocket = ListenSocket;
  188. }
  189. //---------------------------------------------------------------
  190. // CCONNECTION::GetListenSocket()
  191. //---------------------------------------------------------------
  192. inline SOCKET CCONNECTION::GetListenSocket()
  193. {
  194. return m_ListenSocket;
  195. }
  196. //---------------------------------------------------------------
  197. // CCONNECTION::IncrementPendingReads()
  198. //---------------------------------------------------------------
  199. inline LONG CCONNECTION::IncrementPendingReads()
  200. {
  201. return InterlockedIncrement(&m_lPendingReads);
  202. }
  203. //---------------------------------------------------------------
  204. // CCONNECTION::DecrementPendingReads()
  205. //---------------------------------------------------------------
  206. inline LONG CCONNECTION::DecrementPendingReads()
  207. {
  208. return InterlockedDecrement(&m_lPendingReads);
  209. }
  210. //---------------------------------------------------------------
  211. // CCONNECTION::IncrementPendingWrites()
  212. //---------------------------------------------------------------
  213. inline LONG CCONNECTION::IncrementPendingWrites()
  214. {
  215. return InterlockedIncrement(&m_lPendingWrites);
  216. }
  217. //---------------------------------------------------------------
  218. // CCONNECTION::DecrementPendingReads()
  219. //---------------------------------------------------------------
  220. inline LONG CCONNECTION::DecrementPendingWrites()
  221. {
  222. return InterlockedDecrement(&m_lPendingWrites);
  223. }
  224. //---------------------------------------------------------------
  225. // CCONNECTION::NumPendingIos()
  226. //---------------------------------------------------------------
  227. inline LONG CCONNECTION::NumPendingIos()
  228. {
  229. return m_lPendingReads + m_lPendingWrites;
  230. }
  231. //---------------------------------------------------------------
  232. // CCONNECTION::SetIoCompletionPort()
  233. //---------------------------------------------------------------
  234. inline void CCONNECTION::SetIoCompletionPort( HANDLE hIoCP )
  235. {
  236. m_hIoCompletionPort = hIoCP;
  237. }
  238. //---------------------------------------------------------------
  239. // CCONNECTION::GetIoCompletionPort()
  240. //---------------------------------------------------------------
  241. inline HANDLE CCONNECTION::GetIoCompletionPort()
  242. {
  243. return m_hIoCompletionPort;
  244. }
  245. //---------------------------------------------------------------
  246. // CCONNECTION::SetScepConnection()
  247. //---------------------------------------------------------------
  248. inline void CCONNECTION::SetScepConnection( CSCEP_CONNECTION *pScepConnection )
  249. {
  250. m_pScepConnection = pScepConnection;
  251. }
  252. //---------------------------------------------------------------
  253. // CCONNECTION::GetScepConnection()
  254. //---------------------------------------------------------------
  255. inline CSCEP_CONNECTION *CCONNECTION::GetScepConnection()
  256. {
  257. return m_pScepConnection;
  258. }
  259. //---------------------------------------------------------------
  260. // CCONNECTION::SetJpegOffset()
  261. //---------------------------------------------------------------
  262. inline void CCONNECTION::SetJpegOffsetAndSize( IN DWORD dwOffset,
  263. IN DWORD dwSize )
  264. {
  265. m_dwJpegOffset = dwOffset;
  266. m_dwJpegSize = dwSize;
  267. }
  268. //---------------------------------------------------------------
  269. // CCONNECTION::CheckSaveAsUPF()
  270. //---------------------------------------------------------------
  271. inline BOOL CCONNECTION::CheckSaveAsUPF()
  272. {
  273. return m_fSaveAsUPF;
  274. }
  275. //---------------------------------------------------------------
  276. // CCONNECTION::SetReceiveComplete()
  277. //---------------------------------------------------------------
  278. inline void CCONNECTION::SetReceiveComplete( IN BOOL fReceiveComplete )
  279. {
  280. m_fReceiveComplete = fReceiveComplete;
  281. }
  282. //---------------------------------------------------------------
  283. // CCONNECTION::SetUiCookie()
  284. //---------------------------------------------------------------
  285. inline void
  286. CCONNECTION::SetUiCookie(
  287. COOKIE cookie
  288. )
  289. {
  290. m_Cookie = cookie;
  291. }
  292. //---------------------------------------------------------------
  293. // CCONNECTION::GetUiCookie()
  294. //---------------------------------------------------------------
  295. inline COOKIE
  296. CCONNECTION::GetUiCookie(
  297. )
  298. {
  299. return m_Cookie;
  300. }
  301. #endif