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.

216 lines
5.4 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1994-1997 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: helpcli.c
  6. * Content: client code to talk to dplaysvr.exe
  7. * allows multiple dplay winscock clients to share
  8. * a single port. see %manroot%\dplay\dplaysvr\dphelp.c
  9. * History:
  10. * Date By Reason
  11. * ==== == ======
  12. * 2/15/97 andyco created from w95help.h
  13. *
  14. ***************************************************************************/
  15. #include "helpcli.h"
  16. extern DWORD dwHelperPid;
  17. /*
  18. * sendRequest
  19. *
  20. * communicate a request to DPHELP
  21. */
  22. static BOOL sendRequest( LPDPHELPDATA req_phd )
  23. {
  24. LPDPHELPDATA phd;
  25. HANDLE hmem;
  26. HANDLE hmutex;
  27. HANDLE hackevent;
  28. HANDLE hstartevent;
  29. BOOL rc;
  30. /*
  31. * get events start/ack events
  32. */
  33. hstartevent = CreateEvent( NULL, FALSE, FALSE, DPHELP_EVENT_NAME );
  34. if( hstartevent == NULL )
  35. {
  36. return FALSE;
  37. }
  38. hackevent = CreateEvent( NULL, FALSE, FALSE, DPHELP_ACK_EVENT_NAME );
  39. if( hackevent == NULL )
  40. {
  41. CloseHandle( hstartevent );
  42. return FALSE;
  43. }
  44. /*
  45. * create shared memory area
  46. */
  47. hmem = CreateFileMapping( INVALID_HANDLE_VALUE, NULL,
  48. PAGE_READWRITE, 0, sizeof( DPHELPDATA ),
  49. DPHELP_SHARED_NAME );
  50. if( hmem == NULL )
  51. {
  52. DPF( 1, "Could not create file mapping!" );
  53. CloseHandle( hstartevent );
  54. CloseHandle( hackevent );
  55. return FALSE;
  56. }
  57. phd = (LPDPHELPDATA) MapViewOfFile( hmem, FILE_MAP_ALL_ACCESS, 0, 0, 0 );
  58. if( phd == NULL )
  59. {
  60. DPF( 1, "Could not create view of file!" );
  61. CloseHandle( hmem );
  62. CloseHandle( hstartevent );
  63. CloseHandle( hackevent );
  64. return FALSE;
  65. }
  66. /*
  67. * wait for access to the shared memory
  68. */
  69. hmutex = OpenMutex( SYNCHRONIZE, FALSE, DPHELP_MUTEX_NAME );
  70. if( hmutex == NULL )
  71. {
  72. DPF( 1, "Could not create mutex!" );
  73. UnmapViewOfFile( phd );
  74. CloseHandle( hmem );
  75. CloseHandle( hstartevent );
  76. CloseHandle( hackevent );
  77. return FALSE;
  78. }
  79. WaitForSingleObject( hmutex, INFINITE );
  80. /*
  81. * wake up DPHELP with our request
  82. */
  83. memcpy( phd, req_phd, sizeof( DPHELPDATA ) );
  84. if( SetEvent( hstartevent ) )
  85. {
  86. WaitForSingleObject( hackevent, INFINITE );
  87. memcpy( req_phd, phd, sizeof( DPHELPDATA ) );
  88. rc = TRUE;
  89. }
  90. else
  91. {
  92. DPF( 1, "Could not signal event to notify DPHELP" );
  93. rc = FALSE;
  94. }
  95. /*
  96. * done with things
  97. */
  98. ReleaseMutex( hmutex );
  99. CloseHandle( hmutex );
  100. CloseHandle( hstartevent );
  101. CloseHandle( hackevent );
  102. UnmapViewOfFile( phd );
  103. CloseHandle( hmem );
  104. return rc;
  105. } /* sendRequest */
  106. /*
  107. * WaitForHelperStartup
  108. */
  109. BOOL WaitForHelperStartup( void )
  110. {
  111. HANDLE hevent;
  112. DWORD rc;
  113. hevent = CreateEvent( NULL, TRUE, FALSE, DPHELP_STARTUP_EVENT_NAME );
  114. if( hevent == NULL )
  115. {
  116. return FALSE;
  117. }
  118. DPF( 3, "Wait DPHELP startup event to be triggered" );
  119. rc = WaitForSingleObject( hevent, INFINITE );
  120. CloseHandle( hevent );
  121. return TRUE;
  122. } /* WaitForHelperStartup */
  123. /*
  124. * CreateHelperProcess
  125. */
  126. BOOL CreateHelperProcess( LPDWORD ppid )
  127. {
  128. if( dwHelperPid == 0 )
  129. {
  130. STARTUPINFO si;
  131. PROCESS_INFORMATION pi;
  132. HANDLE h;
  133. h = OpenEvent( SYNCHRONIZE, FALSE, DPHELP_STARTUP_EVENT_NAME );
  134. if( h == NULL )
  135. {
  136. si.cb = sizeof(STARTUPINFO);
  137. si.lpReserved = NULL;
  138. si.lpDesktop = NULL;
  139. si.lpTitle = NULL;
  140. si.dwFlags = 0;
  141. si.cbReserved2 = 0;
  142. si.lpReserved2 = NULL;
  143. DPF( 3, "Creating helper process dplaysvr.exe now" );
  144. if( !CreateProcess(NULL, "dplaysvr.exe", NULL, NULL, FALSE,
  145. NORMAL_PRIORITY_CLASS,
  146. NULL, NULL, &si, &pi) )
  147. {
  148. DPF( 2, "Could not create DPHELP.EXE" );
  149. return FALSE;
  150. }
  151. dwHelperPid = pi.dwProcessId;
  152. DPF( 3, "Helper Process created" );
  153. }
  154. else
  155. {
  156. DPHELPDATA hd;
  157. DPF( 3, "dplaysvr already exists, waiting for dplaysvr event" );
  158. WaitForSingleObject( h, INFINITE );
  159. CloseHandle( h );
  160. DPF( 3, "Asking for DPHELP pid" );
  161. hd.req = DPHELPREQ_RETURNHELPERPID;
  162. sendRequest( &hd );
  163. dwHelperPid = hd.pid;
  164. DPF( 3, "DPHELP pid = %08lx", dwHelperPid );
  165. }
  166. *ppid = dwHelperPid;
  167. return TRUE;
  168. }
  169. *ppid = dwHelperPid;
  170. return FALSE;
  171. } /* CreateHelperProcess */
  172. // notify dphelp.c that we have a new server on this system
  173. HRESULT HelperAddDPlayServer(USHORT port)
  174. {
  175. DPHELPDATA hd;
  176. DWORD pid = GetCurrentProcessId();
  177. memset(&hd, 0, sizeof(DPHELPDATA));
  178. hd.req = DPHELPREQ_DPLAYADDSERVER;
  179. hd.pid = pid;
  180. hd.port = port;
  181. if (sendRequest(&hd)) return hd.hr;
  182. else return E_FAIL;
  183. } // HelperAddDPlayServer
  184. // server is going away
  185. BOOL HelperDeleteDPlayServer(USHORT port)
  186. {
  187. DPHELPDATA hd;
  188. DWORD pid = GetCurrentProcessId();
  189. memset(&hd, 0, sizeof(DPHELPDATA));
  190. hd.req = DPHELPREQ_DPLAYDELETESERVER;
  191. hd.pid = pid;
  192. hd.port = port;
  193. return sendRequest(&hd);
  194. } // HelperDeleteDPlayServer