GError* gridd_client_step(struct client_s *client) { struct pollfd pfd = {-1, 0, 0}; if (!_client_to_pollfd(client, &pfd)) { return NULL; } int rc = metautils_syscall_poll(&pfd, 1, 1000); if (rc == 0) { GTimeVal now; g_get_current_time(&now); gridd_client_expire(client, &now); return NULL; } if (rc < 0) return NEWERROR(errno, "poll errno=%d %s", errno, strerror(errno)); if (pfd.revents & POLLERR) { GError *err = socket_get_error(pfd.fd); g_prefix_error(&err, "%s: ", gridd_client_url(client)); gridd_client_fail(client, err); g_clear_error(&err); } else gridd_client_react(client); return NULL; }
GError* gridd_client_step(struct gridd_client_s *client) { int rc; struct pollfd pfd = {-1, 0, 0}; if (!_client_to_pollfd(client, &pfd)) return NULL; retry: rc = metautils_syscall_poll(&pfd, 1, 1000); if (rc == 0) { gridd_client_expire(client, oio_ext_monotonic_time ()); return NULL; } if (rc < 0) { if (errno == EINTR) goto retry; return NEWERROR(errno, "poll errno=%d %s", errno, strerror(errno)); } if (pfd.revents & POLLERR) { GError *err = socket_get_error(pfd.fd); g_prefix_error(&err, "%s: ", gridd_client_url(client)); gridd_client_fail(client, err); g_clear_error(&err); } else if (pfd.revents & (POLLIN|POLLOUT)) { gridd_client_react(client); } return NULL; }
GError * gridd_clients_step(struct client_s **clients) { struct client_s ** _lookup_client(int fd, struct client_s **ppc) { struct client_s *c; for (; (c = *ppc) ;ppc++) { if (gridd_client_fd(c) == fd) return ppc; } return ppc; } guint i, j; int rc; struct client_s *last, **plast; GTimeVal now; guint nbclients; EXTRA_ASSERT(clients != NULL); nbclients = g_strv_length((gchar**)clients); EXTRA_ASSERT(nbclients > 0); struct pollfd pfd[nbclients]; for (j=0,plast=clients; NULL != (last = *plast) ;plast++) { if (_client_to_pollfd(last, pfd+j)) j++; } if (!j) return NULL; /* Wait for an event to happen */ if (!(rc = poll(pfd, j, 100))) { g_get_current_time(&now); _clients_expire(clients, &now); return NULL; } if (rc < 0) return NEWERROR(errno, "poll error (%s)", strerror(errno)); /* Then manage each event */ for (plast=clients,i=0; i<j ;i++) { if (!pfd[i].revents) continue; /* Find the client for this pollfd */ plast = _lookup_client(pfd[i].fd, plast); EXTRA_ASSERT(plast != NULL); last = *plast; /* Manage the poll() event */ if (pfd[i].revents & POLLERR) { GError *err = socket_get_error(pfd[i].fd); g_prefix_error(&err, "%s: ", gridd_client_url(last)); gridd_client_fail(last, err); g_clear_error(&err); } else gridd_client_react(last); } /* Now check for expired clients */ g_get_current_time(&now); _clients_expire(clients, &now); return NULL; }