]> git.mxchange.org Git - flightgear.git/blob - src/Network/fg_socket.hxx
UIUC flight model contribution. This is based on LaRCsim, but can read
[flightgear.git] / src / Network / fg_socket.hxx
1 // fg_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 _FG_SOCKET_HXX
25 #define _FG_SOCKET_HXX
26
27
28 #ifndef __cplusplus
29 # error This library requires C++
30 #endif
31
32 #include <simgear/compiler.h>
33
34 #include <string>
35
36 #include <simgear/math/fg_types.hxx>
37
38 #include "iochannel.hxx"
39 #include "protocol.hxx"
40
41 FG_USING_STD(string);
42
43
44 #define FG_MAX_SOCKET_QUEUE 32
45
46
47 class FGSocket : public FGIOChannel {
48
49     string hostname;
50     string port_str;
51
52     char save_buf[ 2 * FG_MAX_MSG_SIZE ];
53     int save_len;
54
55     int sock;
56     short unsigned int port;
57
58     // make a server (master listening) socket
59     int make_server_socket();
60
61     // make a client socket
62     int make_client_socket();
63
64     int_list client_connections;
65
66 public:
67
68     FGSocket();
69     ~FGSocket();
70
71     // open the file based on specified direction
72     bool open( FGProtocol::fgProtocolDir dir );
73
74     // read data from socket
75     int read( char *buf, int length );
76
77     // read data from socket
78     int readline( char *buf, int length );
79
80     // write data to a socket
81     int write( char *buf, int length );
82
83     // write null terminated string to a socket
84     int writestring( char *str );
85
86     // close file
87     bool close();
88
89     inline string get_hostname() const { return hostname; }
90     inline void set_hostname( const string& hn ) { hostname = hn; }
91     inline string get_port_str() const { return port_str; }
92     inline void set_port_str( const string& p ) { port_str = p; }
93 };
94
95
96 #endif // _FG_SOCKET_HXX
97
98