diff options
-rw-r--r-- | plop.c | 38 |
1 files changed, 27 insertions, 11 deletions
@@ -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); } } |