2018-05-31 21:45:37 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2009-04-28 19:57:51 +08:00
|
|
|
*
|
2018-05-31 21:45:37 +08:00
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2014-11-27 01:08:16 +08:00
|
|
|
*
|
2018-05-31 21:45:37 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2009-04-28 19:57:51 +08:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2014-11-27 01:08:16 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2018-05-31 21:45:37 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
2009-04-28 19:57:51 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CLUSTER_H
|
|
|
|
#define CLUSTER_H
|
|
|
|
|
|
|
|
#include <osg/Matrix>
|
|
|
|
#include <osg/FrameStamp>
|
|
|
|
|
|
|
|
#include <osgViewer/Viewer>
|
|
|
|
|
|
|
|
#if !defined(WIN32) || defined(__CYGWIN__)
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#else
|
|
|
|
#include "winsock.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Receiver.h
|
|
|
|
//
|
|
|
|
// Class definition for the recipient of a broadcasted message
|
|
|
|
//
|
2014-11-27 01:08:16 +08:00
|
|
|
class Receiver
|
2009-04-28 19:57:51 +08:00
|
|
|
{
|
|
|
|
public :
|
|
|
|
|
2014-11-27 01:08:16 +08:00
|
|
|
Receiver();
|
|
|
|
~Receiver();
|
2009-04-28 19:57:51 +08:00
|
|
|
|
2014-11-27 01:08:16 +08:00
|
|
|
// setBuffer defines the buffer into which the broadcasted
|
|
|
|
// message will be received.
|
|
|
|
void setBuffer( void *buffer, const unsigned int size );
|
2009-04-28 19:57:51 +08:00
|
|
|
|
2014-11-27 01:08:16 +08:00
|
|
|
// Define what port to listen and bind to
|
|
|
|
void setPort( const short port );
|
2009-04-28 19:57:51 +08:00
|
|
|
|
2017-08-16 00:23:49 +08:00
|
|
|
// Sync does a blocking wait to receive next message
|
2014-11-27 01:08:16 +08:00
|
|
|
void sync( void );
|
2009-04-28 19:57:51 +08:00
|
|
|
|
|
|
|
private :
|
2014-11-27 01:08:16 +08:00
|
|
|
bool init( void );
|
2009-04-28 19:57:51 +08:00
|
|
|
|
|
|
|
private :
|
|
|
|
#if defined (WIN32) && !defined(__CYGWIN__)
|
|
|
|
SOCKET _so;
|
|
|
|
SOCKADDR_IN saddr;
|
|
|
|
#else
|
|
|
|
int _so;
|
|
|
|
struct sockaddr_in saddr;
|
|
|
|
#endif
|
|
|
|
bool _initialized;
|
|
|
|
short _port;
|
|
|
|
void *_buffer;
|
|
|
|
unsigned int _buffer_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Broadcaster.h
|
|
|
|
//
|
|
|
|
// Class definition for broadcasting a buffer to a LAN
|
|
|
|
//
|
2014-11-27 01:08:16 +08:00
|
|
|
class Broadcaster
|
2009-04-28 19:57:51 +08:00
|
|
|
{
|
|
|
|
public :
|
|
|
|
|
2014-11-27 01:08:16 +08:00
|
|
|
Broadcaster( void );
|
|
|
|
~Broadcaster( void );
|
2009-04-28 19:57:51 +08:00
|
|
|
|
2014-11-27 01:08:16 +08:00
|
|
|
// Set the broadcast port
|
|
|
|
void setPort( const short port );
|
2009-04-28 19:57:51 +08:00
|
|
|
|
2014-11-27 01:08:16 +08:00
|
|
|
// Set the buffer to be broadcast
|
|
|
|
void setBuffer( void *buffer, const unsigned int buffer_size );
|
2009-04-28 19:57:51 +08:00
|
|
|
|
2014-11-27 01:08:16 +08:00
|
|
|
// Set a recipient host. If this is used, the Broadcaster
|
|
|
|
// no longer broadcasts, but rather directs UDP packets at
|
|
|
|
// host.
|
|
|
|
void setHost( const char *hostname );
|
2009-04-28 19:57:51 +08:00
|
|
|
|
2014-11-27 01:08:16 +08:00
|
|
|
// Sync broadcasts the buffer
|
|
|
|
void sync( void );
|
2009-04-28 19:57:51 +08:00
|
|
|
|
|
|
|
private :
|
2014-11-27 01:08:16 +08:00
|
|
|
bool init( void );
|
2009-04-28 19:57:51 +08:00
|
|
|
|
|
|
|
private :
|
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__)
|
|
|
|
SOCKET _so;
|
|
|
|
#else
|
|
|
|
int _so;
|
|
|
|
#endif
|
|
|
|
bool _initialized;
|
|
|
|
short _port;
|
|
|
|
void *_buffer;
|
|
|
|
unsigned int _buffer_size;
|
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__)
|
|
|
|
SOCKADDR_IN saddr;
|
|
|
|
#else
|
|
|
|
struct sockaddr_in saddr;
|
|
|
|
#endif
|
|
|
|
unsigned long _address;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CameraPacket {
|
|
|
|
public:
|
2014-11-27 01:08:16 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
static const unsigned int MAX_NUM_EVENTS;
|
|
|
|
static const unsigned int SWAP_BYTES_COMPARE;
|
2014-11-27 01:08:16 +08:00
|
|
|
|
|
|
|
CameraPacket():_masterKilled(false)
|
2009-04-28 19:57:51 +08:00
|
|
|
{
|
|
|
|
_byte_order = SWAP_BYTES_COMPARE;
|
|
|
|
}
|
2014-11-27 01:08:16 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
void setPacket(const osg::Matrix& matrix,const osg::FrameStamp* frameStamp)
|
|
|
|
{
|
|
|
|
_matrix = matrix;
|
|
|
|
if (frameStamp)
|
|
|
|
{
|
|
|
|
_frameStamp = *frameStamp;
|
|
|
|
}
|
|
|
|
}
|
2014-11-27 01:08:16 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
void getModelView(osg::Matrix& matrix,float angle_offset=0.0f)
|
|
|
|
{
|
2014-11-27 01:08:16 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
matrix = _matrix * osg::Matrix::rotate(osg::DegreesToRadians(angle_offset),0.0f,1.0f,0.0f);
|
|
|
|
}
|
2014-11-27 01:08:16 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
void readEventQueue(osgViewer::Viewer& viewer);
|
2014-11-27 01:08:16 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
void writeEventQueue(osgViewer::Viewer& viewer);
|
|
|
|
|
|
|
|
void setMasterKilled(const bool flag) { _masterKilled = flag; }
|
2016-06-08 18:02:51 +08:00
|
|
|
bool getMasterKilled() const { return _masterKilled; }
|
2014-11-27 01:08:16 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
unsigned int _byte_order;
|
|
|
|
bool _masterKilled;
|
|
|
|
osg::Matrix _matrix;
|
|
|
|
|
|
|
|
// note don't use a ref_ptr as used elsewhere for FrameStamp
|
|
|
|
// since we don't want to copy the pointer - but the memory.
|
|
|
|
// FrameStamp doesn't have a private destructor to allow
|
2014-11-27 01:08:16 +08:00
|
|
|
// us to do this, even though its a reference counted object.
|
2009-04-28 19:57:51 +08:00
|
|
|
osg::FrameStamp _frameStamp;
|
2014-11-27 01:08:16 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
osgGA::EventQueue::Events _events;
|
2014-11-27 01:08:16 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class DataConverter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
DataConverter(unsigned int numBytes):
|
|
|
|
_startPtr(0),
|
|
|
|
_endPtr(0),
|
|
|
|
_swapBytes(false),
|
|
|
|
_currentPtr(0)
|
|
|
|
{
|
|
|
|
_currentPtr = _startPtr = new char[numBytes];
|
|
|
|
_endPtr = _startPtr+numBytes;
|
|
|
|
_numBytes = numBytes;
|
|
|
|
}
|
|
|
|
|
2011-05-06 17:22:17 +08:00
|
|
|
~DataConverter()
|
|
|
|
{
|
|
|
|
delete [] _startPtr;
|
|
|
|
}
|
2009-04-28 19:57:51 +08:00
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
_currentPtr = _startPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void write1(char* ptr)
|
|
|
|
{
|
|
|
|
if (_currentPtr+1>=_endPtr) return;
|
|
|
|
|
2014-11-27 01:08:16 +08:00
|
|
|
*(_currentPtr++) = *(ptr);
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void read1(char* ptr)
|
|
|
|
{
|
|
|
|
if (_currentPtr+1>=_endPtr) return;
|
|
|
|
|
2014-11-27 01:08:16 +08:00
|
|
|
*(ptr) = *(_currentPtr++);
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void write2(char* ptr)
|
|
|
|
{
|
|
|
|
if (_currentPtr+2>=_endPtr) return;
|
|
|
|
|
2014-11-27 01:08:16 +08:00
|
|
|
*(_currentPtr++) = *(ptr++);
|
|
|
|
*(_currentPtr++) = *(ptr);
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void read2(char* ptr)
|
|
|
|
{
|
|
|
|
if (_currentPtr+2>=_endPtr) return;
|
|
|
|
|
|
|
|
if (_swapBytes)
|
|
|
|
{
|
2014-11-27 01:08:16 +08:00
|
|
|
*(ptr+1) = *(_currentPtr++);
|
|
|
|
*(ptr) = *(_currentPtr++);
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-27 01:08:16 +08:00
|
|
|
*(ptr++) = *(_currentPtr++);
|
|
|
|
*(ptr) = *(_currentPtr++);
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void write4(char* ptr)
|
|
|
|
{
|
|
|
|
if (_currentPtr+4>=_endPtr) return;
|
|
|
|
|
2014-11-27 01:08:16 +08:00
|
|
|
*(_currentPtr++) = *(ptr++);
|
|
|
|
*(_currentPtr++) = *(ptr++);
|
|
|
|
*(_currentPtr++) = *(ptr++);
|
|
|
|
*(_currentPtr++) = *(ptr);
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void read4(char* ptr)
|
|
|
|
{
|
|
|
|
if (_currentPtr+4>=_endPtr) return;
|
|
|
|
|
|
|
|
if (_swapBytes)
|
|
|
|
{
|
2014-11-27 01:08:16 +08:00
|
|
|
*(ptr+3) = *(_currentPtr++);
|
|
|
|
*(ptr+2) = *(_currentPtr++);
|
|
|
|
*(ptr+1) = *(_currentPtr++);
|
|
|
|
*(ptr) = *(_currentPtr++);
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-27 01:08:16 +08:00
|
|
|
*(ptr++) = *(_currentPtr++);
|
|
|
|
*(ptr++) = *(_currentPtr++);
|
|
|
|
*(ptr++) = *(_currentPtr++);
|
|
|
|
*(ptr) = *(_currentPtr++);
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void write8(char* ptr)
|
|
|
|
{
|
|
|
|
if (_currentPtr+8>=_endPtr) return;
|
|
|
|
|
2014-11-27 01:08:16 +08:00
|
|
|
*(_currentPtr++) = *(ptr++);
|
|
|
|
*(_currentPtr++) = *(ptr++);
|
|
|
|
*(_currentPtr++) = *(ptr++);
|
|
|
|
*(_currentPtr++) = *(ptr++);
|
2009-04-28 19:57:51 +08:00
|
|
|
|
2014-11-27 01:08:16 +08:00
|
|
|
*(_currentPtr++) = *(ptr++);
|
|
|
|
*(_currentPtr++) = *(ptr++);
|
|
|
|
*(_currentPtr++) = *(ptr++);
|
|
|
|
*(_currentPtr++) = *(ptr);
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void read8(char* ptr)
|
|
|
|
{
|
|
|
|
char* endPtr = _currentPtr+8;
|
|
|
|
if (endPtr>=_endPtr) return;
|
|
|
|
|
|
|
|
if (_swapBytes)
|
|
|
|
{
|
2014-11-27 01:08:16 +08:00
|
|
|
*(ptr+7) = *(_currentPtr++);
|
|
|
|
*(ptr+6) = *(_currentPtr++);
|
|
|
|
*(ptr+5) = *(_currentPtr++);
|
|
|
|
*(ptr+4) = *(_currentPtr++);
|
|
|
|
|
|
|
|
*(ptr+3) = *(_currentPtr++);
|
|
|
|
*(ptr+2) = *(_currentPtr++);
|
|
|
|
*(ptr+1) = *(_currentPtr++);
|
|
|
|
*(ptr) = *(_currentPtr++);
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-27 01:08:16 +08:00
|
|
|
*(ptr++) = *(_currentPtr++);
|
|
|
|
*(ptr++) = *(_currentPtr++);
|
|
|
|
*(ptr++) = *(_currentPtr++);
|
|
|
|
*(ptr++) = *(_currentPtr++);
|
|
|
|
|
|
|
|
*(ptr++) = *(_currentPtr++);
|
|
|
|
*(ptr++) = *(_currentPtr++);
|
|
|
|
*(ptr++) = *(_currentPtr++);
|
|
|
|
*(ptr) = *(_currentPtr++);
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void writeChar(char c) { write1(&c); }
|
|
|
|
inline void writeUChar(unsigned char c) { write1((char*)&c); }
|
|
|
|
inline void writeShort(short c) { write2((char*)&c); }
|
|
|
|
inline void writeUShort(unsigned short c) { write2((char*)&c); }
|
|
|
|
inline void writeInt(int c) { write4((char*)&c); }
|
|
|
|
inline void writeUInt(unsigned int c) { write4((char*)&c); }
|
|
|
|
inline void writeFloat(float c) { write4((char*)&c); }
|
|
|
|
inline void writeDouble(double c) { write8((char*)&c); }
|
|
|
|
|
2014-11-27 01:08:16 +08:00
|
|
|
inline char readChar() { char c=0; read1(&c); return c; }
|
|
|
|
inline unsigned char readUChar() { unsigned char c=0; read1((char*)&c); return c; }
|
|
|
|
inline short readShort() { short c=0; read2((char*)&c); return c; }
|
|
|
|
inline unsigned short readUShort() { unsigned short c=0; read2((char*)&c); return c; }
|
|
|
|
inline int readInt() { int c=0; read4((char*)&c); return c; }
|
|
|
|
inline unsigned int readUInt() { unsigned int c=0; read4((char*)&c); return c; }
|
|
|
|
inline float readFloat() { float c=0.0f; read4((char*)&c); return c; }
|
|
|
|
inline double readDouble() { double c=0.0; read8((char*)&c); return c; }
|
2009-04-28 19:57:51 +08:00
|
|
|
|
|
|
|
void write(const osg::FrameStamp& fs);
|
|
|
|
void read(osg::FrameStamp& fs);
|
|
|
|
|
|
|
|
void write(const osg::Matrix& matrix);
|
|
|
|
void read(osg::Matrix& matrix);
|
|
|
|
|
|
|
|
void write(const osgGA::GUIEventAdapter& event);
|
|
|
|
void read(osgGA::GUIEventAdapter& event);
|
2014-11-27 01:08:16 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
void write(CameraPacket& cameraPacket);
|
|
|
|
void read(CameraPacket& cameraPacket);
|
2011-05-06 17:22:17 +08:00
|
|
|
|
|
|
|
char* startPtr() { return _startPtr; }
|
|
|
|
unsigned int numBytes() { return _numBytes; }
|
2014-11-27 01:08:16 +08:00
|
|
|
|
2011-05-06 17:22:17 +08:00
|
|
|
protected:
|
|
|
|
|
|
|
|
char* _startPtr;
|
|
|
|
char* _endPtr;
|
|
|
|
unsigned int _numBytes;
|
|
|
|
bool _swapBytes;
|
|
|
|
|
|
|
|
char* _currentPtr;
|
2009-04-28 19:57:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-11-27 01:08:16 +08:00
|
|
|
#endif
|