fgms 0.11.8
The
FlightGear MultiPlayer Server
project
netSocket.h
Go to the documentation of this file.
1 /**
2  * @file netSocket.h
3  * @author Dave McClurg <dpm@efn.org>
4  * @brief netSocket is a thin C++ wrapper over bsd sockets to
5  * facilitate porting to other platforms.
6  * Part of <a href="../../plib/html">PLIB</a> - A Suite of Portable Game Libraries
7  */
8 
9 
10 /*
11  PLIB - A Suite of Portable Game Libraries
12  Copyright (C) 1998,2002 Steve Baker
13 
14  This library is free software; you can redistribute it and/or
15  modify it under the terms of the GNU Library General Public
16  License as published by the Free Software Foundation; either
17  version 2 of the License, or (at your option) any later version.
18 
19  This library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  Library General Public License for more details.
23 
24  You should have received a copy of the GNU Library General Public
25  License along with this library; if not, write to the Free Software
26  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 
28  For further information visit http://plib.sourceforge.net
29 
30  $Id: netSocket.h,v 1.1.1.1 2007/06/12 10:10:24 oliver Exp $
31 */
32 
33 /****
34 * NAME
35 * netSocket - network sockets
36 *
37 * DESCRIPTION
38 * netSocket is a thin C++ wrapper over bsd sockets to
39 * facilitate porting to other platforms
40 *
41 * AUTHOR
42 * Dave McClurg <dpm@efn.org>
43 *
44 * CREATION DATE
45 * Dec-2000
46 *
47 ****/
48 
49 #ifndef NET_SOCKET_H
50 #define NET_SOCKET_H
51 
52 #if __FreeBSD__ || defined(__CYGWIN__)
53  #include <sys/types.h>
54  #include <sys/socket.h>
55 #endif
56 
57 #ifdef __CYGWIN__
58 #include <netinet/in.h>
59 #endif
60 
61 #include <errno.h>
62 #include <stdlib.h>
63 #include <stdio.h>
64 #include <string.h>
65 #include <assert.h>
66 #include <stdarg.h>
67 #include <string>
68 #include <server/fg_util.hxx>
69 
70 #ifdef _MSC_VER
71  static int recoverable_wsa_error()
72  {
73  int wsa_errno = WSAGetLastError();
74  WSASetLastError(0); // clear the error
75  return (wsa_errno == WSAEINTR) ? 1 : 0;
76  }
77  #define RECOVERABLE_ERROR recoverable_wsa_error()
78 #else
79  #define RECOVERABLE_ERROR (errno == EINTR)
80  #define SOCKET_ERROR -1
81 #endif
82 
83 /**
84  * @class netAddress
85  * @brief Socket address, internet style.
86  */
88 {
89  /* DANGER!!! This MUST match 'struct sockaddr_in' exactly! */
90 #if __FreeBSD__
91  int8_t sin_len ;
92  sa_family_t sin_family ;
93  in_port_t sin_port ;
94  in_addr_t sin_addr ;
95 #else
96  short sin_family ;
97  unsigned short sin_port ;
98  unsigned int sin_addr ;
99 #endif
100  char sin_zero [ 8 ] ;
101 
102 public:
104  netAddress ( const char* host, int port ) ;
105 
106  void set ( const char* host, int port ) ;
107  void setPort ( int port );
108  const std::string getHost () const ;
109  unsigned int getPort() const ;
110  unsigned int getIP () const ;
111  unsigned int getFamily () const ;
112  static const char* getLocalHost () ;
113 
114  bool getBroadcast () const ;
115  bool operator == ( const netAddress& Value ) const;
116  bool operator != ( const netAddress& Value ) const;
117  void operator = ( const netAddress& Value );
118 };
119 
120 
121 /**
122  * @class netSocket
123  * @brief Socket type
124  */
126 {
127  int handle ;
128  bool isStream;
129 
130 public:
131 
132  netSocket () ;
133  virtual ~netSocket () ;
134 
135  int getHandle () const { return handle; }
136  void setHandle (int handle) ;
137 
138  bool open ( bool stream=true ) ;
139  void close ( void ) ;
140  void shutdown ( void ) ;
141  int bind ( const char* host, int port ) ;
142  int listen ( int backlog ) ;
143  int accept ( netAddress* addr ) ;
144  int connect ( const char* host, int port ) ;
145  int write_str ( const char* str, int len );
146  int write_str ( const string& str );
147  int write_char ( const char& c );
148  int send ( const void * buffer, int size, int flags = 0 ) ;
149  int sendto ( const void * buffer, int size, int flags, const netAddress* to ) ;
150  int read_char ( unsigned char& c);
151  int recv ( void * buffer, int size, int flags = 0 ) ;
152  int recvfrom ( void * buffer, int size, int flags, netAddress* from ) ;
153 
154  void setSockOpt ( int SocketOption, bool Set );
155  void setBlocking ( bool blocking ) ;
156  void setBroadcast ( bool broadcast ) ;
157 
158  static bool isNonBlockingError () ;
159  static int select ( netSocket** reads, netSocket** writes, int timeout ) ;
160 } ;
161 
162 
163 #ifdef __cplusplus
164 extern "C" {
165 #endif /* __cplusplus */
166 
167 extern int netInit () ;
168 
169 extern const char* netFormat ( const char* fmt, ... ) ;
170 #if defined(UL_WIN32)
171 extern void win_wsa_perror( char * msg );
172 #endif
173 
174 #ifdef __cplusplus
175 }
176 #endif /* __cplusplus */
177 
178 #endif // NET_SOCKET_H
179 
unsigned int getPort() const
Return the port no as int.
Definition: netSocket.cxx:140
int getHandle() const
Definition: netSocket.h:135
int sendto(const void *buffer, int size, int flags, const netAddress *to)
Definition: netSocket.cxx:378
void set(const char *host, int port)
Definition: netSocket.cxx:73
int accept(netAddress *addr)
Definition: netSocket.cxx:313
Socket type.
Definition: netSocket.h:125
unsigned int getFamily() const
Definition: netSocket.cxx:146
unsigned int getIP() const
Definition: netSocket.cxx:134
int handle
Definition: netSocket.h:127
unsigned int sin_addr
Definition: netSocket.h:98
void close(void)
Definition: netSocket.cxx:419
void setHandle(int handle)
Definition: netSocket.cxx:217
bool operator!=(const netAddress &Value) const
Definition: netSocket.cxx:186
bool open(bool stream=true)
Definition: netSocket.cxx:224
void operator=(const netAddress &Value)
Definition: netSocket.cxx:194
const char * netFormat(const char *fmt,...)
Definition: netSocket.cxx:606
int read_char(unsigned char &c)
Definition: netSocket.cxx:386
void shutdown(void)
Definition: netSocket.cxx:436
unsigned short sin_port
Definition: netSocket.h:97
int send(const void *buffer, int size, int flags=0)
Definition: netSocket.cxx:371
int bind(const char *host, int port)
Definition: netSocket.cxx:298
bool operator==(const netAddress &Value) const
Definition: netSocket.cxx:178
int listen(int backlog)
Definition: netSocket.cxx:306
int recv(void *buffer, int size, int flags=0)
Definition: netSocket.cxx:403
bool isStream
Definition: netSocket.h:128
static int select(netSocket **reads, netSocket **writes, int timeout)
Definition: netSocket.cxx:489
int write_str(const char *str, int len)
Definition: netSocket.cxx:339
int connect(const char *host, int port)
Definition: netSocket.cxx:329
void setPort(int port)
Definition: netSocket.cxx:113
static bool isNonBlockingError()
Definition: netSocket.cxx:456
static char * port
Definition: server.c:69
static const char * getLocalHost()
Definition: netSocket.cxx:151
bool getBroadcast() const
Definition: netSocket.cxx:173
int recvfrom(void *buffer, int size, int flags, netAddress *from)
Definition: netSocket.cxx:410
void setBlocking(bool blocking)
Definition: netSocket.cxx:233
void setSockOpt(int SocketOption, bool Set)
Definition: netSocket.cxx:257
char sin_zero[8]
Definition: netSocket.h:100
void setBroadcast(bool broadcast)
Definition: netSocket.cxx:277
int write_char(const char &c)
Definition: netSocket.cxx:366
const std::string getHost() const
Create a string object representing an IP address. This is always a string of the form 'dd...
Definition: netSocket.cxx:123
Socket address, internet style.
Definition: netSocket.h:87
short sin_family
Definition: netSocket.h:96
int netInit()
Definition: netSocket.cxx:585
virtual ~netSocket()
Definition: netSocket.cxx:211