1 // sg_socket.hxx -- Socket I/O routines
3 // Written by Curtis Olson, started November 1999.
5 // Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #ifndef _SG_SOCKET_HXX
25 #define _SG_SOCKET_HXX
29 # error This library requires C++
32 #include <simgear/compiler.h>
36 #include <simgear/math/sg_types.hxx>
38 #include "iochannel.hxx"
46 #define SG_MAX_SOCKET_QUEUE 32
49 class SGSocket : public SGIOChannel {
52 typedef SOCKET SocketType;
54 typedef int SocketType;
55 # define INVALID_SOCKET (-1)
62 char save_buf[ 2 * SG_IO_MAX_MSG_SIZE ];
66 short unsigned int port;
67 int sock_style; // SOCK_STREAM or SOCK_DGRAM
69 // make a server (master listening) socket
70 SocketType make_server_socket();
72 // make a client socket
73 SocketType make_client_socket();
76 size_t readsocket( int fd, void *buf, size_t count );
77 size_t writesocket( int fd, const void *buf, size_t count );
78 #if !defined(_MSC_VER)
79 int closesocket(int fd);
83 // Ensure winsock has been initialised.
84 static bool wsock_init;
85 static bool wsastartup();
90 SGSocket( const string& host, const string& port, const string& style );
93 // If specified as a server (in direction for now) open the master
94 // listening socket. If specified as a client (out direction),
95 // open a connection to a server.
96 bool open( SGProtocolDir dir );
98 // read data from socket
99 int read( char *buf, int length );
101 // read data from socket
102 int readline( char *buf, int length );
104 // write data to a socket
105 int write( char *buf, int length );
107 // write null terminated string to a socket
108 int writestring( char *str );
113 // Enable non-blocking mode.
116 inline string get_hostname() const { return hostname; }
117 inline string get_port_str() const { return port_str; }
121 #endif // _SG_SOCKET_HXX