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 //---------------------------------------------------------------------------
48 #include "OSGConfig.h"
50 #include "OSGBaseThread.h"
51 #include "OSGSocketSelection.h"
52 #include "OSGBinaryMessage.h"
53 #include "OSGGroupSockPipeline.h"
54 #include "OSGConnectionType.h"
55 #include "OSGBinaryMessage.h"
59 /** \class OSG::GroupSockPipeline
62 /*-------------------------------------------------------------------------*/
63 /* constructor destructor */
68 GroupSockPipeline::GroupSockPipeline():
78 GroupSockPipeline::~GroupSockPipeline(void)
83 /*! get connection type
85 const ConnectionType
*GroupSockPipeline::getType()
90 /*-------------------------------------------------------------------------*/
93 /*! connect to the given point. If timeout is reached, -1 is
96 GroupConnection::Channel
GroupSockPipeline::connectPoint(
97 const std::string
&address
,
100 Channel channel
= Inherited::connectPoint(address
,timeout
);
104 /*! disconnect the given channel
106 void GroupSockPipeline::disconnect(Channel channel
)
108 Inherited::disconnect(channel
);
111 /*! accept an icomming point connection. If timeout is reached,
112 -1 is returned. If timeout is -1 then wait without timeout
114 GroupConnection::Channel
GroupSockPipeline::acceptPoint(Time timeout
)
116 Connection::Channel channel
= Inherited::acceptPoint(timeout
);
120 /*-------------------------- helpers --------------------------------------*/
122 /** \brief create conneciton
125 GroupConnection
*GroupSockPipeline::create(void)
127 return new GroupSockPipeline();
130 /*-------------------------------------------------------------------------*/
133 /** Write data to all destinations
135 * \param mem Pointer to data buffer
136 * \param size Size of bytes to write
140 void GroupSockPipeline::write(MemoryHandle mem
,UInt32 size
)
147 if(getChannelCount())
148 _next
.send(mem
,size
);
150 catch(SocketException
&e
)
152 throw WriteError(e
.what());
158 * Write blocksize and data.
161 void GroupSockPipeline::writeBuffer(void)
166 UInt32 size
= writeBufBegin()->getDataSize();
168 // write size to header
169 (reinterpret_cast<SocketBufferHeader
*>(&_socketWriteBuffer
[0]))->size
=
170 osgHostToNet
<UInt32
>(size
);
174 _next
.send(&_socketWriteBuffer
[0],
175 size
+sizeof(SocketBufferHeader
));
179 /*-------------------------------------------------------------------------*/
180 /* private helpers */
182 /*! initialize pipeline
184 void GroupSockPipeline::initialize(void)
188 std::string nextHost
;
189 BinaryMessage message
;
191 for(index
= 0 ; index
<_sockets
.size() ; ++index
)
193 len
= _sockets
[index
].recv(message
);
195 throw ReadError("Channel closed\n");
196 nextHost
= message
.getString();
197 nextPort
= message
.getUInt32();
202 message
.putUInt32(true);
203 _sockets
[_sockets
.size()-1].send(message
);
208 _next
.connect(SocketAddress(nextHost
.c_str(),
220 message
.putUInt32(false);
221 message
.putString(nextHost
);
222 message
.putUInt32(nextPort
);
223 _sockets
[index
-1].send(message
);
229 /*-------------------------------------------------------------------------*/
232 ConnectionType
GroupSockPipeline::_type(
233 &GroupSockPipeline::create
,