fixed: gcc8 compile issues
[opensg.git] / Source / Base / Network / Socket / OSGPointSockPipeline.cpp
blob3164a53fc0eea8fc081cf0009ae9a9c4a265698c
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
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. *
18 * *
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. *
23 * *
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. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
42 #include <cstdlib>
43 #include <cstdio>
44 #include <cassert>
46 #include "OSGConfig.h"
47 #include "OSGLog.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"
56 OSG_USING_NAMESPACE
58 /** \class OSG::PointSockPipeline
59 **/
61 /*-------------------------------------------------------------------------*/
62 /* constructor destructor */
64 /*! Constructor
67 PointSockPipeline::PointSockPipeline():
68 Inherited ( ),
69 _next ( ),
70 _prev ( ),
71 _last (false),
72 _initialized(false)
74 _prev.open();
75 _next.open();
78 /*! Destructor
80 PointSockPipeline::~PointSockPipeline(void)
82 _prev.close();
83 _next.close();
86 /*! get connection type
88 const ConnectionType *PointSockPipeline::getType()
90 return &_type;
93 /*-------------------------------------------------------------------------*/
94 /* connection */
96 /*! connect to the given group. If timeout is reached, -1 is
97 returned
99 Connection::Channel PointSockPipeline::connectGroup(
100 const std::string &address,
101 Time timeout)
103 Channel channel = Inherited::connectGroup(address,timeout);
104 return channel;
107 /*! disconnect the given channel
109 void PointSockPipeline::disconnect(void)
111 _socket.close();
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);
120 return channel;
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)
132 if(!_initialized)
133 initialize();
136 if(_prev.waitReadable(timeout))
137 return 0;
139 catch(SocketError &e)
141 throw ReadError(e.what());
143 return -1;
146 /*-------------------------- create ---------------------------------------*/
148 /** \brief create conneciton
151 PointConnection *PointSockPipeline::create(void)
153 return new PointSockPipeline();
156 /*-------------------------------------------------------------------------*/
157 /* read write */
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)
168 int len;
170 if(!_initialized)
171 initialize();
172 // read data
173 len=_prev.recv(mem,size);
174 if(len==0)
176 throw ReadError("read got 0 bytes!");
178 // send to next in chain
179 if(!_last)
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)
192 int size;
193 int len;
195 if(!_initialized)
196 initialize();
198 // read buffer header
199 len=_prev.recv(&_socketReadBuffer[0],sizeof(SocketBufferHeader));
201 if(len==0)
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)],
206 size);
208 if(len==0)
209 throw ReadError("read got 0 bytes!");
210 readBufBegin()->setDataSize(size);
211 // send to next in chain
212 if(!_last)
213 _next.send(&_socketReadBuffer[0],
214 sizeof(SocketBufferHeader)+size);
217 /*-------------------------------------------------------------------------*/
218 /* private helpers */
220 /*! initialize pipeline
222 void PointSockPipeline::initialize(void)
224 BinaryMessage message;
225 StreamSocket sock;
226 UInt32 nextPort;
227 std::string nextHost;
228 UInt32 len;
229 char localhost[256];
230 std::string interf;
232 // get local host name
233 osgGetHostname(localhost,255);
234 if(!getInterface().empty())
235 interf = getInterface();
236 else
237 interf = localhost;
239 sock.open();
240 sock.bind(SocketAddress(interf.c_str(),0));
241 sock.listen();
243 // send my own address
244 message.putString(interf);
245 message.putUInt32(sock.getAddress().getPort());
246 _socket.send(message);
247 // accept prev
248 _prev = sock.accept();
249 sock.close();
251 len = _socket.recv(message);
252 if(len == 0)
253 throw ReadError("Channel closed\n");
254 _last = message.getUInt32() != 0;
256 if(!_last)
258 nextHost = message.getString();
259 nextPort = message.getUInt32();
260 for(;;)
264 _next.connect(SocketAddress(nextHost.c_str(),
265 nextPort));
266 break;
268 catch(...)
274 _initialized = true;
277 /*-------------------------------------------------------------------------*/
278 /* static type */
280 ConnectionType PointSockPipeline::_type(
281 &PointSockPipeline::create,
282 "SockPipeline");