00001 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
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 <string>
00038 
00039 #include <simgear/math/sg_types.hxx>
00040 #include <simgear/io/iochannel.hxx>
00041 
00042 #include <plib/netSocket.h>
00043 
00044 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     
00072     bool make_server_socket();
00073 
00074     
00075     bool  make_client_socket();
00076 
00077     
00078     int poll();
00079 
00080 public:
00081 
00129     SGSocket( const string& host, const string& port, const string& style );
00130 
00132     ~SGSocket();
00133 
00134     
00135     
00136     
00137     bool open( const SGProtocolDir d );
00138 
00139     
00140     int read( char *buf, int length );
00141 
00142     
00143     int readline( char *buf, int length );
00144 
00145     
00146     int write( const char *buf, const int length );
00147 
00148     
00149     int writestring( const char *str );
00150 
00151     
00152     bool close();
00153 
00158     bool nonblock();
00159 
00160     
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