root/OpenSceneGraph/trunk/examples/osgcluster/receiver.cpp @ 6941

Revision 6941, 4.0 kB (checked in by robert, 7 years ago)

From Martin Lavery and Robert Osfield, Updated examples to use a variation of the MIT License

  • 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#if defined (WIN32) && !defined(__CYGWIN__)
23#include <winsock.h>
24#else
25#include <unistd.h>
26#include <sys/uio.h>
27#include <sys/socket.h>
28#include <netinet/in.h>
29#include <netdb.h>
30#include <arpa/inet.h>
31#include <sys/time.h>
32#endif
33#include <string.h>
34
35
36#include "receiver.h"
37
38#include <iostream>
39
40Receiver::Receiver( void )
41{
42    _port = 0;
43    _initialized = false;
44    _buffer = 0L;
45}
46
47Receiver::~Receiver( void )
48{
49#if defined (WIN32) && !defined(__CYGWIN__)
50    closesocket( _so);
51#else
52    close( _so );
53#endif
54}
55
56bool Receiver::init( void )
57{
58#if defined(WIN32) && !defined(__CYGWIN__)
59    WORD version = MAKEWORD(1,1);
60    WSADATA wsaData;
61    // First, we start up Winsock
62    WSAStartup(version, &wsaData);
63#endif
64
65    if( _port == 0 )
66    {
67    fprintf( stderr, "Receiver::init() - port not defined\n" );
68    return false;
69    }
70
71    if( (_so = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 )
72    {
73        perror( "Socket" );
74    return false;
75    }
76#if defined (WIN32) && !defined(__CYGWIN__)
77//    const BOOL on = TRUE;
78//    setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, sizeof(int));
79#else
80    int on = 1;
81    setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
82#endif
83
84//    struct sockaddr_in saddr;
85    saddr.sin_family = AF_INET;
86    saddr.sin_port   = htons( _port );
87#if defined (WIN32) && !defined(__CYGWIN__)
88    saddr.sin_addr.s_addr =  htonl(INADDR_ANY);
89#else
90    saddr.sin_addr.s_addr =  0;
91#endif
92
93    if( bind( _so, (struct sockaddr *)&saddr, sizeof( saddr )) < 0 )
94    {
95        perror( "bind" );
96        return false;
97    }
98
99    _initialized = true;
100    return _initialized;
101}
102
103
104void Receiver::setPort( const short port )
105{
106    _port = port;
107}
108
109void Receiver::setBuffer( void *buffer, const unsigned int size )
110{
111    _buffer = buffer;
112    _buffer_size = size;
113}
114
115void Receiver::sync( void )
116{
117    if(!_initialized) init();
118
119    if( _buffer == 0L )
120    {
121        fprintf( stderr, "Receiver::sync() - No buffer\n" );
122        return;
123    }
124
125#if defined(__linux) || defined(__FreeBSD__) || defined( __APPLE__ )
126    socklen_t
127#else
128    int
129#endif
130        size = sizeof( struct sockaddr_in );
131
132    fd_set fdset;
133    FD_ZERO( &fdset );
134    FD_SET( _so, &fdset );
135
136    struct timeval tv;
137    tv.tv_sec = 0;
138    tv.tv_usec = 0;
139
140#if defined (WIN32) && !defined(__CYGWIN__)
141//    saddr.sin_port   = htons( _port );
142    recvfrom( _so, (char *)_buffer, _buffer_size, 0, (sockaddr*)&saddr, &size );
143//    recvfrom(sock_Receive, szMessage, 256, 0, (sockaddr*)&addr_Cli, &clilen)
144    int err = WSAGetLastError ();
145    int *dum = (int*) _buffer;
146
147    while( select( _so+1, &fdset, 0L, 0L, &tv ) )
148    {
149        if( FD_ISSET( _so, &fdset ) )
150        {
151            recvfrom( _so, (char *)_buffer, _buffer_size, 0, (sockaddr*)&saddr, &size );
152        }
153    }
154#else
155    recvfrom( _so, (caddr_t)_buffer, _buffer_size, 0, 0, &size );
156    while( select( _so+1, &fdset, 0L, 0L, &tv ) )
157    {
158        if( FD_ISSET( _so, &fdset ) )
159        {
160            recvfrom( _so, (caddr_t)_buffer, _buffer_size, 0, 0, &size );
161        }
162    }
163#endif
164}
Note: See TracBrowser for help on using the browser.