struct pollfd allfds[IPC_CONNECTION_MAX*2+1];
nextfd = 0;
for ( .. )
{
allfds[nextfd].fd = XXX[i].sock;
allfds[nextfd].events = POLLIN | POLLRDHUP;
if (ip->output_end - ip->output_start > 0)
{
allfds[nextfd].events = allfds[nextfd].events | POLLOUT;
}
nextfd++;
}
res = poll(allfds, nextfd, 20);
if (res > 0)
{
//printf("Found %d events\n", res);
if (allfds[0].revents & (POLLERR | POLLNVAL))
{
printf("listen error()\n");
}
if (allfds[0].revents & POLLIN)
{
//printf("call ipc_accept()\n");
ipc_accept();
}
for (int i = 1; i < nextfd; i++)
{
if (allfds[i].revents & POLLOUT)
{
// handle write
//printf("call ipc_write[%d]\n", i);
ipc_write(conn[i]);
}
if (allfds[i].revents & POLLIN)
{
// handle read
//printf("call ipc_read[%d]\n", i);
ipc_read(conn[i]);
}
if ((conn[i]->state == ipc_conn_state_conndrop)
|| (allfds[i].revents & (POLLRDHUP | POLLERR | POLLHUP)))
{
if (conn[i]->retries > 0 && conn[i]->ipc_send_buf_used)
{
// retry a few times?
if (debug) fprintf(stderr,"ipc_service_process_active: retry socket (sock=%d) on new socket\n", conn[i]->sock);
conn[i]->retries--;
last_response.causeCode = 0;
conn[i]->state = ipc_conn_state_start;
continue;
}
// handle problem by closing it and tossing it
if (debug) fprintf(stderr,"closing connection (sock=%d) revent=%x\n", conn[i]->sock, allfds[i].revents);
ipc_close_conn(conn[i]);
// set a result
result = IPC_MsgType_Connection_Error;
last_response.causeCode = IPC_CauseCode_CPU_Busy;
last_response.remote_dest = conn[i]->remote_dest;
continue;
}
}
}
else if (res < 0)
{
if ( errno == EINTR || errno == EAGAIN )
return 0; // Ignore these errors
// barf
}