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.

385 lines
12 KiB

  1. //--------------------------------------------------------------------
  2. // Copyright (C)1998 Microsoft Corporation, All Rights Reserved.
  3. //
  4. // io.h
  5. //
  6. // Author:
  7. //
  8. // Edward Reus (edwardr) 02-24-98 Initial coding.
  9. //
  10. //--------------------------------------------------------------------
  11. #ifndef _IO_H_
  12. #define _IO_H_
  13. // Different debug flags for various aspects of the code:
  14. #ifdef DBG
  15. // #define DBG_ERROR
  16. // #define DBG_IO
  17. // #define DBG_TARGET_DIR
  18. // #define DBG_MEM
  19. // #define DBG_MEM_VALIDATE
  20. // #define DBG_ASSEMBLE
  21. // #define DBG_DATE
  22. // #define DBG_REGISTRY
  23. // #define DBG_RETURN_STATUS
  24. // #define DBG_IMPERSONATE
  25. // #define DBG_PROPERTIES
  26. #endif
  27. // Maximum number of "Copy XX of" files to create:
  28. #define MAX_COPYOF_TRIES 100
  29. // CIOPACKET objects can have the following kinds:
  30. #define PACKET_KIND_LISTEN 0
  31. #define PACKET_KIND_READ 1
  32. #define PACKET_KIND_WRITE_SOCKET 2
  33. #define PACKET_KIND_WRITE_FILE 3
  34. // The number of pending IOs depends on what you are doing:
  35. #define MAX_PENDING_LISTEN 1
  36. #define MAX_PENDING_READ 2
  37. #define MAX_PENDING_WRITE 3
  38. // This dwKey value for the key in IO completion is a special
  39. // value used to shutdown the IrTran-P thread:
  40. #define IOKEY_SHUTDOWN 0xFFFFFFFF
  41. // This is the default size for the read buffer in IO reads
  42. // posted to the IO completion port:
  43. #define DEFAULT_READ_BUFFER_SIZE 4096
  44. // Used in setting up the IrCOMM listen socket:
  45. #define IAS_SET_ATTRIB_MAX_LEN 32
  46. #define IAS_QUERY_ATTRIB_MAX_LEN IAS_SET_ATTRIB_MAX_LEN
  47. #define IRDA_PARAMETERS "Parameters"
  48. #define OCTET_SEQ_SIZE 6
  49. #define OCTET_SEQ "\000\001\006\001\001\001"
  50. // The names of the services we will provide listen sockets for:
  51. #define IRTRANP_SERVICE "IrTranPv1"
  52. #define IRCOMM_9WIRE "IrDA:IrCOMM"
  53. // The status of the listen socket for each service:
  54. #define STATUS_STOPPED 0
  55. #define STATUS_RUNNING 1
  56. // Registry paths and value names:
  57. #define REG_PATH_HKCU "Control Panel\\Infrared\\IrTranP"
  58. #define REG_DWORD_SAVE_AS_UPF "SaveAsUPF"
  59. #define REG_DWORD_DISABLE_IRTRANP "DisableIrTranPv1"
  60. #define REG_DWORD_DISABLE_IRCOMM "DisableIrCOMM"
  61. #define REG_DWORD_EXPLORE "ExploreOnCompletion"
  62. #define REG_SZ_DESTINATION "RecvdFileLocation"
  63. // Last chance location to put image files.
  64. #define WSZ_BACKUP_MY_PICTURES TEXT("\\My Pictures")
  65. #define WSZ_BACKUP_DRIVE TEXT("C:")
  66. // File Suffix:
  67. #define PERIOD L'.'
  68. #define WSZ_JPEG TEXT(".JPG")
  69. #define WSZ_UPF TEXT(".UPF")
  70. // Forward reference:
  71. class CIOSTATUS;
  72. extern "C" DWORD ProcessIoPackets( CIOSTATUS *pIoStatus );
  73. //--------------------------------------------------------------------
  74. // Global functions (in irtranp.cpp)
  75. //--------------------------------------------------------------------
  76. extern HANDLE GetUserToken();
  77. extern handle_t GetRpcBinding();
  78. extern BOOL CheckSaveAsUPF();
  79. extern BOOL CheckExploreOnCompletion();
  80. extern BOOL ReceivesAllowed();
  81. extern WCHAR *GetUserDirectory();
  82. //--------------------------------------------------------------------
  83. // class CIOPACKET
  84. //--------------------------------------------------------------------
  85. class CIOPACKET
  86. {
  87. public:
  88. CIOPACKET();
  89. ~CIOPACKET();
  90. void *operator new( IN size_t Size );
  91. void operator delete( IN void *pObj,
  92. IN size_t Size );
  93. DWORD Initialize( IN DWORD dwKind = PACKET_KIND_LISTEN,
  94. IN SOCKET ListenSocket = INVALID_SOCKET,
  95. IN SOCKET Socket = INVALID_SOCKET,
  96. IN HANDLE hIoCP = INVALID_HANDLE_VALUE );
  97. // void * operator new( size_t ObjectSize );
  98. // void operator delete( void * pObject );
  99. DWORD PostIo();
  100. DWORD PostIoListen(); // Called by PostIo().
  101. DWORD PostIoRead(); // Called by PostIo().
  102. DWORD PostIoWrite( IN void *pvBuffer,
  103. IN DWORD dwBufferSize,
  104. IN DWORD dwOffset );
  105. void GetSockAddrs( OUT SOCKADDR_IRDA **ppAddrLocal,
  106. OUT SOCKADDR_IRDA **ppAddrFrom );
  107. DWORD GetIoPacketKind();
  108. void SetIoPacketKind( IN DWORD dwKind );
  109. HANDLE GetIoCompletionPort();
  110. char *GetReadBuffer();
  111. SOCKET GetSocket();
  112. void SetSocket( SOCKET Socket );
  113. SOCKET GetListenSocket();
  114. void SetListenSocket( SOCKET Socket );
  115. HANDLE GetFileHandle();
  116. void SetFileHandle( HANDLE hFile );
  117. void *GetWritePdu();
  118. void SetWritePdu( void *pvPdu );
  119. static CIOPACKET *CIoPacketFromOverlapped( OVERLAPPED *pOverlapped );
  120. private:
  121. DWORD m_dwKind;
  122. HANDLE m_hIoCompletionPort;
  123. SOCKET m_ListenSocket;
  124. SOCKET m_Socket;
  125. HANDLE m_hFile;
  126. DWORD m_dwReadBufferSize;
  127. SOCKADDR_IRDA *m_pLocalAddr;
  128. SOCKADDR_IRDA *m_pFromAddr;
  129. void *m_pAcceptBuffer;
  130. void *m_pReadBuffer;
  131. void *m_pvWritePdu; // SCEP_HEADER PDU holder.
  132. OVERLAPPED m_Overlapped;
  133. };
  134. //--------------------------------------------------------------------
  135. // class CIOSTATUS
  136. //
  137. //--------------------------------------------------------------------
  138. class CIOSTATUS
  139. {
  140. public:
  141. CIOSTATUS();
  142. ~CIOSTATUS();
  143. void *operator new( IN size_t Size );
  144. void operator delete( IN void *pObj,
  145. IN size_t Size );
  146. DWORD Initialize();
  147. BOOL IsMainThreadId( DWORD dwTid );
  148. HANDLE GetIoCompletionPort();
  149. LONG IncrementNumThreads();
  150. LONG DecrementNumThreads();
  151. LONG IncrementNumPendingThreads();
  152. LONG DecrementNumPendingThreads();
  153. void SaveRpcBinding( handle_t *phRpcBinding );
  154. handle_t GetRpcBinding();
  155. private:
  156. DWORD m_dwMainThreadId;
  157. HANDLE m_hIoCompletionPort;
  158. LONG m_lNumThreads;
  159. LONG m_lNumPendingThreads;
  160. handle_t *m_phRpcBinding;
  161. };
  162. //--------------------------------------------------------------------
  163. // CIOPACKET::GetIoCompletionPort()
  164. //--------------------------------------------------------------------
  165. inline HANDLE CIOPACKET::GetIoCompletionPort()
  166. {
  167. return m_hIoCompletionPort;
  168. }
  169. //--------------------------------------------------------------------
  170. // CIOPACKET::GetSocket()
  171. //--------------------------------------------------------------------
  172. inline SOCKET CIOPACKET::GetSocket()
  173. {
  174. return m_Socket;
  175. }
  176. //--------------------------------------------------------------------
  177. // CIOPACKET::SetSocket()
  178. //--------------------------------------------------------------------
  179. inline void CIOPACKET::SetSocket( SOCKET Socket )
  180. {
  181. m_Socket = Socket;
  182. }
  183. //--------------------------------------------------------------------
  184. // CIOPACKET::GetListenSocket()
  185. //--------------------------------------------------------------------
  186. inline SOCKET CIOPACKET::GetListenSocket()
  187. {
  188. return m_ListenSocket;
  189. }
  190. //--------------------------------------------------------------------
  191. // CIOPACKET::SetListenSocket()
  192. //--------------------------------------------------------------------
  193. inline void CIOPACKET::SetListenSocket( SOCKET ListenSocket )
  194. {
  195. m_ListenSocket = ListenSocket;
  196. }
  197. //--------------------------------------------------------------------
  198. // CIOPACKET::CIoPacketFromOverlapped()
  199. //--------------------------------------------------------------------
  200. inline CIOPACKET *CIOPACKET::CIoPacketFromOverlapped( OVERLAPPED *pOverlapped )
  201. {
  202. return CONTAINING_RECORD(pOverlapped,CIOPACKET,m_Overlapped);
  203. }
  204. //--------------------------------------------------------------------
  205. // CIOPACKET::GetIoPacketKind()
  206. //--------------------------------------------------------------------
  207. inline DWORD CIOPACKET::GetIoPacketKind()
  208. {
  209. return m_dwKind;
  210. }
  211. //--------------------------------------------------------------------
  212. // CIOPACKET::SetIoPacketKind()
  213. //--------------------------------------------------------------------
  214. inline void CIOPACKET::SetIoPacketKind( DWORD dwKind )
  215. {
  216. m_dwKind = dwKind;
  217. }
  218. //--------------------------------------------------------------------
  219. // CIOPACKET::GetReadBuffer()
  220. //--------------------------------------------------------------------
  221. inline char *CIOPACKET::GetReadBuffer()
  222. {
  223. return (char*)m_pReadBuffer;
  224. }
  225. //--------------------------------------------------------------------
  226. // CIOPACKET::GetFileHandle()
  227. //--------------------------------------------------------------------
  228. inline HANDLE CIOPACKET::GetFileHandle()
  229. {
  230. return m_hFile;
  231. }
  232. //--------------------------------------------------------------------
  233. // CIOPACKET::SetFileHandle()
  234. //--------------------------------------------------------------------
  235. inline void CIOPACKET::SetFileHandle( HANDLE hFile )
  236. {
  237. m_hFile = hFile;
  238. }
  239. //--------------------------------------------------------------------
  240. // CIOPACKET::GetWritePdu()
  241. //--------------------------------------------------------------------
  242. inline void *CIOPACKET::GetWritePdu()
  243. {
  244. return m_pvWritePdu;
  245. }
  246. //--------------------------------------------------------------------
  247. // CIOPACKET::SetWritePdu()
  248. //--------------------------------------------------------------------
  249. inline void CIOPACKET::SetWritePdu( void *pvWritePdu )
  250. {
  251. m_pvWritePdu = pvWritePdu;
  252. }
  253. //********************************************************************
  254. //--------------------------------------------------------------------
  255. // CIOSTATUS::IsMainTheadId()
  256. //--------------------------------------------------------------------
  257. inline BOOL CIOSTATUS::IsMainThreadId( DWORD dwTid )
  258. {
  259. return (dwTid == m_dwMainThreadId);
  260. }
  261. //--------------------------------------------------------------------
  262. // CIOSTATUS::GetIoCompletionPort()
  263. //--------------------------------------------------------------------
  264. inline HANDLE CIOSTATUS::GetIoCompletionPort()
  265. {
  266. return m_hIoCompletionPort;
  267. }
  268. //--------------------------------------------------------------------
  269. // CIOSTATUS::IncrementNumThreads()
  270. //--------------------------------------------------------------------
  271. inline LONG CIOSTATUS::IncrementNumThreads()
  272. {
  273. return InterlockedIncrement(&m_lNumThreads);
  274. }
  275. //--------------------------------------------------------------------
  276. // CIOSTATUS::DecrementNumThreads()
  277. //--------------------------------------------------------------------
  278. inline LONG CIOSTATUS::DecrementNumThreads()
  279. {
  280. return InterlockedDecrement(&m_lNumThreads);
  281. }
  282. //--------------------------------------------------------------------
  283. // CIOSTATUS::IncrementNumPendingThreads()
  284. //--------------------------------------------------------------------
  285. inline LONG CIOSTATUS::IncrementNumPendingThreads()
  286. {
  287. return InterlockedIncrement(&m_lNumPendingThreads);
  288. }
  289. //--------------------------------------------------------------------
  290. // CIOSTATUS::DecrementNumPendingThreads()
  291. //--------------------------------------------------------------------
  292. inline LONG CIOSTATUS::DecrementNumPendingThreads()
  293. {
  294. return InterlockedDecrement(&m_lNumPendingThreads);
  295. }
  296. //--------------------------------------------------------------------
  297. // CIOSTATUS::SaveRpcBinding()
  298. //--------------------------------------------------------------------
  299. inline void CIOSTATUS::SaveRpcBinding( handle_t *phRpcBinding )
  300. {
  301. m_phRpcBinding = phRpcBinding;
  302. }
  303. //--------------------------------------------------------------------
  304. // CIOSTATUS::GetRpcBinding()
  305. //--------------------------------------------------------------------
  306. inline handle_t CIOSTATUS::GetRpcBinding()
  307. {
  308. if (m_phRpcBinding)
  309. {
  310. return *m_phRpcBinding;
  311. }
  312. else
  313. {
  314. return NULL;
  315. }
  316. }
  317. #endif //_IO_H_