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

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