root/OpenSceneGraph/branches/OpenSceneGraph-2.8/examples/osgcluster/broadcaster.cpp @ 11238

Revision 11238, 5.4 kB (checked in by paulmartz, 4 years ago)

2.8 branch: Minor bug fixes for ac and 3dc plugins. Merges these revisions from trunk: 10010, 10758, and 11175.

  • Property svn:mergeinfo set to
    /OpenSceneGraph/branches/OpenSceneGraph-2.8.2/examples/osgcluster/broadcaster.cpp:10664
    /OpenSceneGraph/trunk/examples/osgcluster/broadcaster.cpp:9769,9828,9879-9880,9895,9932,10010,10064,10208,10340,10417,10456,10487,10622-10623,10625,10671-10672,10697,10722,10753,10758,10788,10818,10854-10855,10858,10887,10891,10923,10933,11009,11019,11032,11034-11035,11111,11127,11131,11175
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgcluster.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <stdio.h>
20#include <fcntl.h>
21#include <sys/types.h>
22
23#if !defined (WIN32) || defined(__CYGWIN__)
24#include <sys/ioctl.h>
25#include <sys/uio.h>
26#include <sys/socket.h>
27#include <netinet/in.h>
28#include <netdb.h>
29#include <arpa/inet.h>
30#include <sys/time.h>
31#include <net/if.h>
32#include <netdb.h>
33#endif
34
35#include <string.h>
36
37#if defined(__linux)
38    #include <unistd.h>
39    #include <linux/sockios.h>
40#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
41    #include <unistd.h>
42    #include <sys/sockio.h>
43#elif defined(__sgi)
44    #include <unistd.h>
45    #include <net/soioctl.h>
46#elif defined(__CYGWIN__)
47    #include <unistd.h>
48#elif defined(__sun)
49    #include <unistd.h>
50    #include <sys/sockio.h>
51#elif defined (__APPLE__)
52    #include <unistd.h>
53    #include <sys/sockio.h>
54#elif defined (WIN32)
55    #include <winsock.h>
56    #include <stdio.h>
57#elif defined (__hpux__)
58    #include <unistd.h>
59#else
60    #error Teach me how to build on this system
61#endif
62
63#include "broadcaster.h"
64
65#define _VERBOSE 1
66
67Broadcaster::Broadcaster( void )
68{
69    _port = 0;
70    _initialized = false;
71    _buffer = 0L;
72    _address = 0;
73}
74
75Broadcaster::~Broadcaster( void )
76{
77#if defined (WIN32) && !defined(__CYGWIN__)
78    closesocket( _so);
79#else
80    close( _so );
81#endif
82}
83
84bool Broadcaster::init( void )
85{
86#if defined (WIN32) && !defined(__CYGWIN__)
87    WORD version = MAKEWORD(1,1);
88    WSADATA wsaData;
89    // First, we start up Winsock
90    WSAStartup(version, &wsaData);
91#endif
92
93    if( _port == 0 )
94    {
95        fprintf( stderr, "Broadcaster::init() - port not defined\n" );
96        return false;
97    }
98
99    if( (_so = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 )
100    {
101        perror( "Socket" );
102        return false;
103    }
104#if defined (WIN32) && !defined(__CYGWIN__)
105    const BOOL on = TRUE;
106#else
107    int on = 1;
108#endif
109
110#if defined (WIN32) && !defined(__CYGWIN__)
111    setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, (const char *) &on, sizeof(int));
112#else
113    setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
114#endif
115
116    saddr.sin_family = AF_INET;
117    saddr.sin_port   = htons( _port );
118    if( _address == 0 )
119    {
120#if defined (WIN32) && !defined(__CYGWIN__)
121        setsockopt( _so, SOL_SOCKET, SO_BROADCAST, (const char *) &on, sizeof(int));
122#else
123        setsockopt( _so, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
124#endif
125
126#if !defined (WIN32) || defined(__CYGWIN__)
127        struct ifreq ifr;
128#endif
129#if defined (__linux) || defined(__CYGWIN__)
130        strcpy( ifr.ifr_name, "eth0" );
131#elif defined(__sun)
132        strcpy( ifr.ifr_name, "hme0" );
133#elif !defined (WIN32)
134        strcpy( ifr.ifr_name, "ef0" );
135#endif
136#if defined (WIN32) // get the server address
137        saddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
138    }
139#else
140        if( (ioctl( _so, SIOCGIFBRDADDR, &ifr)) < 0 )
141        {
142            perror( "Broadcaster::init() Cannot get Broadcast Address" );
143            return false;
144        }
145            saddr.sin_addr.s_addr = (((sockaddr_in *)&ifr.ifr_broadaddr)->sin_addr.s_addr);
146        }
147        else
148        {
149            saddr.sin_addr.s_addr = _address;
150        }
151#endif
152#define _VERBOSE 1
153#ifdef _VERBOSE
154    unsigned char *ptr = (unsigned char *)&saddr.sin_addr.s_addr;
155    printf( "Broadcast address : %u.%u.%u.%u\n", ptr[0], ptr[1], ptr[2], ptr[3] );
156#endif
157
158    _initialized = true;
159    return _initialized;
160}
161
162void Broadcaster::setHost( const char *hostname )
163{
164    struct hostent *h;
165    if( (h = gethostbyname( hostname )) == 0L )
166    {
167        fprintf( stderr, "Broadcaster::setHost() - Cannot resolve an address for \"%s\".\n", hostname );
168        _address = 0;
169    }
170    else
171        _address = *(( unsigned long  *)h->h_addr);
172}
173
174void Broadcaster::setPort( const short port )
175{
176    _port = port;
177}
178
179void Broadcaster::setBuffer( void *buffer, const unsigned int size )
180{
181    _buffer = buffer;
182    _buffer_size = size;
183}
184
185void Broadcaster::sync( void )
186{
187    if(!_initialized) init();
188
189    if( _buffer == 0L )
190    {
191        fprintf( stderr, "Broadcaster::sync() - No buffer\n" );
192        return;
193    }
194
195#if defined (WIN32) && !defined(__CYGWIN__)
196    unsigned int size = sizeof( SOCKADDR_IN );
197    sendto( _so, (const char *)_buffer, _buffer_size, 0, (struct sockaddr *)&saddr, size );
198    int err = WSAGetLastError ();
199    if (err!=0) fprintf( stderr, "Broadcaster::sync() - error %d\n",err );
200#else
201    unsigned int size = sizeof( struct sockaddr_in );
202    sendto( _so, (const void *)_buffer, _buffer_size, 0, (struct sockaddr *)&saddr, size );
203#endif
204
205}
Note: See TracBrowser for help on using the browser.