]> git.mxchange.org Git - simgear.git/blob - simgear/io/raw_socket.hxx
Merge branch 'next' of git@gitorious.org:fg/simgear into next
[simgear.git] / simgear / io / raw_socket.hxx
1 /*
2      simgear::Socket, adapted from PLIB netSocket by James Turner
3      Copyright (C) 2010  James Turner
4      
5      PLIB - A Suite of Portable Game Libraries
6      Copyright (C) 1998,2002  Steve Baker
7  
8      This library is free software; you can redistribute it and/or
9      modify it under the terms of the GNU Library General Public
10      License as published by the Free Software Foundation; either
11      version 2 of the License, or (at your option) any later version.
12  
13      This library is distributed in the hope that it will be useful,
14      but WITHOUT ANY WARRANTY; without even the implied warranty of
15      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16      Library General Public License for more details.
17  
18      You should have received a copy of the GNU Library General Public
19      License along with this library; if not, write to the Free Software
20      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21 */
22
23 #ifndef SG_IO_SOCKET_HXX
24 #define SG_IO_SOCKET_HXX
25
26 #include <errno.h>
27
28 #if defined(__APPLE__)
29 #  include <netinet/in.h>
30 #endif
31
32 namespace simgear
33 {
34
35 /*
36  * Socket address, internet style.
37  */
38 class IPAddress
39 {
40   /* DANGER!!!  This MUST match 'struct sockaddr_in' exactly! */
41 #ifdef __APPLE__
42   __uint8_t      sin_len;
43   __uint8_t      sin_family;
44   in_port_t      sin_port;
45   in_addr_t      sin_addr;
46   char           sin_zero[8];
47 #else
48   short          sin_family     ;
49   unsigned short sin_port       ;
50   unsigned int   sin_addr       ;
51   char           sin_zero [ 8 ] ;
52 #endif
53
54 public:
55   IPAddress () {}
56   IPAddress ( const char* host, int port ) ;
57
58   void set ( const char* host, int port ) ;
59   const char* getHost () const ;
60   unsigned int getPort() const ;
61   unsigned int getIP () const ;
62   unsigned int getFamily () const ;
63   static const char* getLocalHost () ;
64
65   bool getBroadcast () const ;
66 };
67
68
69 /*
70  * Socket type
71  */
72 class Socket
73 {
74   int handle ;
75
76 public:
77   
78   Socket () ;
79   virtual ~Socket () ;
80
81   static int initSockets();
82
83   int getHandle () const { return handle; }
84   void setHandle (int handle) ;
85   
86   bool  open        ( bool stream=true ) ;
87   void  close       ( void ) ;
88   int   bind        ( const char* host, int port ) ;
89   int   listen      ( int backlog ) ;
90   int   accept      ( IPAddress* addr ) ;
91   int   connect     ( const char* host, int port ) ;
92   int   send        ( const void * buffer, int size, int flags = 0 ) ;
93   int   sendto      ( const void * buffer, int size, int flags, const IPAddress* to ) ;
94   int   recv        ( void * buffer, int size, int flags = 0 ) ;
95   int   recvfrom    ( void * buffer, int size, int flags, IPAddress* from ) ;
96
97   void setBlocking ( bool blocking ) ;
98   void setBroadcast ( bool broadcast ) ;
99
100   static bool isNonBlockingError () ;
101   
102   static int select ( Socket** reads, Socket** writes, int timeout ) ;
103 } ;
104
105 //const char* netFormat ( const char* fmt, ... ) ;
106
107 } // of namespace simgear
108
109 #endif // SG_IO_SOCKET_HXX
110