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.

161 lines
4.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #if !defined( SOCKET_H )
  8. #define SOCKET_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "netadr.h"
  13. #include "MsgBuffer.h"
  14. #include "utlvector.h"
  15. #include <stdio.h>
  16. class CMsgBuffer;
  17. class CSocket;
  18. class IGameList;
  19. // Use this to pick apart the network stream, must be packed
  20. #pragma pack(1)
  21. typedef struct
  22. {
  23. int netID;
  24. int sequenceNumber;
  25. char packetID;
  26. } SPLITPACKET;
  27. #pragma pack()
  28. #define MAX_PACKETS 16 // 4 bits for the packet count, so only
  29. #define MAX_RETRIES 2 // the number of fragments from other packets to drop before we declare the outstanding
  30. // fragment lost :)
  31. //-----------------------------------------------------------------------------
  32. // Purpose: Instances a message handler for incoming messages.
  33. //-----------------------------------------------------------------------------
  34. class CMsgHandler
  35. {
  36. public:
  37. enum
  38. {
  39. MAX_HANDLER_STRING = 64
  40. };
  41. typedef enum
  42. {
  43. MSGHANDLER_ALL = 0,
  44. MSGHANDLER_BYTECODE,
  45. MSGHANDLER_STRING
  46. } HANDLERTYPE;
  47. // Construction
  48. CMsgHandler( HANDLERTYPE type, void *typeinfo = 0 );
  49. virtual ~CMsgHandler( void );
  50. // Message received, process it
  51. virtual bool Process( netadr_t *from, CMsgBuffer *msg ) = 0;
  52. // For linking togethr handler chains
  53. virtual CMsgHandler *GetNext( void ) const;
  54. virtual void SetNext( CMsgHandler *next );
  55. // Access/set associated socket
  56. virtual CSocket *GetSocket( void ) const;
  57. virtual void SetSocket( CSocket *socket );
  58. private:
  59. // Internal message received, crack type info and check it before calling process
  60. bool ProcessMessage( netadr_t *from, CMsgBuffer *msg );
  61. // Opaque pointer to underlying recipient class
  62. IGameList *m_pBaseObject;
  63. // Next handler in chain
  64. HANDLERTYPE m_Type;
  65. unsigned char m_ByteCode;
  66. char m_szString[ MAX_HANDLER_STRING ];
  67. // Next handler in chain
  68. CMsgHandler *m_pNext;
  69. // Associated socket
  70. CSocket *m_pSocket;
  71. friend CSocket;
  72. };
  73. //-----------------------------------------------------------------------------
  74. // Purpose: Creates a non-blocking, broadcast capable, UDP socket. If port is
  75. // specified, binds it to listen on that port, otherwise, chooses a random port.
  76. //-----------------------------------------------------------------------------
  77. class CSocket
  78. {
  79. public:
  80. // Construction/destruction
  81. CSocket( const char *socketname, int port = -1 );
  82. virtual ~CSocket( void );
  83. // Adds the message hander to the head of the sockets handler chain
  84. virtual void AddMessageHandler( CMsgHandler *handler );
  85. // Removes the specified message handler
  86. virtual void RemoveMessageHandler( CMsgHandler *handler );
  87. // Send the message to the recipient, if msg == NULL, use the internal message buffer
  88. virtual int SendMessage( netadr_t *to, CMsgBuffer *msg = NULL );
  89. // Broadcast the message on the specified port, if msg == NULL use the internal message buffer
  90. virtual int Broadcast( int port, CMsgBuffer *msg = NULL );
  91. // Get access to the internal message buffer
  92. virtual CMsgBuffer *GetSendBuffer( void );
  93. // Called once per frame to check for new data
  94. virtual void Frame( void );
  95. // Check whether the socket was created and set up properly
  96. virtual bool IsValid( void ) const;
  97. // Get the address this socket is bound to
  98. virtual const netadr_t *GetAddress( void );
  99. // Allow creating object to store a 32 bit value and retrieve it
  100. virtual void SetUserData( unsigned int userData );
  101. virtual unsigned int GetUserData(void ) const;
  102. // Allow other objects to get the raw socket interger
  103. virtual int GetSocketNumber( void ) const;
  104. // Called when FD_ISSET noted that the socket has incoming data
  105. virtual bool ReceiveData( void );
  106. // Called to get current time
  107. static float GetClock( void );
  108. private:
  109. const char *m_pSocketName;
  110. // Socket listen address
  111. bool m_bValid;
  112. // Socket IP address
  113. netadr_t m_Address;
  114. // Has the IP address been resolved
  115. bool m_bResolved;
  116. // Internal message buffers
  117. CUtlVector<CMsgBuffer> m_MsgBuffers;
  118. CMsgBuffer m_SendBuffer;
  119. // critical section for accessing buffers
  120. void *m_pBufferCS;
  121. // One or more listeners for the incoming message
  122. CMsgHandler *m_pMessageHandlers;
  123. // Winsock socket number
  124. int m_Socket;
  125. // User 32 bit value
  126. unsigned int m_nUserData;
  127. // Socket to which non Broadcast SendMessage was directed. The socket will wait for a response
  128. // from that exact address
  129. netadr_t m_ToAddress;
  130. // Set to true if the send was a Broadcast, and therefore from != to address is okay
  131. bool m_bBroadcastSend;
  132. int m_iTotalPackets; // total number of packets in a fragment
  133. int m_iCurrentPackets; // current packet count
  134. int m_iSeqNo; // the sequence number of the packet
  135. int m_iRetries;
  136. CMsgBuffer m_CurPacket[MAX_PACKETS]; // store for the packet
  137. };
  138. #endif // SOCKET_H