Panda3D
connectionListener.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file connectionListener.cxx
10  * @author drose
11  * @date 2000-02-09
12  */
13 
14 #include "dcast.h"
15 #include "connectionListener.h"
16 #include "connection.h"
17 #include "connectionManager.h"
18 #include "netAddress.h"
19 #include "config_net.h"
20 #include "socket_tcp_listen.h"
21 
22 static std::string
23 listener_thread_name(const std::string &thread_name) {
24  if (!thread_name.empty()) {
25  return thread_name;
26  }
27  return "ListenerThread";
28 }
29 
30 /**
31  *
32  */
33 ConnectionListener::
34 ConnectionListener(ConnectionManager *manager, int num_threads,
35  const std::string &thread_name) :
36  ConnectionReader(manager, num_threads, listener_thread_name(thread_name))
37 {
38 }
39 
40 /**
41  * This function must be declared because it is pure virtual in the base
42  * class, but it isn't used in this class and doesn't do anything.
43  */
44 void ConnectionListener::
45 receive_datagram(const NetDatagram &) {
46  net_cat.error()
47  << "ConnectionListener::receive_datagram called.\n";
48 }
49 
50 /**
51  * This is the function that is called when activity is detected on a
52  * rendezvous port. In this case, it performs the accept().
53  */
54 bool ConnectionListener::
55 process_incoming_data(SocketInfo *sinfo) {
56  Socket_TCP_Listen *socket;
57  DCAST_INTO_R(socket, sinfo->get_socket(), false);
58 
59  Socket_Address addr;
60  Socket_TCP *session = new Socket_TCP;
61 
62  bool got_connection = socket->GetIncomingConnection(*session, addr);
63 #if defined(HAVE_THREADS) && defined(SIMPLE_THREADS)
64  while (!got_connection && socket->GetLastError() == LOCAL_BLOCKING_ERROR) {
66  got_connection = socket->GetIncomingConnection(*session, addr);
67  }
68 #endif // SIMPLE_THREADS
69 
70  if (!got_connection) {
71  net_cat.error()
72  << "Error when accepting new connection.\n";
73  delete session;
74  finish_socket(sinfo);
75  return false;
76  }
77 
78  NetAddress net_addr(addr);
79  net_cat.info()
80  << "Received TCP connection from client " << net_addr
81  << " on port " << sinfo->_connection->get_address().get_port()
82  << "\n";
83 
84  PT(Connection) new_connection = new Connection(_manager, session);
85  if (_manager != nullptr) {
86  _manager->new_connection(new_connection);
87  }
88  connection_opened(sinfo->_connection, net_addr, new_connection);
89 
90  finish_socket(sinfo);
91  return true;
92 }
Base functionality for a TCP connected socket This class is pretty useless by itself but it does hide...
Definition: socket_tcp.h:12
A specific kind of Datagram, especially for sending across or receiving from a network.
Definition: netDatagram.h:40
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
The primary interface to the low-level networking layer in this package.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static void force_yield()
Suspends the current thread for the rest of the current epoch.
Definition: thread.I:201
This is an abstract base class for a family of classes that listen for activity on a socket and respo...
Base functionality for a TCP rendezvous socket.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A simple place to store and manipulate tcp and port address for communication layer.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static int GetLastError()
Gets the last errcode from a socket operation.
Definition: socket_ip.h:140
Represents a single TCP or UDP socket for input or output.
Definition: connection.h:29
Represents a network address to which UDP packets may be sent or to which a TCP socket may be bound.
Definition: netAddress.h:25