#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:
// 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( SGProtocolDir dir );
+ bool open( const SGProtocolDir d );
// read data from socket
int read( char *buf, int length );
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 string get_port_str() const { return port_str; }
};