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