-
Notifications
You must be signed in to change notification settings - Fork 2
/
SocketBase.cpp
243 lines (203 loc) · 4.74 KB
/
SocketBase.cpp
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "SocketBase.h"
EASY_NS_BEGIN
SocketBase::SocketBase()
: protocol(4), addr(0), del(0), errorCode(0),
set_unblock(true), set_reuseAddr(true), socket_type(SOCK_STREAM)
{
}
SocketBase::~SocketBase()
{
if (addr) {
addr->release();
addr = 0;
}
}
void SocketBase::close(bool hasError /*= false*/)
{
if (del) {
del->onClose(hasError);
}
}
bool SocketBase::addMembership(const char *addr, const char *interface_ /*= 0*/)
{
int sock = getSockFd();
if (sock == -1) {
setError(0, SocketInvalid);
EASY_LOG("SocketBase::addMembership error: invalid socket");
return false;
}
int ret = -1;
if (protocol == 4) {
ip_mreq addrGroup;
memset(&addrGroup, 0, sizeof(struct ip_mreq));
addrGroup.imr_multiaddr.s_addr = inet_addr(addr);
addrGroup.imr_interface.s_addr = interface_ ? inet_addr(interface_) : htonl(INADDR_ANY);
ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(char*)&addrGroup, sizeof(ip_mreq));
} else {
#ifndef EASY_IOS
// TODO: ios IPV6_ADD_MEMBERSHIP is undefined.
ipv6_mreq addrGroup;
memset(&addrGroup, 0, sizeof(struct ip_mreq));
inet_pton(AF_INET6, addr, &addrGroup.ipv6mr_multiaddr);
if (ret == -1){
EASY_LOG("SocketBase::addMembership.inet_pton error: %s", strerror(errno));
return false;
}
addrGroup.ipv6mr_interface = interface_ ? 0 : atoi(interface_);
ret = setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
(char*)&addrGroup, sizeof(ipv6_mreq));
#endif
}
if (ret == -1){
EASY_LOG("SocketBase::addMembership.setsockopt error: %s", strerror(errno));
return false;
}
return true;
}
bool SocketBase::dropMembership(const char *addr, const char *interface_ /*= 0*/)
{
int sock = getSockFd();
if (sock == -1) {
setError(0, SocketInvalid);
EASY_LOG("SocketBase::addMembership error: invalid socket");
return false;
}
int ret = -1;
if (protocol == 4) {
ip_mreq addrGroup;
addrGroup.imr_multiaddr.s_addr = inet_addr(addr);
addrGroup.imr_interface.s_addr = interface_ ? inet_addr(interface_) : htonl(INADDR_ANY);
ret = setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP,
(char*)&addrGroup, sizeof(ip_mreq));
} else {
#ifndef EASY_IOS
ipv6_mreq addrGroup;
inet_pton(AF_INET6, addr, &addrGroup.ipv6mr_multiaddr);
if (ret == -1){
EASY_LOG("SocketBase::addMembership.inet_pton error: %s", strerror(errno));
return false;
}
addrGroup.ipv6mr_interface = interface_ ? 0 : atoi(interface_);
ret = setsockopt(sock, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP,
(char*)&addrGroup, sizeof(ipv6_mreq));
#endif
}
if (ret == -1){
EASY_LOG("SocketBase::dropMembership.setsockopt error: %s", strerror(errno));
return false;
}
return true;
}
int SocketBase::select(int ms)
{
timeval tv;
timeval *pt = &tv;
if (ms < 0) {
pt = 0;
} else {
pt->tv_sec = ms / 1000;
pt->tv_usec = ms % 1000;
}
fd_set rfd, wfd;
FD_ZERO(&rfd);
FD_ZERO(&wfd);
int fd = getSockFd();
FD_SET(fd, &rfd);
FD_SET(fd, &wfd);
int ret = 0;
if (::select(fd + 1, &rfd, &wfd, 0, pt) > 0) {
if (FD_ISSET(fd, &rfd)) {
ret |= 1;
}
if (FD_ISSET(fd, &wfd)) {
ret |= 2;
}
}
return ret;
}
bool SocketBase::canRead()
{
return select() & 1;
}
bool SocketBase::canWrite()
{
return select() & 2;
}
int SocketBase::getError()
{
int cur = errorCode;
errorCode = 0;
return cur;
}
int SocketBase::getInternalError()
{
int cur = internalError;
internalError = 0;
return cur;
}
SockAddr* SocketBase::getSockAddr(int port, const char *ip)
{
if (!addr) {
addr = new SockAddr(protocol);
}
if (port != 0 || ip != 0) {
addr->setPort(port);
addr->setIp(ip);
}
return addr;
}
void SocketBase::setSockAddr(SockAddr *p)
{
if (addr) {
addr->release();
}
addr = p;
addr->retain();
}
void SocketBase::setDelegate(ISocketBaseEvent *p)
{
del = p;
}
void SocketBase::setError(int e, int ie /*= 0*/)
{
errorCode = e != 0 ? e : 0;
internalError = ie != 0 ? ie : 0;
}
void SocketBase::emitError()
{
if (del) {
del->onError(errorCode, internalError);
}
close(true);
}
bool SocketBase::noDelay(bool no)
{
int d = no ? 1 : 0;
int ret = setsockopt(getSockFd(), IPPROTO_TCP, TCP_NODELAY, (const char *)&d, sizeof(d));
if (ret == -1) {
EASY_LOG("SocketBase::noDelay.setsockopt error: %s", strerror(errno));
return false;
}
return true;
}
bool SocketBase::reuseAddr(bool use)
{
int d = use ? 1 : 0;
int ret = setsockopt(getSockFd(), SOL_SOCKET, SO_REUSEADDR, (const char *)&d, sizeof(d));
if (ret == -1) {
EASY_LOG("SocketBase::reuseAddr.setsockopt error: %s", strerror(errno));
return false;
}
return true;
}
bool SocketBase::setSendBufferSize(int size)
{
int ret = setsockopt(getSockFd(), SOL_SOCKET, SO_SNDBUF, (const char *)&size, sizeof(size));
if (ret == -1) {
EASY_LOG("SocketBase::setSendBufferSize.setsockopt error: %s", strerror(errno));
return false;
}
return true;
}
EASY_NS_END