Panda3D
Loading...
Searching...
No Matches
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 */
11class EXPCL_PANDA_NATIVENET Socket_TCP_Listen : public Socket_IP {
12public:
13PUBLISHED:
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
20public:
21 inline bool GetIncomingConnection(SOCKET &newsession, Socket_Address &address);
22
23public:
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
37private:
38 static TypeHandle _type_handle;
39};
40
41/**
42 * This function will initialize a listening Socket
43 */
45OpenForListen(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 */
76OpenForListen(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 */
97inline bool Socket_TCP_Listen::
98GetIncomingConnection(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
106inline bool Socket_TCP_Listen::
107GetIncomingConnection(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__
A simple place to store and manipulate tcp and port address for communication layer.
sa_family_t get_family() const
Returns AF_INET if this is an IPv4 address, or AF_INET6 if this is an IPv6 address.
bool set_any_IPv6(unsigned short port)
Set to any IPv6 address and a specified port.
bool set_any_IP(unsigned short port)
Set to any address and a specified port.
Base functionality for a INET domain Socket This call should be the starting point for all other unix...
Definition socket_ip.h:27
bool SetReuseAddress(bool flag=true)
Informs a socket to reuse IP address as needed.
Definition socket_ip.h:212
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
Base functionality for a TCP rendezvous socket.
bool OpenForListen(unsigned short port, int backlog_size=1024)
This function will initialize a listening Socket.
Base functionality for a TCP connected socket This class is pretty useless by itself but it does hide...
Definition socket_tcp.h:12
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
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(),...