]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_socket.hxx
MacOS tweaks contributed by Darrell Walisser.
[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 #include <simgear/io/iochannel.hxx>
38
39 FG_USING_STD(string);
40
41 #if defined(_MSC_VER)
42 #  include <winsock.h>
43 #endif
44
45 #define SG_MAX_SOCKET_QUEUE 32
46
47
48 class SGSocket : public SGIOChannel {
49 public:
50 #if defined(_MSC_VER)
51     typedef SOCKET SocketType;
52 #else
53     typedef int SocketType;
54 #   define INVALID_SOCKET (-1)
55 #endif
56
57 private:
58     string hostname;
59     string port_str;
60
61     char save_buf[ 2 * SG_IO_MAX_MSG_SIZE ];
62     int save_len;
63
64     SocketType sock;
65     SocketType msgsock;
66     short unsigned int port;
67     int sock_style;                     // SOCK_STREAM or SOCK_DGRAM
68
69     bool first_read;
70
71     // make a server (master listening) socket
72     SocketType make_server_socket();
73
74     // make a client socket
75     SocketType make_client_socket();
76
77     // wrapper functions
78     size_t readsocket( int fd, void *buf, size_t count );
79     size_t writesocket( int fd, const void *buf, size_t count );
80 #if !defined(_MSC_VER)
81     int closesocket(int fd);
82 #endif
83
84 #if defined(_MSC_VER)
85     // Ensure winsock has been initialised.
86     static bool wsock_init;
87     static bool wsastartup();
88 #endif
89
90 public:
91
92     SGSocket( const string& host, const string& port, const string& style );
93     ~SGSocket();
94
95     // If specified as a server (in direction for now) open the master
96     // listening socket.  If specified as a client (out direction),
97     // open a connection to a server.
98     bool open( const SGProtocolDir d );
99
100     // read data from socket
101     int read( char *buf, int length );
102
103     // read data from socket
104     int readline( char *buf, int length );
105
106     // write data to a socket
107     int write( const char *buf, const int length );
108
109     // write null terminated string to a socket
110     int writestring( const char *str );
111
112     // close file
113     bool close();
114
115     // Enable non-blocking mode.
116     bool nonblock();
117
118     inline string get_hostname() const { return hostname; }
119     inline string get_port_str() const { return port_str; }
120 };
121
122
123 #endif // _SG_SOCKET_HXX
124
125