Panda3D
recentConnectionReader.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 recentConnectionReader.cxx
10  * @author drose
11  * @date 2000-06-23
12  */
13 
14 #include "recentConnectionReader.h"
15 #include "config_net.h"
16 #include "lightMutexHolder.h"
17 
18 /**
19  *
20  */
21 RecentConnectionReader::
22 RecentConnectionReader(ConnectionManager *manager) :
23  ConnectionReader(manager, 1)
24 {
25  // We should not receive any datagrams before the constructor is done
26  // initializing, or our thread may get confused. Fortunately this should be
27  // impossible, because we can't receive datagrams before we call
28  // add_connection().
29  _available = false;
30 }
31 
32 /**
33  *
34  */
35 RecentConnectionReader::
36 ~RecentConnectionReader() {
37  // We call shutdown() here to guarantee that all threads are gone before the
38  // RecentConnectionReader destructs.
39  shutdown();
40 }
41 
42 /**
43  * Returns true if a datagram is available on the queue; call get_data() to
44  * extract the datagram.
45  */
48  return _available;
49 }
50 
51 /**
52  * If a previous call to data_available() returned true, this function will
53  * return the datagram that has become available.
54  *
55  * The return value is true if a datagram was successfully returned, or false
56  * if there was, in fact, no datagram available. (This may happen if there
57  * are multiple threads accessing the RecentConnectionReader).
58  */
61  LightMutexHolder holder(_mutex);
62  if (!_available) {
63  // Huh. Nothing after all.
64  return false;
65  }
66 
67  result = _datagram;
68  _available = false;
69  return true;
70 }
71 
72 /**
73  * This flavor of RecentConnectionReader::get_data(), works like the other,
74  * except that it only fills a Datagram object, not a NetDatagram object.
75  * This means that the Datagram cannot be queried for its source Connection
76  * and/or NetAddress, but it is useful in all other respects.
77  */
79 get_data(Datagram &result) {
80  NetDatagram nd;
81  if (!get_data(nd)) {
82  return false;
83  }
84  result = nd;
85  return true;
86 }
87 
88 /**
89  * An internal function called by ConnectionReader() when a new datagram has
90  * become available. The RecentConnectionReader simply queues it up for later
91  * retrieval by get_data().
92  */
93 void RecentConnectionReader::
94 receive_datagram(const NetDatagram &datagram) {
95  if (net_cat.is_debug()) {
96  net_cat.debug()
97  << "Received datagram of " << datagram.get_length()
98  << " bytes\n";
99  }
100 
101  LightMutexHolder holder(_mutex);
102  _datagram = datagram;
103  _available = true;
104 }
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.
void shutdown()
Terminates all threads cleanly.
bool get_data(NetDatagram &result)
If a previous call to data_available() returned true, this function will return the datagram that has...
This is an abstract base class for a family of classes that listen for activity on a socket and respo...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool data_available()
Returns true if a datagram is available on the queue; call get_data() to extract the datagram.
Similar to MutexHolder, but for a light mutex.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
size_t get_length() const
Returns the number of bytes in the datagram.
Definition: datagram.I:335