-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSocket.hpp
127 lines (100 loc) · 2.37 KB
/
Socket.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#pragma once
#include <nstd/String.hpp>
class Socket
{
public:
enum Address
{
anyAddress = 0,
loopbackAddress = 0x7f000001,
broadcastAddress = 0xffffffff,
};
enum Protocol
{
tcpProtocol,
udpProtocol,
};
public:
Socket();
~Socket();
bool open(Protocol protocol = tcpProtocol);
void close();
bool isOpen() const;
int64 getFileDescriptor() const;
void swap(Socket& other);
bool pair(Socket& other);
bool accept(Socket& to, uint32& ip, uint16& port);
bool bind(uint32 ip, uint16 port);
bool listen();
bool connect(uint32 ip, uint16 port);
ssize send(const byte* data, usize size);
ssize recv(byte* data, usize maxSize, usize minSize = 0);
ssize sendTo(const byte* data, usize size, uint32 ip, uint16 port);
ssize recvFrom(byte* data, usize maxSize, uint32& ip, uint16& port);
bool setKeepAlive();
bool setReuseAddress();
bool setNonBlocking();
bool setNoDelay();
bool setSendBufferSize(int size);
bool setReceiveBufferSize(int size);
bool setBroadcast();
bool joinMulticastGroup(uint32 ip, uint32 interfaceIp = anyAddress);
bool setMulticastLoopback(bool enable);
int getAndResetErrorStatus();
bool getSockName(uint32& ip, uint16& port);
bool getPeerName(uint32& ip, uint16& port);
bool getSockOpt(int level, int optname, void *optval, usize& optlen);
static void setLastError(int error);
static int getLastError();
static String getErrorString(int error = getLastError());
static uint32 inetAddr(const String& addr, uint16* port = 0);
static String inetNtoA(uint32 ip);
static String getHostName();
static bool getHostByName(const String& host, uint32& addr);
public:
class Poll
{
public:
enum Flag
{
readFlag = 0x01,
writeFlag = 0x02,
acceptFlag = 0x04,
connectFlag = 0x08,
};
struct Event
{
uint flags;
Socket* socket;
};
public:
Poll();
~Poll();
void set(Socket& socket, uint flags);
void remove(Socket& socket);
void clear();
bool poll(Event& event, int64 timeout);
bool interrupt();
private:
Poll(const Poll&);
Poll& operator=(const Poll&);
private:
class Private;
Private* p;
};
private:
#ifdef _WIN32
#ifdef _AMD64
uint64 s;
#else
uint s;
#endif
#else
int s;
#endif
private:
Socket(const Socket&);
Socket& operator=(const Socket&);
private:
class Private;
};