root/OpenSceneGraph/trunk/examples/osgcluster/broadcaster.cpp @ 4805

Revision 4805, 4.3 kB (checked in by robert, 8 years ago)

Replaced tabs with spaces in examples.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <stdio.h>
2#include <fcntl.h>
3#include <sys/types.h>
4
5#if !defined (WIN32) || defined(__CYGWIN__)
6#include <sys/ioctl.h>
7#include <sys/uio.h>
8#include <sys/socket.h>
9#include <netinet/in.h>
10#include <netdb.h>
11#include <arpa/inet.h>
12#include <sys/time.h>
13#include <net/if.h>
14#include <netdb.h>
15#endif
16
17#include <string.h>
18
19#if defined(__linux)
20    #include <unistd.h>
21    #include <linux/sockios.h>
22#elif defined(__FreeBSD__)
23    #include <unistd.h>
24    #include <sys/sockio.h>
25#elif defined(__sgi)
26    #include <unistd.h>
27    #include <net/soioctl.h>
28#elif defined(__CYGWIN__)
29    #include <unistd.h>
30#elif defined(__sun)
31    #include <unistd.h>
32    #include <sys/sockio.h>
33#elif defined (__APPLE__)
34    #include <unistd.h>
35    #include <sys/sockio.h>
36#elif defined (WIN32)
37    #include <winsock.h>
38    #include <stdio.h>
39#elif defined (__hpux__)
40    #include <unistd.h>
41#else
42    #error Teach me how to build on this system
43#endif
44
45#include "broadcaster.h"
46
47#define _VERBOSE 1
48
49Broadcaster::Broadcaster( void )
50{
51    _port = 0;
52    _initialized = false;
53    _buffer = 0L;
54    _address = 0;
55}
56
57Broadcaster::~Broadcaster( void )
58{
59#if defined (WIN32) && !defined(__CYGWIN__)
60    closesocket( _so);
61#else
62    close( _so );
63#endif
64}
65
66bool Broadcaster::init( void )
67{
68#if defined (WIN32) && !defined(__CYGWIN__)
69    WORD version = MAKEWORD(1,1);
70    WSADATA wsaData;
71    // First, we start up Winsock
72    WSAStartup(version, &wsaData);
73#endif
74
75    if( _port == 0 )
76    {
77        fprintf( stderr, "Broadcaster::init() - port not defined\n" );
78        return false;
79    }
80
81    if( (_so = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 )
82    {
83        perror( "Socket" );
84        return false;
85    }
86#if defined (WIN32) && !defined(__CYGWIN__)
87    const BOOL on = TRUE;
88#else
89    int on = 1;
90#endif
91
92#if defined (WIN32) && !defined(__CYGWIN__)
93    setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, (const char *) &on, sizeof(int));
94#else
95    setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
96#endif
97
98    saddr.sin_family = AF_INET;
99    saddr.sin_port   = htons( _port );
100    if( _address == 0 )
101    {
102#if defined (WIN32) && !defined(__CYGWIN__)
103        setsockopt( _so, SOL_SOCKET, SO_BROADCAST, (const char *) &on, sizeof(int));
104#else
105        setsockopt( _so, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
106#endif
107
108#if !defined (WIN32) || defined(__CYGWIN__)
109        struct ifreq ifr;
110#endif
111#if defined (__linux) || defined(__CYGWIN__)
112        strcpy( ifr.ifr_name, "eth0" );
113#elif defined(__sun)
114        strcpy( ifr.ifr_name, "hme0" );
115#elif !defined (WIN32)
116        strcpy( ifr.ifr_name, "ef0" );
117#endif
118#if defined (WIN32) // get the server address
119        saddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
120    }
121#else
122        if( (ioctl( _so, SIOCGIFBRDADDR, &ifr)) < 0 )
123        {
124            perror( "Broadcaster::init() Cannot get Broadcast Address" );
125            return false;
126        }
127            saddr.sin_addr.s_addr = (((sockaddr_in *)&ifr.ifr_broadaddr)->sin_addr.s_addr);
128        }
129        else
130        {
131            saddr.sin_addr.s_addr = _address;
132        }
133#endif
134#define _VERBOSE 1
135#ifdef _VERBOSE
136    unsigned char *ptr = (unsigned char *)&saddr.sin_addr.s_addr;
137    printf( "Broadcast address : %u.%u.%u.%u\n", ptr[0], ptr[1], ptr[2], ptr[3] );
138#endif
139
140    _initialized = true;
141    return _initialized;
142}
143
144void Broadcaster::setHost( const char *hostname )
145{
146    struct hostent *h;
147    if( (h = gethostbyname( hostname )) == 0L )
148    {
149        fprintf( stderr, "Broadcaster::setHost() - Cannot resolv an address for \"%s\".\n", hostname );
150        _address = 0;
151    }
152    else
153        _address = *(( unsigned long  *)h->h_addr);
154}
155
156void Broadcaster::setPort( const short port )
157{
158    _port = port;
159}
160
161void Broadcaster::setBuffer( void *buffer, const unsigned int size )
162{
163    _buffer = buffer;
164    _buffer_size = size;
165}
166
167void Broadcaster::sync( void )
168{
169    if(!_initialized) init();
170
171    if( _buffer == 0L )
172    {
173        fprintf( stderr, "Broadcaster::sync() - No buffer\n" );
174        return;
175    }
176
177#if defined (WIN32) && !defined(__CYGWIN__)
178    unsigned int size = sizeof( SOCKADDR_IN );
179    sendto( _so, (const char *)_buffer, _buffer_size, 0, (struct sockaddr *)&saddr, size );
180    int err = WSAGetLastError ();
181    int *dum = (int*) _buffer;
182#else
183    unsigned int size = sizeof( struct sockaddr_in );
184    sendto( _so, (const void *)_buffer, _buffer_size, 0, (struct sockaddr *)&saddr, size );
185#endif
186
187}
Note: See TracBrowser for help on using the browser.