Panda3D
socket_ip.h
1 #ifndef __SOCKET_IP_H__
2 #define __SOCKET_IP_H__
3 
4 #include "pandabase.h"
5 #include "socket_portable.h"
6 #include "socket_address.h"
7 #include "typedObject.h"
8 
9 // forward declarations for friends...
10 class Socket_TCP;
11 class Socket_UDP;
12 class Socket_TCP_Listen;
15 /////////////////////////////////////////////////////////////////////
16 // Class : Socket_IP
17 //
18 // Description : Base functionality for a INET domain Socket
19 // this call should be the starting point for all other
20 // unix domain sockets
21 //
22 //
23 // SocketIP
24 // |
25 // -------------------------------------------------------------------
26 // | | | |
27 // SocketTCP SocketTCP_Listen SocketUDP_Incoming SocketUDP_OutBound
28 //
29 //
30 //
31 // socket_fdset
32 //
33 /////////////////////////////////////////////////////////////////////
34 class EXPCL_PANDA_NATIVENET Socket_IP : public TypedObject {
35 public:
36 PUBLISHED:
37 
38  inline Socket_IP();
39  inline Socket_IP(SOCKET in);
40  virtual ~Socket_IP();
41 
42  inline void Close();
43  inline static int GetLastError();
44  inline int SetNonBlocking();
45  inline int SetBlocking();
46  inline bool SetReuseAddress(bool flag = true);
47  inline bool Active();
48  inline int SetRecvBufferSize(int size);
49  inline void SetSocket(SOCKET ins);
50  inline SOCKET GetSocket();
51  inline SOCKET GetSocket() const;
52  inline Socket_Address GetPeerName(void) const;
53 
54 
55  inline static int InitNetworkDriver() { return init_network(); };
56 
57 public:
58 private:
59  inline bool ErrorClose();
60 
61  SOCKET _socket; // see socket_portable.h
62 
63  friend class Socket_TCP;
64  friend class Socket_UDP;
65  friend class Socket_TCP_Listen;
66  friend class Socket_UDP_Incoming;
67  friend class Socket_UDP_Outgoing;
68  friend class Socket_TCP_SSL;
69 
70 public:
71  static TypeHandle get_class_type() {
72  return _type_handle;
73  }
74  static void init_type() {
76  register_type(_type_handle, "Socket_IP",
77  TypedObject::get_class_type());
78  }
79  virtual TypeHandle get_type() const {
80  return get_class_type();
81  }
82  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
83 
84 private:
85  static TypeHandle _type_handle;
86 };
87 
88 ////////////////////////////////////////////////////////////////////
89 // Function name : Socket_IP::ErrorClose
90 // Description : Used by internal to force a close
91 // note that it always returns a false
92 ////////////////////////////////////////////////////////////////////
93 inline bool Socket_IP::ErrorClose()
94 {
95  if (Active())
96  DO_CLOSE(_socket);
97  _socket = BAD_SOCKET;
98  return false;
99 }
100 
101 ////////////////////////////////////////////////////////////////////
102 // Function name : Socket_IP::Active
103 // Description : Ask if the socket is open (allocated)
104 ////////////////////////////////////////////////////////////////////
105 inline bool Socket_IP::Active()
106 {
107  return (_socket != BAD_SOCKET);
108 }
109 
110 ////////////////////////////////////////////////////////////////////
111 // Function name : Socket_IP::Socket_IP
112 // Description : Def Constructor
113 ////////////////////////////////////////////////////////////////////
115 {
116  _socket = BAD_SOCKET;
117 }
118 
119 ////////////////////////////////////////////////////////////////////
120 // Function name : Socket_IP::SetSocket
121 // Description : Assigns an existing socket to this class
122 ////////////////////////////////////////////////////////////////////
123 inline Socket_IP::Socket_IP(SOCKET ins)
124 {
125  _socket = ins;
126 }
127 
128 ////////////////////////////////////////////////////////////////////
129 // Function name : Socket_IP::~Socket_IP
130 // Description : Destructor
131 ////////////////////////////////////////////////////////////////////
133 {
134  Close();
135 }
136 
137 ////////////////////////////////////////////////////////////////////
138 // Function name : Socket_IP::Close
139 // Description : closes a socket if it is open (allocated)
140 ////////////////////////////////////////////////////////////////////
141 inline void Socket_IP::Close()
142 {
143  if (Active())
144  DO_CLOSE(_socket);
145  _socket = BAD_SOCKET;
146 }
147 
148 ////////////////////////////////////////////////////////////////////
149 // Function name : Socket_IP::GetLastError
150 // Description : gets the last errcode from a socket operation
151 ////////////////////////////////////////////////////////////////////
153 {
154  return GETERROR();
155 }
156 
157 ////////////////////////////////////////////////////////////////////
158 // Function name : Socket_IP::SetSocket
159 // Description : Assigns an existing socket to this class
160 ////////////////////////////////////////////////////////////////////
161 inline void Socket_IP::SetSocket(SOCKET ins)
162 {
163  Close();
164  _socket = ins;
165 }
166 
167 ////////////////////////////////////////////////////////////////////
168 // Function name : Socket_IP::SetRecvBufferSize
169 // Description : Ok it sets the recv buffer size for both tcp and UDP
170 ////////////////////////////////////////////////////////////////////
172 {
173  if (setsockopt(_socket, (int) SOL_SOCKET, (int) SO_RCVBUF, (char *) &insize, sizeof(int)))
174  return BASIC_ERROR;
175 
176  return ALL_OK;
177 }
178 
179 ////////////////////////////////////////////////////////////////////
180 // Function name : SetNonBlocking
181 // Description : this function will throw a socket into non-blocking mode
182 ////////////////////////////////////////////////////////////////////
184 {
185 #ifdef BSDBLOCK
186 
187  int flags = fcntl(_socket, F_GETFL, 0);
188  flags = flags | O_NONBLOCK;
189  fcntl(_socket, F_SETFL, flags);
190  return ALL_OK;
191 #else
192  unsigned long val = LOCAL_NONBLOCK;
193  unsigned lanswer = 0;
194  lanswer = SOCKIOCTL(_socket, LOCAL_FL_SET, &val);
195  if (lanswer != 0)
196  return BASIC_ERROR;
197  return ALL_OK;
198 
199 #endif
200 }
201 
202 ////////////////////////////////////////////////////////////////////
203 // Function name : Socket_IP::SetBlocking
204 // Description : Set the socket to block on subsequent calls to
205 // socket functions that address this socket
206 ////////////////////////////////////////////////////////////////////
208 {
209 #ifdef BSDBLOCK
210  int flags = fcntl(_socket, F_GETFL, 0);
211  flags &= ~O_NONBLOCK;
212  fcntl(_socket, F_SETFL, flags);
213  return ALL_OK;
214 #else
215  unsigned long val = 0;
216  unsigned lanswer = 0;
217  lanswer = SOCKIOCTL(_socket, LOCAL_FL_SET, &val);
218  if (lanswer != 0)
219  return BASIC_ERROR;
220  return ALL_OK;
221 #endif
222 }
223 
224 ////////////////////////////////////////////////////////////////////
225 // Function name : SetReuseAddress
226 // Description : Informs a socket to reuse IP address as needed
227 ////////////////////////////////////////////////////////////////////
228 inline bool Socket_IP::SetReuseAddress(bool flag)
229 {
230  int bOption = flag;
231  if (setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&bOption, sizeof(bOption)) != 0)
232  return false;
233  return true;
234 }
235 
236 ////////////////////////////////////////////////////////////////////
237 // Function name : Socket_IP::GetSocket
238 // Description : Gets the base socket type
239 ////////////////////////////////////////////////////////////////////
240 inline SOCKET Socket_IP::GetSocket()
241 {
242  return _socket;
243 }
244 
245 //////////////////////////////////////////////////////////////
246 // Function name : Socket_IP::GetSocket
247 // Description : Get The RAW file id of the socket
248 //////////////////////////////////////////////////////////////
249 inline SOCKET Socket_IP::GetSocket() const
250 {
251  return _socket;
252 }
253 //////////////////////////////////////////////////////////////
254 // Function name : Socket_IP::GetPeerName
255 // Description : Wrapper on berkly getpeername...
256 //////////////////////////////////////////////////////////////
258 {
259  sockaddr_in name;
260  socklen_t name_len = sizeof(name);
261  memset(&name,0,name_len);
262 
263  getpeername(_socket,(sockaddr * )&name,&name_len);
264  return Socket_Address(name);
265 }
266 
267 
268 #endif //__SOCKET_IP_H__
int SetRecvBufferSize(int size)
Ok it sets the recv buffer size for both tcp and UDP.
Definition: socket_ip.h:171
Base functionality for a TCP connected socket This class is pretty useless by itself but it does hide...
Definition: socket_tcp.h:15
static void init_type()
This function is declared non-inline to work around a compiler bug in g++ 2.96.
Definition: typedObject.cxx:52
Base functionality for a INET domain Socket this call should be the starting point for all other unix...
Definition: socket_ip.h:34
int SetBlocking()
Set the socket to block on subsequent calls to socket functions that address this socket...
Definition: socket_ip.h:207
int SetNonBlocking()
this function will throw a socket into non-blocking mode
Definition: socket_ip.h:183
Base functionality for a UDP Sending Socket.
This is an abstract class that all classes which use TypeHandle, and also provide virtual functions t...
Definition: typedObject.h:98
Socket_Address GetPeerName(void) const
Wrapper on berkly getpeername...
Definition: socket_ip.h:257
bool Active()
Ask if the socket is open (allocated)
Definition: socket_ip.h:105
Base functionality for a TCP rendezvous socket.
void SetSocket(SOCKET ins)
Assigns an existing socket to this class.
Definition: socket_ip.h:161
Base functionality for a combination UDP Reader and Writer.
Definition: socket_udp.h:28
Base functionality for a UDP Reader.
virtual ~Socket_IP()
Destructor.
Definition: socket_ip.h:132
A simple place to store and munipulate tcp and port address for communication layer.
Socket_IP()
Def Constructor.
Definition: socket_ip.h:114
static int GetLastError()
gets the last errcode from a socket operation
Definition: socket_ip.h:152
bool SetReuseAddress(bool flag=true)
Informs a socket to reuse IP address as needed.
Definition: socket_ip.h:228
void Close()
closes a socket if it is open (allocated)
Definition: socket_ip.h:141
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
SOCKET GetSocket()
Gets the base socket type.
Definition: socket_ip.h:240