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.

449 lines
12 KiB

  1. // Direct Play object class implementation
  2. #ifndef _DP_SPIMP_H
  3. #define _DP_SPIMP_H
  4. #include "..\dplay\dplayi.h"
  5. #include "socket.h"
  6. // Begin: declaration of main implementation class for IDirectPlay
  7. #define MAX_MSG (512 + sizeof(DPHDR))
  8. #define MAX_PLAYERS 256
  9. typedef struct
  10. {
  11. DPID pid;
  12. char chNickName[DPSHORTNAMELEN];
  13. char chFullName[DPLONGNAMELEN ];
  14. HANDLE hEvent;
  15. BOOL bPlayer;
  16. BOOL bValid;
  17. BOOL bLocal;
  18. DPID *aGroup;
  19. SOCKADDR sockaddr;
  20. } PLAYER_RECORD;
  21. typedef struct
  22. {
  23. DPSESSIONDESC dpDesc;
  24. SOCKADDR sockaddr;
  25. DWORD dwUnique;
  26. USHORT usGamePort;
  27. } NS_SESSION_SAVE;
  28. /****************************************************************************
  29. *
  30. * DIRECTPLAY MESSAGES
  31. *
  32. * Errors are represented by negative values and cannot be combined.
  33. *
  34. ****************************************************************************/
  35. #pragma pack(push, 1)
  36. #define SYS_MSG 0x8000
  37. typedef struct
  38. {
  39. union
  40. {
  41. DWORD dwConnect1;
  42. struct
  43. {
  44. USHORT usCookie;
  45. USHORT usGame;
  46. };
  47. };
  48. union
  49. {
  50. DWORD dwConnect2;
  51. struct
  52. {
  53. USHORT to;
  54. USHORT from;
  55. };
  56. };
  57. union
  58. {
  59. DWORD dwConnect3;
  60. struct
  61. {
  62. USHORT usSeq;
  63. USHORT usCount;
  64. };
  65. };
  66. } DPHDR;
  67. typedef struct
  68. {
  69. DPHDR dpHdr;
  70. char chMsgCompose[1000];
  71. } MSG_BUILDER;
  72. #define SP_ENUM_COOKIE 0x794b
  73. typedef struct
  74. {
  75. DPHDR dpHdr;
  76. DPMSG_GENERIC sMsg;
  77. } SPMSG_GENERIC;
  78. #define SIZE_GENERIC (sizeof(SPMSG_GENERIC) - sizeof(DPHDR))
  79. typedef struct
  80. {
  81. DWORD dwType;
  82. DPID dpId;
  83. } DPMSG_GETPLAYER;
  84. typedef struct
  85. {
  86. DPHDR dpHdr;
  87. DPMSG_GETPLAYER sMsg;
  88. } SPMSG_GETPLAYER;
  89. #define SIZE_GETPLAYER (sizeof(SPMSG_GETPLAYER) - sizeof(DPHDR))
  90. typedef struct
  91. {
  92. DPHDR dpHdr;
  93. DPMSG_ADDPLAYER sMsg;
  94. SOCKADDR sockaddr;
  95. DWORD dwUnique;
  96. } SPMSG_ADDPLAYER;
  97. #define SIZE_ADDPLAYER (sizeof(SPMSG_ADDPLAYER) - sizeof(DPHDR))
  98. typedef SPMSG_ADDPLAYER SPMSG_SETPLAYER;
  99. #define SIZE_SETPLAYER (sizeof(SPMSG_SETPLAYER) - sizeof(DPHDR))
  100. typedef struct
  101. {
  102. DPHDR dpHdr;
  103. DPMSG_GROUPADD sMsg;
  104. } SPMSG_GROUPADD;
  105. #define SIZE_GROUPADD (sizeof(SPMSG_GROUPADD) - sizeof(DPHDR))
  106. typedef struct
  107. {
  108. DPHDR dpHdr;
  109. DWORD dwType;
  110. DWORD dwTicks;
  111. } SPMSG_PING;
  112. #define SIZE_PING (sizeof(SPMSG_PING) - sizeof(DPHDR))
  113. typedef struct
  114. {
  115. DPHDR dpHdr;
  116. DWORD dwType;
  117. GUID guid;
  118. } SPMSG_INVITE;
  119. typedef struct
  120. {
  121. DPHDR dpHdr;
  122. DWORD dwType;
  123. DPCAPS dpCaps;
  124. } SPMSG_GETPLAYERCAPS;
  125. #define SIZE_GETPLAYERCAPS (sizeof(SPMSG_GETPLAYERCAPS) - sizeof(DPHDR))
  126. typedef struct
  127. {
  128. DWORD dwType;
  129. DPSESSIONDESC dpDesc;
  130. } DPMSG_SENDDESC;
  131. typedef struct
  132. {
  133. DPHDR dpHdr;
  134. DPMSG_SENDDESC sMsg;
  135. } SPMSG_SENDDESC;
  136. #define SIZE_SENDDESC (sizeof(SPMSG_SENDDESC) - sizeof(DPHDR))
  137. typedef struct
  138. {
  139. DWORD dwType;
  140. BOOL bEnable;
  141. } DPMSG_ENABLEPLAYER;
  142. typedef struct
  143. {
  144. DPHDR dpHdr;
  145. DPMSG_ENABLEPLAYER sMsg;
  146. } SPMSG_ENABLEPLAYER;
  147. typedef struct
  148. {
  149. DPHDR dpHdr;
  150. DWORD dwType;
  151. DPSESSIONDESC dpSessionDesc;
  152. DWORD dwUnique;
  153. USHORT usPort;
  154. USHORT usVerMajor;
  155. USHORT usVerMinor;
  156. } SPMSG_ENUM;
  157. #pragma pack(pop, 1)
  158. class CImpIDP_SP : public IDirectPlaySP {
  159. public:
  160. // IUnknown methods
  161. // IDirectPlay methods
  162. virtual HRESULT STDMETHODCALLTYPE QueryInterface( REFIID iid, LPVOID *ppvObj );
  163. virtual ULONG STDMETHODCALLTYPE AddRef( void);
  164. virtual ULONG STDMETHODCALLTYPE Release( void );
  165. virtual HRESULT STDMETHODCALLTYPE AddPlayerToGroup(
  166. DPID dwDPIDGroup,
  167. DPID dwDPIDPlayer);
  168. virtual HRESULT STDMETHODCALLTYPE Close(DWORD);
  169. virtual HRESULT STDMETHODCALLTYPE CreatePlayer(
  170. LPDPID pPlayerID,
  171. LPSTR pNickName,
  172. LPSTR pFullName,
  173. LPHANDLE lpReceiveEvent,
  174. BOOL bPlayer);
  175. virtual HRESULT STDMETHODCALLTYPE DeletePlayerFromGroup(
  176. DPID DPid,
  177. DPID dwDPIDPlayer);
  178. virtual HRESULT STDMETHODCALLTYPE DestroyPlayer( DPID pPlayerID, BOOL );
  179. virtual HRESULT STDMETHODCALLTYPE EnumGroupPlayers(
  180. DPID dwGroupPid,
  181. LPDPENUMPLAYERSCALLBACK EnumCallback,
  182. LPVOID pContext,
  183. DWORD dwFlags);
  184. virtual HRESULT STDMETHODCALLTYPE EnumPlayers(
  185. DWORD dwSessionID,
  186. LPDPENUMPLAYERSCALLBACK EnumCallback,
  187. LPVOID pContext,
  188. DWORD dwFlags);
  189. virtual HRESULT STDMETHODCALLTYPE EnumSessions(
  190. LPDPSESSIONDESC,
  191. DWORD dwTimeout,
  192. LPDPENUMSESSIONSCALLBACK EnumCallback,
  193. LPVOID,
  194. DWORD);
  195. virtual HRESULT STDMETHODCALLTYPE GetCaps(LPDPCAPS lpDPCaps);
  196. virtual HRESULT STDMETHODCALLTYPE GetMessageCount(DPID pidPlayer, LPDWORD lpdwCount);
  197. virtual HRESULT STDMETHODCALLTYPE GetPlayerCaps(
  198. DPID dwDPId,
  199. LPDPCAPS lpDPCaps);
  200. virtual HRESULT STDMETHODCALLTYPE GetPlayerName(DPID dpID,
  201. LPSTR lpFriendlyName, // buffer to hold name
  202. LPDWORD pdwFriendlyNameLength, // length of name buffer
  203. LPSTR lpFormalName,
  204. LPDWORD pdwFormalNameLength
  205. );
  206. virtual HRESULT STDMETHODCALLTYPE Initialize(LPGUID lpguid);
  207. virtual HRESULT STDMETHODCALLTYPE Open(
  208. LPDPSESSIONDESC lpSDesc, HANDLE hEvent);
  209. virtual HRESULT STDMETHODCALLTYPE Receive(
  210. LPDPID from,
  211. LPDPID to,
  212. DWORD dwReceiveFlags,
  213. LPVOID,
  214. LPDWORD);
  215. virtual HRESULT STDMETHODCALLTYPE SaveSession(LPVOID lpv, LPDWORD lpdw);
  216. virtual HRESULT STDMETHODCALLTYPE SetPrevPlayer(LPSTR lpName, LPVOID lpv, DWORD dw);
  217. virtual HRESULT STDMETHODCALLTYPE SetPrevSession(LPSTR lpName, LPVOID lpv, DWORD dw);
  218. virtual HRESULT STDMETHODCALLTYPE EnableNewPlayers(BOOL bEnable);
  219. virtual HRESULT STDMETHODCALLTYPE Send(
  220. DPID from,
  221. DPID to,
  222. DWORD dwFlags,
  223. LPVOID lpvMsg,
  224. DWORD dwLength);
  225. virtual HRESULT STDMETHODCALLTYPE SetPlayerName(
  226. DPID from,
  227. LPSTR lpFriendlyName,
  228. LPSTR lpFormalName,
  229. BOOL bPlayer);
  230. static CImpIDP_SP* NewCImpIDP_SP(int af);
  231. DWORD ServerThreadProc();
  232. DWORD ClientThreadProc();
  233. BOOL CompareSessions(DWORD dwType, LPDPSESSIONDESC lpSDesc);
  234. VOID HandleConnect(LPVOID lpv, DWORD dwSize, SOCKADDR *pSAddr, INT SockAddrLen);
  235. VOID HandleMessage(LPVOID lpv, DWORD dwSize, SOCKADDR *pSAddr, INT SockAddrLen);
  236. VOID PulseBlock() {PulseEvent(m_hBlockingEvent);}
  237. VOID SetBlock() {SetEvent(m_hBlockingEvent);}
  238. DWORD BlockNicely(DWORD dwTimeout);
  239. VOID SendDesc(LPDPSESSIONDESC);
  240. VOID SendPing();
  241. VOID ISend(LONG, LONG, DWORD, LPVOID, DWORD);
  242. VOID LocalMsg(LONG, LPVOID, DWORD);
  243. VOID RemoteMsg(LONG, LPVOID, DWORD);
  244. LONG FindInvalidIndex();
  245. VOID ConnectPlayers(SOCKADDR *pSAddr, INT SockAddrLen);
  246. VOID DeleteRemotePlayers();
  247. VOID ResetSessionDesc() {memset(&m_dpDesc, 0x00, sizeof(DPSESSIONDESC));}
  248. BOOL GetSockAddress(SOCKADDR *pSAddr, LPINT pSAddrLen, USHORT usPort,
  249. SOCKET *pSocket, BOOL bBroadcast);
  250. BOOL PostGameMessage(LPVOID lpv, DWORD dw);
  251. BOOL PostPlayerMessage(LONG iIndex, LPVOID lpv, DWORD dw);
  252. BOOL PostNSMessage(LPVOID lpv, DWORD dw);
  253. USHORT NextSequence();
  254. USHORT UpdateSequence(USHORT us);
  255. VOID SaveSessionData(NS_SESSION_SAVE *lpSDesc);
  256. BOOL GetSessionData(DWORD dwSession);
  257. BOOL m_bConnected;
  258. BOOL m_bPlayer0; // If I created the call, I am player
  259. // zero.
  260. void *operator new( size_t size );
  261. void operator delete( void *ptr );
  262. protected:
  263. void EnumDataLock();
  264. void EnumDataUnlock();
  265. void PlayerDataLock();
  266. void PlayerDataUnlock();
  267. void ParanoiaLock();
  268. void ParanoiaUnlock();
  269. private:
  270. LONG GetPlayerIndex(DPID);
  271. BOOL SetSession(DWORD dw);
  272. CImpIDP_SP(void);
  273. ~CImpIDP_SP(void);
  274. DWORD m_dwPingSent;
  275. HANDLE m_hBlockingEvent;
  276. HANDLE m_hEnumBlkEventMain; // User Thread can run.
  277. HANDLE m_hEnumBlkEventRead; // Enum Thread can read again.
  278. HANDLE m_hPlayerBlkEventMain; // User Thread can run.
  279. HANDLE m_hPlayerBlkEventRead; // Enum Thread can read again.
  280. DWORD m_dwNextPlayer;
  281. BOOL m_bEnablePlayerAdd;
  282. volatile LPDPENUMSESSIONSCALLBACK m_fpEnumSessions;
  283. volatile BOOL m_bRunEnumReceiveLoop;
  284. LPDPENUMPLAYERSCALLBACK m_fpEnumPlayers;
  285. LPVOID m_lpvSessionContext;
  286. LPVOID m_lpvPlayersContext;
  287. PLAYER_RECORD m_aPlayer[MAX_PLAYERS];
  288. LONG m_iPlayerIndex;
  289. DPSESSIONDESC m_dpDesc;
  290. int m_refCount;
  291. CRITICAL_SECTION m_critSection;
  292. CRITICAL_SECTION m_critSectionPlayer;
  293. CRITICAL_SECTION m_critSectionParanoia;
  294. DPCAPS m_dpcaps;
  295. HANDLE m_hNewPlayerEvent;
  296. char **m_ppSessionArray;
  297. DWORD m_dwSessionPrev;
  298. DWORD m_dwSessionAlloc;
  299. int m_af;
  300. DWORD m_remoteaddrlen;
  301. char m_chComputerName[64];
  302. BOOL m_bShutDown;
  303. HANDLE m_hNSThread;
  304. DWORD m_dwNSId;
  305. USHORT m_usGamePort;
  306. USHORT m_usGameCookie;
  307. HANDLE m_hClientThread;
  308. DWORD m_dwClientId;
  309. volatile HANDLE m_hEnumThread;
  310. DWORD m_dwEnumId;
  311. volatile SOCKET m_ServerSocket;
  312. volatile SOCKET m_ClientSocket;
  313. volatile SOCKET m_EnumSocket;
  314. BOOL m_bEnumSocket;
  315. BOOL m_bClientSocket;
  316. DWORD m_dwSession;
  317. SOCKADDR m_NSSockAddr;
  318. INT m_SessionAddrLen;
  319. SOCKADDR m_GameSockAddr;
  320. USHORT m_usSeq;
  321. USHORT m_usSeqSys;
  322. SOCKADDR *m_aMachineAddr;
  323. DWORD m_cMachines;
  324. DWORD m_dwUnique;
  325. volatile SPMSG_ENUM m_spmsgEnum;
  326. volatile SPMSG_ADDPLAYER m_spmsgAddPlayer;
  327. };
  328. BOOL SetIDP_SP( CImpIDP_SP *pSP);
  329. // End : declaration of main implementation class for IDirectPlay
  330. extern BOOL AddMessage(LPVOID lpvMsg, DWORD dwSize, DPID pidTo, DPID pidFrom, BOOL bHigh);
  331. extern HRESULT GetQMessage(LPVOID lpvMsg, LPDWORD pdwSize, DPID *ppidTo, DPID *ppidFrom,
  332. DWORD dwFlags, BOOL bPeek);
  333. extern VOID FlushQueue(DPID pid);
  334. extern DWORD GetPlayerCount(DPID spid);
  335. #define DPSYS_JOHN 0x6e686f4a
  336. // Enumeration Messages
  337. //
  338. // Thread Messages
  339. //
  340. #define PWM_BASE 0x00007000
  341. #define PWM_COMMWRITE PWM_BASE +1
  342. #define PWM_SETIDP PWM_BASE +2
  343. #define PWM_HANGUP PWM_BASE +3
  344. #define PWM_CLOSE PWM_BASE +4
  345. #define DPNS_PORT 8787
  346. #endif
  347.