-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtcp.cc
92 lines (78 loc) · 2.03 KB
/
tcp.cc
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
#include <string.h>
#include <string>
#include <cassert>
#include "tcp.h"
#include "netsnoop.h"
#include "sock.h"
Tcp::Tcp() : Sock(SOCK_STREAM, IPPROTO_TCP) {}
Tcp::Tcp(int fd) : Sock(SOCK_STREAM, IPPROTO_TCP, fd) {}
int Tcp::Listen(int count)
{
ASSERT(fd_ > 0);
if (listen(fd_, count) < 0)
{
PSOCKETERROR("listen error");
return -1;
}
return 0;
}
int Tcp::Accept()
{
ASSERT(fd_ > 0);
int peerfd;
int result;
fd_set fds;
FD_ZERO(&fds);
struct sockaddr_in peeraddr;
socklen_t peeraddr_size = sizeof(peeraddr);
memset(&peeraddr, 0, sizeof(peeraddr));
if ((peerfd = accept(fd_, (struct sockaddr *)&peeraddr, &peeraddr_size)) == -1)
{
PSOCKETERROR("accept error");
return -1;
}
LOGDP("accept tcp(%d): fd=%d, %s:%d", fd_, peerfd, inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port));
return peerfd;
}
int Tcp::InitializeEx(int fd) const
{
int opt = 1;
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&opt, sizeof(opt)) < 0)
{
PSOCKETERROR("setsockopt TCP_NODELAY error");
closesocket(fd);
return -1;
}
opt = 1;
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&opt, sizeof(opt)) < 0)
{
PSOCKETERROR("setsockopt TCP_NODELAY error");
closesocket(fd);
return -1;
}
#ifndef WIN32
srand(time(NULL));
int keepidle = 5+rand()%5;
int keepintvl = 5;
int keepcnt = 10;
if(setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &keepidle, sizeof(int))<0)
{
PSOCKETERROR("setsocketopt TCP_KEEPIDLE error");
closesocket(fd);
return -1;
}
if(setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &keepintvl, sizeof(int))<0)
{
PSOCKETERROR("setsocketopt TCP_KEEPINTVL error");
closesocket(fd);
return -1;
}
if(setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &keepcnt, sizeof(int))<0)
{
PSOCKETERROR("setsocketopt TCP_KEEPCNT error");
closesocket(fd);
return -1;
}
#endif // !WIN32
return fd;
}