OSG::PointSockConnection Class Reference

#include <OSGPointSockConnection.h>

Inheritance diagram for OSG::PointSockConnection:

OSG::PointConnection OSG::Connection OSG::ExceptionBinaryDataHandler OSG::PointMCastConnection OSG::PointSockPipeline List of all members.

Public Types

public types


typedef Int32 Channel

Public Member Functions

Constructors


 PointSockConnection (void)
virtual ~PointSockConnection (void)
type info


virtual const ConnectionTypegetType (void)
connection


virtual Channel connectPoint (const std::string &address, Time timeout=-1)
virtual Channel connectGroup (const std::string &address, Time timeout=-1)
virtual void disconnect (void)
virtual Channel acceptGroup (Time timeout=-1)
virtual Channel acceptPoint (Time timeout=-1)
virtual std::string bind (const std::string &interf)
synchronisation


virtual bool wait (Time timeout) throw (ReadError )
virtual void signal (void) throw (WriteError)
channel handling


virtual Channel selectChannel (Time timeout=-1) throw (ReadError)
params


virtual void setParams (const std::string &params)
channel handling


const std::string & getInterface (void)
void setInterface (const std::string &interf)
Put


void put (void const *src, UInt32 size)
void putAndFree (MemoryHandle src, UInt32 size)
void putValue (const bool &value)
void putValue (const UInt8 &value)
void putValue (const UInt16 &value)
void putValue (const UInt32 &value)
void putValue (const UInt64 &value)
void putValue (const Int8 &value)
void putValue (const Int16 &value)
void putValue (const Int32 &value)
void putValue (const Int64 &value)
void putValue (const Real16 &value)
void putValue (const Fixed32 &value)
void putValue (const Real32 &value)
void putValue (const Real64 &value)
void putValue (const Real128 &value)
void putValue (const std::string &value)
void putValues (const bool *value, UInt32 size)
void putValues (const UInt8 *value, UInt32 size)
void putValues (const UInt16 *value, UInt32 size)
void putValues (const UInt32 *value, UInt32 size)
void putValues (const UInt64 *value, UInt32 size)
void putValues (const Int8 *value, UInt32 size)
void putValues (const Int16 *value, UInt32 size)
void putValues (const Int32 *value, UInt32 size)
void putValues (const Int64 *value, UInt32 size)
void putValues (const Real16 *value, UInt32 size)
void putValues (const Fixed32 *value, UInt32 size)
void putValues (const Real32 *value, UInt32 size)
void putValues (const Real64 *value, UInt32 size)
void putValues (const Real128 *value, UInt32 size)
void putValues (const std::string *value, UInt32 size)
Get


void get (void *dst, UInt32 size) throw (ReadError)
void getAndAlloc (MemoryHandle &src, UInt32 size) throw (ReadError)
void getValue (bool &value) throw (ReadError)
void getValue (UInt8 &value) throw (ReadError)
void getValue (UInt16 &value) throw (ReadError)
void getValue (UInt32 &value) throw (ReadError)
void getValue (UInt64 &value) throw (ReadError)
void getValue (Int8 &value) throw (ReadError)
void getValue (Int16 &value) throw (ReadError)
void getValue (Int32 &value) throw (ReadError)
void getValue (Int64 &value) throw (ReadError)
void getValue (Real16 &value) throw (ReadError)
void getValue (Fixed32 &value) throw (ReadError)
void getValue (Real32 &value) throw (ReadError)
void getValue (Real64 &value) throw (ReadError)
void getValue (Real128 &value) throw (ReadError)
void getValue (std::string &value) throw (ReadError)
void getValues (bool *value, UInt32 size) throw (ReadError)
void getValues (UInt8 *value, UInt32 size) throw (ReadError)
void getValues (UInt16 *value, UInt32 size) throw (ReadError)
void getValues (UInt32 *value, UInt32 size) throw (ReadError)
void getValues (UInt64 *value, UInt32 size) throw (ReadError)
void getValues (Int8 *value, UInt32 size) throw (ReadError)
void getValues (Int16 *value, UInt32 size) throw (ReadError)
void getValues (Int32 *value, UInt32 size) throw (ReadError)
void getValues (Int64 *value, UInt32 size) throw (ReadError)
void getValues (Real16 *value, UInt32 size) throw (ReadError)
void getValues (Fixed32 *value, UInt32 size) throw (ReadError)
void getValues (Real32 *value, UInt32 size) throw (ReadError)
void getValues (Real64 *value, UInt32 size) throw (ReadError)
void getValues (Real128 *value, UInt32 size) throw (ReadError)
void getValues (std::string *value, UInt32 size) throw (ReadError)
Helper


