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