-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtcp_server.cpp
182 lines (161 loc) · 5.86 KB
/
tcp_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
178
179
180
181
182
//
// Created by jason on 5/26/21.
//
#include "tcp_server.h"
#include "tcp_client.h"
#include <asm-generic/ioctls.h>
#include <csignal>
#include <cstring>
#include <fcntl.h>
#include <iostream>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
void tcp_server::errExit(std::string s) {
std::cout << s << std::endl;
exit(1);
}
// https://www.geeksforgeeks.org/socket-programming-cc/
// https://www.ibm.com/docs/en/i/7.2?topic=designs-example-nonblocking-io-select
tcp_server::tcp_server() {
}
void tcp_server::init() {
server_socket = socket(AF_INET, SOCK_STREAM, 0);
int opt = 1;
if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
errExit("resuable");
}
if (ioctl(server_socket, FIONBIO, &opt) < 0) {
errExit("non-blocking");
}
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( 8888);
if (bind(server_socket, (struct sockaddr *)&address,sizeof(address)) < 0) {
errExit("bind");
} else {
std::cout << "bound to port: " << ntohs(address.sin_port) << std::endl;
}
FD_ZERO(&readfds);
if (pipe(tcp_server::pfd) == -1)
errExit("pipe");
FD_SET(tcp_server::pfd[0], &readfds); /* Add read end of pipe to 'readfds' */
nfds = std::max(nfds, tcp_server::pfd[0] + 1); /* And adjust 'nfds' if required */
std::cout << "PFD: " << pfd[0] << " NFDS: " << nfds << std::endl;
/* Make read and write ends of pipe nonblocking */
int flags = fcntl(tcp_server::pfd[0], F_GETFL);
if (flags == -1)
errExit("fcntl-F_GETFL");
flags |= O_NONBLOCK; /* Make read end nonblocking */
if (fcntl(tcp_server::pfd[0], F_SETFL, flags) == -1)
errExit("fcntl-F_SETFL");
flags = fcntl(tcp_server::pfd[1], F_GETFL);
if (flags == -1)
errExit("fcntl-F_GETFL");
flags |= O_NONBLOCK; /* Make write end nonblocking */
if (fcntl(tcp_server::pfd[1], F_SETFL, flags) == -1)
errExit("fcntl-F_SETFL");
// register the signal handlers
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART; /* Restart interrupted reads()s */
sa.sa_handler = handler;
if (sigaction(SIGUSR1, &sa, NULL) == -1)
errExit("sigaction");
}
void tcp_server::listen() {
FD_SET(server_socket, &readfds); /* Add server socket to 'readfds' */
nfds = std::max(nfds, server_socket + 1); /* And adjust 'nfds' if required */
std::cout << "SS: " << server_socket << " NFDS: " << nfds << std::endl;
if(::listen(server_socket, 3) < 0) {
errExit("listen");
}
struct sockaddr_in address;
int addrlen = sizeof(address);
listen_thread = std::thread([&]() {
running = true;
std::cout << "Listening" << std::endl;
while(running) {
int ready;
struct timeval *pto = NULL;
// need to use a working set because after select is done, it screws up the original
fd_set working_set;
memcpy(&working_set, &readfds, sizeof(readfds));
while ((ready = select(nfds, &working_set, NULL, NULL, pto)) == -1 &&
errno == EINTR) {
std::cout << "L" << std::endl;
continue; /* Restart if interrupted by signal */
}
if (ready == -1) /* Unexpected error */
errExit("select");
if (FD_ISSET(pfd[0], &working_set)) { /* Handler was called */
printf("A signal was caught\n");
for (;;) { /* Consume bytes from pipe */
char ch;
if (read(pfd[0], &ch, 1) == -1) {
if (errno == EAGAIN)
break; /* No more bytes */
else
errExit("read"); /* Some other error */
}
/* Perform any actions that should be taken in response to signal */
return;
}
}
if (FD_ISSET(server_socket, &working_set)) {
std::cout << "readfds server socket ready" << std::endl;
int client_socket = accept(server_socket, (struct sockaddr *)&address, (socklen_t*)&addrlen);
if (client_socket < 0) {
std::cout << "Error accepting connection";
} else {
std::cout << "accepted connection" << std::endl;
close(client_socket);
}
}
}
std::cout << "Done listening thread" << std::endl;
});
}
void tcp_server::handler(int sig)
{
std::cout << "IN SIG HANDLER" << std::endl;
int savedErrno; /* In case we change 'errno' */
savedErrno = errno;
if (write(pfd[1], "x", 1) == -1 && errno != EAGAIN)
errExit("write");
std::cout << "Wrote to pipe" << std::endl;
errno = savedErrno;
}
void tcp_server::stop() {
std::cout << "stopping listening" << std::endl;
running = false;
FD_CLR(server_socket, &readfds);
close(server_socket);
std::cout << "waiting on listen_thread to stop" << std::endl;
raise(SIGUSR1);
listen_thread.join();
std::cout << "done stopping" << std::endl;
}
int main() {
// case 1: no connection
tcp_server server;
server.init();
usleep(0.5e6);
server.listen();
usleep(0.5e6);
std::cout << "STOPPING SERVER" << std::endl;
server.stop();
// case 2: connection
server.init();
usleep(0.5e6);
server.listen();
usleep(0.5e6);
tcp_client client;
client.connect("127.0.0.1", 8888);
std::cout << "STOPPING SERVER" << std::endl;
usleep(0.5e6);
server.stop();
return 0;
}