TracySocket.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #ifndef __TRACYSOCKET_HPP__
  2. #define __TRACYSOCKET_HPP__
  3. #include <atomic>
  4. #include <stdint.h>
  5. #include "TracyForceInline.hpp"
  6. struct addrinfo;
  7. struct sockaddr;
  8. namespace tracy
  9. {
  10. #ifdef _WIN32
  11. void InitWinSock();
  12. #endif
  13. class Socket
  14. {
  15. public:
  16. Socket();
  17. Socket( int sock );
  18. ~Socket();
  19. bool Connect( const char* addr, uint16_t port );
  20. bool ConnectBlocking( const char* addr, uint16_t port );
  21. void Close();
  22. int Send( const void* buf, int len );
  23. int GetSendBufSize();
  24. int ReadUpTo( void* buf, int len, int timeout );
  25. bool Read( void* buf, int len, int timeout );
  26. template<typename ShouldExit>
  27. bool Read( void* buf, int len, int timeout, ShouldExit exitCb )
  28. {
  29. auto cbuf = (char*)buf;
  30. while( len > 0 )
  31. {
  32. if( exitCb() ) return false;
  33. if( !ReadImpl( cbuf, len, timeout ) ) return false;
  34. }
  35. return true;
  36. }
  37. bool ReadRaw( void* buf, int len, int timeout );
  38. bool HasData();
  39. bool IsValid() const;
  40. Socket( const Socket& ) = delete;
  41. Socket( Socket&& ) = delete;
  42. Socket& operator=( const Socket& ) = delete;
  43. Socket& operator=( Socket&& ) = delete;
  44. private:
  45. int RecvBuffered( void* buf, int len, int timeout );
  46. int Recv( void* buf, int len, int timeout );
  47. bool ReadImpl( char*& buf, int& len, int timeout );
  48. char* m_buf;
  49. char* m_bufPtr;
  50. std::atomic<int> m_sock;
  51. int m_bufLeft;
  52. struct addrinfo *m_res;
  53. struct addrinfo *m_ptr;
  54. int m_connSock;
  55. };
  56. class ListenSocket
  57. {
  58. public:
  59. ListenSocket();
  60. ~ListenSocket();
  61. bool Listen( uint16_t port, int backlog );
  62. Socket* Accept();
  63. void Close();
  64. ListenSocket( const ListenSocket& ) = delete;
  65. ListenSocket( ListenSocket&& ) = delete;
  66. ListenSocket& operator=( const ListenSocket& ) = delete;
  67. ListenSocket& operator=( ListenSocket&& ) = delete;
  68. private:
  69. int m_sock;
  70. };
  71. class UdpBroadcast
  72. {
  73. public:
  74. UdpBroadcast();
  75. ~UdpBroadcast();
  76. bool Open( const char* addr, uint16_t port );
  77. void Close();
  78. int Send( uint16_t port, const void* data, int len );
  79. UdpBroadcast( const UdpBroadcast& ) = delete;
  80. UdpBroadcast( UdpBroadcast&& ) = delete;
  81. UdpBroadcast& operator=( const UdpBroadcast& ) = delete;
  82. UdpBroadcast& operator=( UdpBroadcast&& ) = delete;
  83. private:
  84. int m_sock;
  85. uint32_t m_addr;
  86. };
  87. class IpAddress
  88. {
  89. public:
  90. IpAddress();
  91. ~IpAddress();
  92. void Set( const struct sockaddr& addr );
  93. uint32_t GetNumber() const { return m_number; }
  94. const char* GetText() const { return m_text; }
  95. IpAddress( const IpAddress& ) = delete;
  96. IpAddress( IpAddress&& ) = delete;
  97. IpAddress& operator=( const IpAddress& ) = delete;
  98. IpAddress& operator=( IpAddress&& ) = delete;
  99. private:
  100. uint32_t m_number;
  101. char m_text[17];
  102. };
  103. class UdpListen
  104. {
  105. public:
  106. UdpListen();
  107. ~UdpListen();
  108. bool Listen( uint16_t port );
  109. void Close();
  110. const char* Read( size_t& len, IpAddress& addr, int timeout );
  111. UdpListen( const UdpListen& ) = delete;
  112. UdpListen( UdpListen&& ) = delete;
  113. UdpListen& operator=( const UdpListen& ) = delete;
  114. UdpListen& operator=( UdpListen&& ) = delete;
  115. private:
  116. int m_sock;
  117. };
  118. }
  119. #endif