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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 # include <simgear_config.h>
28 #include <simgear/compiler.h>
36 #include <simgear/debug/logstream.hxx>
38 #include "sg_socket.hxx"
40 bool SGSocket::init = false;
42 SGSocket::SGSocket( const string& host, const string& port_,
43 const string& style ) :
55 netInit(NULL, NULL); // plib-1.4.2 compatible
63 else if ( style != "udp" )
65 SG_LOG( SG_IO, SG_ALERT,
66 "Error: SGSocket() unknown style = " << style );
69 set_type( sgSocketType );
80 SGSocket::make_server_socket()
82 if (!sock.open( is_tcp ))
84 SG_LOG( SG_IO, SG_ALERT,
85 "Error: socket() failed in make_server_socket()" );
89 if (sock.bind( "", port ) < 0)
91 SG_LOG( SG_IO, SG_ALERT,
92 "Error: bind() failed in make_server_socket()" );
102 SGSocket::make_client_socket()
104 if (!sock.open( is_tcp ))
106 SG_LOG( SG_IO, SG_ALERT,
107 "Error: socket() failed in make_client_socket()" );
111 if (sock.connect( hostname.c_str(), port ) < 0)
113 SG_LOG( SG_IO, SG_ALERT,
114 "Error: connect() failed in make_client_socket()" );
122 // If specified as a server (in direction for now) open the master
123 // listening socket. If specified as a client (out direction), open a
124 // connection to a server.
126 SGSocket::open( SGProtocolDir direction )
128 set_dir( direction );
130 is_server = is_tcp &&
131 (direction == SG_IO_IN || direction == SG_IO_BI);
133 if ( port_str == "" || port_str == "any" ) {
136 port = atoi( port_str.c_str() );
139 if (direction == SG_IO_IN)
141 // this means server for now
143 // Setup socket to listen on. Set "port" before making this
144 // call. A port of "0" indicates that we want to let the os
145 // pick any available port.
146 if (!make_server_socket())
148 SG_LOG( SG_IO, SG_ALERT, "SG_IO_IN socket creation failed" );
160 // Specify the maximum length of the connection queue
161 sock.listen( SG_MAX_SOCKET_QUEUE );
165 else if (direction == SG_IO_OUT)
167 // this means client for now
169 if (!make_client_socket())
171 SG_LOG( SG_IO, SG_ALERT, "SG_IO_OUT socket creation failed" );
181 else if (direction == SG_IO_BI && is_tcp)
183 // this means server for TCP sockets
185 // Setup socket to listen on. Set "port" before making this
186 // call. A port of "0" indicates that we want to let the os
187 // pick any available port.
188 if (!make_server_socket())
190 SG_LOG( SG_IO, SG_ALERT, "SG_IO_BI socket creation failed" );
194 // Specify the maximum length of the connection queue
195 sock.listen( SG_MAX_SOCKET_QUEUE );
199 SG_LOG( SG_IO, SG_ALERT,
200 "Error: bidirection mode not available for UDP sockets." );
210 // read data from socket (server)
211 // read a block of data of specified size
213 SGSocket::read( char *buf, int length )
215 if (sock.getHandle() == -1 &&
216 (client == 0 || client->getHandle() == -1))
221 // test for any input available on sock (returning immediately, even if
227 if (is_tcp && is_server)
229 result = client->recv( buf, length );
233 result = sock.recv( buf, length );
236 if ( result != length )
238 SG_LOG( SG_IO, SG_INFO,
239 "Warning: read() not enough bytes." );
247 // read a line of data, length is max size of input buffer
249 SGSocket::readline( char *buf, int length )
251 if (sock.getHandle() == -1 &&
252 (client == 0 || client->getHandle() == -1))
257 // test for any input read on sock (returning immediately, even if
259 int result = this->poll();
263 // read a chunk, keep in the save buffer until we have the
264 // requested amount read
266 if (is_tcp && is_server)
268 char *buf_ptr = save_buf + save_len;
269 result = client->recv( buf_ptr, SG_IO_MAX_MSG_SIZE - save_len );
278 // Try and detect that the remote end died. This
279 // could cause problems so if you see connections
280 // dropping for unexplained reasons, LOOK HERE!
281 if (result == 0 && save_len == 0 && first_read == true)
283 SG_LOG( SG_IO, SG_ALERT,
284 "Connection closed by foreign host." );
291 char *buf_ptr = save_buf + save_len;
292 result = sock.recv( buf_ptr, SG_IO_MAX_MSG_SIZE - save_len );
297 // look for the end of line in save_buf
299 for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i )
301 if ( save_buf[i] == '\n' ) {
304 // no end of line yet
308 // we found an end of line
310 // copy to external buffer
311 strncpy( buf, save_buf, result );
315 //memmove( save_buf+, save_buf+, ? );
316 for ( i = result; i < save_len; ++i ) {
317 save_buf[ i - result ] = save_buf[i];
325 // write data to socket (client)
327 SGSocket::write( const char *buf, const int length )
329 netSocket* s = client == 0 ? &sock : client;
330 if (s->getHandle() == -1)
335 bool error_condition = false;
337 if ( s->send( buf, length ) < 0 )
339 SG_LOG( SG_IO, SG_WARN, "Error writing to socket: " << port );
340 error_condition = true;
343 if ( error_condition ) {
351 // write null terminated string to socket (server)
353 SGSocket::writestring( const char *str )
355 int length = strlen( str );
356 return this->write( str, length );
372 // configure the socket as non-blocking
376 if (sock.getHandle() == -1) {
380 sock.setBlocking( false );
387 netSocket* readers[2];
389 readers[0] = client != 0 ? client : &sock;
392 netSocket* writers[1];
395 int result = netSocket::select( readers, writers, timeout );
397 if (result > 0 && is_server && client == 0)
399 // Accept a new client connection
401 int new_fd = sock.accept( &addr );
402 SG_LOG( SG_IO, SG_INFO, "Accepted connection from "
403 << addr.getHost() << ":" << addr.getPort() );
404 client = new netSocket();
405 client->setHandle( new_fd );