Added handling of return values

This commit is contained in:
Robert Osfield 2016-06-23 10:01:19 +01:00
parent 818843a6b6
commit 07b6016c61

View File

@ -15,6 +15,7 @@
#include <stdio.h> #include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/types.h> #include <sys/types.h>
#include <errno.h>
#if !defined (WIN32) || defined(__CYGWIN__) #if !defined (WIN32) || defined(__CYGWIN__)
#include <sys/ioctl.h> #include <sys/ioctl.h>
@ -290,14 +291,23 @@ bool Receiver::init( void )
perror( "Socket" ); perror( "Socket" );
return false; return false;
} }
int result = 0;
#if defined (WIN32) && !defined(__CYGWIN__) #if defined (WIN32) && !defined(__CYGWIN__)
// const BOOL on = TRUE; // const BOOL on = TRUE;
// setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, sizeof(int)); // setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, sizeof(int));
#else #else
int on = 1; int on = 1;
setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); result = setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
#endif #endif
if (result)
{
OSG_NOTICE<<"Warning: Reciever::init() setsockopt(..) failed, errno="<<errno<<std::endl;
return false;
}
// struct sockaddr_in saddr; // struct sockaddr_in saddr;
saddr.sin_family = AF_INET; saddr.sin_family = AF_INET;
saddr.sin_port = htons( _port ); saddr.sin_port = htons( _port );
@ -357,11 +367,17 @@ void Receiver::sync( void )
#if defined (WIN32) && !defined(__CYGWIN__) #if defined (WIN32) && !defined(__CYGWIN__)
// saddr.sin_port = htons( _port ); // saddr.sin_port = htons( _port );
recvfrom( _so, (char *)_buffer, _buffer_size, 0, (sockaddr*)&saddr, &size ); int result = recvfrom( _so, (char *)_buffer, _buffer_size, 0, (sockaddr*)&saddr, &size );
// recvfrom(sock_Receive, szMessage, 256, 0, (sockaddr*)&addr_Cli, &clilen) // recvfrom(sock_Receive, szMessage, 256, 0, (sockaddr*)&addr_Cli, &clilen)
//int err = WSAGetLastError (); //int err = WSAGetLastError ();
//int *dum = (int*) _buffer; //int *dum = (int*) _buffer;
if (result<0)
{
OSG_NOTICE<<"Warning: Receiver::sync() recvfrom(..) failed, errno="<<errno<<std::endl;
return;
}
while( select( _so+1, &fdset, 0L, 0L, &tv ) ) while( select( _so+1, &fdset, 0L, 0L, &tv ) )
{ {
if( FD_ISSET( _so, &fdset ) ) if( FD_ISSET( _so, &fdset ) )
@ -370,7 +386,13 @@ void Receiver::sync( void )
} }
} }
#else #else
recvfrom( _so, (caddr_t)_buffer, _buffer_size, 0, 0, &size ); int result = recvfrom( _so, (caddr_t)_buffer, _buffer_size, 0, 0, &size );
if (result<0)
{
OSG_NOTICE<<"Warning: Receiver::sync() recvfrom(..) failed, errno="<<errno<<std::endl;
return;
}
while( select( _so+1, &fdset, 0L, 0L, &tv ) ) while( select( _so+1, &fdset, 0L, 0L, &tv ) )
{ {
if( FD_ISSET( _so, &fdset ) ) if( FD_ISSET( _so, &fdset ) )
@ -429,21 +451,26 @@ bool Broadcaster::init( void )
int on = 1; int on = 1;
#endif #endif
int result = 0;
#if defined (WIN32) && !defined(__CYGWIN__) #if defined (WIN32) && !defined(__CYGWIN__)
setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, (const char *) &on, sizeof(int)); result = setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, (const char *) &on, sizeof(int));
#else #else
setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); result = setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
#endif #endif
if (result) return false;
saddr.sin_family = AF_INET; saddr.sin_family = AF_INET;
saddr.sin_port = htons( _port ); saddr.sin_port = htons( _port );
if( _address == 0 ) if( _address == 0 )
{ {
#if defined (WIN32) && !defined(__CYGWIN__) #if defined (WIN32) && !defined(__CYGWIN__)
setsockopt( _so, SOL_SOCKET, SO_BROADCAST, (const char *) &on, sizeof(int)); result = setsockopt( _so, SOL_SOCKET, SO_BROADCAST, (const char *) &on, sizeof(int));
#else #else
setsockopt( _so, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)); result = setsockopt( _so, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
#endif #endif
if (result) return false;
#if !defined (WIN32) || defined(__CYGWIN__) #if !defined (WIN32) || defined(__CYGWIN__)
struct ifreq ifr; struct ifreq ifr;
@ -514,14 +541,19 @@ void Broadcaster::sync( void )
return; return;
} }
int result = 0;
#if defined (WIN32) && !defined(__CYGWIN__) #if defined (WIN32) && !defined(__CYGWIN__)
unsigned int size = sizeof( SOCKADDR_IN ); unsigned int size = sizeof( SOCKADDR_IN );
sendto( _so, (const char *)_buffer, _buffer_size, 0, (struct sockaddr *)&saddr, size ); result = sendto( _so, (const char *)_buffer, _buffer_size, 0, (struct sockaddr *)&saddr, size );
// int err = WSAGetLastError (); // int err = WSAGetLastError ();
// int *dum = (int*) _buffer; // int *dum = (int*) _buffer;
#else #else
unsigned int size = sizeof( struct sockaddr_in ); unsigned int size = sizeof( struct sockaddr_in );
sendto( _so, (const void *)_buffer, _buffer_size, 0, (struct sockaddr *)&saddr, size ); result = sendto( _so, (const void *)_buffer, _buffer_size, 0, (struct sockaddr *)&saddr, size );
#endif #endif
if (result)
{
OSG_NOTICE<<"Warning: sentTo(...) failed : errno="<<errno<<std::endl;
}
} }