25#include "local_config.h"
31#define SOCKET_HOSTNAME_MAXSIZE 256
32#define SOCKET_RCVBUF_MINSIZE 16384
33#define SOCKET_TIMEOUT_SEC 10
34#define SOCKET_TIMEOUT_USEC 0
35#define SOCKET_READ_ATTEMPT 3
36#define SOCKET_BUFFER_SIZE 1472
37#define SOCKET_LISTEN_QUEUE_SIZE 50
54 m_timeout.tv_sec = SOCKET_TIMEOUT_SEC;
55 m_timeout.tv_usec = SOCKET_TIMEOUT_USEC;
57 virtual ~NetSocket() { }
58 virtual bool SendData(
const char* buf,
size_t size) = 0;
59 virtual size_t ReceiveData(
void* buf,
size_t n) = 0;
60 void SetTimeout(timeval timeout) { m_timeout = timeout; }
63 struct timeval m_timeout;
66 class TcpSocket :
public NetSocket
68 friend class TcpServerSocket;
92 virtual bool Connect(
const char* server,
unsigned port,
int rcvbuf);
100 virtual bool SendData(
const char* buf,
size_t size);
108 virtual size_t ReceiveData(
void* buf,
size_t n);
116 virtual size_t BlockingRead(
void* buf,
size_t n);
121 virtual void Disconnect();
126 virtual bool IsValid()
const;
134 int Listen(timeval *timeout);
144 std::string GetRemoteAddrInfo();
149 std::string GetHostAddrInfo();
154 static const char* GetMyHostName();
157 net_socket_t m_socket;
173 class TcpServerSocket
177 virtual ~TcpServerSocket();
188 bool Create(SOCKET_AF_t af);
193 bool IsValid()
const;
200 bool Bind(
unsigned port);
207 bool ListenConnection(
int queueSize = SOCKET_LISTEN_QUEUE_SIZE);
222 AcceptStatus AcceptConnection(TcpSocket& socket,
int timeout);
227 std::string GetRemoteAddrInfo();
241 net_socket_t m_socket;
243 unsigned m_requestQueueSize;
250 class UdpSocket :
public NetSocket
254 UdpSocket(
size_t bufferSize);
255 virtual ~UdpSocket();
268 bool SendData(
const char* buf,
size_t size);
276 size_t ReceiveData(
void* buf,
size_t n);
281 bool IsValid()
const;
290 bool Open(SOCKET_AF_t af,
const char* target,
unsigned port);
298 bool Open(SOCKET_AF_t af,
bool broadcast =
false);
306 bool SetAddress(
const char* target,
unsigned port);
314 bool SetMulticastTTL(
int multicastTTL);
319 std::string GetRemoteAddrInfo()
const;
329 net_socket_t m_socket;
341 class UdpServerSocket :
public NetSocket
345 UdpServerSocket(
size_t bufferSize);
357 bool Create(SOCKET_AF_t af);
362 bool IsValid()
const;
369 bool Bind(
unsigned port,
bool reuse =
false);
377 bool Bind(
const char* addr,
unsigned port,
bool reuse =
false);
385 bool SetMulticastTTL(
int multicastTTL);
393 bool SetMulticastMembership(
const char *group,
bool join);
399 size_t AwaitIncoming();
405 std::string GetRemoteAddrInfo()
const;
411 bool SendData(
const char*,
size_t) {
return false; }
438 size_t ReadData(
void* buf,
size_t n);
448 net_socket_t m_socket;
net_socket_t GetHandle() const
net_socket_t GetHandle() const
void SetReadAttempt(int n)
bool SendData(const char *, size_t)
net_socket_t GetHandle() const
size_t ReadData(void *buf, size_t n)
size_t ReceiveData(void *buf, size_t n)
net_socket_t GetHandle() const