From c683087e78d8338a3a118178200a3137b666331e Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 6 May 2020 00:05:16 +0200 Subject: Allowed to store client data between poll calls --- plop.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/plop.c b/plop.c index 601e172..8d21362 100644 --- a/plop.c +++ b/plop.c @@ -74,10 +74,11 @@ int make_server(const char * node, const char * service) /// Progresses the connection for a client and shifts it back in the array. /// `pfd` must be at least `shift_by + 1`-th element of the array. /// \param pfd Element in array processed by poll +/// \param data Pointer to data associated with the client /// \param shift_by Element will be moved by in array this many indices /// \return Shift value for next handler /// \see poll(2) -int handle_client(struct pollfd * pfd, const int shift_by) +int handle_client(struct pollfd * pfd, char ** data, const int shift_by) { if (0 == pfd->revents) { @@ -89,20 +90,19 @@ int handle_client(struct pollfd * pfd, const int shift_by) return -1; // TODO: Handle errors properly } - char * request = NULL; - - if (-1 == collect_request(pfd->fd, &request)) + if (-1 == collect_request(pfd->fd, data)) { return -1; // TODO: Handle errors properly } - respond_with_body(pfd->fd, STATUS_OK, request); + respond_with_body(pfd->fd, STATUS_OK, *data); close(pfd->fd); (pfd - shift_by)->fd = -1; (pfd - shift_by)->events = pfd->events; - free(request); + free(*data); + *data = NULL; return shift_by + 1; } @@ -146,6 +146,7 @@ int main(int argc, char ** argv) return 4; } + char * data[200] = {0}; struct pollfd fdv[200]; memset(fdv, 0, sizeof(fdv)); @@ -159,7 +160,7 @@ int main(int argc, char ** argv) for (int i = 1; i < fdc; ++i) { - shift_by = handle_client(&fdv[i], shift_by); + shift_by = handle_client(&fdv[i], &data[i], shift_by); } fdc = handle_server(fdv, fdc - shift_by, 200); -- cgit v1.1