virtual void forceCopy (void)
virtual void forceDirectIO (void)
void flush (void)
 write data not yet written
void setNetworkOrder (bool value)
bool getNetworkOrder (void)

Static Public Member Functions

create


static PointConnectioncreate (void)
 create conneciton

Protected Types

typedef std::vector< MemoryBlock > BuffersT
typedef std::list< MemoryHandleFreeMemT

Protected Member Functions

IO Implementation


virtual void read (MemoryHandle mem, UInt32 size)
virtual void readBuffer (void) throw (ReadError)
virtual void write (MemoryHandle mem, UInt32 size)
virtual void writeBuffer (void)
Read


BuffersT::iterator readBufBegin (void)
BuffersT::iterator readBufEnd (void)
void readBufAdd (MemoryHandle mem, UInt32 size, UInt32 dataSize=0)
void readBufClear (void)
Write


BuffersT::iterator writeBufBegin (void)
BuffersT::iterator writeBufEnd (void)
void writeBufAdd (MemoryHandle mem, UInt32 size, UInt32 dataSize=0)
void writeBufClear (void)
Helper


bool isReadBufferEmpty (void)

Protected Attributes

members


StreamSocket _acceptSocket
StreamSocket _socket
SocketAddress _remoteAddress
std::vector< UInt8 > _socketReadBuffer
std::vector< UInt8 > _socketWriteBuffer
members


bool _pointToPoint
protected members


std::string _interface
Member


BuffersT _readBuffers
BuffersT _writeBuffers
BuffersT _zeroCopyBuffers
UInt32 _zeroCopyThreshold
FreeMemT _freeMem
BuffersT::iterator _currentReadBuffer
UInt32 _currentReadBufferPos
BuffersT::iterator _currentWriteBuffer
UInt32 _currentWriteBufferPos
bool _networkOrder

Private Types

typedef PointConnection Inherited

Private Member Functions

 PointSockConnection (const PointSockConnection &source)
PointSockConnectionoperator= (const PointSockConnection &source)

Static Private Attributes

static type


static ConnectionType _type

Classes

struct  SocketBufferHeader

Detailed Description

Definition at line 60 of file OSGPointSockConnection.h.


Member Typedef Documentation

Reimplemented from OSG::PointConnection.

Reimplemented in OSG::PointSockPipeline, and OSG::PointMCastConnection.

Definition at line 162 of file OSGPointSockConnection.h.

typedef Int32 OSG::Connection::Channel [inherited]

Definition at line 71 of file OSGConnection.h.

typedef std::vector<MemoryBlock> OSG::ExceptionBinaryDataHandler::BuffersT [protected, inherited]

Definition at line 229 of file OSGExceptionBinaryDataHandler.h.

typedef std::list<MemoryHandle> OSG::ExceptionBinaryDataHandler::FreeMemT [protected, inherited]

Definition at line 230 of file OSGExceptionBinaryDataHandler.h.


Constructor & Destructor Documentation

PointSockConnection::PointSockConnection ( void   ) 

Definition at line 65 of file OSGPointSockConnection.cpp.

References _acceptSocket, _socketReadBuffer, _socketWriteBuffer, OSG::StreamSocket::open(), OSG::ExceptionBinaryDataHandler::readBufAdd(), OSG::Socket::setReusePort(), and OSG::ExceptionBinaryDataHandler::writeBufAdd().

Referenced by create().

