1 // sg_socket.cxx -- Socket I/O routines
3 // Written by Curtis Olson, started November 1999.
4 // Modified by Bernie Bright <bbright@bigpond.net.au>, May 2002.
6 // Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <simgear/compiler.h>
33 #include <simgear/debug/logstream.hxx>
35 #include "sg_socket.hxx"
37 bool SGSocket::init = false;
39 SGSocket::SGSocket( const string& host, const string& port_,
40 const string& style ) :
52 netInit(NULL, NULL); // plib-1.4.2 compatible
60 else if ( style != "udp" )
62 SG_LOG( SG_IO, SG_ALERT,
63 "Error: SGSocket() unknown style = " << style );
66 set_type( sgSocketType );
77 SGSocket::make_server_socket()
79 if (!sock.open( is_tcp ))
81 SG_LOG( SG_IO, SG_ALERT,
82 "Error: socket() failed in make_server_socket()" );
86 if (sock.bind( "", port ) < 0)
88 SG_LOG( SG_IO, SG_ALERT,
89 "Error: bind() failed in make_server_socket()" );
99 SGSocket::make_client_socket()
101 if (!sock.open( is_tcp ))
103 SG_LOG( SG_IO, SG_ALERT,
104 "Error: socket() failed in make_client_socket()" );
108 if (sock.connect( hostname.c_str(), port ) < 0)
110 SG_LOG( SG_IO, SG_ALERT,
111 "Error: connect() failed in make_client_socket()" );
119 // If specified as a server (in direction for now) open the master
120 // listening socket. If specified as a client (out direction), open a
121 // connection to a server.
123 SGSocket::open( SGProtocolDir direction )
125 set_dir( direction );
127 is_server = is_tcp &&
128 (direction == SG_IO_IN || direction == SG_IO_BI);
130 if ( port_str == "" || port_str == "any" ) {
133 port = atoi( port_str.c_str() );
136 if (direction == SG_IO_IN)
138 // this means server for now
140 // Setup socket to listen on. Set "port" before making this
141 // call. A port of "0" indicates that we want to let the os
142 // pick any available port.
143 if (!make_server_socket())
145 SG_LOG( SG_IO, SG_ALERT, "SG_IO_IN socket creation failed" );
157 // Specify the maximum length of the connection queue
158 sock.listen( SG_MAX_SOCKET_QUEUE );
162 else if (direction == SG_IO_OUT)
164 // this means client for now
166 if (!make_client_socket())
168 SG_LOG( SG_IO, SG_ALERT, "SG_IO_OUT socket creation failed" );
178 else if (direction == SG_IO_BI && is_tcp)
180 // this means server for TCP sockets
182 // Setup socket to listen on. Set "port" before making this
183 // call. A port of "0" indicates that we want to let the os
184 // pick any available port.
185 if (!make_server_socket())
187 SG_LOG( SG_IO, SG_ALERT, "SG_IO_BI socket creation failed" );
191 // Specify the maximum length of the connection queue
192 sock.listen( SG_MAX_SOCKET_QUEUE );
196 SG_LOG( SG_IO, SG_ALERT,
197 "Error: bidirection mode not available for UDP sockets." );
207 // read data from socket (server)
208 // read a block of data of specified size
210 SGSocket::read( char *buf, int length )
212 if (sock.getHandle() == -1 &&
213 (client == 0 || client->getHandle() == -1))
218 // test for any input available on sock (returning immediately, even if
224 if (is_tcp && is_server)
226 result = client->recv( buf, length );
230 result = sock.recv( buf, length );
233 if ( result != length )
235 SG_LOG( SG_IO, SG_INFO,
236 "Warning: read() not enough bytes." );
244 // read a line of data, length is max size of input buffer
246 SGSocket::readline( char *buf, int length )
248 if (sock.getHandle() == -1 &&
249 (client == 0 || client->getHandle() == -1))
254 // test for any input read on sock (returning immediately, even if
256 int result = this->poll();
260 // read a chunk, keep in the save buffer until we have the
261 // requested amount read
263 if (is_tcp && is_server)
265 char *buf_ptr = save_buf + save_len;
266 result = client->recv( buf_ptr, SG_IO_MAX_MSG_SIZE - save_len );
275 // Try and detect that the remote end died. This
276 // could cause problems so if you see connections
277 // dropping for unexplained reasons, LOOK HERE!
278 if (result == 0 && save_len == 0 && first_read == true)
280 SG_LOG( SG_IO, SG_ALERT,
281 "Connection closed by foreign host." );
288 char *buf_ptr = save_buf + save_len;
289 result = sock.recv( buf_ptr, SG_IO_MAX_MSG_SIZE - save_len );
294 // look for the end of line in save_buf
296 for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i )
298 if ( save_buf[i] == '\n' ) {
301 // no end of line yet
305 // we found an end of line
307 // copy to external buffer
308 strncpy( buf, save_buf, result );
312 //memmove( save_buf+, save_buf+, ? );
313 for ( i = result; i < save_len; ++i ) {
314 save_buf[ i - result ] = save_buf[i];
322 // write data to socket (client)
324 SGSocket::write( const char *buf, const int length )
326 netSocket* s = client == 0 ? &sock : client;
327 if (s->getHandle() == -1)
332 bool error_condition = false;
334 if ( s->send( buf, length ) < 0 )
336 SG_LOG( SG_IO, SG_WARN, "Error writing to socket: " << port );
337 error_condition = true;
340 if ( error_condition ) {
348 // write null terminated string to socket (server)
350 SGSocket::writestring( const char *str )
352 int length = strlen( str );
353 return this->write( str, length );
369 // configure the socket as non-blocking
373 if (sock.getHandle() == -1) {
377 sock.setBlocking( false );
384 netSocket* readers[2];
386 readers[0] = client != 0 ? client : &sock;
389 netSocket* writers[1];
392 int result = netSocket::select( readers, writers, timeout );
394 if (result > 0 && is_server && client == 0)
396 // Accept a new client connection
398 int new_fd = sock.accept( &addr );
399 SG_LOG( SG_IO, SG_INFO, "Accepted connection from "
400 << addr.getHost() << ":" << addr.getPort() );
401 client = new netSocket();
402 client->setHandle( new_fd );