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 ) :
50 netInit(NULL, NULL); // plib-1.4.2 compatible
58 else if ( style != "udp" )
60 SG_LOG( SG_IO, SG_ALERT,
61 "Error: SGSocket() unknown style = " << style );
64 set_type( sgSocketType );
75 SGSocket::make_server_socket()
77 if (!sock.open( is_tcp ))
79 SG_LOG( SG_IO, SG_ALERT,
80 "Error: socket() failed in make_server_socket()" );
84 if (sock.bind( "", port ) < 0)
86 SG_LOG( SG_IO, SG_ALERT,
87 "Error: bind() failed in make_server_socket()" );
97 SGSocket::make_client_socket()
99 if (!sock.open( is_tcp ))
101 SG_LOG( SG_IO, SG_ALERT,
102 "Error: socket() failed in make_client_socket()" );
106 if (sock.connect( hostname.c_str(), port ) < 0)
108 SG_LOG( SG_IO, SG_ALERT,
109 "Error: connect() failed in make_client_socket()" );
117 // If specified as a server (in direction for now) open the master
118 // listening socket. If specified as a client (out direction), open a
119 // connection to a server.
121 SGSocket::open( SGProtocolDir direction )
123 set_dir( direction );
125 is_server = is_tcp &&
126 (direction == SG_IO_IN || direction == SG_IO_BI);
128 if ( port_str == "" || port_str == "any" ) {
131 port = atoi( port_str.c_str() );
134 if (direction == SG_IO_IN)
136 // this means server for now
138 // Setup socket to listen on. Set "port" before making this
139 // call. A port of "0" indicates that we want to let the os
140 // pick any available port.
141 if (!make_server_socket())
143 SG_LOG( SG_IO, SG_ALERT, "SG_IO_IN socket creation failed" );
155 // Specify the maximum length of the connection queue
156 sock.listen( SG_MAX_SOCKET_QUEUE );
160 else if (direction == SG_IO_OUT)
162 // this means client for now
164 if (!make_client_socket())
166 SG_LOG( SG_IO, SG_ALERT, "SG_IO_OUT socket creation failed" );
176 else if (direction == SG_IO_BI && is_tcp)
178 // this means server for TCP sockets
180 // Setup socket to listen on. Set "port" before making this
181 // call. A port of "0" indicates that we want to let the os
182 // pick any available port.
183 if (!make_server_socket())
185 SG_LOG( SG_IO, SG_ALERT, "SG_IO_BI socket creation failed" );
189 // Specify the maximum length of the connection queue
190 sock.listen( SG_MAX_SOCKET_QUEUE );
194 SG_LOG( SG_IO, SG_ALERT,
195 "Error: bidirection mode not available for UDP sockets." );
205 // read data from socket (server)
206 // read a block of data of specified size
208 SGSocket::read( char *buf, int length )
210 if (sock.getHandle() == -1 &&
211 (client == 0 || client->getHandle() == -1))
216 // test for any input available on sock (returning immediately, even if
222 if (is_tcp && is_server)
224 result = client->recv( buf, length );
228 result = sock.recv( buf, length );
231 if ( result != length )
233 SG_LOG( SG_IO, SG_INFO,
234 "Warning: read() not enough bytes." );
242 // read a line of data, length is max size of input buffer
244 SGSocket::readline( char *buf, int length )
246 if (sock.getHandle() == -1 &&
247 (client == 0 || client->getHandle() == -1))
252 // test for any input read on sock (returning immediately, even if
254 int result = this->poll();
258 // read a chunk, keep in the save buffer until we have the
259 // requested amount read
261 if (is_tcp && is_server)
263 char *buf_ptr = save_buf + save_len;
264 result = client->recv( buf_ptr, SG_IO_MAX_MSG_SIZE - save_len );
273 // Try and detect that the remote end died. This
274 // could cause problems so if you see connections
275 // dropping for unexplained reasons, LOOK HERE!
276 if (result == 0 && save_len == 0 && first_read == true)
278 SG_LOG( SG_IO, SG_ALERT,
279 "Connection closed by foreign host." );
286 char *buf_ptr = save_buf + save_len;
287 result = sock.recv( buf_ptr, SG_IO_MAX_MSG_SIZE - save_len );
292 // look for the end of line in save_buf
294 for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i )
296 if ( save_buf[i] == '\n' ) {
299 // no end of line yet
303 // we found an end of line
305 // copy to external buffer
306 strncpy( buf, save_buf, result );
310 //memmove( save_buf+, save_buf+, ? );
311 for ( i = result; i < save_len; ++i ) {
312 save_buf[ i - result ] = save_buf[i];
320 // write data to socket (client)
322 SGSocket::write( const char *buf, const int length )
324 netSocket* s = client == 0 ? &sock : client;
325 if (s->getHandle() == -1)
330 bool error_condition = false;
332 if ( s->send( buf, length ) < 0 )
334 SG_LOG( SG_IO, SG_ALERT, "Error writing to socket: " << port );
335 error_condition = true;
338 if ( error_condition ) {
346 // write null terminated string to socket (server)
348 SGSocket::writestring( const char *str )
350 int length = strlen( str );
351 return this->write( str, length );
367 // configure the socket as non-blocking
371 if (sock.getHandle() == -1) {
375 sock.setBlocking( false );
382 netSocket* readers[2];
384 readers[0] = client != 0 ? client : &sock;
387 netSocket* writers[1];
390 int result = netSocket::select( readers, writers, timeout );
392 if (result > 0 && is_server && client == 0)
394 // Accept a new client connection
396 int new_fd = sock.accept( &addr );
397 SG_LOG( SG_IO, SG_INFO, "Accepted connection from "
398 << addr.getHost() << ":" << addr.getPort() );
399 client = new netSocket();
400 client->setHandle( new_fd );