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 - curt@flightgear.org
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>
31 #include <simgear/debug/logstream.hxx>
33 #include "sg_socket.hxx"
35 bool SGSocket::init = false;
37 SGSocket::SGSocket( const string& host, const string& port_,
38 const string& style ) :
49 netInit(NULL, NULL); // plib-1.4.2 compatible
57 else if ( style != (string)"udp" )
59 SG_LOG( SG_IO, SG_ALERT,
60 "Error: SGSocket() unknown style = " << style );
63 set_type( sgSocketType );
74 SGSocket::make_server_socket()
76 if (!sock.open( is_tcp ))
78 SG_LOG( SG_IO, SG_ALERT,
79 "Error: socket() failed in make_server_socket()" );
83 if (sock.bind( "", port ) < 0)
85 SG_LOG( SG_IO, SG_ALERT,
86 "Error: bind() failed in make_server_socket()" );
96 SGSocket::make_client_socket()
98 if (!sock.open( is_tcp ))
100 SG_LOG( SG_IO, SG_ALERT,
101 "Error: socket() failed in make_client_socket()" );
105 if (sock.connect( hostname.c_str(), port ) < 0)
107 SG_LOG( SG_IO, SG_ALERT,
108 "Error: connect() failed in make_client_socket()" );
116 // If specified as a server (in direction for now) open the master
117 // listening socket. If specified as a client (out direction), open a
118 // connection to a server.
120 SGSocket::open( SGProtocolDir direction )
122 set_dir( direction );
124 is_server = is_tcp &&
125 (direction == SG_IO_IN || direction == SG_IO_BI);
127 if ( port_str == "" || port_str == "any" ) {
130 port = atoi( port_str.c_str() );
133 if (direction == SG_IO_IN)
135 // this means server for now
137 // Setup socket to listen on. Set "port" before making this
138 // call. A port of "0" indicates that we want to let the os
139 // pick any available port.
140 if (!make_server_socket())
142 SG_LOG( SG_IO, SG_ALERT, "SG_IO_IN socket creation failed" );
154 // Specify the maximum length of the connection queue
155 sock.listen( SG_MAX_SOCKET_QUEUE );
159 else if (direction == SG_IO_OUT)
161 // this means client for now
163 if (!make_client_socket())
165 SG_LOG( SG_IO, SG_ALERT, "SG_IO_OUT socket creation failed" );
175 else if (direction == SG_IO_BI && is_tcp)
177 // this means server for TCP sockets
179 // Setup socket to listen on. Set "port" before making this
180 // call. A port of "0" indicates that we want to let the os
181 // pick any available port.
182 if (!make_server_socket())
184 SG_LOG( SG_IO, SG_ALERT, "SG_IO_BI socket creation failed" );
188 // Specify the maximum length of the connection queue
189 sock.listen( SG_MAX_SOCKET_QUEUE );
193 SG_LOG( SG_IO, SG_ALERT,
194 "Error: bidirection mode not available for UDP sockets." );
204 // read data from socket (server)
205 // read a block of data of specified size
207 SGSocket::read( char *buf, int length )
209 if (sock.getHandle() == -1 &&
210 (client == 0 || client->getHandle() == -1))
215 // test for any input available on sock (returning immediately, even if
221 result = sock.recv( buf, length );
223 if ( result != length )
225 SG_LOG( SG_IO, SG_INFO,
226 "Warning: read() not enough bytes." );
234 // read a line of data, length is max size of input buffer
236 SGSocket::readline( char *buf, int length )
238 if (sock.getHandle() == -1 &&
239 (client == 0 || client->getHandle() == -1))
244 // test for any input read on sock (returning immediately, even if
246 int result = this->poll();
250 // read a chunk, keep in the save buffer until we have the
251 // requested amount read
255 char *buf_ptr = save_buf + save_len;
256 result = client->recv( buf_ptr, SG_IO_MAX_MSG_SIZE - save_len );
265 // Try and detect that the remote end died. This
266 // could cause problems so if you see connections
267 // dropping for unexplained reasons, LOOK HERE!
268 if (result == 0 && save_len == 0 && first_read == true)
270 SG_LOG( SG_IO, SG_ALERT,
271 "Connection closed by foreign host." );
278 char *buf_ptr = save_buf + save_len;
279 result = sock.recv( buf_ptr, SG_IO_MAX_MSG_SIZE - save_len );
284 // look for the end of line in save_buf
286 for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i )
288 if ( save_buf[i] == '\n' ) {
291 // no end of line yet
295 // we found an end of line
297 // copy to external buffer
298 strncpy( buf, save_buf, result );
302 //memmove( save_buf+, save_buf+, ? );
303 for ( i = result; i < save_len; ++i ) {
304 save_buf[ i - result ] = save_buf[i];
312 // write data to socket (client)
314 SGSocket::write( const char *buf, const int length )
316 netSocket* s = client == 0 ? &sock : client;
317 if (s->getHandle() == -1)
322 bool error_condition = false;
324 if ( s->send( buf, length ) < 0 )
326 SG_LOG( SG_IO, SG_ALERT, "Error writing to socket: " << port );
327 error_condition = true;
330 if ( error_condition ) {
338 // write null terminated string to socket (server)
340 SGSocket::writestring( const char *str )
342 int length = strlen( str );
343 return this->write( str, length );
359 // configure the socket as non-blocking
363 if (sock.getHandle() == -1) {
367 sock.setBlocking( false );
374 netSocket* readers[2];
376 readers[0] = client != 0 ? client : &sock;
379 netSocket* writers[1];
382 int result = netSocket::select( readers, writers, 0 );
384 if (result > 0 && is_server && client == 0)
386 // Accept a new client connection
388 int new_fd = sock.accept( &addr );
389 SG_LOG( SG_IO, SG_INFO, "Accepted connection from "
390 << addr.getHost() << ":" << addr.getPort() );
391 client = new netSocket();
392 client->setHandle( new_fd );