From 03360ae6aa07289eac060870972d4ba246ca88d3 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 1 May 2020 15:02:14 +0200 Subject: Moved out server handling to a separate function --- plop.c | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) (limited to 'plop.c') diff --git a/plop.c b/plop.c index 931618e..bc30138 100644 --- a/plop.c +++ b/plop.c @@ -101,6 +101,31 @@ int handle_client(struct pollfd * pfd, const int shift_by) return shift_by + 1; } +/// Accepts awaiting connections if any. +/// \param fdv Array of descriptors for poll +/// \param fdc Number of valid descriptors in the array including the server +/// \param size Size limit for the array +/// \param Number of valid descriptors in the array or -1 if an error occurred. +/// \see poll(2) +int handle_server(struct pollfd * fdv, int fdc, const int size) +{ + if (0 == fdv[0].revents) + return fdc; + + while (size > fdc + 1 && -1 != (fdv[fdc].fd = accept(fdv[0].fd, NULL, NULL))) + { + fdv[fdc].events = POLLIN | POLLOUT; + fdc++; + } + + if (errno != EWOULDBLOCK && errno != EAGAIN) + { + return -1; + } + + return fdc; +} + /// Standard entry point for the program. /// \param argc Argument count /// \param argv Argument array @@ -119,7 +144,7 @@ int main(int argc, char ** argv) fdv[0].events = POLLIN; int fdc = 1; - while (-1 != poll(fdv, fdc, -1)) + while (-1 != fdc && -1 != poll(fdv, fdc, -1)) { int shift_by = 0; @@ -128,15 +153,6 @@ int main(int argc, char ** argv) shift_by = handle_client(&fdv[i], shift_by); } - fdc = fdc - shift_by; - - if (POLLIN == fdv[0].revents) - { - if (-1 != (fdv[fdc].fd = accept(fdv[0].fd, NULL, NULL))) - { - fdv[fdc].events = POLLIN | POLLOUT; - fdc++; - } - } + fdc = handle_server(fdv, fdc - shift_by, 200); } } -- cgit v1.1