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.

162 lines
4.3 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #ifndef IPHELPERS_H
  9. #define IPHELPERS_H
  10. #include "ichannel.h"
  11. // Loops that poll sockets should Sleep for this amount of time between iterations
  12. // so they don't hog all the CPU.
  13. #define LOOP_POLL_INTERVAL 5
  14. // Useful for putting the arguments into a printf statement.
  15. #define EXPAND_ADDR( x ) (x).ip[0], (x).ip[1], (x).ip[2], (x).ip[3], (x).port
  16. // This is a simple wrapper layer for UDP sockets.
  17. class CIPAddr
  18. {
  19. public:
  20. CIPAddr();
  21. CIPAddr( const int inputIP[4], const int inputPort );
  22. CIPAddr( int ip0, int ip1, int ip2, int ip3, int ipPort );
  23. void Init( int ip0, int ip1, int ip2, int ip3, int ipPort );
  24. bool operator==( const CIPAddr &o ) const;
  25. bool operator!=( const CIPAddr &o ) const;
  26. // Setup to send to the local machine on the specified port.
  27. void SetupLocal( int inPort );
  28. public:
  29. unsigned char ip[4];
  30. unsigned short port;
  31. };
  32. // The chunk walker provides an easy way to copy data out of the chunks as though it were a
  33. // single contiguous chunk of memory.s
  34. class CChunkWalker
  35. {
  36. public:
  37. CChunkWalker( void const * const *pChunks, const int *pChunkLengths, int nChunks );
  38. int GetTotalLength() const;
  39. void CopyTo( void *pOut, int nBytes );
  40. private:
  41. void const * const *m_pChunks;
  42. const int *m_pChunkLengths;
  43. int m_nChunks;
  44. int m_iCurChunk;
  45. int m_iCurChunkPos;
  46. int m_TotalLength;
  47. };
  48. // This class makes loop that wait on something look nicer. ALL loops using this class
  49. // should follow this pattern, or they can wind up with unforeseen delays that add a whole
  50. // lot of lag.
  51. //
  52. // CWaitTimer waitTimer( 5.0 );
  53. // while ( 1 )
  54. // {
  55. // do your thing here like Recv() from a socket.
  56. //
  57. // if ( waitTimer.ShouldKeepWaiting() )
  58. // Sleep() for some time interval like 5ms so you don't hog the CPU
  59. // else
  60. // BREAK HERE
  61. // }
  62. class CWaitTimer
  63. {
  64. public:
  65. CWaitTimer( double flSeconds );
  66. bool ShouldKeepWaiting();
  67. private:
  68. unsigned long m_StartTime;
  69. unsigned long m_WaitMS;
  70. };
  71. // Helper function to get time in milliseconds.
  72. unsigned long SampleMilliseconds();
  73. class ISocket
  74. {
  75. public:
  76. // Call this when you're done.
  77. virtual void Release() = 0;
  78. // Bind the socket so you can send and receive with it.
  79. // If you bind to port 0, then the system will select the port for you.
  80. virtual bool Bind( const CIPAddr *pAddr ) = 0;
  81. virtual bool BindToAny( const unsigned short port ) = 0;
  82. // Broadcast some data.
  83. virtual bool Broadcast( const void *pData, const int len, const unsigned short port ) = 0;
  84. // Send a packet.
  85. virtual bool SendTo( const CIPAddr *pAddr, const void *pData, const int len ) = 0;
  86. virtual bool SendChunksTo( const CIPAddr *pAddr, void const * const *pChunks, const int *pChunkLengths, int nChunks ) = 0;
  87. // Receive a packet. Returns the length received or -1 if no data came in.
  88. // If pFrom is set, then it is filled in with the sender's IP address.
  89. virtual int RecvFrom( void *pData, int maxDataLen, CIPAddr *pFrom ) = 0;
  90. // How long has it been since we successfully received a packet?
  91. virtual double GetRecvTimeout() = 0;
  92. };
  93. // Create a connectionless socket that you can send packets out of.
  94. ISocket* CreateIPSocket();
  95. // This sets up the socket to receive multicast data on the specified group.
  96. // By default, localInterface is INADDR_ANY, but if you want to specify a specific interface
  97. // the data should come in through, you can.
  98. ISocket* CreateMulticastListenSocket(
  99. const CIPAddr &addr,
  100. const CIPAddr &localInterface = CIPAddr()
  101. );
  102. // Setup a CIPAddr from the string. The string can be a dotted IP address or
  103. // a hostname, and it can be followed by a colon and a port number like "1.2.3.4:3443"
  104. // or "myhostname.valvesoftware.com:2342".
  105. //
  106. // Note: if the string does not contain a port, then pOut->port will be left alone.
  107. bool ConvertStringToIPAddr( const char *pStr, CIPAddr *pOut );
  108. // Do a DNS lookup on the IP.
  109. // You can optionally get a service name back too.
  110. bool ConvertIPAddrToString( const CIPAddr *pIn, char *pOut, int outLen );
  111. void IP_GetLastErrorString( char *pStr, int maxLen );
  112. void SockAddrToIPAddr( const struct sockaddr_in *pIn, CIPAddr *pOut );
  113. void IPAddrToSockAddr( const CIPAddr *pIn, struct sockaddr_in *pOut );
  114. #endif