Panda3D
socket_tcp_listen.h
1 #ifndef __SOCKET_TCP_LISTEN_H__
2 #define __SOCKET_TCP_LISTEN_H__
3 
4 #include "pandabase.h"
5 #include "socket_ip.h"
6 #include "socket_tcp.h"
7 
8 /**
9  * Base functionality for a TCP rendezvous socket
10  */
11 class EXPCL_PANDA_NATIVENET Socket_TCP_Listen : public Socket_IP {
12 public:
13 PUBLISHED:
14  Socket_TCP_Listen() {};
15  ~Socket_TCP_Listen() {};
16  inline bool OpenForListen(unsigned short port, int backlog_size = 1024);
17  inline bool OpenForListen(const Socket_Address &address, int backlog_size = 1024);
18  inline bool GetIncomingConnection(Socket_TCP &newsession, Socket_Address &address);
19 
20 public:
21  inline bool GetIncomingConnection(SOCKET &newsession, Socket_Address &address);
22 
23 public:
24  static TypeHandle get_class_type() {
25  return _type_handle;
26  }
27  static void init_type() {
28  Socket_IP::init_type();
29  register_type(_type_handle, "Socket_TCP_Listen",
30  Socket_IP::get_class_type());
31  }
32  virtual TypeHandle get_type() const {
33  return get_class_type();
34  }
35  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
36 
37 private:
38  static TypeHandle _type_handle;
39 };
40 
41 /**
42  * This function will initialize a listening Socket
43  */
44 inline bool Socket_TCP_Listen::
45 OpenForListen(unsigned short port, int backlog_size) {
46  ErrorClose();
47 
48  Socket_Address address;
49  if (support_ipv6) {
50  // Create a socket supporting both IPv4 and IPv6.
51  address.set_any_IPv6(port);
52  _socket = DO_NEWTCP(AF_INET6);
53  SetV6Only(false);
54  } else {
55  address.set_any_IP(port);
56  _socket = DO_NEWTCP(AF_INET);
57  }
58 
60 
61  if (DO_BIND(_socket, &address.GetAddressInfo()) != 0) {
62  return ErrorClose();
63  }
64 
65  if (DO_LISTEN(_socket, backlog_size) != 0) {
66  return ErrorClose();
67  }
68 
69  return true;
70 }
71 
72 /**
73  * This function will initialize a listening Socket
74  */
75 inline bool Socket_TCP_Listen::
76 OpenForListen(const Socket_Address &address, int backlog_size) {
77  ErrorClose();
78 
79  _socket = DO_NEWTCP(address.get_family());
80 
82 
83  if (DO_BIND(_socket, &address.GetAddressInfo()) != 0) {
84  return ErrorClose();
85  }
86 
87  if (DO_LISTEN(_socket, backlog_size) != 0) {
88  return ErrorClose();
89  }
90 
91  return true;
92 }
93 
94 /**
95  * This function is used to accept new connections
96  */
97 inline bool Socket_TCP_Listen::
98 GetIncomingConnection(SOCKET &newsession, Socket_Address &address) {
99  newsession = DO_ACCEPT(_socket, &address.GetAddressInfo());
100  if (newsession == BAD_SOCKET) {
101  return false;
102  }
103  return true;
104 }
105 
106 inline bool Socket_TCP_Listen::
107 GetIncomingConnection(Socket_TCP &newsession, Socket_Address &address) {
108  SOCKET sck;
109  if (!GetIncomingConnection(sck, address)) {
110  return false;
111  }
112 
113  newsession.SetSocket(sck);
114  return true;
115 }
116 
117 #endif //__SOCKET_TCP_LISTEN_H__
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 INET domain Socket This call should be the starting point for all other unix...
Definition: socket_ip.h:27
bool set_any_IP(unsigned short port)
Set to any address and a specified port.
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
Base functionality for a TCP rendezvous socket.
void SetSocket(SOCKET ins)
Assigns an existing socket to this class.
Definition: socket_ip.h:148
bool set_any_IPv6(unsigned short port)
Set to any IPv6 address and a specified port.
sa_family_t get_family() const
Returns AF_INET if this is an IPv4 address, or AF_INET6 if this is an IPv6 address.
A simple place to store and manipulate tcp and port address for communication layer.
bool SetReuseAddress(bool flag=true)
Informs a socket to reuse IP address as needed.
Definition: socket_ip.h:212
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
bool OpenForListen(unsigned short port, int backlog_size=1024)
This function will initialize a listening Socket.