summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2020-05-01 15:02:14 +0200
committerAki <please@ignore.pl>2020-05-01 15:02:14 +0200
commit03360ae6aa07289eac060870972d4ba246ca88d3 (patch)
tree4ed1c73df40238ce8f5ac67a59f2dcf0473e9f74
parent8acc060eef3799b0f3f8c7601c67870f553795c7 (diff)
downloadplop-03360ae6aa07289eac060870972d4ba246ca88d3.zip
plop-03360ae6aa07289eac060870972d4ba246ca88d3.tar.gz
plop-03360ae6aa07289eac060870972d4ba246ca88d3.tar.bz2
Moved out server handling to a separate function
-rw-r--r--plop.c38
1 files changed, 27 insertions, 11 deletions
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);
}
}