Team Fortress 2 Source Code as on 22/4/2020
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.

217 lines
7.7 KiB

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