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.

98 lines
3.1 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #ifndef SOCKET_CREATOR_H
  7. #define SOCKET_CREATOR_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "tier1/utlvector.h"
  12. #include "tier1/utlbuffer.h"
  13. #include "tier1/utllinkedlist.h"
  14. #include "tier1/netadr.h"
  15. #include "igameserverdata.h"
  16. typedef int SocketHandle_t;
  17. struct ISocketCreatorListener
  18. {
  19. public:
  20. // Methods to allow other classes to allocate data associated w/ sockets
  21. // Return false to disallow socket acceptance
  22. virtual bool ShouldAcceptSocket( SocketHandle_t hSocket, const netadr_t &netAdr ) = 0;
  23. virtual void OnSocketAccepted( SocketHandle_t hSocket, const netadr_t &netAdr, void** ppData ) = 0;
  24. virtual void OnSocketClosed( SocketHandle_t hSocket, const netadr_t &netAdr, void* pData ) = 0;
  25. };
  26. //-----------------------------------------------------------------------------
  27. // container class to handle network streams
  28. //-----------------------------------------------------------------------------
  29. class CSocketCreator
  30. {
  31. public:
  32. CSocketCreator( ISocketCreatorListener *pListener = NULL );
  33. ~CSocketCreator();
  34. // Call this once per frame
  35. void RunFrame();
  36. // This method is used to put the socket in a mode where it's listening
  37. // for connections and a connection is made once the request is received
  38. bool CreateListenSocket( const netadr_t &netAdr, bool bListenOnAllInterfaces = false );
  39. void CloseListenSocket();
  40. bool IsListening() const;
  41. // This method is used to connect to/disconnect from an external listening socket creator
  42. // Returns accepted socket index, or -1 if it failed.
  43. // Use GetAcceptedSocket* methods to access this socket's data
  44. // if bSingleSocket == true, all accepted sockets are closed before the new one is opened
  45. // NOTE: Closing an accepted socket will re-index all the sockets with higher indices
  46. int ConnectSocket( const netadr_t &netAdr, bool bSingleSocket );
  47. void CloseAcceptedSocket( int nIndex );
  48. void CloseAllAcceptedSockets();
  49. int GetAcceptedSocketCount() const;
  50. SocketHandle_t GetAcceptedSocketHandle( int nIndex ) const;
  51. const netadr_t& GetAcceptedSocketAddress( int nIndex ) const;
  52. void* GetAcceptedSocketData( int nIndex );
  53. // Closes all open sockets (listen + accepted)
  54. void Disconnect();
  55. private:
  56. enum
  57. {
  58. SOCKET_TCP_MAX_ACCEPTS = 2
  59. };
  60. void ProcessAccept();
  61. bool ConfigureSocket( int sock );
  62. public:
  63. struct AcceptedSocket_t
  64. {
  65. SocketHandle_t m_hSocket;
  66. netadr_t m_Address;
  67. void *m_pData;
  68. bool operator==( const AcceptedSocket_t &rhs ) const { return ( m_Address.CompareAdr( rhs.m_Address ) == 0 ); }
  69. };
  70. ISocketCreatorListener *m_pListener;
  71. CUtlVector< AcceptedSocket_t > m_hAcceptedSockets;
  72. SocketHandle_t m_hListenSocket; // Used to accept connections
  73. netadr_t m_ListenAddress; // Address used to listen on
  74. };
  75. //-----------------------------------------------------------------------------
  76. // Returns true if the socket would block because of the last socket command
  77. //-----------------------------------------------------------------------------
  78. bool SocketWouldBlock();
  79. #endif // SOCKET_CREATOR_H