Panda3D
 All Classes Functions Variables Enumerations
queuedConnectionListener.cxx
1 // Filename: queuedConnectionListener.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 "queuedConnectionListener.h"
16 #include "config_net.h"
17 
18 ////////////////////////////////////////////////////////////////////
19 // Function: QueuedConnectionListener::Constructor
20 // Access: Public
21 // Description:
22 ////////////////////////////////////////////////////////////////////
23 QueuedConnectionListener::
24 QueuedConnectionListener(ConnectionManager *manager, int num_threads) :
25  ConnectionListener(manager, num_threads)
26 {
27 }
28 
29 ////////////////////////////////////////////////////////////////////
30 // Function: QueuedConnectionListener::Destructor
31 // Access: Public, Virtual
32 // Description:
33 ////////////////////////////////////////////////////////////////////
34 QueuedConnectionListener::
35 ~QueuedConnectionListener() {
36  // We call shutdown() here to guarantee that all threads are gone
37  // before the QueuedReturn destructs.
38  shutdown();
39 }
40 
41 ////////////////////////////////////////////////////////////////////
42 // Function: QueuedConnectionListener::new_connection_available
43 // Access: Public
44 // Description: Returns true if a new connection was recently
45 // established; the connection information may then be
46 // retrieved via get_new_connection().
47 ////////////////////////////////////////////////////////////////////
50  poll();
51  return thing_available();
52 }
53 
54 ////////////////////////////////////////////////////////////////////
55 // Function: QueuedConnectionListener::get_new_connection
56 // Access: Public
57 // Description: If a previous call to new_connection_available()
58 // returned true, this function will return information
59 // about the newly established connection.
60 //
61 // The rendezvous parameter is the particular rendezvous
62 // socket this new connection originally communicated
63 // with; it is provided in case the ConnectionListener
64 // was monitorind more than one and you care which one
65 // it was. The address parameter is the net address of
66 // the new client, and new_connection is the socket of
67 // the newly established connection.
68 //
69 // The return value is true if a connection was
70 // successfully returned, or false if there was, in
71 // fact, no new connection. (This may happen if there
72 // are multiple threads accessing the
73 // QueuedConnectionListener).
74 ////////////////////////////////////////////////////////////////////
77  NetAddress &address,
78  PT(Connection) &new_connection) {
80  if (!get_thing(result)) {
81  return false;
82  }
83 
84  rendezvous = result._rendezvous;
85  address = result._address;
86  new_connection = result._new_connection;
87  return true;
88 }
89 
90 ////////////////////////////////////////////////////////////////////
91 // Function: QueuedConnectionListener::get_new_connection
92 // Access: Public
93 // Description: This flavor of get_new_connection() simply returns a
94 // new connection, assuming the user doesn't care about
95 // the rendezvous socket that originated it or the
96 // address it came from.
97 ////////////////////////////////////////////////////////////////////
99 get_new_connection(PT(Connection) &new_connection) {
100  PT(Connection) rendezvous;
101  NetAddress address;
102  return get_new_connection(rendezvous, address, new_connection);
103 }
104 
105 
106 ////////////////////////////////////////////////////////////////////
107 // Function: QueuedConnectionListener::connection_opened
108 // Access: Protected, Virtual
109 // Description: An internal function called by ConnectionListener()
110 // when a new TCP connection has been established. The
111 // QueuedConnectionListener simply queues up this fact
112 // for later retrieval by get_new_connection().
113 ////////////////////////////////////////////////////////////////////
114 void QueuedConnectionListener::
115 connection_opened(const PT(Connection) &rendezvous,
116  const NetAddress &address,
117  const PT(Connection) &new_connection) {
119  nc._rendezvous = rendezvous;
120  nc._address = address;
121  nc._new_connection = new_connection;
122 
123  if (!enqueue_thing(nc)) {
124  net_cat.error()
125  << "QueuedConnectionListener queue full!\n";
126  }
127 }
The primary interface to the low-level networking layer in this package.
void shutdown()
Terminates all threads cleanly.
void poll()
Explicitly polls the available sockets to see if any of them have any noise.
bool new_connection_available()
Returns true if a new connection was recently established; the connection information may then be ret...
Represents a single TCP or UDP socket for input or output.
Definition: connection.h:32
This is a special kind of ConnectionReader that waits for activity on a rendezvous port and accepts a...
bool get_new_connection(PT(Connection)&rendezvous, NetAddress &address, PT(Connection)&new_connection)
If a previous call to new_connection_available() returned true, this function will return information...
Represents a network address to which UDP packets may be sent or to which a TCP socket may be bound...
Definition: netAddress.h:27