source: osgVisual/src/cluster/dataIO_clusterUDP_Transmission.cpp @ 46

Last change on this file since 46 was 31, checked in by Torben Dannhauer, 15 years ago

Adding first version of osgVisual!!

File size: 6.5 KB
Line 
1/* -*-c++-*- osgVisual - Copyright (C) 2009-2010 Torben Dannhauer
2 *
3 * This library is based on OpenSceneGraph, open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * osgVisual requires for some proprietary modules a license from the correspondig manufacturer.
9 * You have to aquire licenses for all used proprietary modules.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * OpenSceneGraph Public License for more details.
15*/
16
17#include "dataIO_clusterUDP_Transmission.h"
18
19using namespace osgVisual;
20
21
22Broadcaster::Broadcaster()
23{
24    _initialized = false;
25    _destinationAddress = 0;
26        requestedWSAVersion = MAKEWORD(2,0);
27}
28
29Broadcaster::~Broadcaster()
30{
31    closesocket( _so);
32}
33
34bool Broadcaster::init(const short port_)
35{
36    WSADATA wsaData;
37
38    // First, we start up Winsock
39    WSAStartup(requestedWSAVersion, &wsaData);
40
41    if( port_ == 0 )
42    {
43                OSG_NOTIFY( osg::FATAL ) << "ERROR: Broadcaster::init() - port not defined: " << std::endl;
44        return false;
45    }
46
47        // Create Socket
48    if( (_so = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 )
49    {
50                OSG_NOTIFY( osg::FATAL ) << "ERROR: Broadcaster::init() - Unable to create socket!" << std::endl;
51        return false;
52    }
53    const BOOL on = TRUE;
54
55        // Set Socket options
56    setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, (const char *) &on, sizeof(int));
57    saddr.sin_family = AF_INET; // Internetwork UDP, TCP etc.
58    saddr.sin_port   = htons( port_ );  // Port festlegen
59    if( _destinationAddress == 0 )      // Wenn keine Zieladresse festgelegt: Broadcasten
60    {
61        setsockopt( _so, SOL_SOCKET, SO_BROADCAST, (const char *) &on, sizeof(int));
62        saddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
63    }
64
65        // Print out destination address
66    unsigned char *ptr = (unsigned char *)&saddr.sin_addr.s_addr;
67        OSG_NOTIFY( osg::ALWAYS ) << "INFO: Broadcaster::init() - Broadcast address = " << (unsigned int)ptr[0] << "." << (unsigned int)ptr[1] << "." << (unsigned int)ptr[2] << "." << (unsigned int)ptr[3] << std::endl;
68
69        // Note: Initialization finished.
70    _initialized = true;
71    return _initialized;
72}
73
74void Broadcaster::setDestinationHost( const char *hostname )
75{
76    struct hostent *h;
77    if( (h = gethostbyname( hostname )) == 0L )
78    {
79                OSG_NOTIFY( osg::FATAL ) << "ERROR: Broadcaster::setHost() - Cannot resolve an address for " << hostname;
80        _destinationAddress = 0;
81    }
82    else
83        _destinationAddress = *(( unsigned long  *)h->h_addr);
84}
85
86void Broadcaster::transmitBuffer( const char* buffer_, const unsigned int size_ )
87{
88    if(!_initialized) 
89        {
90                OSG_NOTIFY( osg::FATAL ) << "ERROR: Broadcaster not initialized!" << std::endl;
91                return;
92        }
93
94    if( buffer_ == NULL )
95    {
96                OSG_NOTIFY( osg::FATAL ) << "ERROR: Broadcaster::transmitBuffer() - No buffer" << std::endl;
97        return;
98    }
99
100    unsigned int addrSize = sizeof( SOCKADDR_IN );
101    sendto( _so, buffer_, size_, 0, (struct sockaddr *)&saddr, addrSize );
102    int err = WSAGetLastError ();
103    if (err!=0)
104                OSG_NOTIFY( osg::FATAL ) << "ERROR: Broadcaster::transmitBuffer() - Error #: " << err << std::endl;
105}
106
107//
108//--------------------------------------------------------
109//
110
111Receiver::Receiver( void )
112{
113    _initialized = false;
114        requestedWSAVersion = MAKEWORD(2,0);
115}
116
117Receiver::~Receiver( void )
118{
119    closesocket( _so);
120}
121
122bool Receiver::init( const short port_ )
123{
124    WSADATA wsaData;
125    // First, we start up Winsock
126    WSAStartup(requestedWSAVersion, &wsaData);
127
128        if( port_ == 0 )
129    {
130                OSG_NOTIFY( osg::FATAL ) << "ERROR: Receiver::init() - port not defined: " << std::endl;
131        return false;
132    }
133
134        // Create Socket
135    if( (_so = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 )
136    {
137                OSG_NOTIFY( osg::FATAL ) << "ERROR: Receiver::init() -Could not create Socket" << std::endl;
138                return false;
139    }
140
141        // Set Socket Protocolfamily and Protocoldata (IP address, port, etc.)
142    //// Protokollfamilie
143        saddr.sin_family = AF_INET;
144    //// Port:
145        saddr.sin_port   = htons( port_ );      // The htons function converts a u_short from host to TCP/IP network byte order (which is big-endian)
146        //////Socketadresse. INADDR_ANY ist das Schlüsselwort für 0x00000000 und bindet den Socket auf alle lokalen IP-Adressen:
147        saddr.sin_addr.s_addr =  htonl(INADDR_ANY);             // The htonl function converts a u_long from host to TCP/IP network byte order (which is big endian).
148 
149
150        // Bind socket to local address and port.
151    if( bind( _so, (struct sockaddr *)&saddr, sizeof( saddr )) < 0 )
152    {
153        OSG_NOTIFY( osg::FATAL ) << "ERROR: Receiver::init() - Could not bind Socket" << std::endl;
154        return false;
155    }
156
157        // Note: Initialization finished.
158    _initialized = true;
159    return _initialized;
160}
161
162void Receiver::waitBlockingforSocketData( std::stringstream& writeInto_ )
163{
164        if(!_initialized) 
165        {
166                OSG_NOTIFY( osg::FATAL ) << "ERROR: Receiver not initialized!" << std::endl;
167                return;
168        }
169
170        // ?? Size des Addresspaketes ermitteln
171    int size = sizeof( struct sockaddr_in );
172
173    fd_set fdset;       // Filedescriptor Set
174    FD_ZERO( &fdset );  // Initialisiert das FDS als Null
175    FD_SET( _so, &fdset );      // Fügt dem FDS ein FileDescriptor hinzu
176
177        // Set Timeintervall for select() timeout
178    struct timeval tv;
179    tv.tv_sec = 0;
180    tv.tv_usec = 0;
181
182        // In einer Schleife: Solange lesen bis keine Daten mehr in dem Socket warten.
183    while( select( _so, &fdset, 0L, 0L, &tv ) ) // <0: error, =0 timeout, >0: socket to read
184    {
185        if( FD_ISSET( _so, &fdset ) )
186        {
187            int ret = recvfrom( _so, buffer, 65000, 0, (sockaddr*)&saddr, &size );
188                        // check for size
189                        if (ret > 0)
190                        {
191                                writeInto_.str("");
192                                OSG_NOTIFY( osg::INFO ) <<  "Receiver::waitBlockingforSocketData() - Empfangene Bytes:" << ret << std::endl;
193                                for(int i=0;i<ret;i++)
194                                        writeInto_ << buffer[i];
195                        }
196                        else
197                        {
198                                int err = WSAGetLastError ();
199                                if (err!=0) 
200                                        OSG_NOTIFY( osg::FATAL ) <<  "Receiver::waitBlockingforSocketData() - Error #" << err << std::endl;
201                        }
202                }       // END IF_ISSET
203    }
204        OSG_NOTIFY( osg::INFO ) << "Socket leergelesen!" << std::endl;
205}
206
207
Note: See TracBrowser for help on using the repository browser.