3 /* rl_transport - RL specific functions for transport */
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
12 #include "gras/Transport/transport_private.h"
13 XBT_LOG_EXTERNAL_CATEGORY(transport);
14 XBT_LOG_DEFAULT_CATEGORY(transport);
19 * Returns the next socket to service because it receives a message.
21 * if timeout<0, we ought to implement the adaptative timeout (FIXME)
23 * if timeout=0, do not wait for new message, only handle the ones already there.
25 * if timeout>0 and no message there, wait at most that amount of time before giving up.
27 gras_socket_t gras_trp_select(double timeout) {
28 xbt_dynar_t sockets= ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->sockets;
30 double wakeup = gras_os_time() + timeout;
32 /* nextToService used to make sure socket with high number do not starve */
33 /* static int nextToService = 0; */
34 struct timeval tout, *p_tout;
36 int max_fds=0; /* first arg of select: number of existing sockets */
37 /* but accept() of winsock returns sockets bigger than the limit, so don't bother
38 with this tiny optimisation on BillWare */
40 int ready; /* return of select: number of socket ready to be serviced */
41 int fd_setsize; /* FD_SETSIZE not always defined. Get this portably */
43 gras_socket_t sock_iter; /* iterating over all sockets */
44 int cursor; /* iterating over all sockets */
47 /* Compute FD_SETSIZE */
49 fd_setsize = sysconf( _SC_OPEN_MAX );
51 # ifdef HAVE_GETDTABLESIZE
52 fd_setsize = getdtablesize();
54 fd_setsize = FD_SETSIZE;
55 # endif /* !USE_SYSCONF */
59 if (timeout > 0) { /* did we timeout already? */
61 DEBUG2("wakeup=%f now=%f",wakeup, now);
62 if (now == -1 || now >= wakeup) {
63 /* didn't find anything */
64 THROW1(timeout_error,0,
65 "Timeout (%f) elapsed with selecting for incomming connexions",
70 /* construct the set of socket to ear from */
73 xbt_dynar_foreach(sockets,cursor,sock_iter) {
74 if (sock_iter->incoming) {
75 DEBUG1("Considering socket %d for select",sock_iter->sd);
76 #ifndef HAVE_WINSOCK_H
77 if (max_fds < sock_iter->sd)
78 max_fds = sock_iter->sd;
80 FD_SET(sock_iter->sd, &FDS);
82 DEBUG1("Not considering socket %d for select",sock_iter->sd);
88 DEBUG1("No socket to select onto. Sleep %f sec instead.",timeout);
89 gras_os_sleep(timeout);
90 THROW1(timeout_error,0,"No socket to select onto. Sleep %f sec instead",timeout);
92 DEBUG0("No socket to select onto. Return directly.");
93 THROW0(timeout_error,0, "No socket to select onto. Return directly.");
97 #ifndef HAVE_WINSOCK_H
98 /* we cannot have more than FD_SETSIZE sockets
99 ... but with WINSOCK which returns sockets higher than the limit (killing this optim) */
100 if (++max_fds > fd_setsize && fd_setsize > 0) {
101 WARN1("too many open sockets (%d).",max_fds);
106 max_fds = fd_setsize;
110 /* set the timeout */
111 tout.tv_sec = (unsigned long)(wakeup - now);
112 tout.tv_usec = ((wakeup -now) - ((unsigned long)(wakeup - now))) * 1000000;
114 } else if (timeout == 0) {
119 /* we just do one loop around */
122 /* no timeout: good luck! */
126 DEBUG2("Selecting over %d socket(s); timeout=%f", max_fds-1,timeout);
127 ready = select(max_fds, &FDS, NULL, NULL, p_tout);
128 DEBUG1("select returned %d", ready);
131 case EINTR: /* a signal we don't care about occured. we don't care */
132 /* if we cared, we would have set an handler */
134 case EINVAL: /* invalid value */
135 THROW3(system_error,EINVAL,"invalid select: nb fds: %d, timeout: %d.%d",
136 max_fds, (int)tout.tv_sec,(int) tout.tv_usec);
138 xbt_assert0(0,"Malloc error during the select");
140 THROW2(system_error,errno,"Error during select: %s (%d)",
141 strerror(errno),errno);
144 } else if (ready == 0) {
145 continue; /* this was a timeout */
148 xbt_dynar_foreach(sockets,cursor,sock_iter) {
149 if(!FD_ISSET(sock_iter->sd, &FDS)) { /* this socket is not ready */
153 /* Got a socket to serve */
156 if ( sock_iter->accepting
157 && sock_iter->plugin->socket_accept) {
158 /* not a socket but an ear. accept on it and serve next socket */
159 gras_socket_t accepted=NULL;
161 accepted = (sock_iter->plugin->socket_accept)(sock_iter);
162 DEBUG2("accepted=%p,&accepted=%p",accepted,&accepted);
163 accepted->meas = sock_iter->meas;
166 FIXME: this fails of files. quite logical
167 /* Make sure the socket is still alive by reading the first byte */
171 recvd = recv(sock_iter->sd, &lookahead, 1, MSG_PEEK);
173 WARN2("socket %d failed: %s", sock_iter->sd, strerror(errno));
174 /* done with this socket */
175 gras_socket_close(&sock_iter);
177 } else if (recvd == 0) {
178 /* Connection reset (=closed) by peer. */
179 DEBUG1("Connection %d reset by peer", sock_iter->sd);
180 gras_socket_close(&sock_iter);
184 /* Got a suited socket ! */
193 /* if we're here, the socket we found wasn't really ready to be served */
194 if (ready == 0) /* exausted all sockets given by select. Request new ones */
204 void gras_trp_sg_setup(gras_trp_plugin_t plug) {
205 THROW0(mismatch_error,0,"No SG transport on live platforms");