3 * Copyright 2007,2008,2009,2010 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
26 #include <gr_udp_sink.h>
27 #include <gr_io_signature.h>
32 #if defined(HAVE_NETDB_H)
34 #include <sys/socket.h> //usually included by <netdb.h>?
35 typedef void* optval_t;
36 #elif defined(HAVE_WINDOWS_H)
37 // if not posix, assume winsock
42 typedef char* optval_t;
45 #include <gruel/thread.h>
49 static int is_error( int perr )
51 // Compare error to posix error code; return nonzero if match.
52 #if defined(USING_WINSOCK)
53 #define ENOPROTOOPT 109
54 #define ECONNREFUSED 111
55 // All codes to be checked for must be defined below
56 int werr = WSAGetLastError();
59 return( perr == EAGAIN );
61 return( perr == ENOPROTOOPT );
63 return( perr == ECONNREFUSED );
65 fprintf(stderr,"gr_udp_source/is_error: unknown error %d\n", perr );
66 throw std::runtime_error("internal error");
70 return( perr == errno );
74 static void report_error( const char *msg1, const char *msg2 )
76 // Deal with errors, both posix and winsock
77 #if defined(USING_WINSOCK)
78 int werr = WSAGetLastError();
79 fprintf(stderr, "%s: winsock error %d\n", msg1, werr );
84 throw std::runtime_error(msg2);
88 gr_udp_sink::gr_udp_sink (size_t itemsize,
89 const char *host, unsigned short port,
90 int payload_size, bool eof)
91 : gr_sync_block ("udp_sink",
92 gr_make_io_signature (1, 1, itemsize),
93 gr_make_io_signature (0, 0, 0)),
94 d_itemsize (itemsize), d_payload_size(payload_size), d_eof(eof),
97 #if defined(USING_WINSOCK) // for Windows (with MinGW)
98 // initialize winsock DLL
100 int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
101 if( iResult != NO_ERROR ) {
102 report_error( "gr_udp_source WSAStartup", "can't open socket" );
107 d_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
109 report_error("socket open","can't open socket");
112 // Don't wait when shutting down
116 if(setsockopt(d_socket, SOL_SOCKET, SO_LINGER, (optval_t)&lngr, sizeof(linger)) == -1) {
117 if( !is_error(ENOPROTOOPT) ) { // no SO_LINGER for SOCK_DGRAM on Windows
118 report_error("SO_LINGER","can't set socket option SO_LINGER");
122 // Get the destination address
126 // public constructor that returns a shared_ptr
129 gr_make_udp_sink (size_t itemsize,
130 const char *host, unsigned short port,
131 int payload_size, bool eof)
133 return gr_udp_sink_sptr (new gr_udp_sink (itemsize,
138 gr_udp_sink::~gr_udp_sink ()
144 shutdown(d_socket, SHUT_RDWR);
145 #if defined(USING_WINSOCK)
146 closesocket(d_socket);
153 #if defined(USING_WINSOCK) // for Windows (with MinGW)
154 // free winsock resources
160 gr_udp_sink::work (int noutput_items,
161 gr_vector_const_void_star &input_items,
162 gr_vector_void_star &output_items)
164 const char *in = (const char *) input_items[0];
165 ssize_t r=0, bytes_sent=0, bytes_to_send=0;
166 ssize_t total_size = noutput_items*d_itemsize;
169 printf("Entered udp_sink\n");
172 gruel::scoped_lock guard(d_mutex); // protect d_socket
174 while(bytes_sent < total_size) {
175 bytes_to_send = std::min((ssize_t)d_payload_size, (total_size-bytes_sent));
178 r = send(d_socket, (in+bytes_sent), bytes_to_send, 0);
179 if(r == -1) { // error on send command
180 if( is_error(ECONNREFUSED) )
181 r = bytes_to_send; // discard data until receiver is started
183 report_error("udp_sink",NULL); // there should be no error case where
184 return -1; // this function should not exit immediately
189 r = bytes_to_send; // discarded for lack of connection
193 printf("\tbyte sent: %d bytes\n", r);
198 printf("Sent: %d bytes (noutput_items: %d)\n", bytes_sent, noutput_items);
201 return noutput_items;
204 void gr_udp_sink::connect( const char *host, unsigned short port )
210 // Get the destination address
211 struct addrinfo *ip_dst;
212 struct addrinfo hints;
213 memset( (void*)&hints, 0, sizeof(hints) );
214 hints.ai_family = AF_INET;
215 hints.ai_socktype = SOCK_DGRAM;
216 hints.ai_protocol = IPPROTO_UDP;
218 sprintf( port_str, "%d", port );
219 int ret = getaddrinfo( host, port_str, &hints, &ip_dst );
221 report_error("gr_udp_source/getaddrinfo",
222 "can't initialize destination socket" );
224 // don't need d_mutex lock when !d_connected
225 if(::connect(d_socket, ip_dst->ai_addr, ip_dst->ai_addrlen) == -1) {
226 report_error("socket connect","can't connect to socket");
230 freeaddrinfo(ip_dst);
236 void gr_udp_sink::disconnect()
242 printf("gr_udp_sink disconnecting\n");
245 gruel::scoped_lock guard(d_mutex); // protect d_socket from work()
247 // Send a few zero-length packets to signal receiver we are done
250 for( i = 0; i < 3; i++ )
251 (void) send( d_socket, NULL, 0, 0 ); // ignore errors
254 // Sending EOF can produce ERRCONNREFUSED errors that won't show up
255 // until the next send or recv, which might confuse us if it happens
256 // on a new connection. The following does a nonblocking recv to
257 // clear any such errors.
259 timeout.tv_sec = 0; // zero time for immediate return
263 FD_SET(d_socket, &readfds);
264 int r = select(FD_SETSIZE, &readfds, NULL, NULL, &timeout);
267 report_error("udp_sink/select",NULL);
270 else if(r > 0) { // call recv() to get error return
271 r = recv(d_socket, (char*)&readfds, sizeof(readfds), 0);
274 report_error("udp_sink/recv",NULL);
279 // Since I can't find any way to disconnect a datagram socket in Cygwin,
280 // we just leave it connected but disable sending.
282 // zeroed address structure should reset connection
283 struct sockaddr addr;
284 memset( (void*)&addr, 0, sizeof(addr) );
285 // addr.sa_family = AF_UNSPEC; // doesn't work on Cygwin
286 // addr.sa_family = AF_INET; // doesn't work on Cygwin
288 if(::connect(d_socket, &addr, sizeof(addr)) == -1)
289 report_error("socket connect","can't connect to socket");