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.

395 lines
10 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // The copyright to the contents herein is the property of Valve, L.L.C.
  4. // The contents may be used and/or copied only with the written permission of
  5. // Valve, L.L.C., or in accordance with the terms and conditions stipulated in
  6. // the agreement/contract under which the contents have been supplied.
  7. //
  8. // $Header: $
  9. // $NoKeywords: $
  10. //
  11. // Material editor
  12. //=============================================================================
  13. #include <windows.h>
  14. #include "appframework/appframework.h"
  15. #include "networksystem/inetworksystem.h"
  16. #include "networksystem/inetworkmessage.h"
  17. #include "bitbuf.h"
  18. #include "filesystem.h"
  19. #include "filesystem_init.h"
  20. #include "tier0/icommandline.h"
  21. #include "vstdlib/cvar.h"
  22. //-----------------------------------------------------------------------------
  23. // Singleton interfaces
  24. //-----------------------------------------------------------------------------
  25. IFileSystem *g_pFileSystem;
  26. INetworkSystem *g_pNetworkSystem;
  27. //-----------------------------------------------------------------------------
  28. // Purpose: Warning/Msg call back through this API
  29. // Input : type -
  30. // *pMsg -
  31. // Output : SpewRetval_t
  32. //-----------------------------------------------------------------------------
  33. SpewRetval_t SpewFunc( SpewType_t type, char const *pMsg )
  34. {
  35. OutputDebugString( pMsg );
  36. if ( type == SPEW_ASSERT )
  37. {
  38. DebuggerBreak();
  39. }
  40. return SPEW_CONTINUE;
  41. }
  42. //-----------------------------------------------------------------------------
  43. // The application object
  44. //-----------------------------------------------------------------------------
  45. class CNetworkTestApp : public CSteamAppSystemGroup
  46. {
  47. typedef CSteamAppSystemGroup BaseClass;
  48. public:
  49. // Methods of IApplication
  50. virtual bool Create();
  51. virtual bool PreInit( );
  52. virtual int Main();
  53. virtual void PostShutdown( );
  54. virtual void Destroy();
  55. virtual const char *GetAppName() { return "NetworkTest"; }
  56. virtual bool AppUsesReadPixels() { return false; }
  57. private:
  58. // Window management
  59. bool CreateAppWindow( char const *pTitle, bool bWindowed, int w, int h );
  60. // Sets up the game path
  61. bool SetupSearchPaths();
  62. HWND m_HWnd;
  63. };
  64. DEFINE_WINDOWED_STEAM_APPLICATION_OBJECT( CNetworkTestApp );
  65. //-----------------------------------------------------------------------------
  66. // Create all singleton systems
  67. //-----------------------------------------------------------------------------
  68. bool CNetworkTestApp::Create()
  69. {
  70. SpewOutputFunc( SpewFunc );
  71. // Add in the cvar factory
  72. AppModule_t cvarModule = LoadModule( VStdLib_GetICVarFactory() );
  73. AddSystem( cvarModule, CVAR_INTERFACE_VERSION );
  74. AppSystemInfo_t appSystems[] =
  75. {
  76. { "networksystem.dll", NETWORKSYSTEM_INTERFACE_VERSION },
  77. { "", "" } // Required to terminate the list
  78. };
  79. if ( !AddSystems( appSystems ) )
  80. return false;
  81. g_pFileSystem = (IFileSystem*)FindSystem( FILESYSTEM_INTERFACE_VERSION );
  82. g_pNetworkSystem = (INetworkSystem*)FindSystem( NETWORKSYSTEM_INTERFACE_VERSION );
  83. if (!g_pFileSystem || !g_pNetworkSystem )
  84. return false;
  85. return true;
  86. }
  87. void CNetworkTestApp::Destroy()
  88. {
  89. g_pFileSystem = NULL;
  90. g_pNetworkSystem = NULL;
  91. }
  92. //-----------------------------------------------------------------------------
  93. // Window management
  94. //-----------------------------------------------------------------------------
  95. bool CNetworkTestApp::CreateAppWindow( char const *pTitle, bool bWindowed, int w, int h )
  96. {
  97. WNDCLASSEX wc;
  98. memset( &wc, 0, sizeof( wc ) );
  99. wc.cbSize = sizeof( wc );
  100. wc.style = CS_OWNDC | CS_DBLCLKS;
  101. wc.lpfnWndProc = DefWindowProc;
  102. wc.hInstance = (HINSTANCE)GetAppInstance();
  103. wc.lpszClassName = "Valve001";
  104. wc.hIcon = NULL; //LoadIcon( s_HInstance, MAKEINTRESOURCE( IDI_LAUNCHER ) );
  105. wc.hIconSm = wc.hIcon;
  106. RegisterClassEx( &wc );
  107. // Note, it's hidden
  108. DWORD style = WS_POPUP | WS_CLIPSIBLINGS;
  109. if ( bWindowed )
  110. {
  111. // Give it a frame
  112. style |= WS_OVERLAPPEDWINDOW;
  113. style &= ~WS_THICKFRAME;
  114. }
  115. // Never a max box
  116. style &= ~WS_MAXIMIZEBOX;
  117. RECT windowRect;
  118. windowRect.top = 0;
  119. windowRect.left = 0;
  120. windowRect.right = w;
  121. windowRect.bottom = h;
  122. // Compute rect needed for that size client area based on window style
  123. AdjustWindowRectEx(&windowRect, style, FALSE, 0);
  124. // Create the window
  125. m_HWnd = CreateWindow( wc.lpszClassName, pTitle, style, 0, 0,
  126. windowRect.right - windowRect.left, windowRect.bottom - windowRect.top,
  127. NULL, NULL, (HINSTANCE)GetAppInstance(), NULL );
  128. if (!m_HWnd)
  129. return false;
  130. int CenterX, CenterY;
  131. CenterX = (GetSystemMetrics(SM_CXSCREEN) - w) / 2;
  132. CenterY = (GetSystemMetrics(SM_CYSCREEN) - h) / 2;
  133. CenterX = (CenterX < 0) ? 0: CenterX;
  134. CenterY = (CenterY < 0) ? 0: CenterY;
  135. // In VCR modes, keep it in the upper left so mouse coordinates are always relative to the window.
  136. SetWindowPos (m_HWnd, NULL, CenterX, CenterY, 0, 0,
  137. SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
  138. return true;
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Sets up the game path
  142. //-----------------------------------------------------------------------------
  143. bool CNetworkTestApp::SetupSearchPaths()
  144. {
  145. CFSSteamSetupInfo steamInfo;
  146. steamInfo.m_pDirectoryName = NULL;
  147. steamInfo.m_bOnlyUseDirectoryName = false;
  148. steamInfo.m_bToolsMode = true;
  149. steamInfo.m_bSetSteamDLLPath = true;
  150. steamInfo.m_bSteam = g_pFileSystem->IsSteam();
  151. if ( FileSystem_SetupSteamEnvironment( steamInfo ) != FS_OK )
  152. return false;
  153. CFSMountContentInfo fsInfo;
  154. fsInfo.m_pFileSystem = g_pFileSystem;
  155. fsInfo.m_bToolsMode = true;
  156. fsInfo.m_pDirectoryName = steamInfo.m_GameInfoPath;
  157. if ( FileSystem_MountContent( fsInfo ) != FS_OK )
  158. return false;
  159. // Finally, load the search paths for the "GAME" path.
  160. CFSSearchPathsInit searchPathsInit;
  161. searchPathsInit.m_pDirectoryName = steamInfo.m_GameInfoPath;
  162. searchPathsInit.m_pFileSystem = g_pFileSystem;
  163. if ( FileSystem_LoadSearchPaths( searchPathsInit ) != FS_OK )
  164. return false;
  165. g_pFileSystem->AddSearchPath( steamInfo.m_GameInfoPath, "SKIN", PATH_ADD_TO_HEAD );
  166. char platform[MAX_PATH];
  167. Q_strncpy( platform, steamInfo.m_GameInfoPath, MAX_PATH );
  168. Q_StripTrailingSlash( platform );
  169. Q_strncat( platform, "/../platform", MAX_PATH, MAX_PATH );
  170. g_pFileSystem->AddSearchPath( platform, "PLATFORM" );
  171. return true;
  172. }
  173. //-----------------------------------------------------------------------------
  174. // PreInit, PostShutdown
  175. //-----------------------------------------------------------------------------
  176. bool CNetworkTestApp::PreInit( )
  177. {
  178. // Add paths...
  179. if ( !SetupSearchPaths() )
  180. return false;
  181. const char *pArg;
  182. int iWidth = 1024;
  183. int iHeight = 768;
  184. bool bWindowed = (CommandLine()->CheckParm( "-fullscreen" ) == NULL);
  185. if (CommandLine()->CheckParm( "-width", &pArg ))
  186. {
  187. iWidth = atoi( pArg );
  188. }
  189. if (CommandLine()->CheckParm( "-height", &pArg ))
  190. {
  191. iHeight = atoi( pArg );
  192. }
  193. if (!CreateAppWindow( "NetworkTest", bWindowed, iWidth, iHeight ))
  194. return false;
  195. return true;
  196. }
  197. void CNetworkTestApp::PostShutdown( )
  198. {
  199. }
  200. //-----------------------------------------------------------------------------
  201. // Network message ids
  202. //-----------------------------------------------------------------------------
  203. enum
  204. {
  205. TEST_GROUP = NETWORKSYSTEM_FIRST_GROUP,
  206. };
  207. enum
  208. {
  209. TEST_MESSAGE_1 = 0,
  210. };
  211. //-----------------------------------------------------------------------------
  212. // Test network message
  213. //-----------------------------------------------------------------------------
  214. class CTestNetworkMessage : public CNetworkMessage
  215. {
  216. public:
  217. CTestNetworkMessage() { SetReliable( false ); }
  218. CTestNetworkMessage( int nValue ) : m_Data( nValue ) { SetReliable( false ); }
  219. DECLARE_BASE_MESSAGE( TEST_GROUP, TEST_MESSAGE_1, "Test Message 1" )
  220. bool Process();
  221. int m_Data;
  222. };
  223. bool CTestNetworkMessage::WriteToBuffer( bf_write &buffer )
  224. {
  225. buffer.WriteShort( m_Data );
  226. return !buffer.IsOverflowed();
  227. }
  228. bool CTestNetworkMessage::ReadFromBuffer( bf_read &buffer )
  229. {
  230. m_Data = buffer.ReadShort();
  231. return !buffer.IsOverflowed();
  232. }
  233. bool CTestNetworkMessage::Process()
  234. {
  235. Msg( "Received test message %d\n", m_Data );
  236. return true;
  237. }
  238. //-----------------------------------------------------------------------------
  239. // main application
  240. //-----------------------------------------------------------------------------
  241. int CNetworkTestApp::Main()
  242. {
  243. // Network messages must be registered before the server or client is started
  244. g_pNetworkSystem->RegisterMessage( new CTestNetworkMessage() );
  245. int nRetVal = 0;
  246. if ( !g_pNetworkSystem->StartServer( ) )
  247. return 0;
  248. if ( !g_pNetworkSystem->StartClient( ) )
  249. goto shutdownServer;
  250. // Set the channel up for receiving
  251. INetChannel *pChan = g_pNetworkSystem->ConnectClientToServer( "localhost", 27001 );
  252. if ( !pChan )
  253. goto shutdownClient;
  254. INetChannel *pServerChan = NULL;
  255. {
  256. while( true )
  257. {
  258. // Helps avoid a buffer overflow
  259. Sleep( 1 );
  260. // Send a message out
  261. if ( pChan->GetConnectionState() == CONNECTION_STATE_CONNECTED )
  262. {
  263. CTestNetworkMessage msg( 5 );
  264. pChan->AddNetMsg( &msg, false );
  265. msg.m_Data = 4;
  266. pChan->AddNetMsg( &msg, false );
  267. }
  268. if ( pServerChan )
  269. {
  270. CTestNetworkMessage msg( 6 );
  271. pServerChan->AddNetMsg( &msg, false );
  272. msg.m_Data = 7;
  273. pServerChan->AddNetMsg( &msg, false );
  274. }
  275. g_pNetworkSystem->ClientSendMessages();
  276. g_pNetworkSystem->ServerReceiveMessages();
  277. g_pNetworkSystem->ServerSendMessages();
  278. g_pNetworkSystem->ClientReceiveMessages();
  279. NetworkEvent_t *pEvent = g_pNetworkSystem->FirstNetworkEvent();
  280. for ( ; pEvent; pEvent = g_pNetworkSystem->NextNetworkEvent( ) )
  281. {
  282. switch ( pEvent->m_nType )
  283. {
  284. case NETWORK_EVENT_CONNECTED:
  285. pServerChan = ((NetworkConnectionEvent_t*)pEvent)->m_pChannel;
  286. break;
  287. case NETWORK_EVENT_DISCONNECTED:
  288. if ( pServerChan == ((NetworkDisconnectionEvent_t*)pEvent)->m_pChannel )
  289. {
  290. pServerChan = NULL;
  291. }
  292. break;
  293. case NETWORK_EVENT_MESSAGE_RECEIVED:
  294. {
  295. NetworkMessageReceivedEvent_t *pReceivedEvent = static_cast<NetworkMessageReceivedEvent_t*>( pEvent );
  296. if ( ( pReceivedEvent->m_pNetworkMessage->GetGroup() == TEST_GROUP ) && ( pReceivedEvent->m_pNetworkMessage->GetType() == TEST_MESSAGE_1 ) )
  297. {
  298. static_cast<CTestNetworkMessage*>( pReceivedEvent->m_pNetworkMessage )->Process();
  299. }
  300. }
  301. break;
  302. }
  303. }
  304. }
  305. nRetVal = 1;
  306. g_pNetworkSystem->DisconnectClientFromServer( pChan );
  307. }
  308. shutdownClient:
  309. g_pNetworkSystem->ShutdownClient( );
  310. shutdownServer:
  311. g_pNetworkSystem->ShutdownServer( );
  312. return nRetVal;
  313. }