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 // Class : Socket_TCP_Listen
10 // Description : Base functionality for a TCP rendezvous socket
11 /////////////////////////////////////////////////////////////////////
12 class EXPCL_PANDA_NATIVENET Socket_TCP_Listen : public Socket_IP
13 {
14 public:
15 PUBLISHED:
16  Socket_TCP_Listen() {};
17  ~Socket_TCP_Listen() {};
18  inline bool OpenForListen(const Socket_Address & Inaddess, int backlog_size = 1024);
19  inline bool GetIncomingConnection(Socket_TCP & newsession, Socket_Address &address);
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 // Function name : OpenForListen
43 // Description : This function will initialize a listening Socket
44 ////////////////////////////////////////////////////////////////////
45 inline bool Socket_TCP_Listen::OpenForListen(const Socket_Address & Inaddess, int backlog_size )
46 {
47  ErrorClose();
48  _socket = DO_NEWTCP();
49 
51 
52  if (DO_BIND(_socket, &Inaddess.GetAddressInfo()) != 0) {
53  return ErrorClose();
54  }
55 
56  if (DO_LISTEN(_socket, backlog_size) != 0) {
57  return ErrorClose();
58  }
59 
60  return true;
61 }
62 ////////////////////////////////////////////////////////////////////
63 // Function name : GetIncomingConnection
64 // Description : This function is used to accept new connections
65 ////////////////////////////////////////////////////////////////////
66 inline bool Socket_TCP_Listen::GetIncomingConnection(SOCKET & newsession, Socket_Address &address)
67 {
68  newsession = DO_ACCEPT(_socket, &address.GetAddressInfo());
69  if (newsession == BAD_SOCKET)
70  return false;
71  return true;
72 }
73 
74 
75 inline bool Socket_TCP_Listen::GetIncomingConnection(Socket_TCP & newsession, Socket_Address &address)
76 {
77  SOCKET sck;
78  if(!GetIncomingConnection(sck,address))
79  return false;
80 
81  newsession.SetSocket(sck);
82  return true;
83 }
84 
85 #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:15
Base functionality for a INET domain Socket this call should be the starting point for all other unix...
Definition: socket_ip.h:34
bool OpenForListen(const Socket_Address &Inaddess, int backlog_size=1024)
This function will initialize a listening Socket.
Base functionality for a TCP rendezvous socket.
void SetSocket(SOCKET ins)
Assigns an existing socket to this class.
Definition: socket_ip.h:161
A simple place to store and munipulate 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:228
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85