]> git.mxchange.org Git - simgear.git/blobdiff - simgear/io/sg_socket.hxx
Fix a build order problem.
[simgear.git] / simgear / io / sg_socket.hxx
index 199b6dba10073356ecc4d6c7858cef8935ae95d7..0d393f4f1b4e897320f722f5da7c6b09f50e41d8 100644 (file)
 
 #include <simgear/compiler.h>
 
-#include <string>
+#include STL_STRING
 
-#include <simgear/math/fg_types.hxx>
-
-#include "iochannel.hxx"
+#include <simgear/math/sg_types.hxx>
+#include <simgear/io/iochannel.hxx>
 
 FG_USING_STD(string);
 
+#if defined(_MSC_VER)
+#  include <winsock.h>
+#endif
 
 #define SG_MAX_SOCKET_QUEUE 32
 
 
 class SGSocket : public SGIOChannel {
+public:
+#if defined(_MSC_VER)
+    typedef SOCKET SocketType;
+#else
+    typedef int SocketType;
+#   define INVALID_SOCKET (-1)
+#endif
 
+private:
     string hostname;
     string port_str;
 
     char save_buf[ 2 * SG_IO_MAX_MSG_SIZE ];
     int save_len;
 
-    int sock;
+    SocketType sock;
+    SocketType msgsock;
     short unsigned int port;
+    int sock_style;                    // SOCK_STREAM or SOCK_DGRAM
+
+    bool first_read;
 
     // make a server (master listening) socket
-    int make_server_socket();
+    SocketType make_server_socket();
 
     // make a client socket
-    int make_client_socket();
+    SocketType make_client_socket();
 
-    int_list client_connections;
+    // wrapper functions
+    size_t readsocket( int fd, void *buf, size_t count );
+    size_t writesocket( int fd, const void *buf, size_t count );
+#if !defined(_MSC_VER)
+    int closesocket(int fd);
+#endif
+
+#if defined(_MSC_VER)
+    // Ensure winsock has been initialised.
+    static bool wsock_init;
+    static bool wsastartup();
+#endif
 
 public:
 
-    SGSocket();
+    SGSocket( const string& host, const string& port, const string& style );
     ~SGSocket();
 
-    // open the file based on specified direction
-    bool open( SGProtocolDir dir );
+    // If specified as a server (in direction for now) open the master
+    // listening socket.  If specified as a client (out direction),
+    // open a connection to a server.
+    bool open( const SGProtocolDir d );
 
     // read data from socket
     int read( char *buf, int length );
@@ -77,18 +104,19 @@ public:
     int readline( char *buf, int length );
 
     // write data to a socket
-    int write( char *buf, int length );
+    int write( const char *buf, const int length );
 
     // write null terminated string to a socket
-    int writestring( char *str );
+    int writestring( const char *str );
 
     // close file
     bool close();
 
+    // Enable non-blocking mode.
+    bool nonblock();
+
     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; }
 };