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);
21 gras_socket_server(unsigned short port,
23 /* OUT */ gras_socket_t **dst) {
26 gras_trp_plugin_t *tcp;
28 TRY(gras_trp_plugin_get_by_name("TCP",&tcp));
29 TRY( tcp->socket_server(tcp, port, bufSize, dst));
32 (*dst)->accepting = 1;
34 TRY(gras_dynar_push(_gras_trp_sockets,dst));
40 gras_socket_client(const char *host,
43 /* OUT */ gras_socket_t **dst) {
46 gras_trp_plugin_t *tcp;
48 TRY(gras_trp_plugin_get_by_name("TCP",&tcp));
49 TRY( (*tcp->socket_client)(tcp, host, port, bufSize, dst));
52 (*dst)->accepting = 0;
61 * Returns the next socket to service having a message awaiting.
63 * if timeout<0, we ought to implement the adaptative timeout (FIXME)
65 * if timeout=0, do not wait for new message, only handle the ones already there.
67 * if timeout>0 and no message there, wait at most that amount of time before giving up.
70 gras_trp_select(double timeout,
71 gras_socket_t **dst) {
75 double wakeup = gras_time() + 1000000*timeout;
77 /* nextToService used to make sure socket with high number do not starve */
78 // static int nextToService = 0;
79 struct timeval tout, *p_tout;
81 int max_fds=0; /* first arg of select: number of existing sockets */
83 int ready; /* return of select: number of socket ready to be serviced */
85 gras_socket_t *sock_iter; /* iterating over all sockets */
86 int cursor; /* iterating over all sockets */
90 if (timeout > 0) { /* did we timeout already? */
92 if (now == -1 || now >= wakeup) {
93 done = 0; /* didn't find anything */
98 /* construct the set of socket to ear from */
100 gras_dynar_foreach(_gras_trp_sockets,cursor,sock_iter) {
101 if (max_fds < sock_iter->sd)
102 max_fds = sock_iter->sd;
103 FD_SET(sock_iter->sd, &FDS);
106 /* we cannot have more than FD_SETSIZE sockets */
107 if (++max_fds > FD_SETSIZE) {
108 WARNING0("too many open sockets.");
114 /* set the timeout */
115 tout.tv_sec = (unsigned long)((wakeup - now)/1000000);
116 tout.tv_usec = (unsigned long)(wakeup - now) % 1000000;
118 } else if (timeout == 0) {
123 /* we just do one loop around */
126 /* no timeout: good luck! */
130 ready = select(max_fds, &FDS, NULL, NULL, p_tout);
133 case EINTR: /* a signal we don't care about occured. we don't care */
134 /* if we cared, we would have set an handler */
136 case EINVAL: /* invalid value */
137 RAISE3(system_error,"invalid select: nb fds: %d, timeout: %d.%d",
138 max_fds, (int)tout.tv_sec,(int) tout.tv_usec);
142 RAISE2(system_error,"Error during select: %s (%d)",
143 strerror(errno),errno);
146 } else if (ready == 0) {
147 continue; /* this was a timeout */
150 gras_dynar_foreach(_gras_trp_sockets,cursor,sock_iter) {
151 if(!FD_ISSET(sock_iter->sd, &FDS)) { /* this socket is not ready */
155 /* Got a socket to serve */
158 if ( sock_iter->accepting
159 && sock_iter->plugin->socket_accept) {
160 /* not a socket but an ear. accept on it and serve next socket */
161 gras_socket_t *accepted;
163 TRY(sock_iter->plugin->socket_accept(sock_iter,&accepted));
164 TRY(gras_dynar_push(_gras_trp_sockets,&accepted));
166 /* Make sure the socket is still alive by reading the first byte */
170 recvd = recv(sock_iter->sd, &lookahead, 1, MSG_PEEK);
172 WARNING2("socket %d failed: %s", sock_iter->sd, strerror(errno));
173 /* done with this socket */
174 gras_socket_close(sock_iter);
176 } else if (recvd == 0) {
177 /* Connection reset (=closed) by peer. */
178 DEBUG1("Connection %d reset by peer", sock_iter->sd);
179 gras_socket_close(sock_iter);
182 /* Got a suited socket ! */
189 /* if we're here, the socket we found wasn't really ready to be served */
190 if (ready == 0) /* exausted all sockets given by select. Request new ones */
196 return timeout_error;