]> git.mxchange.org Git - simgear.git/blob - simgear/io/raw_socket.hxx
e3f50daf67af17a710b1d5396458dcbda188ea6c
[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 #include <netinet/in.h>
28
29 namespace simgear
30 {
31
32 /*
33  * Socket address, internet style.
34  */
35 class IPAddress
36 {
37 public:
38   IPAddress () {}
39   IPAddress ( const char* host, int port ) ;
40   IPAddress ( struct sockaddr* addr, size_t len );
41   
42   void set ( const char* host, int port ) ;
43   const char* getHost () const ;
44   unsigned int getPort() const ;
45   unsigned int getIP () const ;
46   unsigned int getFamily () const ;
47   static const char* getLocalHost () ;
48
49   bool getBroadcast () const ;
50   
51   void setPort(unsigned int port);
52   const struct sockaddr_in* getAddress() const
53       { return &_raw; }
54       
55   const size_t getAddressSize() const
56       { return sizeof(struct sockaddr_in); }
57 private:
58     struct sockaddr_in _raw;
59 };
60
61
62 /*
63  * Socket type
64  */
65 class Socket
66 {
67   int handle ;
68
69 public:
70   
71   Socket () ;
72   virtual ~Socket () ;
73
74   static int initSockets();
75
76   int getHandle () const { return handle; }
77   void setHandle (int handle) ;
78   
79   bool  open        ( bool stream=true ) ;
80   void  close       ( void ) ;
81   int   bind        ( const char* host, int port ) ;
82   int   listen      ( int backlog ) ;
83   int   accept      ( IPAddress* addr ) ;
84   int   connect     ( const char* host, int port ) ;
85   int   connect     ( const IPAddress& addr ) ;
86   int   send        ( const void * buffer, int size, int flags = 0 ) ;
87   int   sendto      ( const void * buffer, int size, int flags, const IPAddress* to ) ;
88   int   recv        ( void * buffer, int size, int flags = 0 ) ;
89   int   recvfrom    ( void * buffer, int size, int flags, IPAddress* from ) ;
90
91   void setBlocking ( bool blocking ) ;
92   void setBroadcast ( bool broadcast ) ;
93
94   static bool isNonBlockingError () ;
95   
96   static int select ( Socket** reads, Socket** writes, int timeout ) ;
97 } ;
98
99 //const char* netFormat ( const char* fmt, ... ) ;
100
101 } // of namespace simgear
102
103 #endif // SG_IO_SOCKET_HXX
104