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


Copyright © 2005-2023 MAK Technologies. All Rights Reserved (www.mak.com)