-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
191 lines (137 loc) · 4.69 KB
/
server.cpp
File metadata and controls
191 lines (137 loc) · 4.69 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
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
183
184
185
186
187
188
189
190
191
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include "write_and_read.h"
#include <vector>
#include <poll.h>
#include "conn.h"
#include "buff.h"
const size_t K_MAX_MSG = 4096;
void handle_write(Conn* conn);
Conn* handle_accept(int fd) {
int connfd = accept(fd, nullptr, nullptr);
if (connfd < 0) {
return NULL;
}
// set it to be non blocking
fd_set_nb(connfd);
Conn* conn = new Conn();
conn->fd = connfd;
conn->want_read = true;
return conn;
}
void handle_read(Conn* conn) {
// do a non-blocking read
uint8_t buf[4096];
ssize_t rv = read(conn->fd, buf, sizeof(buf));
if (rv <= 0) { // handle IO error (rv < 0) or EOF (rv == 0)
conn->want_close = true;
return;
}
//append this onto the incoming buff
buf_append(&conn->incoming, buf, (size_t) rv);
// 3. Try to parse the accumulated buffer.
// 4. Process the parsed message.
// 5. Remove the message from `Conn::incoming`
while(try_one_request(conn)){
}
if (conn->outgoing.size() > 0) {
conn->want_read = false;
conn->want_write = true;
handle_write(conn);
}
return;
}
void handle_write(Conn* conn) {
assert(conn->outgoing.size() > 0);
ssize_t rv = send(conn->fd, conn->outgoing.data(), conn->outgoing.size(), 0);
if (rv <= 0) {
conn->want_close = true;
return;
}
// with the want write optimization in the handle_read we need to be sure about the write
if (rv < 0 && errno == EAGAIN) {
return;
}
buf_consume(&conn->outgoing, (size_t) rv);
if (conn->outgoing.size() == 0) {
conn->want_write = false;
conn->want_read = true;
}
return;
}
int main() {
int fd = socket(AF_INET, SOCK_STREAM, 0);
int val = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = htons(1234);
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
int rv = bind(fd, (const struct sockaddr *)&addr, sizeof(addr));
if (rv) { die("bind()"); }
rv = listen(fd, SOMAXCONN);
while (true) {
// main event loop will go here
std::vector<Conn *> fd2conn; // vector filled with connections + states
std::vector<struct pollfd> poll_args; // should eventually transition to epoll
while (true){
poll_args.clear();
struct pollfd pfd = {fd, POLLIN, 0};
poll_args.push_back(pfd);
// prepare tha polls
for (Conn* conn : fd2conn) {
if (!conn) { // checking nullness
continue;
}
struct pollfd pfd = {conn->fd, POLLERR, 0};
if (conn->want_read) {
pfd.events |= POLLIN;
}
if (conn->want_write) {
pfd.events |= POLLOUT;
}
poll_args.push_back(pfd);
}
int rv = poll(poll_args.data(), (nfds_t)poll_args.size(), -1); // kinda not great - could in theory block forever
if (rv < 0 && errno == EINTR) {
continue;
}
if (rv < 0) {
die("poll");
}
if (poll_args[0].revents) {
if (Conn* connfd = handle_accept(fd)) {
if (connfd->fd > fd2conn.size()){
fd2conn.resize(connfd->fd + 1);
}
fd2conn[connfd->fd] = connfd;
}
}
for (size_t i = 1; i < poll_args.size(); ++i) { // skip the first one
int32_t ready = poll_args[i].revents; //did something happen?
Conn* conn = fd2conn[poll_args[i].fd];
//bitwise masks
if (POLLIN & ready) {
handle_read(conn);
}
if (POLLOUT & ready) {
handle_write(conn);
}
if ((ready & POLLERR) || conn->want_close) {
(void)close(conn->fd);
fd2conn[conn->fd] = NULL;
delete conn;
}
}
}
}
return 0;
}