2017-09-11 20:51:11 +00:00
|
|
|
#ifndef __TRACYSOCKET_HPP__
|
|
|
|
#define __TRACYSOCKET_HPP__
|
|
|
|
|
2020-05-13 17:12:52 +00:00
|
|
|
#include <atomic>
|
2022-04-26 19:23:22 +00:00
|
|
|
#include <stddef.h>
|
2020-04-13 12:14:36 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2020-05-14 00:27:04 +00:00
|
|
|
struct addrinfo;
|
2019-06-17 17:23:43 +00:00
|
|
|
struct sockaddr;
|
|
|
|
|
2017-09-11 20:51:11 +00:00
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
2019-01-19 11:03:30 +00:00
|
|
|
#ifdef _WIN32
|
2018-08-19 15:45:03 +00:00
|
|
|
void InitWinSock();
|
|
|
|
#endif
|
|
|
|
|
2017-09-11 20:51:11 +00:00
|
|
|
class Socket
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Socket();
|
|
|
|
Socket( int sock );
|
|
|
|
~Socket();
|
|
|
|
|
2020-10-02 16:51:54 +00:00
|
|
|
bool Connect( const char* addr, uint16_t port );
|
|
|
|
bool ConnectBlocking( const char* addr, uint16_t port );
|
2017-09-11 20:51:11 +00:00
|
|
|
void Close();
|
|
|
|
|
|
|
|
int Send( const void* buf, int len );
|
2019-04-01 16:50:37 +00:00
|
|
|
int GetSendBufSize();
|
2017-09-11 20:51:11 +00:00
|
|
|
|
2020-09-10 19:39:58 +00:00
|
|
|
int ReadUpTo( void* buf, int len, int timeout );
|
2020-04-13 12:22:58 +00:00
|
|
|
bool Read( void* buf, int len, int timeout );
|
|
|
|
|
2020-04-13 12:14:36 +00:00
|
|
|
template<typename ShouldExit>
|
|
|
|
bool Read( void* buf, int len, int timeout, ShouldExit exitCb )
|
|
|
|
{
|
|
|
|
auto cbuf = (char*)buf;
|
|
|
|
while( len > 0 )
|
|
|
|
{
|
|
|
|
if( exitCb() ) return false;
|
|
|
|
if( !ReadImpl( cbuf, len, timeout ) ) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-10 14:45:23 +00:00
|
|
|
bool ReadRaw( void* buf, int len, int timeout );
|
2017-09-13 23:12:27 +00:00
|
|
|
bool HasData();
|
2019-12-19 16:23:40 +00:00
|
|
|
bool IsValid() const;
|
2017-09-13 00:08:30 +00:00
|
|
|
|
2017-09-11 20:51:11 +00:00
|
|
|
Socket( const Socket& ) = delete;
|
|
|
|
Socket( Socket&& ) = delete;
|
|
|
|
Socket& operator=( const Socket& ) = delete;
|
|
|
|
Socket& operator=( Socket&& ) = delete;
|
|
|
|
|
|
|
|
private:
|
2019-02-10 14:45:23 +00:00
|
|
|
int RecvBuffered( void* buf, int len, int timeout );
|
|
|
|
int Recv( void* buf, int len, int timeout );
|
2018-07-15 17:34:47 +00:00
|
|
|
|
2020-04-13 12:14:36 +00:00
|
|
|
bool ReadImpl( char*& buf, int& len, int timeout );
|
|
|
|
|
2018-07-15 17:34:47 +00:00
|
|
|
char* m_buf;
|
|
|
|
char* m_bufPtr;
|
2020-05-13 17:12:52 +00:00
|
|
|
std::atomic<int> m_sock;
|
2018-07-15 17:34:47 +00:00
|
|
|
int m_bufLeft;
|
2020-05-14 00:27:04 +00:00
|
|
|
|
|
|
|
struct addrinfo *m_res;
|
|
|
|
struct addrinfo *m_ptr;
|
|
|
|
int m_connSock;
|
2017-09-11 20:51:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ListenSocket
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ListenSocket();
|
|
|
|
~ListenSocket();
|
|
|
|
|
2020-10-02 16:51:54 +00:00
|
|
|
bool Listen( uint16_t port, int backlog );
|
2017-10-18 17:49:17 +00:00
|
|
|
Socket* Accept();
|
2017-09-11 20:51:11 +00:00
|
|
|
void Close();
|
|
|
|
|
|
|
|
ListenSocket( const ListenSocket& ) = delete;
|
|
|
|
ListenSocket( ListenSocket&& ) = delete;
|
|
|
|
ListenSocket& operator=( const ListenSocket& ) = delete;
|
|
|
|
ListenSocket& operator=( ListenSocket&& ) = delete;
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_sock;
|
|
|
|
};
|
|
|
|
|
2019-06-17 00:24:55 +00:00
|
|
|
class UdpBroadcast
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UdpBroadcast();
|
|
|
|
~UdpBroadcast();
|
|
|
|
|
2020-10-02 16:51:54 +00:00
|
|
|
bool Open( const char* addr, uint16_t port );
|
2019-06-17 00:24:55 +00:00
|
|
|
void Close();
|
|
|
|
|
2020-10-02 16:51:54 +00:00
|
|
|
int Send( uint16_t port, const void* data, int len );
|
2019-06-17 00:24:55 +00:00
|
|
|
|
|
|
|
UdpBroadcast( const UdpBroadcast& ) = delete;
|
|
|
|
UdpBroadcast( UdpBroadcast&& ) = delete;
|
|
|
|
UdpBroadcast& operator=( const UdpBroadcast& ) = delete;
|
|
|
|
UdpBroadcast& operator=( UdpBroadcast&& ) = delete;
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_sock;
|
2020-12-16 14:24:20 +00:00
|
|
|
uint32_t m_addr;
|
2019-06-17 00:24:55 +00:00
|
|
|
};
|
|
|
|
|
2019-06-17 17:23:43 +00:00
|
|
|
class IpAddress
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
IpAddress();
|
|
|
|
~IpAddress();
|
|
|
|
|
|
|
|
void Set( const struct sockaddr& addr );
|
|
|
|
|
|
|
|
uint32_t GetNumber() const { return m_number; }
|
|
|
|
const char* GetText() const { return m_text; }
|
|
|
|
|
|
|
|
IpAddress( const IpAddress& ) = delete;
|
|
|
|
IpAddress( IpAddress&& ) = delete;
|
|
|
|
IpAddress& operator=( const IpAddress& ) = delete;
|
|
|
|
IpAddress& operator=( IpAddress&& ) = delete;
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint32_t m_number;
|
2019-06-26 15:58:23 +00:00
|
|
|
char m_text[17];
|
2019-06-17 17:23:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class UdpListen
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UdpListen();
|
|
|
|
~UdpListen();
|
|
|
|
|
2020-10-02 16:51:54 +00:00
|
|
|
bool Listen( uint16_t port );
|
2019-06-17 17:23:43 +00:00
|
|
|
void Close();
|
|
|
|
|
2020-09-20 20:06:28 +00:00
|
|
|
const char* Read( size_t& len, IpAddress& addr, int timeout );
|
2019-06-17 17:23:43 +00:00
|
|
|
|
|
|
|
UdpListen( const UdpListen& ) = delete;
|
|
|
|
UdpListen( UdpListen&& ) = delete;
|
|
|
|
UdpListen& operator=( const UdpListen& ) = delete;
|
|
|
|
UdpListen& operator=( UdpListen&& ) = delete;
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_sock;
|
|
|
|
};
|
|
|
|
|
2017-09-11 20:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|