-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cpp
177 lines (149 loc) · 4.74 KB
/
server.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
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
#include <errno.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>
#include <string>
#include <sys/select.h>
#include <vector>
#include <set>
#include <thread>
#include <mutex>
#include <memory>
#include <limits>
int initserver(int type, const sockaddr *addr, socklen_t alen, int qlen) {
int fd;
int reuse = 1;
if ((fd = ::socket(addr->sa_family, type, 0)) < 0)
return -1;
if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int)) < 0)
goto errout;
if (::bind(fd, addr, alen) < 0)
goto errout;
if (type == SOCK_STREAM || type == SOCK_SEQPACKET)
if (::listen(fd, qlen) < 0)
goto errout;
return fd;
errout:
int err = errno;
::close(fd);
errno = err;
return -1;
}
template <typename TEle>
class AsyncSet {
public:
void add(TEle val) {
std::scoped_lock<std::mutex> lock(mutex_);
set_.insert(val);
}
void erase(TEle val) {
std::scoped_lock<std::mutex> lock(mutex_);
set_.erase(val);
}
std::vector<TEle> items() const {
std::scoped_lock<std::mutex> lock(mutex_);
std::vector<TEle> ret(set_.begin(), set_.end());
return ret;
}
private:
mutable std::mutex mutex_;
std::set<TEle> set_;
};
constexpr in_port_t port = 57311;
int main() {
addrinfo hint;
::memset(&hint, 0, sizeof(hint));
hint.ai_family = AF_INET;
hint.ai_socktype = SOCK_STREAM;
addrinfo *addrs;
::getaddrinfo("localhost", nullptr, &hint, &addrs);
char addr_str[INET_ADDRSTRLEN];
addrinfo *cursor = addrs;
sockaddr_in addr;
while (cursor) {
/* The domain is AF_INET */
sockaddr_in *addr_in = reinterpret_cast<sockaddr_in *>(cursor->ai_addr);
if (!::inet_ntop(addr_in->sin_family, &addr_in->sin_addr.s_addr, addr_str, INET_ADDRSTRLEN))
::printf("conversion failed\n");
else {
::printf("%s\n", addr_str);
::memcpy(&addr, addr_in, sizeof(sockaddr_in));
}
cursor = cursor->ai_next;
}
::freeaddrinfo(addrs);
addr.sin_port = port;
int fd = initserver(SOCK_STREAM, reinterpret_cast<sockaddr *>(&addr), sizeof(addr), 1);
if (fd < 0) {
::printf("failed to create socket\n");
return -1;
}
AsyncSet<int> async_fds;
std::thread echo([&async_fds] {
while (true) {
auto fds = async_fds.items();
fd_set fd_set;
FD_ZERO(&fd_set);
int max_fd = std::numeric_limits<int>::min();
for (auto fd : fds) {
max_fd = std::max(max_fd, fd);
FD_SET(fd, &fd_set);
}
max_fd = std::max(max_fd, 0);
timeval timeout {
.tv_sec = 0,
.tv_usec = 1000 * 50,
};
auto res = ::select(max_fd + 1, &fd_set, nullptr, nullptr, &timeout);
if (res < 0) {
::printf("Failed to select, %s\n", ::strerror(errno));
return;
}
else {
for (int fd = 0, cnt = 0; fd <= max_fd && cnt < res; ++fd) {
if (FD_ISSET(fd, &fd_set)) {
++cnt;
int client = fd;
std::string request;
char buf[1025];
while (true) {
ssize_t res = ::read(client, buf, 1024);
if (res < 0)
break;
buf[res] = '\0';
request += buf;
if (request.size() >= 2 && !request.compare(request.size() - 2, 2, "\r\n"))
break;
}
::write(client, request.c_str(), request.size());
if (request == "exit\r\n") {
::close(client);
async_fds.erase(client);
}
}
}
}
}
});
echo.detach();
while (true) {
int client;
if ((client = ::accept(fd, nullptr, nullptr)) < 0) {
::printf("failed to accept, %s\n", ::strerror(errno));
}
sockaddr peer;
socklen_t len = sizeof(peer);
::getpeername(client, &peer, &len);
sockaddr_in *ppeer = reinterpret_cast<sockaddr_in *>(&peer);
::inet_ntop(AF_INET, &ppeer->sin_addr.s_addr, addr_str, sizeof(ppeer->sin_addr.s_addr));
::printf("connect to %s:%d\n", addr_str, ppeer->sin_port);
async_fds.add(client);
}
::close(fd);
return 0;
}