]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_socket.hxx
22915a2ae0c8e3faf1caa66f2a59ae479dc56353
[simgear.git] / simgear / io / sg_socket.hxx
1 // sg_socket.hxx -- Socket I/O routines
2 //
3 // Written by Curtis Olson, started November 1999.
4 //
5 // Copyright (C) 1999  Curtis L. Olson - curt@flightgear.org
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifndef _SG_SOCKET_HXX
25 #define _SG_SOCKET_HXX
26
27
28 #ifndef __cplusplus
29 # error This library requires C++
30 #endif
31
32 #include <simgear/compiler.h>
33
34 #include STL_STRING
35
36 #include <simgear/math/sg_types.hxx>
37
38 #include "iochannel.hxx"
39
40 FG_USING_STD(string);
41
42 #if defined(_MSC_VER)
43 #  include <winsock.h>
44 #endif
45
46 #define SG_MAX_SOCKET_QUEUE 32
47
48
49 class SGSocket : public SGIOChannel {
50 public:
51 #if defined(_MSC_VER)
52     typedef SOCKET SocketType;
53 #else
54     typedef int SocketType;
55 #   define INVALID_SOCKET (-1)
56 #endif
57
58 private:
59     string hostname;
60     string port_str;
61
62     char save_buf[ 2 * SG_IO_MAX_MSG_SIZE ];
63     int save_len;
64
65     SocketType sock;
66     SocketType msgsock;
67     short unsigned int port;
68     int sock_style;                     // SOCK_STREAM or SOCK_DGRAM
69
70     bool first_read;
71
72     // make a server (master listening) socket
73     SocketType make_server_socket();
74
75     // make a client socket
76     SocketType make_client_socket();
77
78     // wrapper functions
79     size_t readsocket( int fd, void *buf, size_t count );
80     size_t writesocket( int fd, const void *buf, size_t count );
81 #if !defined(_MSC_VER)
82     int closesocket(int fd);
83 #endif
84
85 #if defined(_MSC_VER)
86     // Ensure winsock has been initialised.
87     static bool wsock_init;
88     static bool wsastartup();
89 #endif
90
91 public:
92
93     SGSocket( const string& host, const string& port, const string& style );
94     ~SGSocket();
95
96     // If specified as a server (in direction for now) open the master
97     // listening socket.  If specified as a client (out direction),
98     // open a connection to a server.
99     bool open( const SGProtocolDir d );
100
101     // read data from socket
102     int read( char *buf, int length );
103
104     // read data from socket
105     int readline( char *buf, int length );
106
107     // write data to a socket
108     int write( const char *buf, const int length );
109
110     // write null terminated string to a socket
111     int writestring( const char *str );
112
113     // close file
114     bool close();
115
116     // Enable non-blocking mode.
117     bool nonblock();
118
119     inline string get_hostname() const { return hostname; }
120     inline string get_port_str() const { return port_str; }
121 };
122
123
124 #endif // _SG_SOCKET_HXX
125
126