Debian Codegraph @

3rdparty/tracy/public/common/TracySocket.hpp

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