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 #ifdef WIN32
00044 #include <windows.h>
00045 #include <io.h>
00046 #else
00047 #include <sys/types.h>
00048 #include <sys/socket.h>
00049 #include <netinet/in.h>
00050 #include <netinet/tcp.h>
00051 #include <arpa/inet.h>
00052 #include <netdb.h>
00053 #include <unistd.h>
00054 #include <fcntl.h>
00055 #include <sys/time.h>
00056 #endif
00057
00058 #include <errno.h>
00059 #include <stdio.h>
00060 #include <math.h>
00061 #include <map>
00062 #include <OSGBase.h>
00063 #include <OSGSocketAddress.h>
00064 #include <OSGStreamSocket.h>
00065
00066 OSG_USING_NAMESPACE
00067
00099
00100
00101
00106 StreamSocket::StreamSocket():
00107 Socket()
00108 {
00109 }
00110
00113 StreamSocket::StreamSocket(const StreamSocket &source):
00114 Socket(source)
00115 {
00116 }
00117
00118
00119
00120
00125 void StreamSocket::open()
00126 {
00127 _sd = ::socket(AF_INET, SOCK_STREAM, 0);
00128 if(_sd < 0)
00129 {
00130 throw SocketError("socket()");
00131 }
00132 struct linger li;
00133 li.l_onoff = 1;
00134 li.l_linger = 1;
00135 int rc = setsockopt(_sd, SOL_SOCKET, SO_LINGER,
00136 (SocketOptT*)&li, sizeof(li));
00137 }
00138
00141 void StreamSocket::close(void)
00142 {
00143 #ifdef WIN32
00144 ::closesocket(_sd);
00145 #else
00146 ::close(_sd);
00147 #endif
00148 }
00149
00155 StreamSocket StreamSocket::acceptFrom(SocketAddress &address)
00156 {
00157 StreamSocket client;
00158 SocketLenT len;
00159
00160 len=address.getSockAddrSize();
00161 client._sd=::accept(_sd,
00162 address.getSockAddr(),
00163 &len);
00164 if(client._sd < 0)
00165 {
00166 throw SocketError("accept()");
00167 }
00168 return client;
00169 }
00170
00175 StreamSocket StreamSocket::accept()
00176 {
00177 SocketAddress addr;
00178 return acceptFrom(addr);
00179 }
00180
00185 void StreamSocket::setDelay(bool value)
00186 {
00187 int rc,on;
00188 on=!value;
00189 rc=setsockopt(_sd, IPPROTO_TCP, TCP_NODELAY,
00190 (SocketOptT*)&on, sizeof(on));
00191 if(rc < 0)
00192 {
00193 throw SocketError("setsockopt(,SOCK_STREAM,TCP_NODELAY)");
00194 }
00195 }
00196
00197
00198
00199
00202 const StreamSocket & StreamSocket::operator =(const StreamSocket &source)
00203 {
00204 _sd=source._sd;
00205 return *this;
00206 }
00207
00208
00209
00210
00211 #ifdef __sgi
00212 #pragma set woff 1174
00213 #endif
00214
00215 #ifdef OSG_LINUX_ICC
00216 #pragma warning( disable : 177 )
00217 #endif
00218
00219 namespace
00220 {
00221 static Char8 cvsid_cpp [] = "@(#)$Id: $";
00222 static Char8 cvsid_hpp [] = OSG_STREAMSOCKET_HEADER_CVSID;
00223 }