fixed: gcc8 compile issues
[opensg.git] / Source / Base / Network / Socket / OSGStreamSocket.cpp
blob2834f98cec630ad2fed22ae1539ad8cf2c746091
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 //---------------------------------------------------------------------------
43 #ifdef WIN32
44 #include <windows.h>
45 #include <io.h>
46 #else
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <netinet/tcp.h>
51 #include <arpa/inet.h>
52 #include <netdb.h>
53 #include <unistd.h>
54 #include <fcntl.h>
55 #endif
57 #include <cerrno>
58 #include <cstdio>
59 #include <cmath>
60 #include <map>
61 #include "OSGSocketAddress.h"
62 #include "OSGStreamSocket.h"
63 #include "OSGTime.h"
65 OSG_USING_NAMESPACE
67 /** \class OSG::StreamSocket
68 * \brief Stream socket handler
70 * This class is a Handler to connection oriented sockets. A call to
71 * <EM>open</EM> will assing a stream socket and <EM>close</EM>
72 * releases the socket.
74 * Client example
75 * <PRE>
76 * char buffer[100];
77 * StreamSocket s;
78 * s.open();
79 * s.connect(Address("serverhost.com",4567);
80 * s.send(buffer,100);
81 * s.close();
82 * </PRE>
84 * Server example
85 * <PRE>
86 * char buffer[100];
87 * StreamSocket s;
88 * s.open();
89 * s.bind(AnySocketAddress(4567);
90 * c=s.accept(); // accept incomming client
91 * c.recv(buffer,100); // read client message
92 * c.close();
93 * s.close();
94 * </PRE>
95 **/
97 /*-------------------------------------------------------------------------*/
98 /* constructor destructor */
100 /*! Constructor. Use open to assign a system socket. No system socket is assigned by
101 the constructor.
102 \see StreamSocket::open
104 StreamSocket::StreamSocket():
105 Socket()
109 /** \brief Copy constructor
111 StreamSocket::StreamSocket(const StreamSocket &source):
112 Socket(source)
116 /*-------------------------------------------------------------------------*/
117 /* Socket functionaliy */
119 /*! Assign a socket. <CODE>Open</CODE> assignes a system socket to the
120 StreamSocket.
121 \see Socket::close
123 void StreamSocket::open()
125 _sd = ::socket(AF_INET, SOCK_STREAM, 0);
126 if(_sd < 0)
128 throw SocketError("socket()");
131 struct linger li;
132 li.l_onoff = 1;
133 li.l_linger = 1;
134 /* int rc = */ setsockopt(_sd, SOL_SOCKET, SO_LINGER,
135 reinterpret_cast<SocketOptT*>(&li), sizeof(li));
138 /*! close socket
140 void StreamSocket::close(void)
142 #ifdef WIN32
143 ::closesocket(_sd);
144 #else
145 ::close(_sd);
146 #endif
149 /*! Accept incomming connection. Use the returned StreamSocket to
150 communicate over the accepted communication. If the new StreamSocket
151 is no longer used, you have to close it. A new StreamSocket is
152 returned to communicate with the accepted client.
154 StreamSocket StreamSocket::acceptFrom(SocketAddress &address)
156 StreamSocket client;
157 SocketLenT len;
159 len=address.getSockAddrSize();
160 client._sd=::accept(_sd,
161 address.getSockAddr(),
162 &len);
163 if(client._sd < 0)
165 throw SocketError("accept()");
167 return client;
170 /*! Accept incomming connection. Use the returned StreamSocket to
171 communicate over the accepted communication. If the new StreamSocket
172 is no longer used, you have to close it.
174 StreamSocket StreamSocket::accept()
176 SocketAddress addr;
177 return acceptFrom(addr);
180 /*! A Stream socket doesen't send data immediately. Only if the internal
181 buffer contains enough data, an immediate write is forced. If
182 delay is set to false, then data is written always immediately.
184 void StreamSocket::setDelay(bool value)
186 int rc,on;
187 on=!value;
188 rc=setsockopt(_sd, IPPROTO_TCP, TCP_NODELAY,
189 reinterpret_cast<SocketOptT*>(&on), sizeof(on));
190 if(rc < 0)
192 throw SocketError("setsockopt(,SOCK_STREAM,TCP_NODELAY)");
196 /*-------------------------------------------------------------------------*/
197 /* assignment */
199 /*! assignment
201 const StreamSocket & StreamSocket::operator =(const StreamSocket &source)
203 _sd=source._sd;
204 return *this;