00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include <stdlib.h>
00043 #include <stdio.h>
00044 #include <assert.h>
00045
00046 #include "OSGConfig.h"
00047 #include "OSGLog.h"
00048 #include "OSGBaseFunctions.h"
00049 #include "OSGSocketSelection.h"
00050 #include "OSGPointSockConnection.h"
00051 #include "OSGGroupSockConnection.h"
00052 #include "OSGConnectionType.h"
00053
00054 OSG_USING_NAMESPACE
00055
00060
00061
00062
00066 PointSockConnection::PointSockConnection():
00067 Inherited(0)
00068 {
00069 _acceptSocket.open();
00070 _acceptSocket.setReusePort(true);
00071
00072 _socketReadBuffer.resize(131071);
00073 _socketWriteBuffer.resize( _socketReadBuffer.size() );
00074
00075 readBufAdd (&_socketReadBuffer [sizeof(SocketBufferHeader)],
00076 _socketReadBuffer.size() -sizeof(SocketBufferHeader));
00077 writeBufAdd(&_socketWriteBuffer[sizeof(SocketBufferHeader)],
00078 _socketWriteBuffer.size()-sizeof(SocketBufferHeader));
00079 }
00080
00083 PointSockConnection::~PointSockConnection(void)
00084 {
00085 _acceptSocket.close();
00086 }
00087
00090 const ConnectionType *PointSockConnection::getType()
00091 {
00092 return &_type;
00093 }
00094
00095
00096
00097
00101 Connection::Channel PointSockConnection::connectPoint(
00102 const std::string &address,
00103 Time timeout)
00104 {
00105 StreamSocket socket;
00106 if(GroupSockConnection::connectSocket(socket,address,_remoteAddress,timeout))
00107 {
00108 _socket = socket;
00109 _pointToPoint = true;
00110 return true;
00111 }
00112 else
00113 {
00114 return false;
00115 }
00116 }
00117
00121 Connection::Channel PointSockConnection::connectGroup(
00122 const std::string &address,
00123 Time timeout)
00124 {
00125 StreamSocket socket;
00126 if(GroupSockConnection::connectSocket(socket,address,_remoteAddress,timeout))
00127 {
00128 _socket = socket;
00129 _pointToPoint = false;
00130 return true;
00131 }
00132 else
00133 {
00134 return false;
00135 }
00136 }
00137
00140 void PointSockConnection::disconnect(void)
00141 {
00142 _socket.close();
00143 }
00144
00148 Connection::Channel PointSockConnection::acceptPoint(Time timeout)
00149 {
00150 if(GroupSockConnection::acceptSocket(_acceptSocket,_socket,_remoteAddress,timeout))
00151 {
00152 _pointToPoint = true;
00153 return 0;
00154 }
00155 else
00156 {
00157 return -1;
00158 }
00159 }
00160
00164 Connection::Channel PointSockConnection::acceptGroup(Time timeout)
00165 {
00166 if(GroupSockConnection::acceptSocket(_acceptSocket,_socket,_remoteAddress,timeout))
00167 {
00168 _pointToPoint = false;
00169 return 0;
00170 }
00171 else
00172 {
00173 return -1;
00174 }
00175 }
00176
00183 std::string PointSockConnection::bind(const std::string &address)
00184 {
00185 int port=0;
00186 char localhost[256];
00187 char host[256];
00188 char portStr[256];
00189 std::string interf;
00190
00191
00192 osgGetHostname(localhost,255);
00193 if(!getInterface().empty())
00194 interf = getInterface();
00195 else
00196 interf = localhost;
00197
00198
00199 if(!address.empty())
00200 if(sscanf(address.c_str(),"%*[^:]:%d",&port) != 1)
00201 if(sscanf(address.c_str(),":%d",&port) != 1)
00202 port = 0;
00203
00204 _acceptSocket.setReusePort(true);
00205 if(!getInterface().empty())
00206 _acceptSocket.bind(SocketAddress(getInterface().c_str(),port));
00207 else
00208 _acceptSocket.bind(SocketAddress(SocketAddress::ANY,port));
00209
00210 SINFO << "Connection bound to "
00211 << _acceptSocket.getAddress().getHost() << ":"
00212 << _acceptSocket.getAddress().getPort() << std::endl;
00213 _acceptSocket.listen();
00214
00215 sprintf(portStr,"%d",_acceptSocket.getAddress().getPort());
00216 return interf + ":" + portStr;
00217 }
00218
00219
00220
00221
00225 Connection::Channel PointSockConnection::selectChannel(Time timeout)
00226 throw (ReadError)
00227 {
00228 try
00229 {
00230 if(_socket.waitReadable(timeout))
00231 return 0;
00232 }
00233 catch(SocketError &e)
00234 {
00235 throw ReadError(e.what());
00236 }
00237 return -1;
00238 }
00239
00240
00241
00242
00245 bool PointSockConnection::wait(Time timeout) throw (ReadError)
00246 {
00247 UInt32 tag;
00248 try
00249 {
00250 if(!_socket.waitReadable(timeout))
00251 return false;
00252 if(!_socket.recv(&tag,sizeof(tag)))
00253 throw ReadError("Channel closed");
00254 tag = osgntohl(tag);
00255 if(tag != 314156)
00256 {
00257 FFATAL(("Stream out of sync in SockConnection\n"));
00258 throw ReadError("Stream out of sync");
00259 }
00260 }
00261 catch(SocketError &e)
00262 {
00263 throw ReadError(e.what());
00264 }
00265 return true;
00266 }
00267
00270 void PointSockConnection::signal(void) throw (WriteError)
00271 {
00272 UInt32 tag=osghtonl(314156);
00273 try
00274 {
00275 _socket.send(&tag,sizeof(tag));
00276 }
00277 catch(SocketError &e)
00278 {
00279 throw ReadError(e.what());
00280 }
00281 }
00282
00283
00284
00288 PointConnection *PointSockConnection::create(void)
00289 {
00290 return new PointSockConnection();
00291 }
00292
00293
00294
00295
00303 void PointSockConnection::read(MemoryHandle mem,UInt32 size)
00304 {
00305 int len;
00306
00307
00308 len=_socket.recv(mem,size);
00309 if(len==0)
00310 {
00311 throw ReadError("read got 0 bytes!");
00312 }
00313 }
00314
00322 void PointSockConnection::readBuffer()
00323 {
00324 int size;
00325 int len;
00326
00327
00328 len=_socket.recv(&_socketReadBuffer[0],sizeof(SocketBufferHeader));
00329 if(len==0)
00330 throw ReadError("peek got 0 bytes!");
00331
00332 size=osgntohl(((SocketBufferHeader*)&_socketReadBuffer[0])->size);
00333 len=_socket.recv(&_socketReadBuffer[sizeof(SocketBufferHeader)],
00334 size);
00335 if(len==0)
00336 throw ReadError("read got 0 bytes!");
00337 readBufBegin()->setDataSize(size);
00338 }
00339
00347 void PointSockConnection::write(MemoryHandle mem,UInt32 size)
00348 {
00349 _socket.send(mem,size);
00350 }
00351
00357 void PointSockConnection::writeBuffer(void)
00358 {
00359 Int32 index;
00360 UInt32 size = writeBufBegin()->getDataSize();
00361
00362 ((SocketBufferHeader*)&_socketWriteBuffer[0])->size=osghtonl(size);
00363 if(size)
00364 {
00365
00366 _socket.send(&_socketWriteBuffer[0],
00367 size+sizeof(SocketBufferHeader));
00368 }
00369 }
00370
00371
00372
00373
00374 ConnectionType PointSockConnection::_type(
00375 &PointSockConnection::create,
00376 "StreamSock");
00377
00378
00379
00380
00381 #ifdef __sgi
00382 #pragma set woff 1174
00383 #endif
00384
00385 #ifdef OSG_LINUX_ICC
00386 #pragma warning( disable : 177 )
00387 #endif
00388
00389 namespace
00390 {
00391 static Char8 cvsid_cpp [] = "@(#)$Id: $";
00392 static Char8 cvsid_hpp [] = OSG_GROUPSOCKCONNECTION_HEADER_CVSID;
00393 }
00394