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