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 if (DO_CLOSE(_socket) != 0) {
86#ifndef _WIN32
87 perror("Socket_IP::ErrorClose");
88#endif
89 }
90 }
91
92 _socket = BAD_SOCKET;
93 return false;
94}
95
96/**
97 * Ask if the socket is open (allocated)
98 */
99inline bool Socket_IP::
100Active() {
101 return (_socket != BAD_SOCKET);
102}
103
104/**
105 * Def Constructor
106 */
108Socket_IP() {
109 _socket = BAD_SOCKET;
110}
111
112/**
113 * Assigns an existing socket to this class
114 */
116Socket_IP(SOCKET ins) {
117 _socket = ins;
118}
119
120/**
121 * Destructor
122 */
124~Socket_IP() {
125 Close();
126}
127
128/**
129 * Closes a socket if it is open (allocated).
130 */
131inline void Socket_IP::
132Close() {
133 if (Active()) {
134 if (DO_CLOSE(_socket) != 0) {
135#ifndef _WIN32
136 perror("Socket_IP::ErrorClose");
137#endif
138 }
139 }
140
141 _socket = BAD_SOCKET;
142}
143
144/**
145 * Gets the last errcode from a socket operation.
146 */
147inline int Socket_IP::
148GetLastError() {
149 return GETERROR();
150}
151
152/**
153 * Assigns an existing socket to this class
154 */
155inline void Socket_IP::
156SetSocket(SOCKET ins) {
157 Close();
158 _socket = ins;
159}
160
161/**
162 * Ok it sets the recv buffer size for both tcp and UDP
163 */
165SetRecvBufferSize(int insize) {
166 if (setsockopt(_socket, (int) SOL_SOCKET, (int) SO_RCVBUF, (char *) &insize, sizeof(int))) {
167 return BASIC_ERROR;
168 }
169
170 return ALL_OK;
171}
172
173/**
174 * this function will throw a socket into non-blocking mode
175 */
176inline int Socket_IP::
178#ifdef BSDBLOCK
179 int flags = fcntl(_socket, F_GETFL, 0);
180 flags = flags | O_NONBLOCK;
181 fcntl(_socket, F_SETFL, flags);
182 return ALL_OK;
183#else
184 unsigned long val = LOCAL_NONBLOCK;
185 unsigned lanswer = 0;
186 lanswer = SOCKIOCTL(_socket, LOCAL_FL_SET, &val);
187 if (lanswer != 0) {
188 return BASIC_ERROR;
189 }
190 return ALL_OK;
191#endif
192}
193
194/**
195 * Set the socket to block on subsequent calls to socket functions that
196 * address this socket
197 */
198inline int Socket_IP::
199SetBlocking() {
200#ifdef BSDBLOCK
201 int flags = fcntl(_socket, F_GETFL, 0);
202 flags &= ~O_NONBLOCK;
203 fcntl(_socket, F_SETFL, flags);
204 return ALL_OK;
205#else
206 unsigned long val = 0;
207 unsigned lanswer = 0;
208 lanswer = SOCKIOCTL(_socket, LOCAL_FL_SET, &val);
209 if (lanswer != 0) {
210 return BASIC_ERROR;
211 }
212 return ALL_OK;
213#endif
214}
215
216/**
217 * Informs a socket to reuse IP address as needed
218 */
219inline bool Socket_IP::
220SetReuseAddress(bool flag) {
221 int value = flag;
222 if (setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&value, sizeof(value)) != 0) {
223 return false;
224 }
225 return true;
226}
227
228/**
229 * Sets a flag indicating whether this IPv6 socket should operate in
230 * dual-stack mode or not.
231 */
232inline bool Socket_IP::
233SetV6Only(bool flag) {
234 int value = flag ? 1 : 0;
235 if (setsockopt(_socket, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&value, sizeof(value))) {
236 return false;
237 }
238 return true;
239}
240
241/**
242 * Gets the base socket type
243 */
244inline SOCKET Socket_IP::
245GetSocket() {
246 return _socket;
247}
248
249/**
250 * Get The RAW file id of the socket
251 */
252inline SOCKET Socket_IP::
253GetSocket() const {
254 return _socket;
255}
256
257/**
258 * Wrapper on berkly getpeername...
259 */
261GetPeerName(void) const {
262 sockaddr_storage name;
263 socklen_t name_len = sizeof(name);
264 memset(&name, 0, name_len);
265
266 getpeername(_socket, (sockaddr *)&name, &name_len);
267 return Socket_Address(name);
268}
269
270#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:245
Socket_IP()
Def Constructor.
Definition socket_ip.h:108
bool SetReuseAddress(bool flag=true)
Informs a socket to reuse IP address as needed.
Definition socket_ip.h:220
static int GetLastError()
Gets the last errcode from a socket operation.
Definition socket_ip.h:148
int SetBlocking()
Set the socket to block on subsequent calls to socket functions that address this socket.
Definition socket_ip.h:199
bool SetV6Only(bool flag)
Sets a flag indicating whether this IPv6 socket should operate in dual-stack mode or not.
Definition socket_ip.h:233
void SetSocket(SOCKET ins)
Assigns an existing socket to this class.
Definition socket_ip.h:156
void Close()
Closes a socket if it is open (allocated).
Definition socket_ip.h:132
int SetRecvBufferSize(int size)
Ok it sets the recv buffer size for both tcp and UDP.
Definition socket_ip.h:165
bool Active()
Ask if the socket is open (allocated)
Definition socket_ip.h:100
Socket_Address GetPeerName(void) const
Wrapper on berkly getpeername...
Definition socket_ip.h:261
virtual ~Socket_IP()
Destructor.
Definition socket_ip.h:124
int SetNonBlocking()
this function will throw a socket into non-blocking mode
Definition socket_ip.h:177
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.