Panda3D

Loading...
Searching...
No Matches
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
23listener_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 */
33ConnectionListener::
34ConnectionListener(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::
45receive_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::
55process_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) {
65 Thread::force_yield();
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}
ConnectionManager
The primary interface to the low-level networking layer in this package.
Definition connectionManager.h:41
ConnectionReader
This is an abstract base class for a family of classes that listen for activity on a socket and respo...
Definition connectionReader.h:50
NetDatagram
A specific kind of Datagram, especially for sending across or receiving from a network.
Definition netDatagram.h:40
Socket_IP::GetLastError
static int GetLastError()
Gets the last errcode from a socket operation.
Definition socket_ip.h:148
Thread::force_yield
static void force_yield()
Suspends the current thread for the rest of the current epoch.
Definition thread.I:201
config_net.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
connectionListener.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
connectionManager.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
connection.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
dcast.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
netAddress.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.