Counter Strike : Global Offensive Source Code
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.

243 lines
8.7 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef VMPI_H
  8. #define VMPI_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "vmpi_defs.h"
  13. #include "messbuf.h"
  14. #include "iphelpers.h"
  15. // These are called to handle incoming messages.
  16. // Return true if you handled the message and false otherwise.
  17. // Note: the first byte in each message is the packet ID.
  18. typedef bool (*VMPIDispatchFn)( MessageBuffer *pBuf, int iSource, int iPacketID );
  19. typedef void (*VMPI_Disconnect_Handler)( int procID, const char *pReason );
  20. // Which machine is the master.
  21. #define VMPI_MASTER_ID 0
  22. #define VMPI_SEND_TO_ALL -2
  23. #define VMPI_PERSISTENT -3 // If this is set as the destination for a packet, it is sent to all
  24. // workers, and also to new workers that connect.
  25. #define MAX_VMPI_PACKET_IDS 32
  26. #define VMPI_TIMEOUT_INFINITE 0xFFFFFFFF
  27. #define VMPI_PACKET_SIZE 768
  28. // Instantiate one of these to register a dispatch.
  29. class CDispatchReg
  30. {
  31. public:
  32. CDispatchReg( int iPacketID, VMPIDispatchFn fn );
  33. };
  34. // Enums for all the command line parameters.
  35. #define VMPI_PARAM_SDK_HIDDEN 0x0001 // Hidden in SDK mode.
  36. #define VMPI_PARAM( paramName, paramFlags, helpText ) paramName,
  37. enum EVMPICmdLineParam
  38. {
  39. k_eVMPICmdLineParam_FirstParam=0,
  40. k_eVMPICmdLineParam_VMPIParam,
  41. #include "vmpi_parameters.h"
  42. k_eVMPICmdLineParam_LastParam
  43. };
  44. #undef VMPI_PARAM
  45. // Shared by all the tools.
  46. extern bool g_bUseMPI;
  47. extern bool g_bMPIMaster; // Set to true if we're the master in a VMPI session.
  48. extern int g_iVMPIVerboseLevel; // Higher numbers make it spit out more data.
  49. extern bool g_bMPI_Stats; // Send stats to the MySQL database?
  50. extern bool g_bMPI_StatsTextOutput; // Send text output in the stats?
  51. // These can be watched or modified to check bandwidth statistics.
  52. extern int g_nBytesSent;
  53. extern int g_nMessagesSent;
  54. extern int g_nBytesReceived;
  55. extern int g_nMessagesReceived;
  56. extern int g_nMulticastBytesSent;
  57. extern int g_nMulticastBytesReceived;
  58. extern int g_nMaxWorkerCount;
  59. enum VMPIRunMode
  60. {
  61. VMPI_RUN_NETWORKED,
  62. VMPI_RUN_LOCAL // Just make a local process and have it do the work.
  63. };
  64. enum VMPIFileSystemMode
  65. {
  66. VMPI_FILESYSTEM_MULTICAST, // Multicast out, find workers, have them do work.
  67. VMPI_FILESYSTEM_BROADCAST, // Broadcast out, find workers, have them do work.
  68. VMPI_FILESYSTEM_TCP // TCP filesystem.
  69. };
  70. // If this precedes the dependency filename, then it will transfer all the files in the specified directory.
  71. #define VMPI_DEPENDENCY_DIRECTORY_TOKEN '*'
  72. // It's good to specify a disconnect handler here immediately. If you don't have a handler
  73. // and the master disconnects, you'll lockup forever inside a dispatch loop because you
  74. // never handled the master disconnecting.
  75. //
  76. // Note: runMode is only relevant for the VMPI master. The worker always connects to the master
  77. // the same way.
  78. bool VMPI_Init(
  79. int &argc,
  80. char **&argv,
  81. const char *pDependencyFilename,
  82. VMPI_Disconnect_Handler handler = NULL,
  83. VMPIRunMode runMode = VMPI_RUN_NETWORKED, // Networked or local?,
  84. bool bConnectingAsService = false
  85. );
  86. // Used when hosting a patch.
  87. void VMPI_Init_PatchMaster( int argc, char **argv );
  88. void VMPI_Finalize();
  89. VMPIRunMode VMPI_GetRunMode();
  90. VMPIFileSystemMode VMPI_GetFileSystemMode();
  91. // Note: this number can change on the master.
  92. int VMPI_GetCurrentNumberOfConnections();
  93. // Dispatch messages until it gets one with the specified packet ID.
  94. // If subPacketID is not set to -1, then the second byte must match that as well.
  95. //
  96. // Note: this WILL dispatch packets with matching packet IDs and give them a chance to handle packets first.
  97. //
  98. // If bWait is true, then this function either succeeds or Error() is called. If it's false, then if the first available message
  99. // is handled by a dispatch, this function returns false.
  100. bool VMPI_DispatchUntil( MessageBuffer *pBuf, int *pSource, int packetID, int subPacketID = -1, bool bWait = true );
  101. // This waits for the next message and dispatches it.
  102. // You can specify a timeout in milliseconds. If the timeout expires, the function returns false.
  103. bool VMPI_DispatchNextMessage( unsigned long timeout=VMPI_TIMEOUT_INFINITE );
  104. // This should be called periodically in modal loops that don't call other VMPI functions. This will
  105. // check for disconnected sockets and call disconnect handlers so the app can error out if
  106. // it loses all of its connections.
  107. //
  108. // This can be used in place of a Sleep() call by specifying a timeout value.
  109. void VMPI_HandleSocketErrors( unsigned long timeout=0 );
  110. enum VMPISendFlags
  111. {
  112. k_eVMPISendFlags_GroupPackets = 0x0001
  113. };
  114. // Use these to send data to one of the machines.
  115. // If iDest is VMPI_SEND_TO_ALL, then the message goes to all the machines.
  116. // Flags is a combination of the VMPISendFlags enums.
  117. bool VMPI_SendData( void *pData, int nBytes, int iDest, int fVMPISendFlags=0 );
  118. bool VMPI_SendChunks( void const * const *pChunks, const int *pChunkLengths, int nChunks, int iDest, int fVMPISendFlags=0 );
  119. bool VMPI_Send2Chunks( const void *pChunk1, int chunk1Len, const void *pChunk2, int chunk2Len, int iDest, int fVMPISendFlags=0 ); // for convenience..
  120. bool VMPI_Send3Chunks( const void *pChunk1, int chunk1Len, const void *pChunk2, int chunk2Len, const void *pChunk3, int chunk3Len, int iDest, int fVMPISendFlags=0 );
  121. // Flush any groups that were queued with k_eVMPISendFlags_GroupPackets.
  122. // If msInterval is > 0, then it will check a timer and only flush that often (so you can call this a lot, and have it check).
  123. void VMPI_FlushGroupedPackets( unsigned long msInterval=0 );
  124. // This registers a function that gets called when a connection is terminated ungracefully.
  125. void VMPI_AddDisconnectHandler( VMPI_Disconnect_Handler handler );
  126. // is this a valid process
  127. bool VMPI_IsProcValid( int procID );
  128. // Returns false if the process has disconnected ungracefully (disconnect handlers
  129. // would have been called for it too).
  130. bool VMPI_IsProcConnected( int procID );
  131. // Returns true if the process is just a service (in which case it should only get file IO traffic).
  132. bool VMPI_IsProcAService( int procID );
  133. // Simple wrapper for Sleep() so people can avoid including windows.h
  134. void VMPI_Sleep( unsigned long ms );
  135. // VMPI sends machine names around first thing.
  136. const char* VMPI_GetLocalMachineName();
  137. const char* VMPI_GetMachineName( int iProc );
  138. bool VMPI_HasMachineNameBeenSet( int iProc );
  139. // Returns 0xFFFFFFFF if the ID hasn't been set.
  140. unsigned long VMPI_GetJobWorkerID( int iProc );
  141. void VMPI_SetJobWorkerID( int iProc, unsigned long jobWorkerID );
  142. // Search a command line to find arguments. Looks for pName, and if it finds it, returns the
  143. // argument following it. If pName is the last argument, it returns pDefault. If it doesn't
  144. // find pName, returns NULL.
  145. const char* VMPI_FindArg( int argc, char **argv, const char *pName, const char *pDefault = "" );
  146. // (Threadsafe) get and set the current stage. This info winds up in the VMPI database.
  147. void VMPI_GetCurrentStage( char *pOut, int strLen );
  148. void VMPI_SetCurrentStage( const char *pCurStage );
  149. // VMPI is always broadcasting this job in the background.
  150. // This changes the password to 'debugworker' and allows more workers in.
  151. // This can be used if workers are dying on certain work units. Then a programmer
  152. // can run vmpi_service with -superdebug and debug the whole thing.
  153. void VMPI_InviteDebugWorkers();
  154. bool VMPI_IsSDKMode();
  155. // Lookup a command line parameter string.
  156. const char* VMPI_GetParamString( EVMPICmdLineParam eParam );
  157. int VMPI_GetParamFlags( EVMPICmdLineParam eParam );
  158. const char* VMPI_GetParamHelpString( EVMPICmdLineParam eParam );
  159. bool VMPI_IsParamUsed( EVMPICmdLineParam eParam ); // Returns true if the specified parameter is on the command line.
  160. // Can be called from error handlers and if -mpi_Restart is used, it'll automatically restart the process.
  161. bool VMPI_HandleAutoRestart();
  162. // These are optional debug helpers. When VMPI_SuperSpew is enabled, VMPI can spit out messages
  163. // with strings for packet IDs instead of numbers.
  164. #define VMPI_REGISTER_PACKET_ID( idDefine ) static CVMPIPacketIDReg g_VMPIPacketIDReg_##idDefine( idDefine, -1, #idDefine );
  165. #define VMPI_REGISTER_SUBPACKET_ID( idPacketIDDefine, idSubPacketIDDefine ) static CVMPIPacketIDReg g_VMPISubPacketIDReg_##idSubPacketIDDefine( idPacketIDDefine, idSubPacketIDDefine, #idSubPacketIDDefine );
  166. class CVMPIPacketIDReg
  167. {
  168. public:
  169. CVMPIPacketIDReg( int nPacketID, int nSubPacketID, const char *pName );
  170. static void Lookup( int nPacketID, int nSubPacketID, char *pPacketIDString, int nPacketIDStringSize, char *pSubPacketIDString, int nSubPacketIDStringSize );
  171. private:
  172. int m_nPacketID;
  173. int m_nSubPacketID;
  174. const char *m_pName;
  175. CVMPIPacketIDReg *m_pNext;
  176. };
  177. #endif // VMPI_H