00065                                         :
00066     Inherited(0)
00067 {
00068     _acceptSocket.open();
00069     _acceptSocket.setReusePort(true);
00070 
00071     _socketReadBuffer.resize(131071);
00072     _socketWriteBuffer.resize( _socketReadBuffer.size() );
00073     // reserve first bytes for buffer size
00074     readBufAdd (&_socketReadBuffer [sizeof(SocketBufferHeader)],
00075                 _socketReadBuffer.size() -sizeof(SocketBufferHeader));
00076     writeBufAdd(&_socketWriteBuffer[sizeof(SocketBufferHeader)],
00077                 _socketWriteBuffer.size()-sizeof(SocketBufferHeader));
00078 }

PointSockConnection::~PointSockConnection ( void   )  [virtual]

Destructor

Definition at line 82 of file OSGPointSockConnection.cpp.

References _acceptSocket, and OSG::StreamSocket::close().

00083 {
00084     _acceptSocket.close();
00085 }

OSG::PointSockConnection::PointSockConnection ( const PointSockConnection source  )  [private]


Member Function Documentation

const ConnectionType * PointSockConnection::getType ( void   )  [virtual]

Implements OSG::PointConnection.

Reimplemented in OSG::PointSockPipeline, and OSG::PointMCastConnection.

Definition at line 89 of file OSGPointSockConnection.cpp.

References _type.

00090 {
00091     return &_type;
00092 }

Connection::Channel PointSockConnection::connectPoint ( const std::string &  address,
Time  timeout = -1 
) [virtual]

Implements OSG::Connection.

Definition at line 100 of file OSGPointSockConnection.cpp.

References OSG::PointConnection::_pointToPoint, _remoteAddress, _socket, and OSG::GroupSockConnection::connectSocket().

00103 {
00104     StreamSocket socket;
00105     if(GroupSockConnection::connectSocket( socket,
00106                                            address,
00107                                           _remoteAddress,
00108                                            timeout))
00109     {
00110         _socket = socket;
00111         _pointToPoint = true;
00112         return true;
00113     }
00114     else
00115     {
00116         return false;
00117     }
00118 }

Connection::Channel PointSockConnection::connectGroup ( const std::string &  address,
Time  timeout = -1 
) [virtual]

connect to the given group. If timeout is reached, -1 is returned

Implements OSG::PointConnection.

Reimplemented in OSG::PointSockPipeline, and OSG::PointMCastConnection.

Definition at line 123 of file OSGPointSockConnection.cpp.

References OSG::PointConnection::_pointToPoint, _remoteAddress, _socket, and OSG::GroupSockConnection::connectSocket().

00126 {
00127     StreamSocket socket;
00128     if(GroupSockConnection::connectSocket( socket,
00129                                            address,
00130                                           _remoteAddress,
00131                                            timeout))
00132     {
00133         _socket = socket;
00134         _pointToPoint = false;
00135         return true;
00136     }
00137     else
00138     {
00139         return false;
00140     }
00141 }

void PointSockConnection::disconnect ( void   )  [virtual]

disconnect the given channel

Implements OSG::PointConnection.

Reimplemented in OSG::PointSockPipeline, and OSG::PointMCastConnection.

Definition at line 145 of file OSGPointSockConnection.cpp.

References _socket, and OSG::StreamSocket::close().

00146 {
00147     _socket.close();
00148 }

Connection::Channel PointSockConnection::acceptGroup ( Time  timeout = -1  )  [virtual]

accept an icomming grop connection. If timeout is reached, -1 is returned. If timeout is -1 then wait without timeout

Implements OSG::PointConnection.

Reimplemented in OSG::PointSockPipeline, and OSG::PointMCastConnection.

Definition at line 172 of file OSGPointSockConnection.cpp.

References _acceptSocket, OSG::PointConnection::_pointToPoint, _remoteAddress, _socket, and OSG::GroupSockConnection::acceptSocket().

00173 {
00174     if(GroupSockConnection::acceptSocket(_acceptSocket,
00175                                          _socket,
00176                                          _remoteAddress,
00177                                           timeout))
00178     {
00179         _pointToPoint = false;
00180         return 0;
00181     }
00182     else
00183     {
00184         return -1;
00185     }
00186 }

Connection::Channel PointSockConnection::acceptPoint ( Time  timeout = -1  )  [virtual]

accept an icomming point connection. If timeout is reached, -1 is returned. If timeout is -1 then wait without timeout

