-
Notifications
You must be signed in to change notification settings - Fork 203
/
nonblocking.h
143 lines (130 loc) · 4.12 KB
/
nonblocking.h
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
#ifndef NONBLOCKING_H_INCLUDED
#define NONBLOCKING_H_INCLUDED
#include "config.h"
#include <string.h>
#if WIN32
#include <winsock.h>
#include <winsock2.h>
#include <windows.h>
#else
#include <fcntl.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#endif
#include <list>
#include "sslbio.h"
using namespace std;
#if WIN32
struct tcp_keepalive
{
u_long onoff;
u_long keepalivetime;
u_long keepaliveinterval;
};
#define SIO_RCVALL _WSAIOW(IOC_VENDOR, 1)
#define SIO_RCVALL_MCAST _WSAIOW(IOC_VENDOR, 2)
#define SIO_RCVALL_IGMPMCAST _WSAIOW(IOC_VENDOR, 3)
#define SIO_KEEPALIVE_VALS _WSAIOW(IOC_VENDOR, 4)
#define SIO_ABSORB_RTRALERT _WSAIOW(IOC_VENDOR, 5)
#define SIO_UCAST_IF _WSAIOW(IOC_VENDOR, 6)
#define SIO_LIMIT_BROADCASTS _WSAIOW(IOC_VENDOR, 7)
#define SIO_INDEX_BIND _WSAIOW(IOC_VENDOR, 8)
#define SIO_INDEX_MCASTIF _WSAIOW(IOC_VENDOR, 9)
#define SIO_INDEX_ADD_MCAST _WSAIOW(IOC_VENDOR, 10)
#define SIO_INDEX_DEL_MCAST _WSAIOW(IOC_VENDOR, 11)
#define RCVALL_OFF 0
#define RCVALL_ON 1
#define RCVALL_SOCKETLEVELONLY 2
inline int SetKeepAlive(int sock)
{
BOOL bKeepAlive = TRUE;
int nRet = setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&bKeepAlive, sizeof(bKeepAlive));
if (nRet == SOCKET_ERROR)
{
return -1;
}
// 设置KeepAlive参数
tcp_keepalive alive_in = {0};
tcp_keepalive alive_out = {0};
alive_in.keepalivetime = 60000; // 开始首次KeepAlive探测前的TCP空闭时间
alive_in.keepaliveinterval = 60000; // 两次KeepAlive探测间的时间间隔
alive_in.onoff = TRUE;
unsigned long ulBytesReturn = 0;
//nRet = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &alive_in, sizeof(alive_in),&alive_out, sizeof(alive_out), &ulBytesReturn, NULL, NULL);
if (nRet == SOCKET_ERROR)
{
return -1;
}
return 0;
}
#else
#include <netinet/in.h>
#include <netinet/tcp.h>
inline int SetKeepAlive(int sock)
{
/*有人说是秒单位。。有人说是毫秒单位*/
int keepalive = 1; // 开启keepalive属性
int keepidle = 60000; // 如该连接在60秒内没有任何数据往来,则进行探测
int keepinterval = 60000; // 探测时发包的时间间隔为5 秒
int keepcount = 1; // 探测尝试的次数.如果第1次探测包就收到响应了,则后2次的不再发.
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepalive, sizeof(keepalive));
setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, (void *)&keepidle, sizeof(keepidle));
setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, (void *)&keepinterval, sizeof(keepinterval));
setsockopt(sock, SOL_TCP, TCP_KEEPCNT, (void *)&keepcount, sizeof(keepcount));
return 0;
}
#endif // WIN32
inline int setnonblocking(int sServer, int _nMode)
{
#if WIN32
DWORD nMode = _nMode;
return ioctlsocket(sServer, FIONBIO, &nMode);
#else
if (_nMode == 1)
{
return fcntl(sServer, F_SETFL, O_NONBLOCK);
}
else
{
return fcntl(sServer, F_SETFL, _nMode);
}
#endif
}
inline int net_dns(struct sockaddr_in *server_addr, const char *host, int port)
{
struct hostent *server_host;
if ((server_host = gethostbyname(host)) == NULL)
{
return -1;
}
memcpy((void *)&server_addr->sin_addr, (void *)server_host->h_addr, server_host->h_length);
server_addr->sin_family = AF_INET;
server_addr->sin_port = htons(port);
return 0;
}
inline int check_sock(int sock)
{
int error = -1;
#if WIN32
int len;
#else
socklen_t len;
#endif
len = sizeof(error);
getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&error, &len);
return error;
}
void clearsock(Sockinfo *sock_info);
inline int SetBufSize(int sock)
{
//接收缓冲区
int opt = 25 * 1024; //30K
setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (const char *)&opt, sizeof(opt));
//发送缓冲区 (这个千万不要。。发送不需要缓存区)
// setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (const char*)&opt,sizeof(opt));
return 0;
}
#endif // NONBLOCKING_H_INCLUDED