3 /* rl_transport - RL specific functions for transport */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2004 Martin Quinson. */
8 /* This program is free software; you can redistribute it and/or modify it
9 under the terms of the license (GNU LGPL) which comes with this package. */
12 #include <sys/types.h>
13 #include <sys/socket.h>
15 #include "Transport/transport_private.h"
16 GRAS_LOG_EXTERNAL_CATEGORY(transport);
17 GRAS_LOG_DEFAULT_CATEGORY(transport);
24 * Returns the next socket to service having a message awaiting.
26 * if timeout<0, we ought to implement the adaptative timeout (FIXME)
28 * if timeout=0, do not wait for new message, only handle the ones already there.
30 * if timeout>0 and no message there, wait at most that amount of time before giving up.
33 gras_trp_select(double timeout,
34 gras_socket_t **dst) {
37 gras_dynar_t *sockets= gras_socketset_get();
39 double wakeup = gras_time() + 1000000*timeout;
41 /* nextToService used to make sure socket with high number do not starve */
42 // static int nextToService = 0;
43 struct timeval tout, *p_tout;
45 int max_fds=0; /* first arg of select: number of existing sockets */
47 int ready; /* return of select: number of socket ready to be serviced */
49 gras_socket_t *sock_iter; /* iterating over all sockets */
50 int cursor; /* iterating over all sockets */
54 if (timeout > 0) { /* did we timeout already? */
56 if (now == -1 || now >= wakeup) {
57 done = 0; /* didn't find anything */
62 /* construct the set of socket to ear from */
64 gras_dynar_foreach(sockets,cursor,sock_iter) {
65 if (sock_iter->incoming) {
66 if (max_fds < sock_iter->sd)
67 max_fds = sock_iter->sd;
68 FD_SET(sock_iter->sd, &FDS);
70 DEBUG1("Not considering socket %d for select",sock_iter->sd);
74 /* we cannot have more than FD_SETSIZE sockets */
75 if (++max_fds > FD_SETSIZE) {
76 WARN0("too many open sockets.");
83 tout.tv_sec = (unsigned long)((wakeup - now)/1000000);
84 tout.tv_usec = (unsigned long)(wakeup - now) % 1000000;
86 } else if (timeout == 0) {
91 /* we just do one loop around */
94 /* no timeout: good luck! */
98 DEBUG1("Selecting over %d socket(s)", max_fds-1);
99 ready = select(max_fds, &FDS, NULL, NULL, p_tout);
102 case EINTR: /* a signal we don't care about occured. we don't care */
103 /* if we cared, we would have set an handler */
105 case EINVAL: /* invalid value */
106 RAISE3(system_error,"invalid select: nb fds: %d, timeout: %d.%d",
107 max_fds, (int)tout.tv_sec,(int) tout.tv_usec);
111 RAISE2(system_error,"Error during select: %s (%d)",
112 strerror(errno),errno);
115 } else if (ready == 0) {
116 continue; /* this was a timeout */
119 gras_dynar_foreach(sockets,cursor,sock_iter) {
120 if(!FD_ISSET(sock_iter->sd, &FDS)) { /* this socket is not ready */
124 /* Got a socket to serve */
127 if ( sock_iter->accepting
128 && sock_iter->plugin->socket_accept) {
129 /* not a socket but an ear. accept on it and serve next socket */
130 gras_socket_t *accepted;
132 TRY(sock_iter->plugin->socket_accept(sock_iter,&accepted));
133 accepted->raw = sock_iter->raw;
136 FIXME: this fails of files. quite logical
137 /* Make sure the socket is still alive by reading the first byte */
141 recvd = recv(sock_iter->sd, &lookahead, 1, MSG_PEEK);
143 WARN2("socket %d failed: %s", sock_iter->sd, strerror(errno));
144 /* done with this socket */
145 gras_socket_close(&sock_iter);
147 } else if (recvd == 0) {
148 /* Connection reset (=closed) by peer. */
149 DEBUG1("Connection %d reset by peer", sock_iter->sd);
150 gras_socket_close(&sock_iter);
154 /* Got a suited socket ! */
163 /* if we're here, the socket we found wasn't really ready to be served */
164 if (ready == 0) /* exausted all sockets given by select. Request new ones */
170 return timeout_error;
173 gras_error_t gras_trp_sg_setup(gras_trp_plugin_t *plug) {
174 return mismatch_error;