1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
46 #include "OSGConfig.h"
48 #include "OSGBaseThread.h"
49 #include "OSGSocketSelection.h"
50 #include "OSGBinaryMessage.h"
51 #include "OSGPointSockPipeline.h"
52 #include "OSGGroupSockPipeline.h"
53 #include "OSGConnectionType.h"
54 #include "OSGBinaryMessage.h"
58 /** \class OSG::PointSockPipeline
61 /*-------------------------------------------------------------------------*/
62 /* constructor destructor */
67 PointSockPipeline::PointSockPipeline():
80 PointSockPipeline::~PointSockPipeline(void)
86 /*! get connection type
88 const ConnectionType
*PointSockPipeline::getType()
93 /*-------------------------------------------------------------------------*/
96 /*! connect to the given group. If timeout is reached, -1 is
99 Connection::Channel
PointSockPipeline::connectGroup(
100 const std::string
&address
,
103 Channel channel
= Inherited::connectGroup(address
,timeout
);
107 /*! disconnect the given channel
109 void PointSockPipeline::disconnect(void)
114 /*! accept an icomming grop connection. If timeout is reached,
115 -1 is returned. If timeout is -1 then wait without timeout
117 Connection::Channel
PointSockPipeline::acceptGroup(Time timeout
)
119 Channel channel
= Inherited::acceptGroup(timeout
);
123 /*-------------------------------------------------------------------------*/
124 /* channel handling */
126 /*! select the next channel for reading. If timeout is not -1
127 then -1 is returned if timeout is reached
129 Connection::Channel
PointSockPipeline::selectChannel(Time timeout
)
130 OSG_THROW (ReadError
)
136 if(_prev
.waitReadable(timeout
))
139 catch(SocketError
&e
)
141 throw ReadError(e
.what());
146 /*-------------------------- create ---------------------------------------*/
148 /** \brief create conneciton
151 PointConnection
*PointSockPipeline::create(void)
153 return new PointSockPipeline();
156 /*-------------------------------------------------------------------------*/
159 /** Read data into given memory
161 * Read data form the current read socket. The read socket is that
162 * socket, that was selectet in selectChannel.
166 void PointSockPipeline::read(MemoryHandle mem
,UInt32 size
)
173 len
=_prev
.recv(mem
,size
);
176 throw ReadError("read got 0 bytes!");
178 // send to next in chain
180 _next
.send(mem
,size
);
183 /** Read next data block
185 * The stream connection uses only BinaryDataHandler buffer. If more
186 * then one buffer is present, then this methode must be changed!
190 void PointSockPipeline::readBuffer() OSG_THROW (ReadError
)
198 // read buffer header
199 len
=_prev
.recv(&_socketReadBuffer
[0],sizeof(SocketBufferHeader
));
202 throw ReadError("peek got 0 bytes!");
203 // read remaining data
204 size
=osgNetToHost
<UInt32
>((reinterpret_cast<SocketBufferHeader
*>(&_socketReadBuffer
[0]))->size
);
205 len
=_prev
.recv(&_socketReadBuffer
[sizeof(SocketBufferHeader
)],
209 throw ReadError("read got 0 bytes!");
210 readBufBegin()->setDataSize(size
);
211 // send to next in chain
213 _next
.send(&_socketReadBuffer
[0],
214 sizeof(SocketBufferHeader
)+size
);
217 /*-------------------------------------------------------------------------*/
218 /* private helpers */
220 /*! initialize pipeline
222 void PointSockPipeline::initialize(void)
224 BinaryMessage message
;
227 std::string nextHost
;
232 // get local host name
233 osgGetHostname(localhost
,255);
234 if(!getInterface().empty())
235 interf
= getInterface();
240 sock
.bind(SocketAddress(interf
.c_str(),0));
243 // send my own address
244 message
.putString(interf
);
245 message
.putUInt32(sock
.getAddress().getPort());
246 _socket
.send(message
);
248 _prev
= sock
.accept();
251 len
= _socket
.recv(message
);
253 throw ReadError("Channel closed\n");
254 _last
= message
.getUInt32() != 0;
258 nextHost
= message
.getString();
259 nextPort
= message
.getUInt32();
264 _next
.connect(SocketAddress(nextHost
.c_str(),
277 /*-------------------------------------------------------------------------*/
280 ConnectionType
PointSockPipeline::_type(
281 &PointSockPipeline::create
,