Implements OSG::Connection.

Definition at line 153 of file OSGPointSockConnection.cpp.

References _acceptSocket, OSG::PointConnection::_pointToPoint, _remoteAddress, _socket, and OSG::GroupSockConnection::acceptSocket().

00154 {
00155     if(GroupSockConnection::acceptSocket(_acceptSocket,
00156                                          _socket,
00157                                          _remoteAddress,
00158                                           timeout))
00159     {
00160         _pointToPoint = true;
00161         return 0;
00162     }
00163     else
00164     {
00165         return -1;
00166     }
00167 }

std::string PointSockConnection::bind ( const std::string &  address  )  [virtual]

bind the connection to a network interface. The address is returned, on wich the port could be connected. The interface is determined by the connection interface filed and the address parameter. Address can be empty, wich means to use a free port or address can contain a port number.

Implements OSG::Connection.

Definition at line 194 of file OSGPointSockConnection.cpp.

References _acceptSocket, OSG::SocketAddress::ANY, OSG::Socket::bind(), OSG::Socket::getAddress(), OSG::SocketAddress::getHost(), OSG::Connection::getInterface(), OSG::SocketAddress::getPort(), OSG::Socket::listen(), OSG::osgGetHostname(), OSG::Socket::setReusePort(), and SINFO.

00195 {
00196     int         port=0;
00197     char        localhost[256];
00198     char        host[256];
00199     char        portStr[256];
00200     std::string interf;
00201 
00202     // get local host name
00203     osgGetHostname(localhost,255);
00204     if(!getInterface().empty())
00205         interf = getInterface();
00206     else
00207         interf = localhost;
00208 
00209     // parse address
00210     if(!address.empty())
00211         if(sscanf(address.c_str(),"%*[^:]:%d",&port) != 1)
00212             if(sscanf(address.c_str(),":%d",&port) != 1)
00213                 port = 0;
00214     // bind port
00215     _acceptSocket.setReusePort(true);
00216     if(!getInterface().empty())
00217         _acceptSocket.bind(SocketAddress(getInterface().c_str(),port));
00218     else
00219         _acceptSocket.bind(SocketAddress(SocketAddress::ANY,port));
00220 
00221     SINFO << "Connection bound to "
00222           << _acceptSocket.getAddress().getHost() << ":"
00223           << _acceptSocket.getAddress().getPort() << std::endl;
00224     _acceptSocket.listen();
00225     // create address
00226     sprintf(portStr,"%d",_acceptSocket.getAddress().getPort());
00227     return interf + ":" + portStr;
00228 }

bool PointSockConnection::wait ( Time  timeout  )  throw (ReadError ) [virtual]

Implements OSG::PointConnection.

Reimplemented in OSG::PointMCastConnection.

Definition at line 256 of file OSGPointSockConnection.cpp.

References _socket, FFATAL, OSG::Socket::recv(), OSG::Socket::waitReadable(), and OSG::Exception::what().

00257 {
00258     UInt32 tag;
00259     try
00260     {
00261         if(!_socket.waitReadable(timeout))
00262             return false;
00263         if(!_socket.recv(&tag,sizeof(tag)))
00264             throw ReadError("Channel closed");
00265 
00266         tag = osgNetToHost<UInt32>(tag);
00267         if(tag != 314156)
00268         {
00269             FFATAL(("Stream out of sync in SockConnection\n"));
00270             throw ReadError("Stream out of sync");
00271         }
00272     }
00273     catch(SocketError &e)
00274     {
00275         throw ReadError(e.what());
00276     }
00277     return true;
00278 }

void PointSockConnection::signal ( void   )  throw (WriteError) [virtual]

send signal

Implements OSG::PointConnection.

Definition at line 282 of file OSGPointSockConnection.cpp.

References _socket, OSG::Socket::send(), and OSG::Exception::what().

00283 {
00284     UInt32 tag = osgHostToNet<UInt32>(314156);
00285     try
00286     {
00287         _socket.send(&tag,sizeof(tag));
00288     }
00289     catch(SocketError &e)
00290     {
00291         throw ReadError(e.what());
00292     }
00293 }

