sg_socket.hxx

Go to the documentation of this file.
00001 
00006 // Written by Curtis Olson, started November 1999.
00007 //
00008 // Copyright (C) 1999  Curtis L. Olson - http://www.flightgear.org/~curt
00009 //
00010 // This program is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU General Public License as
00012 // published by the Free Software Foundation; either version 2 of the
00013 // License, or (at your option) any later version.
00014 //
00015 // This program is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU General Public License
00021 // along with this program; if not, write to the Free Software
00022 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023 //
00024 // $Id: sg__socket_8hxx-source.html,v 1.15 2007-12-17 15:37:10 curt Exp $
00025 
00026 
00027 #ifndef _SG_SOCKET_HXX
00028 #define _SG_SOCKET_HXX
00029 
00030 
00031 #ifndef __cplusplus
00032 # error This library requires C++
00033 #endif
00034 
00035 #include <simgear/compiler.h>
00036 
00037 #include STL_STRING
00038 
00039 #include <simgear/math/sg_types.hxx>
00040 #include <simgear/io/iochannel.hxx>
00041 
00042 #include <plib/netSocket.h>
00043 
00044 SG_USING_STD(string);
00045 
00046 #define SG_MAX_SOCKET_QUEUE 32
00047 
00048 
00052 class SGSocket : public SGIOChannel {
00053 public:
00054 private:
00055     string hostname;
00056     string port_str;
00057 
00058     char save_buf[ 2 * SG_IO_MAX_MSG_SIZE ];
00059     int save_len;
00060 
00061     netSocket sock;
00062     netSocket* client;
00063     unsigned short port;
00064     bool is_tcp;
00065     bool is_server;
00066     bool first_read;
00067     int timeout;
00068 
00069     static bool init;
00070 
00071     // make a server (master listening) socket
00072     bool make_server_socket();
00073 
00074     // make a client socket
00075     bool  make_client_socket();
00076 
00077     // Poll for new connections or data to read.
00078     int poll();
00079 
00080 public:
00081 
00129     SGSocket( const string& host, const string& port, const string& style );
00130 
00132     ~SGSocket();
00133 
00134     // If specified as a server (in direction for now) open the master
00135     // listening socket.  If specified as a client (out direction),
00136     // open a connection to a server.
00137     bool open( const SGProtocolDir d );
00138 
00139     // read data from socket
00140     int read( char *buf, int length );
00141 
00142     // read data from socket
00143     int readline( char *buf, int length );
00144 
00145     // write data to a socket
00146     int write( const char *buf, const int length );
00147 
00148     // write null terminated string to a socket
00149     int writestring( const char *str );
00150 
00151     // close file
00152     bool close();
00153 
00158     bool nonblock();
00159 
00160     // set timeout (default: 0)
00161     inline void set_timeout(int i) { timeout = i; }
00162 
00164     inline string get_hostname() const { return hostname; }
00165 
00167     inline string get_port_str() const { return port_str; }
00168 };
00169 
00170 
00171 #endif // _SG_SOCKET_HXX

Generated on Mon Dec 17 09:30:55 2007 for SimGear by  doxygen 1.5.1