2017-09-11 20:51:11 +00:00
|
|
|
#ifndef __TRACYSOCKET_HPP__
|
|
|
|
#define __TRACYSOCKET_HPP__
|
|
|
|
|
2018-02-13 13:57:47 +00:00
|
|
|
#include <functional>
|
|
|
|
|
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
|
|
|
|
{
|
2018-07-15 17:34:47 +00:00
|
|
|
enum { BufSize = 128 * 1024 };
|
|
|
|
|
2017-09-11 20:51:11 +00:00
|
|
|
public:
|
|
|
|
Socket();
|
|
|
|
Socket( int sock );
|
|
|
|
~Socket();
|
|
|
|
|
|
|
|
bool Connect( const char* addr, const char* port );
|
|
|
|
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
|
|
|
|
2019-02-10 14:45:23 +00:00
|
|
|
bool Read( void* buf, int len, int timeout, std::function<bool()> exitCb );
|
|
|
|
bool ReadRaw( void* buf, int len, int timeout );
|
2017-09-13 23:12:27 +00:00
|
|
|
bool HasData();
|
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
|
|
|
|
|
|
|
char* m_buf;
|
|
|
|
char* m_bufPtr;
|
2019-01-29 20:56:10 +00:00
|
|
|
int m_sock;
|
2018-07-15 17:34:47 +00:00
|
|
|
int m_bufLeft;
|
2017-09-11 20:51:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ListenSocket
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ListenSocket();
|
|
|
|
~ListenSocket();
|
|
|
|
|
|
|
|
bool Listen( const char* 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();
|
|
|
|
|
|
|
|
bool Open( const char* addr, const char* port );
|
|
|
|
void Close();
|
|
|
|
|
2019-06-17 16:37:34 +00:00
|
|
|
int Send( int 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;
|
|
|
|
};
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
bool Listen( int port );
|
|
|
|
void Close();
|
|
|
|
|
2019-06-22 12:08:48 +00:00
|
|
|
const char* Read( size_t& len, IpAddress& addr );
|
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
|