-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_kernel.cpp
More file actions
152 lines (126 loc) · 3.46 KB
/
server_kernel.cpp
File metadata and controls
152 lines (126 loc) · 3.46 KB
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
// server_kernel.cpp
#include <cerrno>
#include <cinttypes>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <vector>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "common.h"
constexpr int LISTEN_PORT = 8080;
constexpr int BACKLOG = 1024;
static bool recv_all_bytes(int fd, char* buffer, size_t len)
{
size_t received = 0;
while (received < len) {
ssize_t n = recv(fd, buffer + received, len - received, 0);
if (n == 0) {
return false;
}
if (n < 0) {
if (errno == EINTR) {
continue;
}
perror("recv");
return false;
}
received += static_cast<size_t>(n);
}
return true;
}
static bool send_all_bytes(int fd, const char* buffer, size_t len)
{
size_t sent = 0;
while (sent < len) {
ssize_t n = send(fd, buffer + sent, len - sent, 0);
if (n == 0) {
return false;
}
if (n < 0) {
if (errno == EINTR) {
continue;
}
perror("send");
return false;
}
sent += static_cast<size_t>(n);
}
return true;
}
static bool recv_full_msg(int fd, std::vector<char>& buffer)
{
Msg header{};
if (!recv_all_bytes(fd, reinterpret_cast<char*>(&header), sizeof(header))) {
return false;
}
if (header.payload_size < sizeof(Msg)) {
std::fprintf(stderr,
"invalid payload_size=%" PRIu32 " (< header size %zu)\n",
header.payload_size, sizeof(Msg));
return false;
}
const size_t payload_bytes = header.payload_size - sizeof(Msg);
buffer.resize(header.payload_size);
std::memcpy(buffer.data(), &header, sizeof(header));
if (payload_bytes > 0 &&
!recv_all_bytes(fd, buffer.data() + sizeof(Msg), payload_bytes)) {
return false;
}
return true;
}
static bool send_full_msg(int fd, const std::vector<char>& buffer)
{
return send_all_bytes(fd, buffer.data(), buffer.size());
}
static void handle_conn(int fd) {
std::vector<char> buffer;
for (;;) {
if (!recv_full_msg(fd, buffer))
break;
if (!send_full_msg(fd, buffer))
break;
}
close(fd);
}
int main() {
int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
if (listen_fd < 0) {
perror("socket");
return 1;
}
const int yes = 1;
setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(LISTEN_PORT);
if (bind(listen_fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) < 0) {
perror("bind");
return 1;
}
if (listen(listen_fd, BACKLOG) < 0) {
perror("listen");
return 1;
}
printf("Kernel echo server listening on port %d\n", LISTEN_PORT);
printf("Minimum total message size: %zu bytes\n", sizeof(Msg));
for (;;) {
struct sockaddr_in cliaddr;
socklen_t clilen = sizeof(cliaddr);
int conn_fd = accept(listen_fd, reinterpret_cast<sockaddr*>(&cliaddr),
&clilen);
if (conn_fd < 0) {
if (errno == EINTR)
continue;
perror("accept");
break;
}
handle_conn(conn_fd);
}
close(listen_fd);
return 0;
}