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 <simgear/debug/logstream.hxx>
34 #include "sg_socket.hxx"
36 bool SGSocket::init = false;
38 SGSocket::SGSocket( const string& host, const string& port_,
39 const string& style ) :
51 simgear::Socket::initSockets();
59 else if ( style != "udp" )
61 SG_LOG( SG_IO, SG_ALERT,
62 "Error: SGSocket() unknown style = " << style );
65 set_type( sgSocketType );
76 SGSocket::make_server_socket()
78 if (!sock.open( is_tcp ))
80 SG_LOG( SG_IO, SG_ALERT,
81 "Error: socket() failed in make_server_socket()" );
85 if (sock.bind( "", port ) < 0)
87 SG_LOG( SG_IO, SG_ALERT,
88 "Error: bind() failed in make_server_socket()" );
98 SGSocket::make_client_socket()
100 if (!sock.open( is_tcp ))
102 SG_LOG( SG_IO, SG_ALERT,
103 "Error: socket() failed in make_client_socket()" );
107 if (sock.connect( hostname.c_str(), port ) < 0)
109 SG_LOG( SG_IO, SG_ALERT,
110 "Error: connect() failed in make_client_socket()" );
118 // If specified as a server (in direction for now) open the master
119 // listening socket. If specified as a client (out direction), open a
120 // connection to a server.
122 SGSocket::open( SGProtocolDir direction )
124 set_dir( direction );
126 is_server = is_tcp &&
127 (direction == SG_IO_IN || direction == SG_IO_BI);
129 if ( port_str == "" || port_str == "any" ) {
132 port = atoi( port_str.c_str() );
135 if (direction == SG_IO_IN)
137 // this means server for now
139 // Setup socket to listen on. Set "port" before making this
140 // call. A port of "0" indicates that we want to let the os
141 // pick any available port.
142 if (!make_server_socket())
144 SG_LOG( SG_IO, SG_ALERT, "SG_IO_IN socket creation failed" );
156 // Specify the maximum length of the connection queue
157 sock.listen( SG_MAX_SOCKET_QUEUE );
161 else if (direction == SG_IO_OUT)
163 // this means client for now
165 if (!make_client_socket())
167 SG_LOG( SG_IO, SG_ALERT, "SG_IO_OUT socket creation failed" );
177 else if (direction == SG_IO_BI && is_tcp)
179 // this means server for TCP sockets
181 // Setup socket to listen on. Set "port" before making this
182 // call. A port of "0" indicates that we want to let the os
183 // pick any available port.
184 if (!make_server_socket())
186 SG_LOG( SG_IO, SG_ALERT, "SG_IO_BI socket creation failed" );
190 // Specify the maximum length of the connection queue
191 sock.listen( SG_MAX_SOCKET_QUEUE );
195 SG_LOG( SG_IO, SG_ALERT,
196 "Error: bidirection mode not available for UDP sockets." );
206 // read data from socket (server)
207 // read a block of data of specified size
209 SGSocket::read( char *buf, int length )
211 if (sock.getHandle() == -1 &&
212 (client == 0 || client->getHandle() == -1))
217 // test for any input available on sock (returning immediately, even if
223 if (is_tcp && is_server)
225 result = client->recv( buf, length );
229 result = sock.recv( buf, length );
232 if ( result != length )
234 SG_LOG( SG_IO, SG_INFO,
235 "Warning: read() not enough bytes." );
243 // read a line of data, length is max size of input buffer
245 SGSocket::readline( char *buf, int length )
247 if (sock.getHandle() == -1 &&
248 (client == 0 || client->getHandle() == -1))
253 // test for any input read on sock (returning immediately, even if
255 int result = this->poll();
259 // read a chunk, keep in the save buffer until we have the
260 // requested amount read
262 if (is_tcp && is_server)
264 char *buf_ptr = save_buf + save_len;
265 result = client->recv( buf_ptr, SG_IO_MAX_MSG_SIZE - save_len );
274 // Try and detect that the remote end died. This
275 // could cause problems so if you see connections
276 // dropping for unexplained reasons, LOOK HERE!
277 if (result == 0 && save_len == 0 && first_read == true)
279 SG_LOG( SG_IO, SG_ALERT,
280 "Connection closed by foreign host." );
287 char *buf_ptr = save_buf + save_len;
288 result = sock.recv( buf_ptr, SG_IO_MAX_MSG_SIZE - save_len );
293 // look for the end of line in save_buf
295 for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i )
297 if ( save_buf[i] == '\n' ) {
300 // no end of line yet
304 // we found an end of line
306 // copy to external buffer
307 strncpy( buf, save_buf, result );
311 //memmove( save_buf+, save_buf+, ? );
312 for ( i = result; i < save_len; ++i ) {
313 save_buf[ i - result ] = save_buf[i];
321 // write data to socket (client)
323 SGSocket::write( const char *buf, const int length )
325 simgear::Socket* s = client == 0 ? &sock : client;
326 if (s->getHandle() == -1)
331 bool error_condition = false;
333 if ( s->send( buf, length ) < 0 )
335 SG_LOG( SG_IO, SG_WARN, "Error writing to socket: " << port );
336 error_condition = true;
339 if ( error_condition ) {
347 // write null terminated string to socket (server)
349 SGSocket::writestring( const char *str )
351 int length = strlen( str );
352 return this->write( str, length );
368 // configure the socket as non-blocking
372 if (sock.getHandle() == -1) {
376 sock.setBlocking( false );
383 simgear::Socket* readers[2];
385 readers[0] = client != 0 ? client : &sock;
388 simgear::Socket* writers[1];
391 int result = simgear::Socket::select( readers, writers, timeout );
393 if (result > 0 && is_server && client == 0)
395 // Accept a new client connection
396 simgear::IPAddress addr;
397 int new_fd = sock.accept( &addr );
398 SG_LOG( SG_IO, SG_INFO, "Accepted connection from "
399 << addr.getHost() << ":" << addr.getPort() );
400 client = new simgear::Socket();
401 client->setHandle( new_fd );