summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2020-05-06 00:05:16 +0200
committerAki <please@ignore.pl>2020-05-06 00:05:16 +0200
commitc683087e78d8338a3a118178200a3137b666331e (patch)
tree91e233e514c1e39bfab44a0ef1ed0c3eff9ea5ea
parent048525e062f0dffcb62b825f7452ea27bac2b4d1 (diff)
downloadplop-c683087e78d8338a3a118178200a3137b666331e.zip
plop-c683087e78d8338a3a118178200a3137b666331e.tar.gz
plop-c683087e78d8338a3a118178200a3137b666331e.tar.bz2
Allowed to store client data between poll calls
-rw-r--r--plop.c15
1 files 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);