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.

84 lines
2.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef ITCPSOCKET_H
  8. #define ITCPSOCKET_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "ichannel.h"
  13. #include "iphelpers.h"
  14. class ITCPSocket : public IChannel
  15. {
  16. public:
  17. virtual void Release() = 0;
  18. // Bind to the specified port on any address this host has. Note that the address the
  19. // socket winds up with (and getsockname() returns) isn't decided until you send a packet.
  20. virtual bool BindToAny( const unsigned short port ) = 0;
  21. // Use these to connect to a remote listening socket without blocking.
  22. // Call BeginConnect, then call UpdateConnect until it returns true.
  23. virtual bool BeginConnect( const CIPAddr &addr ) = 0;
  24. virtual bool UpdateConnect() = 0;
  25. // Connection state.
  26. virtual bool IsConnected() = 0;
  27. // If IsConnected returns false, you can call this to find out why the socket got disconnected.
  28. virtual void GetDisconnectReason( CUtlVector<char> &reason ) = 0;
  29. // Send data. Returns true if successful.
  30. //
  31. // Note: TCP likes to clump your packets together in one, so the data multiple send() calls will
  32. // get concatenated and returned in one recv() call. ITCPSocket FIXES this behavior so your recv()
  33. // calls match your send() calls.
  34. //
  35. virtual bool Send( const void *pData, int size ) = 0;
  36. // Receive data. Returns the number of bytes received.
  37. // This will wait as long as flTimeout for something to come in.
  38. // Returns false if no data was waiting.
  39. virtual bool Recv( CUtlVector<unsigned char> &data, double flTimeout=0 ) = 0;
  40. };
  41. // Use these to get incoming connections.
  42. class ITCPListenSocket
  43. {
  44. public:
  45. // Call this to stop listening for connections and delete the object.
  46. virtual void Release() = 0;
  47. // Keep calling this as long as you want to wait for connections.
  48. virtual ITCPSocket* UpdateListen( CIPAddr *pAddr ) = 0; // pAddr is set to the remote process's address.
  49. };
  50. // Use these to create the interfaces.
  51. ITCPSocket* CreateTCPSocket();
  52. // Create a socket to listen with. nQueueLength specifies how many connections to enqueue.
  53. // When the queue runs out, connections can take a little longer to make.
  54. ITCPListenSocket* CreateTCPListenSocket( const unsigned short port, int nQueueLength = -1 );
  55. // By default, timeouts are on. It's helpful to turn them off during debugging.
  56. void TCPSocket_EnableTimeout( bool bEnable );
  57. #endif // ITCPSOCKET_H