-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_and_write.cpp
More file actions
244 lines (175 loc) · 5.7 KB
/
Copy pathread_and_write.cpp
File metadata and controls
244 lines (175 loc) · 5.7 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#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 <errno.h>
#include <assert.h>
#include <fcntl.h> // for fcntl(), F_SETFL, F_GETFL, O_NONBLOCK
#include "conn.h"
#include <cstdint> // for uint8_t
#include "buff.h"
#include <vector>
#include <string>
#include "response.h"
#include <map>
#include "data.h"
const size_t K_MAX_MSG = 4096;
// placeholder; implemented later
static bool read_u32(const uint8_t *&cur, const uint8_t *end, uint32_t &out) {
if (cur + 4 > end) return false;
memcpy(&out, cur, 4);
cur += 4;
return true;
}
static void out_err(Buffer &out, uint8_t err, std::string msg) {
const uint32_t size = 1 + msg.size();
buf_append(&out, (uint8_t *)&size, 4);
buf_append(&out, &err, 1);
buf_append(&out, (uint8_t *) msg.data(), msg.size());
}
// reserve 4 bytes for response header
static void response_begin(Buffer &out, size_t *header) {
uint32_t z = 0;
*header = out.size(); // so we know where to put the header lmfao
buf_append(&out, (uint8_t * ) &z, 4);
}
static size_t response_len(Buffer &out, size_t header) {
return out.size() - header - 4;
}
static void response_end(Buffer &out, size_t header) {
size_t s = response_len(out, header);
if (s > K_MAX_MSG) {
out.data_end = out.data_begin + header + 4; // some room for the header would be nice
out_err(out, 1, "msg too big");
s = response_len(out, header);
}
// FINISH DIS TMRW
uint32_t len = (uint32_t) s;
memcpy(out.data_begin + header , &len, 4);
}
static bool read_str(const uint8_t *&cur, const uint8_t *end, size_t n, std::string &out) {
if (cur + n > end) return false;
out.assign(reinterpret_cast<const char*>(cur), n);
cur += n;
return true;
}
int32_t parse_req(const uint8_t *data, size_t size, std::vector<std::string> &cmd) {
const uint8_t *cur = data;
const uint8_t *end = data + size;
uint32_t nstr = 0;
if (!read_u32(cur, end, nstr)) return -1;
cmd.clear();
cmd.reserve(nstr);
while (cmd.size() < nstr) {
uint32_t len = 0;
if (!read_u32(cur, end, len)) return -1;
cmd.emplace_back();
if (!read_str(cur, end, len, cmd.back())) return -1;
}
if (cur != end) return -1; // trailing garbage
return 0;
}
void do_request(std::vector<std::string> &cmd, Response& out, Buffer& outgoing) {
if (cmd.size() == 2 && cmd[0] == "get") {
do_get(cmd, outgoing);
} else if (cmd.size() == 3 && cmd[0] == "set") {
do_set(cmd, outgoing);
} else if (cmd.size() == 2 && cmd[0] == "del") {
do_delete(cmd, outgoing);
} else {
out.status = 2;
uint32_t &status = out.status;
uint32_t resp_len = 4;
buf_append(&outgoing, reinterpret_cast<uint8_t*>(&resp_len), 4);
buf_append(&outgoing, reinterpret_cast<uint8_t*>(&status), 4); // unrecognized command
}
}
void make_response(Response& resp, Buffer& out) {
// need to serialize
uint32_t resp_len = 4 + (uint32_t) resp.data.size();
buf_append(&out, (const uint8_t *)&resp_len, 4);
buf_append(&out, (const uint8_t *)&resp.status, 4);
buf_append(&out, resp.data.data(), resp.data.size());
}
int32_t read_full(int connfd, char *rbuf, int length) {
while (length > 0) {
ssize_t rv = recv(connfd, rbuf, length , 0);
if (rv <= 0) {
return -1; // error or EOF
}
assert((size_t) rv <= length);
length -= (size_t) rv;
rbuf += (size_t) rv;
}
return 0;
}
int32_t write_all(int connfd, char *rbuf, int length) {
// write everything from buff into the connfd byte stream
while (length > 0 ) {
ssize_t rv = send(connfd, rbuf, length, 0);
if (rv <= 0) {
return -1; // error or EOF
}
assert((size_t) rv <= length);
length -= (size_t) rv;
rbuf += rv;
}
return 0;
}
void msg(const char* message) {
perror(message);
}
void die(const char* message) {
perror(message); exit(1);
}
void fd_set_nb(int fd) {
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
}
int buf_append(struct Buffer *buf, const uint8_t *data, size_t len) {
/**
* Tries to append to the buffer, returns -1 if the buffer is full
* @returns true if sucessful, false if non-successful
*/
if (buf->data_end + len > buf->buffer_end) {return -1;}
memcpy(buf->data_end, data, len);
buf->data_end += len;
return 0;
}
void buf_consume(struct Buffer *buf, size_t len) {
buf->data_begin += len;
if (buf->data_begin == buf->data_end) {
buf->data_begin = buf->buffer_begin;
}
return;
}
bool try_one_request(Conn *conn) {
/**
* Tries to parse the one request within conn incoming, and will parse the command as well as trying to create the response data.
* @returns true if sucessful, false if non-successful
*/
if (conn->incoming.size() < 4){
return false;
}
uint32_t be_len = 0;
memcpy(&be_len, conn->incoming.data(), 4);
uint32_t length = ntohl(be_len);
if (length > K_MAX_MSG) {
conn->want_close = true; // fairly idiomatic what i'm doing here
return false;
}
if (conn->incoming.size() < 4 + length) return false;
std::vector<std::string> cmd;
if (parse_req(conn->incoming.data() + 4, length, cmd) < 0) {
conn->want_close = true;
return false;
}
Response resp; // we may need it later
do_request(cmd, resp, conn->outgoing);
// 5. Remove the message from `Conn::incoming`.
buf_consume(&conn->incoming, 4 + length);
return true;
}