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.

162 lines
4.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: simple TCP socket API for communicating as a TCP client over a TEXT
  4. // connection
  5. //
  6. // $Workfile: $
  7. // $Date: $
  8. //
  9. //-----------------------------------------------------------------------------
  10. // $Log: $
  11. //
  12. // $NoKeywords: $
  13. //=============================================================================//
  14. #include <winsock.h>
  15. #include "simplesocket.h"
  16. static REPORTFUNCTION g_SocketReport = NULL;
  17. //-----------------------------------------------------------------------------
  18. // Purpose: intialize sockets
  19. //-----------------------------------------------------------------------------
  20. void SocketInit( void )
  21. {
  22. WSADATA wsData;
  23. WORD wVersionRequested = MAKEWORD(1, 1);
  24. WSAStartup(wVersionRequested, &wsData);
  25. }
  26. //-----------------------------------------------------------------------------
  27. // Purpose: cleanup all socket resources
  28. //-----------------------------------------------------------------------------
  29. void SocketExit( void )
  30. {
  31. WSACleanup();
  32. }
  33. //-----------------------------------------------------------------------------
  34. // Purpose: sets up a reporting function
  35. // Input : *pReportFunction -
  36. //-----------------------------------------------------------------------------
  37. void SocketReport( REPORTFUNCTION pReportFunction )
  38. {
  39. g_SocketReport = pReportFunction;
  40. }
  41. //-----------------------------------------------------------------------------
  42. // Purpose: Open a TCP socket & connect to a given server
  43. // Input : *pServerName - server name (text or ip)
  44. // port - port number of the server
  45. // Output : HSOCKET
  46. //-----------------------------------------------------------------------------
  47. HSOCKET SocketOpen( const char *pServerName, int port )
  48. {
  49. SOCKADDR_IN sockAddr;
  50. SOCKET s;
  51. memset(&sockAddr,0,sizeof(sockAddr));
  52. s = socket( AF_INET, SOCK_STREAM, 0 );
  53. sockAddr.sin_family = AF_INET;
  54. sockAddr.sin_addr.s_addr = inet_addr(pServerName);
  55. if (sockAddr.sin_addr.s_addr == INADDR_NONE)
  56. {
  57. LPHOSTENT lphost;
  58. lphost = gethostbyname(pServerName);
  59. if (lphost != NULL)
  60. {
  61. sockAddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;
  62. }
  63. else
  64. {
  65. WSASetLastError(WSAEINVAL);
  66. return FALSE;
  67. }
  68. }
  69. sockAddr.sin_port = htons((u_short)port);
  70. if ( connect( s, (SOCKADDR *)&sockAddr, sizeof(sockAddr) ) == SOCKET_ERROR )
  71. {
  72. // printf("Socket error:%d\n", WSAGetLastError()) ;
  73. return NULL;
  74. }
  75. return (HSOCKET)s;
  76. }
  77. //-----------------------------------------------------------------------------
  78. // Purpose: close the socket opened with SocketOpen()
  79. // Input : socket -
  80. //-----------------------------------------------------------------------------
  81. void SocketClose( HSOCKET socket )
  82. {
  83. SOCKET s = (SOCKET)socket;
  84. closesocket( s );
  85. }
  86. //-----------------------------------------------------------------------------
  87. // Purpose: Write a string to the socket. String is NULL terminated on input,
  88. // but terminator is NOT written to the socket
  89. // Input : socket -
  90. // *pString - string to write
  91. //-----------------------------------------------------------------------------
  92. void SocketSendString( HSOCKET socket, const char *pString )
  93. {
  94. if ( !pString )
  95. return;
  96. int len = (int)strlen( pString );
  97. if ( !len )
  98. return;
  99. if ( send( (SOCKET)socket, pString, len, 0 ) != SOCKET_ERROR )
  100. {
  101. if ( g_SocketReport )
  102. {
  103. g_SocketReport( socket, pString );
  104. }
  105. }
  106. else
  107. {
  108. // printf("Send failed\n");
  109. }
  110. }
  111. //-----------------------------------------------------------------------------
  112. // Purpose: receive input from a socket until a certain string is received
  113. // ASSUME: socket data is all text
  114. // Input : socket -
  115. // *pString - string to match, if NULL, just poll the socket once
  116. //-----------------------------------------------------------------------------
  117. void SocketWait( HSOCKET socket, const char *pString )
  118. {
  119. char buf[1024];
  120. bool done = false;
  121. while ( !done )
  122. {
  123. int len = recv( (SOCKET)socket, buf, sizeof(buf)-1, 0 );
  124. buf[len] = 0;
  125. if ( g_SocketReport )
  126. {
  127. g_SocketReport( socket, buf );
  128. }
  129. if ( !pString || strstr( buf, pString ) )
  130. return;
  131. }
  132. }