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>
32 #include <cstdlib> // for atoi
34 #include <simgear/debug/logstream.hxx>
36 #include "sg_socket.hxx"
38 bool SGSocket::init = false;
41 SGSocket::SGSocket( const string& host, const string& port_,
42 const string& style ) :
54 simgear::Socket::initSockets();
62 else if ( style != "udp" )
64 SG_LOG( SG_IO, SG_ALERT,
65 "Error: SGSocket() unknown style = " << style );
68 set_type( sgSocketType );
79 SGSocket::make_server_socket()
81 if (!sock.open( is_tcp ))
83 SG_LOG( SG_IO, SG_ALERT,
84 "Error: socket() failed in make_server_socket()" );
88 if (sock.bind( hostname.c_str(), port ) < 0)
90 SG_LOG( SG_IO, SG_ALERT,
91 "Error: bind() failed in make_server_socket()" );
101 SGSocket::make_client_socket()
103 if (!sock.open( is_tcp ))
105 SG_LOG( SG_IO, SG_ALERT,
106 "Error: socket() failed in make_client_socket()" );
110 if (sock.connect( hostname.c_str(), port ) < 0)
112 SG_LOG( SG_IO, SG_ALERT,
113 "Error: connect() failed in make_client_socket()" );
121 // If specified as a server (in direction for now) open the master
122 // listening socket. If specified as a client (out direction), open a
123 // connection to a server.
125 SGSocket::open( SGProtocolDir direction )
127 set_dir( direction );
129 is_server = is_tcp &&
130 (direction == SG_IO_IN || direction == SG_IO_BI);
132 if ( port_str == "" || port_str == "any" ) {
135 port = atoi( port_str.c_str() );
138 if (direction == SG_IO_IN)
140 // this means server for now
142 // Setup socket to listen on. Set "port" before making this
143 // call. A port of "0" indicates that we want to let the os
144 // pick any available port.
145 if (!make_server_socket())
147 SG_LOG( SG_IO, SG_ALERT, "SG_IO_IN socket creation failed" );
159 // Specify the maximum length of the connection queue
160 sock.listen( SG_MAX_SOCKET_QUEUE );
164 else if (direction == SG_IO_OUT)
166 // this means client for now
168 if (!make_client_socket())
170 SG_LOG( SG_IO, SG_ALERT, "SG_IO_OUT socket creation failed" );
180 else if (direction == SG_IO_BI && is_tcp)
182 // this means server for TCP sockets
184 // Setup socket to listen on. Set "port" before making this
185 // call. A port of "0" indicates that we want to let the os
186 // pick any available port.
187 if (!make_server_socket())
189 SG_LOG( SG_IO, SG_ALERT, "SG_IO_BI socket creation failed" );
193 // Specify the maximum length of the connection queue
194 sock.listen( SG_MAX_SOCKET_QUEUE );
198 SG_LOG( SG_IO, SG_ALERT,
199 "Error: bidirection mode not available for UDP sockets." );
209 // read data from socket (server)
210 // read a block of data of specified size
212 SGSocket::read( char *buf, int length )
214 if (sock.getHandle() == -1 &&
215 (client == 0 || client->getHandle() == -1))
220 // test for any input available on sock (returning immediately, even if
226 if (is_tcp && is_server)
228 result = client->recv( buf, length );
232 result = sock.recv( buf, length );
235 if ( result != length )
237 SG_LOG( SG_IO, SG_INFO,
238 "Warning: read() not enough bytes." );
246 // read a line of data, length is max size of input buffer
248 SGSocket::readline( char *buf, int length )
250 if (sock.getHandle() == -1 &&
251 (client == 0 || client->getHandle() == -1))
256 // test for any input read on sock (returning immediately, even if
258 int result = this->poll();
262 // read a chunk, keep in the save buffer until we have the
263 // requested amount read
265 if (is_tcp && is_server)
267 char *buf_ptr = save_buf + save_len;
268 result = client->recv( buf_ptr, SG_IO_MAX_MSG_SIZE - save_len );
277 // Try and detect that the remote end died. This
278 // could cause problems so if you see connections
279 // dropping for unexplained reasons, LOOK HERE!
280 if (result == 0 && save_len == 0 && first_read == true)
282 SG_LOG( SG_IO, SG_ALERT,
283 "Connection closed by foreign host." );
290 char *buf_ptr = save_buf + save_len;
291 result = sock.recv( buf_ptr, SG_IO_MAX_MSG_SIZE - save_len );
296 // look for the end of line in save_buf
298 for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i )
300 if (( i < save_len ) && ( save_buf[i] == '\n' )) {
303 // no end of line yet
307 // we found an end of line
310 int copy_length = result;
311 if (copy_length >= length) {
312 SG_LOG( SG_IO, SG_ALERT,
313 "Alert: readline() has line exceeding the buffer size." );
314 copy_length = length-1;
316 // copy to external buffer
317 strncpy( buf, save_buf, copy_length );
318 buf[copy_length] = '\0';
321 //memmove( save_buf+, save_buf+, ? );
322 for ( i = result; i < save_len; ++i ) {
323 save_buf[ i - result ] = save_buf[i];
331 // write data to socket (client)
333 SGSocket::write( const char *buf, const int length )
335 simgear::Socket* s = client == 0 ? &sock : client;
336 if (s->getHandle() == -1)
341 bool error_condition = false;
343 if ( s->send( buf, length ) < 0 )
345 SG_LOG( SG_IO, SG_WARN, "Error writing to socket: " << port );
346 error_condition = true;
349 if ( error_condition ) {
357 // write null terminated string to socket (server)
359 SGSocket::writestring( const char *str )
361 int length = strlen( str );
362 return this->write( str, length );
378 // configure the socket as non-blocking
382 if (sock.getHandle() == -1) {
386 sock.setBlocking( false );
393 simgear::Socket* readers[2];
395 readers[0] = client != 0 ? client : &sock;
398 simgear::Socket* writers[1];
401 int result = simgear::Socket::select( readers, writers, timeout );
403 if (result > 0 && is_server && client == 0)
405 // Accept a new client connection
406 simgear::IPAddress addr;
407 int new_fd = sock.accept( &addr );
408 SG_LOG( SG_IO, SG_INFO, "Accepted connection from "
409 << addr.getHost() << ":" << addr.getPort() );
410 client = new simgear::Socket();
411 client->setHandle( new_fd );