]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_socket.hxx
Doxygen ...
[simgear.git] / simgear / io / sg_socket.hxx
1 /**
2  * \file sg_socket.hxx
3  * Socket I/O routines.
4  */
5
6 // Written by Curtis Olson, started November 1999.
7 //
8 // Copyright (C) 1999  Curtis L. Olson - curt@flightgear.org
9 //
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 //
24 // $Id$
25
26
27 #ifndef _SG_SOCKET_HXX
28 #define _SG_SOCKET_HXX
29
30
31 #ifndef __cplusplus
32 # error This library requires C++
33 #endif
34
35 #include <simgear/compiler.h>
36
37 #include STL_STRING
38
39 #include <simgear/math/sg_types.hxx>
40 #include <simgear/io/iochannel.hxx>
41
42 SG_USING_STD(string);
43
44 #if defined(_MSC_VER)
45 #  include <winsock.h>
46 #endif
47
48 #define SG_MAX_SOCKET_QUEUE 32
49
50
51 /**
52  * A socket I/O class based on SGIOChannel.
53  */
54 class SGSocket : public SGIOChannel {
55 public:
56 #if defined(_MSC_VER)
57     typedef SOCKET SocketType;
58 #else
59     typedef int SocketType;
60 #   define INVALID_SOCKET (-1)
61 #endif
62
63 private:
64     string hostname;
65     string port_str;
66
67     char save_buf[ 2 * SG_IO_MAX_MSG_SIZE ];
68     int save_len;
69
70     SocketType sock;
71     SocketType msgsock;
72     short unsigned int port;
73     int sock_style;                     // SOCK_STREAM or SOCK_DGRAM
74
75     bool first_read;
76
77     // make a server (master listening) socket
78     SocketType make_server_socket();
79
80     // make a client socket
81     SocketType make_client_socket();
82
83     // wrapper functions
84     size_t readsocket( int fd, void *buf, size_t count );
85     size_t writesocket( int fd, const void *buf, size_t count );
86 #if !defined(_MSC_VER)
87     int closesocket(int fd);
88 #endif
89
90 #if defined(_MSC_VER)
91     // Ensure winsock has been initialised.
92     static bool wsock_init;
93     static bool wsastartup();
94 #endif
95
96 public:
97
98     /**
99      * Create an instance of SGSocket.
100      *
101      * When calling the constructor you need to provide a host name, a
102      * port number, and a socket style. The convention used by the
103      * SGSocket class is that the server side listens and the client
104      * side sends. For a server socket, the host name should be
105      * empty. For a server, the port number is optional, if you do not
106      * specify a port, the system will assign one. For a client
107      * socket, you need to specify both a destination host and
108      * destination port. For both client and server sockets you must
109      * specify the socket type. Type must be either udp or tcp. Here's
110      * a quick breakdown of the major differences between UDP and TCP
111      * type sockets.
112      *
113      * TCP sockets are the type where you establish a connection and
114      * then can read and write to the socket from both ends. If one
115      * end of TCP socket connect quits, the other end will likely
116      * segfault if it doesn't take special precautions.  But, the nice
117      * thing about TCP connections is that the underlying protocol
118      * guarantees that your message will get through. This imposes a
119      * certain performance overhead though on the communication
120      * because the protocol must resend failed messages. TCP sockets
121      * are good for sending periodic command/response type messages
122      * where performance isn't a big issues, but reliability is.
123      *
124      * UDP sockets on the other hand are a lower level protocol and
125      * don't have the same sort of connection as TCP sockets. With UDP
126      * sockets, the server end just sits and listens for incoming
127      * packets from anywhere. The client end sends it's message and
128      * forgets about it. It doesn't care if there isn't even a server
129      * out there listening and all the packets are getting
130      * lost. Although systems/networks usually do a pretty good job
131      * (statistically) of getting your UDP packets to their
132      * destination, there is no guarantee that any particular packet
133      * will make it. But, because of this low level implementation and
134      * lack of error checking, UDP packets are much faster and
135      * efficient. UDP packets are good for sending positional
136      * information to synchronize two applications. In this case, you
137      * want the information to arrive as quickly as possible, and if
138      * you lose a packet, you'd rather get new updated information
139      * rather than have the system waste time resending a packet that
140      * is becoming older and older with every retry.
141      * @param host name of host if direction is SG_IO_OUT or SG_IO_BI
142      * @param port port number if we care to choose one.
143      * @param style specify "udp" or "tcp"
144      */
145     SGSocket( const string& host, const string& port, const string& style );
146
147     /** Destructor */
148     ~SGSocket();
149
150     // If specified as a server (in direction for now) open the master
151     // listening socket.  If specified as a client (out direction),
152     // open a connection to a server.
153     bool open( const SGProtocolDir d );
154
155     // read data from socket
156     int read( char *buf, int length );
157
158     // read data from socket
159     int readline( char *buf, int length );
160
161     // write data to a socket
162     int write( const char *buf, const int length );
163
164     // write null terminated string to a socket
165     int writestring( const char *str );
166
167     // close file
168     bool close();
169
170     /** Enable non-blocking mode. */
171     bool nonblock();
172
173     /** Return the remote host name */
174     inline string get_hostname() const { return hostname; }
175
176     /** Return the port number (in string form) */
177     inline string get_port_str() const { return port_str; }
178 };
179
180
181 #endif // _SG_SOCKET_HXX