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
00043 #include "OSGConfig.h"
00044
00045 #ifdef WIN32
00046 #ifdef WIN32_LEAN_AND_MEAN
00047 #include <winsock2.h>
00048 #endif
00049 #include <io.h>
00050 #else
00051 #include <sys/types.h>
00052 #include <sys/socket.h>
00053 #include <netinet/in.h>
00054 #include <netinet/tcp.h>
00055 #include <arpa/inet.h>
00056 #include <netdb.h>
00057 #include <unistd.h>
00058 #include <fcntl.h>
00059 #include <sys/time.h>
00060 #endif
00061
00062 #include <errno.h>
00063 #include <stdio.h>
00064 #include <math.h>
00065 #include <map>
00066 #include <OSGBase.h>
00067 #include <OSGSocketAddress.h>
00068 #include <OSGStreamSocket.h>
00069
00070 OSG_USING_NAMESPACE
00071
00103
00104
00105
00110 StreamSocket::StreamSocket():
00111 Socket()
00112 {
00113 }
00114
00117 StreamSocket::StreamSocket(const StreamSocket &source):
00118 Socket(source)
00119 {
00120 }
00121
00122
00123
00124
00129 void StreamSocket::open()
00130 {
00131 _sd = ::socket(AF_INET, SOCK_STREAM, 0);
00132 if(_sd < 0)
00133 {
00134 throw SocketError("socket()");
00135 }
00136 struct linger li;
00137 li.l_onoff = 1;
00138 li.l_linger = 1;
00139 int rc = setsockopt(_sd, SOL_SOCKET, SO_LINGER,
00140 (SocketOptT*)&li, sizeof(li));
00141 }
00142
00145 void StreamSocket::close(void)
00146 {
00147 #ifdef WIN32
00148 ::closesocket(_sd);
00149 #else
00150 ::close(_sd);
00151 #endif
00152 }
00153
00159 StreamSocket StreamSocket::acceptFrom(SocketAddress &address)
00160 {
00161 StreamSocket client;
00162 SocketLenT len;
00163
00164 len=address.getSockAddrSize();
00165 client._sd=::accept(_sd,
00166 address.getSockAddr(),
00167 &len);
00168 if(client._sd < 0)
00169 {
00170 throw SocketError("accept()");
00171 }
00172 return client;
00173 }
00174
00179 StreamSocket StreamSocket::accept()
00180 {
00181 SocketAddress addr;
00182 return acceptFrom(addr);
00183 }
00184
00189 void StreamSocket::setDelay(bool value)
00190 {
00191 int rc,on;
00192 on=!value;
00193 rc=setsockopt(_sd, IPPROTO_TCP, TCP_NODELAY,
00194 (SocketOptT*)&on, sizeof(on));
00195 if(rc < 0)
00196 {
00197 throw SocketError("setsockopt(,SOCK_STREAM,TCP_NODELAY)");
00198 }
00199 }
00200
00201
00202
00203
00206 const StreamSocket & StreamSocket::operator =(const StreamSocket &source)
00207 {
00208 _sd=source._sd;
00209 return *this;
00210 }
00211
00212
00213
00214
00215 #ifdef __sgi
00216 #pragma set woff 1174
00217 #endif
00218
00219 #ifdef OSG_LINUX_ICC
00220 #pragma warning( disable : 177 )
00221 #endif
00222
00223 namespace
00224 {
00225 static Char8 cvsid_cpp [] = "@(#)$Id: $";
00226 static Char8 cvsid_hpp [] = OSG_STREAMSOCKET_HEADER_CVSID;
00227 }