Connection::Channel PointSockConnection::selectChannel ( Time  timeout = -1  )  throw (ReadError) [virtual]

Implements OSG::Connection.

Reimplemented in OSG::PointSockPipeline, and OSG::PointMCastConnection.

Definition at line 236 of file OSGPointSockConnection.cpp.

References _socket, and OSG::Socket::waitReadable().

00238 {
00239     try
00240     {
00241         if(_socket.waitReadable(timeout))
00242             return 0;
00243     }
00244     catch(SocketError &e)
00245     {
00246         throw ReadError(e.what());
00247     }
00248     return -1;
00249 }

PointConnection * PointSockConnection::create ( void   )  [static]

Reimplemented in OSG::PointSockPipeline, and OSG::PointMCastConnection.

Definition at line 300 of file OSGPointSockConnection.cpp.

References PointSockConnection().

00301 {
00302     return new PointSockConnection();
00303 }

void PointSockConnection::read ( MemoryHandle  mem,
UInt32  size 
) [protected, virtual]

Reimplemented from OSG::ExceptionBinaryDataHandler.

Reimplemented in OSG::PointSockPipeline, and OSG::PointMCastConnection.

Definition at line 315 of file OSGPointSockConnection.cpp.

References _socket, and OSG::Socket::recv().

00316 {
00317     int len;
00318 
00319     // read data
00320     len=_socket.recv(mem,size);
00321     if(len==0)
00322     {
00323         throw ReadError("read got 0 bytes!");
00324     }
00325 }

void PointSockConnection::readBuffer ( void   )  throw (ReadError) [protected, virtual]

Read next data block

The stream connection uses only BinaryDataHandler buffer. If more then one buffer is present, then this methode must be changed!

Reimplemented from OSG::ExceptionBinaryDataHandler.

Reimplemented in OSG::PointSockPipeline, and OSG::PointMCastConnection.

Definition at line 334 of file OSGPointSockConnection.cpp.

References _socket, _socketReadBuffer, OSG::ExceptionBinaryDataHandler::readBufBegin(), OSG::Socket::recv(), and size.

00335 {
00336     int size;
00337     int len;
00338 
00339     // read buffer header
00340     len=_socket.recv(&_socketReadBuffer[0],sizeof(SocketBufferHeader));
00341     if(len==0)
00342         throw ReadError("peek got 0 bytes!");
00343     // read remaining data
00344     size=osgNetToHost<UInt32>(((SocketBufferHeader*)&_socketReadBuffer[0])->size);
00345     len=_socket.recv(&_socketReadBuffer[sizeof(SocketBufferHeader)],
00346                      size);
00347     if(len==0)
00348         throw ReadError("read got 0 bytes!");
00349     readBufBegin()->setDataSize(size);
00350 }    

void PointSockConnection::write ( MemoryHandle  mem,
UInt32  size 
) [protected, virtual]

Write data to destinations

Parameters:
mem Pointer to data buffer
size Size of bytes to write

Reimplemented from OSG::ExceptionBinaryDataHandler.

Definition at line 359 of file OSGPointSockConnection.cpp.

References _socket, and OSG::Socket::send().

00360 {
00361     _socket.send(mem,size);
00362 }

void PointSockConnection::writeBuffer ( void   )  [protected, virtual]

Write buffer

Write blocksize and data.

Reimplemented from OSG::ExceptionBinaryDataHandler.

Definition at line 369 of file OSGPointSockConnection.cpp.

References _socket, _socketWriteBuffer, OSG::Socket::send(), size, and OSG::ExceptionBinaryDataHandler::writeBufBegin().

00370 {
00371     Int32 index;
00372     UInt32 size = writeBufBegin()->getDataSize();
00373 
00374     // write size to header
00375     ((SocketBufferHeader*)&_socketWriteBuffer[0])->size = 
00376         osgHostToNet<UInt32>(size);
00377 
00378     if(size)
00379     {
00380         // write whole block
00381         _socket.send(&_socketWriteBuffer[0],
00382                      size+sizeof(SocketBufferHeader));
00383     }
00384 }

PointSockConnection& OSG::PointSockConnection::operator= ( const PointSockConnection source  )  [private]

