Skip to content

Commit 3e934e2

Browse files
committed
fix: don't assume you have a full request with each recv
- Peek at the data to see if you got full request - Don't pop the data unless you do
1 parent b21681d commit 3e934e2

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

source/prime-time/main.c

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#define _POSIX_C_SOURCE 200112L
2+
#define _GNU_SOURCE
23

34
#include <assert.h>
45
#include <errno.h>
@@ -88,7 +89,7 @@ int main()
8889
exit(EXIT_FAILURE);
8990
}
9091

91-
char *data, *sddata;
92+
char *data, *sddata, *complete_req;
9293
int n, fd, res, size, sdsize, rs, result;
9394
struct epoll_ctl_info epci = {epollfd, 0, 0};
9495
struct queue* rcqu = NULL, *sdqu = NULL;
@@ -116,9 +117,8 @@ int main()
116117
}
117118

118119
// Receive all the data into the queue
119-
log_trace("main epoll loop: handling POLLIN event on fd '%d'", fd);
120120
res = recv_request(fd, rcqu);
121-
size = queue_pop_no_copy(rcqu, &data);
121+
log_trace("main epoll loop: handling POLLIN event on fd '%d' with res: '%d'", fd, res);
122122

123123
// Handle error case while recv data
124124
if (res < -1) {
@@ -131,9 +131,18 @@ int main()
131131
continue;
132132
}
133133

134-
// Handle there's data to process
134+
// Peek at the data to check if we have at least one complete request
135+
complete_req = NULL;
136+
size = queue_peek(rcqu, &data);
135137
if (size > 0) {
136-
log_trace("main epoll loop: raw request(%d): '%s'", fd, data);
138+
complete_req = (char *) memrchr(data, PRIME_REQUEST_DELIMITERS[0], size);
139+
log_trace("main epoll loop: complete_req = '%d'", (complete_req == NULL ? 0 : 1));
140+
}
141+
142+
// If we do, process it
143+
if (complete_req != NULL) {
144+
size = queue_pop_no_copy(rcqu, &data);
145+
log_trace("main epoll loop: raw request: fd: '%d', size: '%d', data: '%s'", fd, size, data);
137146
result = handle_request(sdqu, data, (size_t)size);
138147
sdsize = queue_pop_no_copy(sdqu, &sddata);
139148
rs = sendall(fd, sddata, &sdsize);

source/utils/queue.c

+14
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,17 @@ int queue_pop_no_copy(struct queue* qu, char** data)
209209

210210
return s;
211211
}
212+
213+
int queue_peek(struct queue* qu, char** data)
214+
{
215+
assert(qu != NULL);
216+
assert(data != NULL);
217+
218+
if (qu->size == 0) {
219+
log_warn("queue_pop: queue is empty... noop");
220+
return 0;
221+
}
222+
223+
*data = qu->data;
224+
return (int)qu->size;
225+
}

source/utils/queue.h

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ void queue_pop(struct queue* qu, char* data, size_t* size);
2020
void queue_free(struct queue** qu);
2121
void queue_reset(struct queue* qu);
2222
int queue_pop_no_copy(struct queue* qu, char** data);
23+
int queue_peek(struct queue* qu, char** data);
2324

2425
#ifdef __cplusplus
2526
}

0 commit comments

Comments
 (0)