From 4c0a3b3db43f5be847129b77d71f35036c452ad1 Mon Sep 17 00:00:00 2001 From: curt Date: Tue, 11 Jul 2000 20:40:12 +0000 Subject: [PATCH] Moved iochannel stuff over into SimGear. --- src/Main/Makefile.am | 6 +- src/Main/fg_io.cxx | 16 +- src/Main/fg_io.hxx | 4 - src/Network/Makefile.am | 4 - src/Network/fg_file.cxx | 155 ---------------- src/Network/fg_file.hxx | 84 --------- src/Network/fg_serial.cxx | 155 ---------------- src/Network/fg_serial.hxx | 91 --------- src/Network/fg_socket.cxx | 377 -------------------------------------- src/Network/fg_socket.hxx | 98 ---------- src/Network/garmin.cxx | 12 +- src/Network/iochannel.cxx | 74 -------- src/Network/iochannel.hxx | 72 -------- src/Network/joyclient.cxx | 14 +- src/Network/native.cxx | 14 +- src/Network/nmea.cxx | 12 +- src/Network/protocol.cxx | 6 +- src/Network/protocol.hxx | 29 +-- src/Network/pve.cxx | 8 +- src/Network/ray.cxx | 8 +- src/Network/rul.cxx | 8 +- 21 files changed, 61 insertions(+), 1186 deletions(-) delete mode 100644 src/Network/fg_file.cxx delete mode 100644 src/Network/fg_file.hxx delete mode 100644 src/Network/fg_serial.cxx delete mode 100644 src/Network/fg_serial.hxx delete mode 100644 src/Network/fg_socket.cxx delete mode 100644 src/Network/fg_socket.hxx delete mode 100644 src/Network/iochannel.cxx delete mode 100644 src/Network/iochannel.hxx diff --git a/src/Main/Makefile.am b/src/Main/Makefile.am index 705c7aba4..169ed434c 100644 --- a/src/Main/Makefile.am +++ b/src/Main/Makefile.am @@ -43,8 +43,6 @@ fgfs_SOURCES = \ splash.cxx splash.hxx \ views.cxx views.hxx -# fg_serial.cxx fg_serial.hxx \ - fgfs_LDADD = \ $(top_builddir)/src/Aircraft/libAircraft.a \ $(top_builddir)/src/Autopilot/libAutopilot.a \ @@ -65,9 +63,9 @@ fgfs_LDADD = \ $(top_builddir)/src/Time/libTime.a \ $(WEATHER_LIBS) \ $(top_builddir)/src/Joystick/libJoystick.a \ - $(SERIAL_LIBS) \ - -lsgsky -lsgephem -lsgtiming -lsgscreen -lsgmath -lsgbucket \ + -lsgsky -lsgephem -lsgtiming -lsgio -lsgscreen -lsgmath -lsgbucket \ -lsgdebug -lsgmagvar -lsgmisc \ + $(SERIAL_LIBS) \ -lplibpu -lplibfnt -lplibssg -lplibsg \ -lmk4 -lz \ $(opengl_LIBS) \ diff --git a/src/Main/fg_io.cxx b/src/Main/fg_io.cxx index a54f4c0ca..c3859db6e 100644 --- a/src/Main/fg_io.cxx +++ b/src/Main/fg_io.cxx @@ -26,15 +26,15 @@ #include STL_STRING #include +#include +#include +#include +#include + #include #include
-#include -#include -#include -#include - #include #include #include @@ -132,7 +132,7 @@ static FGProtocol *parse_port_config( const string& config ) FG_LOG( FG_IO, FG_INFO, " hertz = " << hertz ); if ( medium == "serial" ) { - FGSerial *ch = new FGSerial; + SGSerial *ch = new SGSerial; io->set_io_channel( ch ); // device name @@ -151,14 +151,14 @@ static FGProtocol *parse_port_config( const string& config ) io->set_io_channel( ch ); } else if ( medium == "file" ) { - FGFile *ch = new FGFile; + SGFile *ch = new SGFile; io->set_io_channel( ch ); // file name ch->set_file_name( config.substr(begin) ); FG_LOG( FG_IO, FG_INFO, " file name = " << ch->get_file_name() ); } else if ( medium == "socket" ) { - FGSocket *ch = new FGSocket; + SGSocket *ch = new SGSocket; io->set_io_channel( ch ); // hostname diff --git a/src/Main/fg_io.hxx b/src/Main/fg_io.hxx index b92a50a84..6f7f55cb1 100644 --- a/src/Main/fg_io.hxx +++ b/src/Main/fg_io.hxx @@ -27,8 +27,6 @@ #include -#include - // initialize I/O channels based on command line options (if any) void fgIOInit(); @@ -39,5 +37,3 @@ void fgIOProcess(); #endif // _FG_IO_HXX - - diff --git a/src/Network/Makefile.am b/src/Network/Makefile.am index 3f200aea4..305c3363a 100644 --- a/src/Network/Makefile.am +++ b/src/Network/Makefile.am @@ -1,10 +1,6 @@ noinst_LIBRARIES = libNetwork.a libNetwork_a_SOURCES = \ - iochannel.cxx iochannel.hxx \ - fg_file.cxx fg_file.hxx \ - fg_serial.cxx fg_serial.hxx \ - fg_socket.cxx fg_socket.hxx \ protocol.cxx protocol.hxx \ native.cxx native.hxx \ garmin.cxx garmin.hxx \ diff --git a/src/Network/fg_file.cxx b/src/Network/fg_file.cxx deleted file mode 100644 index e8e0f3642..000000000 --- a/src/Network/fg_file.cxx +++ /dev/null @@ -1,155 +0,0 @@ -// fg_file.cxx -- File I/O routines -// -// Written by Curtis Olson, started November 1999. -// -// Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -// -// $Id$ - - -#include - -#include STL_STRING - -#ifdef _MSC_VER -# include -#endif - -#include - -#include "fg_file.hxx" - -FG_USING_STD(string); - - -FGFile::FGFile() { - set_type( fgFileType ); -} - - -FGFile::~FGFile() { -} - - -// open the file based on specified direction -bool FGFile::open( FGProtocol::fgProtocolDir dir ) { - if ( dir == FGProtocol::out ) { -#ifdef _MSC_VER - fp = _open( file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC, - 00666 ); -#else - fp = std::open( file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC, - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | - S_IROTH | S_IWOTH ); -#endif - } else if ( dir == FGProtocol::in ) { -#ifdef _MSC_VER - fp = _open( file_name.c_str(), O_RDONLY ); -#else - fp = std::open( file_name.c_str(), O_RDONLY ); -#endif - } else { - FG_LOG( FG_IO, FG_ALERT, - "Error: bidirection mode not available for files." ); - return false; - } - - if ( fp == -1 ) { - FG_LOG( FG_IO, FG_ALERT, "Error opening file: " << file_name ); - return false; - } - - return true; -} - - -// read a block of data of specified size -int FGFile::read( char *buf, int length ) { - // read a chunk -#ifdef _MSC_VER - int result = _read( fp, buf, length ); -#else - int result = std::read( fp, buf, length ); -#endif - - return result; -} - - -// read a line of data, length is max size of input buffer -int FGFile::readline( char *buf, int length ) { - // save our current position - int pos = lseek( fp, 0, SEEK_CUR ); - - // read a chunk -#ifdef _MSC_VER - int result = _read( fp, buf, length ); -#else - int result = std::read( fp, buf, length ); -#endif - - // find the end of line and reset position - int i; - for ( i = 0; i < result && buf[i] != '\n'; ++i ); - if ( buf[i] == '\n' ) { - result = i + 1; - } else { - result = i; - } - lseek( fp, pos + result, SEEK_SET ); - - // just in case ... - buf[ result ] = '\0'; - - return result; -} - - -// write data to a file -int FGFile::write( char *buf, int length ) { -#ifdef _MSC_VER - int result = _write( fp, buf, length ); -#else - int result = std::write( fp, buf, length ); -#endif - if ( result != length ) { - FG_LOG( FG_IO, FG_ALERT, "Error writing data: " << file_name ); - } - - return result; -} - - -// write null terminated string to a file -int FGFile::writestring( char *str ) { - int length = strlen( str ); - return write( str, length ); -} - - -// close the port -bool FGFile::close() { -#ifdef _MSC_VER - if ( _close( fp ) == -1 ) { -#else - if ( std::close( fp ) == -1 ) { -#endif - return false; - } - - return true; -} diff --git a/src/Network/fg_file.hxx b/src/Network/fg_file.hxx deleted file mode 100644 index 4029825a5..000000000 --- a/src/Network/fg_file.hxx +++ /dev/null @@ -1,84 +0,0 @@ -// fg_file.hxx -- File I/O routines -// -// Written by Curtis Olson, started November 1999. -// -// Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -// -// $Id$ - - -#ifndef _FG_FILE_HXX -#define _FG_FILE_HXX - - -#ifndef __cplusplus -# error This library requires C++ -#endif - -#include - -#include - -#include // for open(), read(), write(), close() -#include // for open(), read(), write(), close() -#include // for open(), read(), write(), close() -#if !defined( _MSC_VER ) -# include // for open(), read(), write(), close() -#endif - -#include "iochannel.hxx" -#include "protocol.hxx" - -FG_USING_STD(string); - - -class FGFile : public FGIOChannel { - - string file_name; - int fp; - -public: - - FGFile(); - ~FGFile(); - - // open the file based on specified direction - bool open( FGProtocol::fgProtocolDir dir ); - - // read a block of data of specified size - int read( char *buf, int length ); - - // read a line of data, length is max size of input buffer - int readline( char *buf, int length ); - - // write data to a file - int write( char *buf, int length ); - - // write null terminated string to a file - int writestring( char *str ); - - // close file - bool close(); - - inline string get_file_name() const { return file_name; } - inline void set_file_name( const string& fn ) { file_name = fn; } -}; - - -#endif // _FG_FILE_HXX - - diff --git a/src/Network/fg_serial.cxx b/src/Network/fg_serial.cxx deleted file mode 100644 index 6fae04c8c..000000000 --- a/src/Network/fg_serial.cxx +++ /dev/null @@ -1,155 +0,0 @@ -// fg_serial.cxx -- Serial I/O routines -// -// Written by Curtis Olson, started November 1999. -// -// Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -// -// $Id$ - - -#include - -#include STL_STRING - -#include -#include - -#include - -#include "fg_serial.hxx" - -FG_USING_STD(string); - - -FGSerial::FGSerial() : - save_len(0) -{ - set_type( fgSerialType ); -} - - -FGSerial::~FGSerial() { -} - - -// open the serial port based on specified direction -bool FGSerial::open( FGProtocol::fgProtocolDir dir ) { - if ( ! port.open_port( device ) ) { - FG_LOG( FG_IO, FG_ALERT, "Error opening device: " << device ); - return false; - } - - // cout << "fd = " << port.fd << endl; - - if ( ! port.set_baud( atoi( baud.c_str() ) ) ) { - FG_LOG( FG_IO, FG_ALERT, "Error setting baud: " << baud ); - return false; - } - - return true; -} - - -// Read data from port. If we don't get enough data, save what we did -// get in the save buffer and return 0. The save buffer will be -// prepended to subsequent reads until we get as much as is requested. - -int FGSerial::read( char *buf, int length ) { - int result; - - // read a chunk, keep in the save buffer until we have the - // requested amount read - - char *buf_ptr = save_buf + save_len; - result = port.read_port( buf_ptr, length - save_len ); - - if ( result + save_len == length ) { - strncpy( buf, save_buf, length ); - save_len = 0; - - return length; - } - - return 0; -} - - -// read data from port -int FGSerial::readline( char *buf, int length ) { - int result; - - // read a chunk, keep in the save buffer until we have the - // requested amount read - - char *buf_ptr = save_buf + save_len; - result = port.read_port( buf_ptr, FG_MAX_MSG_SIZE - save_len ); - save_len += result; - - // look for the end of line in save_buf - int i; - for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i ); - if ( save_buf[i] == '\n' ) { - result = i + 1; - } else { - // no end of line yet - return 0; - } - - // we found an end of line - - // copy to external buffer - strncpy( buf, save_buf, result ); - buf[result] = '\0'; - FG_LOG( FG_IO, FG_INFO, "fg_serial line = " << buf ); - - // shift save buffer - for ( i = result; i < save_len; ++i ) { - save_buf[ i - result ] = save_buf[i]; - } - save_len -= result; - - return result; -} - - -// write data to port -int FGSerial::write( char *buf, int length ) { - int result = port.write_port( buf, length ); - - if ( result != length ) { - FG_LOG( FG_IO, FG_ALERT, "Error writing data: " << device ); - } - - return result; -} - - -// write null terminated string to port -int FGSerial::writestring( char *str ) { - int length = strlen( str ); - return write( str, length ); -} - - -// close the port -bool FGSerial::close() { - if ( ! port.close_port() ) { - return false; - } - - return true; -} diff --git a/src/Network/fg_serial.hxx b/src/Network/fg_serial.hxx deleted file mode 100644 index f95b813d1..000000000 --- a/src/Network/fg_serial.hxx +++ /dev/null @@ -1,91 +0,0 @@ -// fg_serial.hxx -- Serial I/O routines -// -// Written by Curtis Olson, started November 1999. -// -// Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -// -// $Id$ - - -#ifndef _FG_SERIAL_HXX -#define _FG_SERIAL_HXX - - -#ifndef __cplusplus -# error This library requires C++ -#endif - -#include - -#include - -// #ifdef FG_HAVE_STD_INCLUDES -// # include -// #else -// # include -// #endif - -#include - -#include "iochannel.hxx" -#include "protocol.hxx" - -FG_USING_STD(string); - - -class FGSerial : public FGIOChannel { - - string device; - string baud; - FGSerialPort port; - - char save_buf[ 2 * FG_MAX_MSG_SIZE ]; - int save_len; - -public: - - FGSerial(); - ~FGSerial(); - - // open the serial port based on specified direction - bool open( FGProtocol::fgProtocolDir dir ); - - // read a block of data of specified size - int read( char *buf, int length ); - - // read a line of data, length is max size of input buffer - int readline( char *buf, int length ); - - // write data to port - int write( char *buf, int length ); - - // write null terminated string to port - int writestring( char *str ); - - // close port - bool close(); - - inline string get_device() const { return device; } - inline void set_device( const string& d ) { device = d; } - inline string get_baud() const { return baud; } - inline void set_baud( const string& b ) { baud = b; } -}; - - -#endif // _FG_SERIAL_HXX - - diff --git a/src/Network/fg_socket.cxx b/src/Network/fg_socket.cxx deleted file mode 100644 index e57b09ad0..000000000 --- a/src/Network/fg_socket.cxx +++ /dev/null @@ -1,377 +0,0 @@ -// fg_socket.cxx -- Socket I/O routines -// -// Written by Curtis Olson, started November 1999. -// -// Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -// -// $Id$ - - -#include - -#if ! defined( _MSC_VER ) -# include // select() -# include // socket(), bind(), select(), accept() -# include // socket(), bind(), listen(), accept() -# include // struct sockaddr_in -# include // gethostbyname() -# include // select(), fsync()/fdatasync() -#else -# include // select() -# include // socket(), bind(), listen(), accept(), - // struct sockaddr_in, gethostbyname() -# include -# include -#endif - -#if defined( sgi ) -#include -#endif - -#include STL_STRING - -#include - -#include "fg_socket.hxx" - -FG_USING_STD(string); - - -FGSocket::FGSocket() : - save_len(0) -{ - set_type( fgSocketType ); -} - - -FGSocket::~FGSocket() { -} - - -int FGSocket::make_server_socket () { - struct sockaddr_in name; - -#if defined( __CYGWIN__ ) || defined( __CYGWIN32__ ) || defined( sgi ) || defined( _MSC_VER ) - int length; -#else - socklen_t length; -#endif - - // Create the socket. - sock = socket (PF_INET, SOCK_STREAM, 0); - if (sock < 0) { - FG_LOG( FG_IO, FG_ALERT, - "Error: socket() failed in make_server_socket()" ); - return -1; - } - - // Give the socket a name. - name.sin_family = AF_INET; - name.sin_addr.s_addr = INADDR_ANY; - name.sin_port = htons(port); // set port to zero to let system pick - name.sin_addr.s_addr = htonl (INADDR_ANY); - if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) { - FG_LOG( FG_IO, FG_ALERT, - "Error: bind() failed in make_server_socket()" ); - return -1; - } - - // Find the assigned port number - length = sizeof(struct sockaddr_in); - if ( getsockname(sock, (struct sockaddr *) &name, &length) ) { - FG_LOG( FG_IO, FG_ALERT, - "Error: getsockname() failed in make_server_socket()" ); - return -1; - } - port = ntohs(name.sin_port); - - return sock; -} - - -int FGSocket::make_client_socket () { - struct sockaddr_in name; - struct hostent *hp; - - FG_LOG( FG_IO, FG_INFO, "Make client socket()" ); - - // Create the socket. - sock = socket (PF_INET, SOCK_STREAM, 0); - if (sock < 0) { - FG_LOG( FG_IO, FG_ALERT, - "Error: socket() failed in make_client_socket()" ); - return -1; - } - - // specify address family - name.sin_family = AF_INET; - - // get the hosts official name/info - hp = gethostbyname( hostname.c_str() ); - - // Connect this socket to the host and the port specified on the - // command line -#if defined( __CYGWIN__ ) || defined( __CYGWIN32__ ) - bcopy(hp->h_addr, (char *)(&(name.sin_addr.s_addr)), hp->h_length); -#else - bcopy(hp->h_addr, &(name.sin_addr.s_addr), hp->h_length); -#endif - name.sin_port = htons(port); - - if ( connect(sock, (struct sockaddr *) &name, - sizeof(struct sockaddr_in)) < 0 ) - { -#ifdef _MSC_VER - _close(sock); -#else - std::close(sock); -#endif - FG_LOG( FG_IO, FG_ALERT, - "Error: connect() failed in make_client_socket()" ); - return -1; - } - - return sock; -} - - -// If specified as a server (out direction for now) open the master -// listening socket. If specified as a client, open a connection to a -// server. - -bool FGSocket::open( FGProtocol::fgProtocolDir dir ) { - if ( port_str == "" || port_str == "any" ) { - port = 0; - } else { - port = atoi( port_str.c_str() ); - } - - client_connections.clear(); - - if ( dir == FGProtocol::out ) { - // this means server for now - - // Setup socket to listen on. Set "port" before making this - // call. A port of "0" indicates that we want to let the os - // pick any available port. - sock = make_server_socket(); - FG_LOG( FG_IO, FG_INFO, "socket is connected to port = " << port ); - - // Specify the maximum length of the connection queue - listen(sock, FG_MAX_SOCKET_QUEUE); - - } else if ( dir == FGProtocol::in ) { - // this means client for now - - sock = make_client_socket(); - } else { - FG_LOG( FG_IO, FG_ALERT, - "Error: bidirection mode not available yet for sockets." ); - return false; - } - - if ( sock < 0 ) { - FG_LOG( FG_IO, FG_ALERT, "Error opening socket: " << hostname - << ":" << port ); - return false; - } - - return true; -} - - -// read data from socket (client) -// read a block of data of specified size -int FGSocket::read( char *buf, int length ) { - int result = 0; - - // check for potential input - fd_set ready; - FD_ZERO(&ready); - FD_SET(sock, &ready); - struct timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 0; - - // test for any input read on sock (returning immediately, even if - // nothing) - select(32, &ready, 0, 0, &tv); - - if ( FD_ISSET(sock, &ready) ) { -#ifdef _MSC_VER - result = _read( sock, buf, length ); -#else - result = std::read( sock, buf, length ); -#endif - if ( result != length ) { - FG_LOG( FG_IO, FG_INFO, - "Warning: read() not enough bytes." ); - } - } - - return result; -} - - -// read a line of data, length is max size of input buffer -int FGSocket::readline( char *buf, int length ) { - int result = 0; - - // check for potential input - fd_set ready; - FD_ZERO(&ready); - FD_SET(sock, &ready); - struct timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 0; - - // test for any input read on sock (returning immediately, even if - // nothing) - select(32, &ready, 0, 0, &tv); - - if ( FD_ISSET(sock, &ready) ) { - // read a chunk, keep in the save buffer until we have the - // requested amount read - - char *buf_ptr = save_buf + save_len; -#ifdef _MSC_VER - result = _read( sock, buf_ptr, FG_MAX_MSG_SIZE - save_len ); -#else - result = std::read( sock, buf_ptr, FG_MAX_MSG_SIZE - save_len ); -#endif - save_len += result; - // cout << "current read = " << buf_ptr << endl; - // cout << "current save_buf = " << save_buf << endl; - // cout << "save_len = " << save_len << endl; - } - - // look for the end of line in save_buf - int i; - for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i ); - if ( save_buf[i] == '\n' ) { - result = i + 1; - } else { - // no end of line yet - // cout << "no eol found" << endl; - return 0; - } - // cout << "line length = " << result << endl; - - // we found an end of line - - // copy to external buffer - strncpy( buf, save_buf, result ); - buf[result] = '\0'; - // cout << "fg_socket line = " << buf << endl; - - // shift save buffer - for ( i = result; i < save_len; ++i ) { - save_buf[ i - result ] = save_buf[i]; - } - save_len -= result; - - return result; -} - - -// write data to socket (server) -int FGSocket::write( char *buf, int length ) { - - // check for any new client connection requests - fd_set ready; - FD_ZERO(&ready); - FD_SET(sock, &ready); - struct timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 0; - - // test for any input on sock (returning immediately, even if - // nothing) - select(32, &ready, 0, 0, &tv); - - // any new connections? - if ( FD_ISSET(sock, &ready) ) { - int msgsock = accept(sock, 0, 0); - if ( msgsock < 0 ) { - FG_LOG( FG_IO, FG_ALERT, - "Error: accept() failed in write()" ); - return 0; - } else { - client_connections.push_back( msgsock ); - } - } - - bool error_condition = false; - FG_LOG( FG_IO, FG_INFO, "Client connections = " << - client_connections.size() ); - for ( int i = 0; i < (int)client_connections.size(); ++i ) { - int msgsock = client_connections[i]; - - // read and junk any possible incoming messages. - // char junk[ FG_MAX_MSG_SIZE ]; - // std::read( msgsock, junk, FG_MAX_MSG_SIZE ); - - // write the interesting data to the socket -#ifdef _MSC_VER - if ( _write(msgsock, buf, length) < 0 ) { -#else - if ( std::write(msgsock, buf, length) < 0 ) { -#endif - FG_LOG( FG_IO, FG_ALERT, "Error writing to socket: " << port ); - error_condition = true; - } else { -#ifdef _POSIX_SYNCHRONIZED_IO - // fdatasync(msgsock); -#else - // fsync(msgsock); -#endif - } - } - - if ( error_condition ) { - return 0; - } - - return length; -} - - -// write null terminated string to socket (server) -int FGSocket::writestring( char *str ) { - int length = strlen( str ); - return write( str, length ); -} - - -// close the port -bool FGSocket::close() { - for ( int i = 0; i < (int)client_connections.size(); ++i ) { - int msgsock = client_connections[i]; -#ifdef _MSC_VER - _close( msgsock ); -#else - std::close( msgsock ); -#endif - } - -#ifdef _MSC_VER - _close( sock ); -#else - std::close( sock ); -#endif - return true; -} diff --git a/src/Network/fg_socket.hxx b/src/Network/fg_socket.hxx deleted file mode 100644 index 21ff9953f..000000000 --- a/src/Network/fg_socket.hxx +++ /dev/null @@ -1,98 +0,0 @@ -// fg_socket.hxx -- Socket I/O routines -// -// Written by Curtis Olson, started November 1999. -// -// Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -// -// $Id$ - - -#ifndef _FG_SOCKET_HXX -#define _FG_SOCKET_HXX - - -#ifndef __cplusplus -# error This library requires C++ -#endif - -#include - -#include - -#include - -#include "iochannel.hxx" -#include "protocol.hxx" - -FG_USING_STD(string); - - -#define FG_MAX_SOCKET_QUEUE 32 - - -class FGSocket : public FGIOChannel { - - string hostname; - string port_str; - - char save_buf[ 2 * FG_MAX_MSG_SIZE ]; - int save_len; - - int sock; - short unsigned int port; - - // make a server (master listening) socket - int make_server_socket(); - - // make a client socket - int make_client_socket(); - - int_list client_connections; - -public: - - FGSocket(); - ~FGSocket(); - - // open the file based on specified direction - bool open( FGProtocol::fgProtocolDir dir ); - - // read data from socket - int read( char *buf, int length ); - - // read data from socket - int readline( char *buf, int length ); - - // write data to a socket - int write( char *buf, int length ); - - // write null terminated string to a socket - int writestring( char *str ); - - // close file - bool close(); - - inline string get_hostname() const { return hostname; } - inline void set_hostname( const string& hn ) { hostname = hn; } - inline string get_port_str() const { return port_str; } - inline void set_port_str( const string& p ) { port_str = p; } -}; - - -#endif // _FG_SOCKET_HXX - - diff --git a/src/Network/garmin.cxx b/src/Network/garmin.cxx index 41aa2a48b..5fdeb81d5 100644 --- a/src/Network/garmin.cxx +++ b/src/Network/garmin.cxx @@ -23,11 +23,11 @@ #include #include +#include #include #include
-#include "iochannel.hxx" #include "garmin.hxx" @@ -350,7 +350,7 @@ bool FGGarmin::open() { return false; } - FGIOChannel *io = get_io_channel(); + SGIOChannel *io = get_io_channel(); if ( ! io->open( get_direction() ) ) { FG_LOG( FG_IO, FG_ALERT, "Error opening channel communication layer." ); @@ -365,15 +365,15 @@ bool FGGarmin::open() { // process work for this port bool FGGarmin::process() { - FGIOChannel *io = get_io_channel(); + SGIOChannel *io = get_io_channel(); - if ( get_direction() == out ) { + if ( get_direction() == SG_IO_OUT ) { gen_message(); if ( ! io->write( buf, length ) ) { FG_LOG( FG_IO, FG_ALERT, "Error writing data." ); return false; } - } else if ( get_direction() == in ) { + } else if ( get_direction() == SG_IO_IN ) { if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) { FG_LOG( FG_IO, FG_ALERT, "Success reading data." ); if ( parse_message() ) { @@ -404,7 +404,7 @@ bool FGGarmin::process() { // close the channel bool FGGarmin::close() { - FGIOChannel *io = get_io_channel(); + SGIOChannel *io = get_io_channel(); set_enabled( false ); diff --git a/src/Network/iochannel.cxx b/src/Network/iochannel.cxx deleted file mode 100644 index afce4006e..000000000 --- a/src/Network/iochannel.cxx +++ /dev/null @@ -1,74 +0,0 @@ -// iochannel.cxx -- High level IO channel class -// -// Written by Curtis Olson, started November 1999. -// -// Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -// -// $Id$ - - -#include "iochannel.hxx" -#include "garmin.hxx" -#include "nmea.hxx" - - -// constructor -FGIOChannel::FGIOChannel() -{ -} - - -// destructor -FGIOChannel::~FGIOChannel() -{ -} - - -// dummy configure routine -bool FGIOChannel::open( FGProtocol::fgProtocolDir dir ) { - return false; -} - - -// dummy process routine -int FGIOChannel::read( char *buf, int length ) { - return 0; -} - - -// dummy process routine -int FGIOChannel::readline( char *buf, int length ) { - return 0; -} - - -// dummy process routine -int FGIOChannel::write( char *buf, int length ) { - return false; -} - - -// dummy process routine -int FGIOChannel::writestring( char *str ) { - return false; -} - - -// dummy close routine -bool FGIOChannel::close() { - return false; -} diff --git a/src/Network/iochannel.hxx b/src/Network/iochannel.hxx deleted file mode 100644 index 0793ef7fc..000000000 --- a/src/Network/iochannel.hxx +++ /dev/null @@ -1,72 +0,0 @@ -// iochannel.hxx -- High level IO channel class -// -// Written by Curtis Olson, started November 1999. -// -// Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -// -// $Id$ - - -#ifndef _IOCHANNEL_HXX -#define _IOCHANNEL_HXX - - -#include - -#include "protocol.hxx" - -#include STL_STRING -#include - -FG_USING_STD(vector); -FG_USING_STD(string); - - -// forward declaration -class FGProtocol; - - -enum FGChannelType { - fgFileType = 0, - fgSerialType = 1, - fgSocketType = 2 -}; - -class FGIOChannel { - - FGChannelType type; - -public: - - FGIOChannel(); - virtual ~FGIOChannel(); - - virtual bool open( FGProtocol::fgProtocolDir dir ); - virtual int read( char *buf, int length ); - virtual int readline( char *buf, int length ); - virtual int write( char *buf, int length ); - virtual int writestring( char *str ); - virtual bool close(); - - virtual void set_type( FGChannelType t ) { type = t; } - virtual FGChannelType get_type() const { return type; } -}; - - -#endif // _IOCHANNEL_HXX - - diff --git a/src/Network/joyclient.cxx b/src/Network/joyclient.cxx index 2d05092fc..4805d639c 100644 --- a/src/Network/joyclient.cxx +++ b/src/Network/joyclient.cxx @@ -22,10 +22,10 @@ #include +#include #include -#include "iochannel.hxx" #include "joyclient.hxx" @@ -44,7 +44,7 @@ bool FGJoyClient::open() { return false; } - FGIOChannel *io = get_io_channel(); + SGIOChannel *io = get_io_channel(); if ( ! io->open( get_direction() ) ) { FG_LOG( FG_IO, FG_ALERT, "Error opening channel communication layer." ); @@ -59,15 +59,15 @@ bool FGJoyClient::open() { // process work for this port bool FGJoyClient::process() { - FGIOChannel *io = get_io_channel(); + SGIOChannel *io = get_io_channel(); int length = sizeof(int[2]); - if ( get_direction() == out ) { + if ( get_direction() == SG_IO_OUT ) { FG_LOG( FG_IO, FG_ALERT, "joyclient protocol is read only" ); return false; - } else if ( get_direction() == in ) { + } else if ( get_direction() == SG_IO_IN ) { FG_LOG( FG_IO, FG_DEBUG, "Searching for data." ); - if ( io->get_type() == fgFileType ) { + if ( io->get_type() == sgFileType ) { if ( io->read( (char *)(& buf), length ) == length ) { FG_LOG( FG_IO, FG_DEBUG, "Success reading data." ); int *msg; @@ -112,7 +112,7 @@ bool FGJoyClient::process() { // close the channel bool FGJoyClient::close() { - FGIOChannel *io = get_io_channel(); + SGIOChannel *io = get_io_channel(); set_enabled( false ); diff --git a/src/Network/native.cxx b/src/Network/native.cxx index f60f0cc90..83682604f 100644 --- a/src/Network/native.cxx +++ b/src/Network/native.cxx @@ -23,8 +23,8 @@ #include #include +#include -#include "iochannel.hxx" #include "native.hxx" @@ -43,7 +43,7 @@ bool FGNative::open() { return false; } - FGIOChannel *io = get_io_channel(); + SGIOChannel *io = get_io_channel(); if ( ! io->open( get_direction() ) ) { FG_LOG( FG_IO, FG_ALERT, "Error opening channel communication layer." ); @@ -58,18 +58,18 @@ bool FGNative::open() { // process work for this port bool FGNative::process() { - FGIOChannel *io = get_io_channel(); + SGIOChannel *io = get_io_channel(); int length = sizeof(*cur_fdm_state); - if ( get_direction() == out ) { + if ( get_direction() == SG_IO_OUT ) { // cout << "size of cur_fdm_state = " << length << endl; buf = *cur_fdm_state; if ( ! io->write( (char *)(& buf), length ) ) { FG_LOG( FG_IO, FG_ALERT, "Error writing data." ); return false; } - } else if ( get_direction() == in ) { - if ( io->get_type() == fgFileType ) { + } else if ( get_direction() == SG_IO_IN ) { + if ( io->get_type() == sgFileType ) { if ( io->read( (char *)(& buf), length ) == length ) { FG_LOG( FG_IO, FG_ALERT, "Success reading data." ); *cur_fdm_state = buf; @@ -88,7 +88,7 @@ bool FGNative::process() { // close the channel bool FGNative::close() { - FGIOChannel *io = get_io_channel(); + SGIOChannel *io = get_io_channel(); set_enabled( false ); diff --git a/src/Network/nmea.cxx b/src/Network/nmea.cxx index 369f30aff..7f9b47184 100644 --- a/src/Network/nmea.cxx +++ b/src/Network/nmea.cxx @@ -23,11 +23,11 @@ #include #include +#include #include #include
-#include "iochannel.hxx" #include "nmea.hxx" @@ -453,7 +453,7 @@ bool FGNMEA::open() { return false; } - FGIOChannel *io = get_io_channel(); + SGIOChannel *io = get_io_channel(); if ( ! io->open( get_direction() ) ) { FG_LOG( FG_IO, FG_ALERT, "Error opening channel communication layer." ); @@ -468,15 +468,15 @@ bool FGNMEA::open() { // process work for this port bool FGNMEA::process() { - FGIOChannel *io = get_io_channel(); + SGIOChannel *io = get_io_channel(); - if ( get_direction() == out ) { + if ( get_direction() == SG_IO_OUT ) { gen_message(); if ( ! io->write( buf, length ) ) { FG_LOG( FG_IO, FG_ALERT, "Error writing data." ); return false; } - } else if ( get_direction() == in ) { + } else if ( get_direction() == SG_IO_IN ) { if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) { parse_message(); } else { @@ -497,7 +497,7 @@ bool FGNMEA::process() { // close the channel bool FGNMEA::close() { - FGIOChannel *io = get_io_channel(); + SGIOChannel *io = get_io_channel(); set_enabled( false ); diff --git a/src/Network/protocol.cxx b/src/Network/protocol.cxx index 07fc52c66..b6918f98f 100644 --- a/src/Network/protocol.cxx +++ b/src/Network/protocol.cxx @@ -22,8 +22,8 @@ #include +#include -#include "iochannel.hxx" #include "protocol.hxx" @@ -47,7 +47,7 @@ bool FGProtocol::open() { return false; } - FGIOChannel *io = get_io_channel(); + SGIOChannel *io = get_io_channel(); if ( ! io->open( get_direction() ) ) { FG_LOG( FG_IO, FG_ALERT, "Error opening channel communication layer." ); @@ -76,7 +76,7 @@ bool FGProtocol::close() { // standard I/O channel close routine bool FGProtocol::gen_message() { - FGIOChannel *io = get_io_channel(); + SGIOChannel *io = get_io_channel(); set_enabled( false ); diff --git a/src/Network/protocol.hxx b/src/Network/protocol.hxx index f3c7fe4d6..0417d1860 100644 --- a/src/Network/protocol.hxx +++ b/src/Network/protocol.hxx @@ -37,26 +37,17 @@ FG_USING_STD(vector); #define FG_MAX_MSG_SIZE 16384 // forward declaration -class FGIOChannel; +class SGIOChannel; class FGProtocol { -public: - - enum fgProtocolDir { - none = 0, - in = 1, - out = 2, - bi = 3 - }; - private: double hz; int count_down; - fgProtocolDir dir; + SGProtocolDir dir; // string protocol_str; @@ -65,7 +56,7 @@ private: bool enabled; - FGIOChannel *io; + SGIOChannel *io; public: @@ -76,16 +67,16 @@ public: virtual bool process(); virtual bool close(); - inline fgProtocolDir get_direction() const { return dir; } + inline SGProtocolDir get_direction() const { return dir; } inline void set_direction( const string& d ) { if ( d == "in" ) { - dir = in; + dir = SG_IO_IN; } else if ( d == "out" ) { - dir = out; + dir = SG_IO_OUT; } else if ( d == "bi" ) { - dir = bi; + dir = SG_IO_BI; } else { - dir = none; + dir = SG_IO_NONE; } } @@ -108,8 +99,8 @@ public: inline bool is_enabled() const { return enabled; } inline void set_enabled( const bool b ) { enabled = b; } - inline FGIOChannel *get_io_channel() const { return io; } - inline void set_io_channel( FGIOChannel *c ) { io = c; } + inline SGIOChannel *get_io_channel() const { return io; } + inline void set_io_channel( SGIOChannel *c ) { io = c; } }; diff --git a/src/Network/pve.cxx b/src/Network/pve.cxx index d54c3b11b..d52f1a6be 100644 --- a/src/Network/pve.cxx +++ b/src/Network/pve.cxx @@ -24,10 +24,10 @@ #include // sprintf() #include #include +#include #include -#include "iochannel.hxx" #include "pve.hxx" @@ -110,15 +110,15 @@ bool FGPVE::parse_message() { // process work for this port bool FGPVE::process() { - FGIOChannel *io = get_io_channel(); + SGIOChannel *io = get_io_channel(); - if ( get_direction() == out ) { + if ( get_direction() == SG_IO_OUT ) { gen_message(); if ( ! io->write( buf, length ) ) { FG_LOG( FG_IO, FG_ALERT, "Error writing data." ); return false; } - } else if ( get_direction() == in ) { + } else if ( get_direction() == SG_IO_IN ) { FG_LOG( FG_IO, FG_ALERT, "in direction not supported for RUL." ); return false; } diff --git a/src/Network/ray.cxx b/src/Network/ray.cxx index df9911974..0cb0b0567 100644 --- a/src/Network/ray.cxx +++ b/src/Network/ray.cxx @@ -22,11 +22,11 @@ #include +#include #include #include -#include "iochannel.hxx" #include "ray.hxx" @@ -199,15 +199,15 @@ bool FGRAY::parse_message() { // process work for this port bool FGRAY::process() { - FGIOChannel *io = get_io_channel(); + SGIOChannel *io = get_io_channel(); - if ( get_direction() == out ) { + if ( get_direction() == SG_IO_OUT ) { gen_message(); if ( ! io->write( buf, length ) ) { FG_LOG( FG_IO, FG_ALERT, "Error writing data." ); return false; } - } else if ( get_direction() == in ) { + } else if ( get_direction() == SG_IO_IN ) { FG_LOG( FG_IO, FG_ALERT, "in direction not supported for RAY." ); return false; } diff --git a/src/Network/rul.cxx b/src/Network/rul.cxx index 343e6b154..c9554956b 100644 --- a/src/Network/rul.cxx +++ b/src/Network/rul.cxx @@ -25,11 +25,11 @@ #include // sprintf() #include +#include #include #include -#include "iochannel.hxx" #include "rul.hxx" @@ -102,15 +102,15 @@ bool FGRUL::parse_message() { // process work for this port bool FGRUL::process() { - FGIOChannel *io = get_io_channel(); + SGIOChannel *io = get_io_channel(); - if ( get_direction() == out ) { + if ( get_direction() == SG_IO_OUT ) { gen_message(); if ( ! io->write( buf, length ) ) { FG_LOG( FG_IO, FG_ALERT, "Error writing data." ); return false; } - } else if ( get_direction() == in ) { + } else if ( get_direction() == SG_IO_IN ) { FG_LOG( FG_IO, FG_ALERT, "in direction not supported for RUL." ); return false; } -- 2.39.5