virtual void OSG::Connection::setParams ( const std::string &  params  )  [virtual, inherited]

const std::string & Connection::getInterface ( void   )  [inherited]

void Connection::setInterface ( const std::string &  interf  )  [inherited]

set network interface

Definition at line 109 of file OSGConnection.cpp.

References OSG::Connection::_interface.

Referenced by OSG::ClusterServer::acceptClient(), and OSG::ClusterWindow::init().

00110 {
00111     _interface = interf;
00112 }

void ExceptionBinaryDataHandler::put ( void const *  src,
UInt32  size 
) [inherited]

Definition at line 85 of file OSGExceptionBinaryDataHandler.cpp.

References OSG::ExceptionBinaryDataHandler::_currentWriteBuffer, OSG::ExceptionBinaryDataHandler::_currentWriteBufferPos, OSG::ExceptionBinaryDataHandler::_zeroCopyBuffers, OSG::ExceptionBinaryDataHandler::_zeroCopyThreshold, OSG::osgMin(), OSG::ExceptionBinaryDataHandler::pushBuffer(), OSG::ExceptionBinaryDataHandler::write(), and OSG::ExceptionBinaryDataHandler::writeBufEnd().

Referenced by OSG::BalancedMultiWindow::clientRender(), OSG::PipelineComposer::composeViewport(), OSG::BinarySwapComposer::composeViewport(), OSG::BalancedMultiWindow::drawSendAndRecv(), OSG::ExceptionBinaryDataHandler::putAndFree(), OSG::ExceptionBinaryDataHandler::putValue(), OSG::ExceptionBinaryDataHandler::putValues(), OSG::ClusterViewBuffer::send(), OSG::BinarySwapComposer::sendToClient(), OSG::BalancedMultiWindow::serverRender(), OSG::PipelineComposer::setTransInfo(), OSG::BinarySwapComposer::writeCombine(), and OSG::PipelineComposer::writeResult().

00086 {
00087     UInt8 const *data = static_cast<UInt8 const *>(src);
00088 
00089     if(_zeroCopyThreshold && size >= _zeroCopyThreshold)
00090     {
00091         if(_zeroCopyThreshold == 1)
00092         {
00093             write(const_cast<MemoryHandle>(data), size);
00094         }
00095         else
00096         {
00097             UInt8 tag = 1;
00098 
00099             // we have to write a tag, to indicate the membership
00100             // of this zero copy block to the current data block
00101             put(&tag, sizeof(tag));
00102 
00103             _zeroCopyBuffers.push_back(
00104                 MemoryBlock(const_cast<MemoryHandle>(data), size, size));
00105         }
00106     }
00107     else
00108     {
00109         UInt32 copySize;
00110 
00111         while(size != 0)
00112         {
00113             if(_currentWriteBuffer == writeBufEnd())
00114             {
00115                 pushBuffer();
00116             }
00117 
00118             copySize = osgMin((_currentWriteBuffer->getSize() -
00119                                _currentWriteBufferPos),
00120                               size);
00121 
00122             memcpy(_currentWriteBuffer->getMem() + _currentWriteBufferPos,
00123                     data,
00124                     copySize);
00125 
00126              size                  -= copySize;
00127             _currentWriteBufferPos += copySize;
00128              data                  += copySize;
00129 
00130             // skip to next buffer if current buffer is full
00131             if(_currentWriteBufferPos == _currentWriteBuffer->getSize())
00132             {
00133                 _currentWriteBuffer->setDataSize(_currentWriteBufferPos);
00134                 _currentWriteBuffer++;
00135                 _currentWriteBufferPos = 0;
00136             }
00137         }
00138     }
00139 }

void ExceptionBinaryDataHandler::putAndFree ( MemoryHandle  src,
UInt32  size 
) [inherited]

Put data from dynamic allocated block The caller doesn't know, when the block will be written. So we are responsible for freeing this block.

Definition at line 146 of file OSGExceptionBinaryDataHandler.cpp.

References OSG::ExceptionBinaryDataHandler::_freeMem, OSG::ExceptionBinaryDataHandler::_zeroCopyThreshold, and OSG::ExceptionBinaryDataHandler::put().