Updates to udp source/sink (select(), wait, cleanup)
[debian/gnuradio] / gnuradio-core / src / lib / io / gr_udp_source.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008,2009,2010 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include <gr_udp_source.h>
27 #include <gr_io_signature.h>
28 #include <stdexcept>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <string.h>
32 #if defined(HAVE_NETDB_H)
33 #include <netdb.h>
34 typedef void* optval_t;
35 #elif defined(HAVE_WINDOWS_H)
36 // if not posix, assume winsock
37 #define USING_WINSOCK
38 #include <winsock2.h>
39 #include <ws2tcpip.h>
40 #define SHUT_RDWR 2
41 typedef char* optval_t;
42 #endif
43
44 #define USE_SELECT    1  // non-blocking receive on all platforms
45 #define USE_RCV_TIMEO 0  // non-blocking receive on all but Cygwin
46 #define SRC_VERBOSE 0
47
48 static int is_error( int perr )
49 {
50   // Compare error to posix error code; return nonzero if match.
51 #if defined(USING_WINSOCK)
52 #define ENOPROTOOPT 109
53   // All codes to be checked for must be defined below
54   int werr = WSAGetLastError();
55   switch( werr ) {
56   case WSAETIMEDOUT:
57     return( perr == EAGAIN );
58   case WSAENOPROTOOPT:
59     return( perr == ENOPROTOOPT );
60   default:
61     fprintf(stderr,"gr_udp_source/is_error: unknown error %d\n", perr );
62     throw std::runtime_error("internal error");
63   }
64   return 0;
65 #else
66   return( perr == errno );
67 #endif
68 }
69
70 static void report_error( char *msg1, char *msg2 )
71 {
72   // Deal with errors, both posix and winsock
73 #if defined(USING_WINSOCK)
74   int werr = WSAGetLastError();
75   fprintf(stderr, "%s: winsock error %d\n", msg1, werr );
76 #else
77   perror(msg1);
78 #endif
79   if( msg2 != NULL )
80     throw std::runtime_error(msg2);
81   return;
82 }
83
84 gr_udp_source::gr_udp_source(size_t itemsize, const char *src, 
85                              unsigned short port_src, int payload_size,
86                              bool wait)
87   : gr_sync_block ("udp_source",
88                    gr_make_io_signature(0, 0, 0),
89                    gr_make_io_signature(1, 1, itemsize)),
90     d_itemsize(itemsize), d_payload_size(payload_size), d_wait(wait), d_residual(0), d_temp_offset(0)
91 {
92   int ret = 0;
93   struct addrinfo *ip_src;      // store the source IP address to use
94
95 #if defined(USING_WINSOCK) // for Windows (with MinGW)
96   // initialize winsock DLL
97   WSADATA wsaData;
98   int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
99   if( iResult != NO_ERROR ) {
100     report_error( "gr_udp_source WSAStartup", "can't open socket" );
101   }
102 #endif
103   
104   // Set up the address stucture for the source address and port numbers
105   // Get the source IP address from the host name
106   struct addrinfo hints;
107   memset( (void*)&hints, 0, sizeof(hints) );
108   hints.ai_family = AF_INET;
109   hints.ai_socktype = SOCK_DGRAM;
110   hints.ai_protocol = IPPROTO_UDP;
111   char port_str[7];
112   sprintf( port_str, "%d", port_src );
113   ret = getaddrinfo( src, port_str, &hints, &ip_src );
114   if( ret != 0 )
115     report_error("gr_udp_source/getaddrinfo",
116                  "can't initialize source socket" );
117
118   d_temp_buff = new char[d_payload_size];   // allow it to hold up to payload_size bytes
119
120   // create socket
121   d_socket = socket(ip_src->ai_family, ip_src->ai_socktype,
122                     ip_src->ai_protocol);
123   if(d_socket == -1) {
124     report_error("socket open","can't open socket");
125   }
126
127   // Turn on reuse address
128   int opt_val = 1;
129   if(setsockopt(d_socket, SOL_SOCKET, SO_REUSEADDR, (optval_t)&opt_val, sizeof(int)) == -1) {
130     report_error("SO_REUSEADDR","can't set socket option SO_REUSEADDR");
131   }
132
133   // Don't wait when shutting down
134   linger lngr;
135   lngr.l_onoff  = 1;
136   lngr.l_linger = 0;
137   if(setsockopt(d_socket, SOL_SOCKET, SO_LINGER, (optval_t)&lngr, sizeof(linger)) == -1) {
138     if( !is_error(ENOPROTOOPT) ) {  // no SO_LINGER for SOCK_DGRAM on Windows
139       report_error("SO_LINGER","can't set socket option SO_LINGER");
140     }
141   }
142
143 #if USE_RCV_TIMEO
144   // Set a timeout on the receive function to not block indefinitely
145   // This value can (and probably should) be changed
146   // Ignored on Cygwin
147 #if defined(USING_WINSOCK)
148   DWORD timeout = 1000;  // milliseconds
149 #else
150   timeval timeout;
151   timeout.tv_sec = 1;
152   timeout.tv_usec = 0;
153 #endif
154   if(setsockopt(d_socket, SOL_SOCKET, SO_RCVTIMEO, (optval_t)&timeout, sizeof(timeout)) == -1) {
155     report_error("SO_RCVTIMEO","can't set socket option SO_RCVTIMEO");
156   }
157 #endif // USE_RCV_TIMEO
158
159   // bind socket to an address and port number to listen on
160   if(bind (d_socket, ip_src->ai_addr, ip_src->ai_addrlen) == -1) {
161     report_error("socket bind","can't bind socket");
162   }
163   freeaddrinfo(ip_src);
164
165 }
166
167 gr_udp_source_sptr
168 gr_make_udp_source (size_t itemsize, const char *ipaddr, 
169                     unsigned short port, int payload_size, bool wait)
170 {
171   return gr_udp_source_sptr (new gr_udp_source (itemsize, ipaddr, 
172                                                 port, payload_size, wait));
173 }
174
175 gr_udp_source::~gr_udp_source ()
176 {
177   delete [] d_temp_buff;
178
179   if (d_socket){
180     shutdown(d_socket, SHUT_RDWR);
181 #if defined(USING_WINSOCK)
182     closesocket(d_socket);
183 #else
184     ::close(d_socket);
185 #endif
186     d_socket = 0;
187   }
188
189 #if defined(USING_WINSOCK) // for Windows (with MinGW)
190   // free winsock resources
191   WSACleanup();
192 #endif
193 }
194
195 int 
196 gr_udp_source::work (int noutput_items,
197                      gr_vector_const_void_star &input_items,
198                      gr_vector_void_star &output_items)
199 {
200   char *out = (char *) output_items[0];
201   ssize_t r=0, nbytes=0, bytes_received=0;
202   ssize_t total_bytes = (ssize_t)(d_itemsize*noutput_items);
203
204   #if SRC_VERBOSE
205   printf("\nEntered udp_source\n");
206   #endif
207
208   // Remove items from temp buffer if they are in there
209   if(d_residual) {
210     nbytes = std::min(d_residual, total_bytes);
211     memcpy(out, d_temp_buff+d_temp_offset, nbytes);
212     bytes_received = nbytes;
213
214     #if SRC_VERBOSE
215     printf("\tTemp buff size: %d  offset: %d (bytes_received: %d) (noutput_items: %d)\n", 
216            d_residual, d_temp_offset, bytes_received, noutput_items);
217     #endif
218
219     // Increment pointer
220     out += bytes_received;
221     
222     // Update indexing of amount of bytes left in the buffer
223     d_residual -= nbytes;
224     d_temp_offset = d_temp_offset+d_residual;
225   }
226
227 #if USE_SELECT
228   // Use select() to determine when socket is readable
229   fd_set readfds;
230   timeval timeout;
231   timeout.tv_sec = 1;
232   timeout.tv_usec = 0;
233 #endif
234   
235   while(1) {
236     // get the data into our output buffer and record the number of bytes
237
238 #if USE_SELECT
239     // RCV_TIMEO doesn't work on all systems (e.g., Cygwin)
240     // use select() instead of, or in addition to RCV_TIMEO
241     FD_ZERO(&readfds);
242     FD_SET(d_socket, &readfds);
243     r = select(FD_SETSIZE, &readfds, NULL, NULL, &timeout);
244     if(r < 0) {
245         report_error("udp_source/select",NULL);
246         return -1;
247     }
248     else if(r == 0 ) {  // timed out
249       if( d_wait ) {
250         // Allow boost thread interrupt, then try again
251         boost::this_thread::interruption_point();
252         continue;
253       }
254       else
255         return -1;
256     }
257 #endif // USE_SELECT
258
259     // This is a non-blocking call with a timeout set in the constructor
260     r = recv(d_socket, d_temp_buff, d_payload_size, 0);  // get the entire payload or the what's available
261
262     // Check if there was a problem; forget it if the operation just timed out
263     if(r == -1) {
264       if( is_error(EAGAIN) ) {  // handle non-blocking call timeout
265         #if SRC_VERBOSE
266         printf("UDP receive timed out\n"); 
267         #endif
268
269         if( d_wait ) {
270           // Allow boost thread interrupt, then try again
271           boost::this_thread::interruption_point();
272           continue;
273         }
274         else
275           return -1;
276       }
277       else {
278         report_error("udp_source/recv",NULL);
279         return -1;
280       }
281     }
282     else {
283       // Calculate the number of bytes we can take from the buffer in this call
284       nbytes = std::min(r, total_bytes-bytes_received);
285       
286       // adjust the total number of bytes we have to round down to nearest integer of an itemsize
287       nbytes -= ((bytes_received+nbytes) % d_itemsize);   
288
289       // copy the number of bytes we want to look at here
290       memcpy(out, d_temp_buff, nbytes);    
291
292       d_residual = r - nbytes;                      // save the number of bytes stored
293       d_temp_offset=nbytes;                         // reset buffer index
294
295       // keep track of the total number of bytes received
296       bytes_received += nbytes;
297
298       // increment the pointer
299       out += nbytes;
300
301       // Immediately return when data comes in
302       break;
303     }
304
305     #if SNK_VERBOSE
306     printf("\tbytes received: %d bytes (nbytes: %d)\n", bytes, nbytes);
307     #endif
308   }
309
310   #if SRC_VERBOSE
311   printf("Total Bytes Received: %d (bytes_received / noutput_items = %d / %d)\n", 
312          bytes_received, bytes_received, noutput_items);
313   #endif
314
315   // bytes_received is already set to some integer multiple of itemsize
316   return bytes_received/d_itemsize;
317 }
318