Panda3D
 All Classes Functions Variables Enumerations
recentConnectionReader.cxx
1 // Filename: recentConnectionReader.cxx
2 // Created by: drose (23Jun00)
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 "recentConnectionReader.h"
16 #include "config_net.h"
17 #include "lightMutexHolder.h"
18 
19 ////////////////////////////////////////////////////////////////////
20 // Function: RecentConnectionReader::Constructor
21 // Access: Public
22 // Description:
23 ////////////////////////////////////////////////////////////////////
24 RecentConnectionReader::
25 RecentConnectionReader(ConnectionManager *manager) :
26  ConnectionReader(manager, 1)
27 {
28  // We should not receive any datagrams before the constructor is
29  // done initializing, or our thread may get confused. Fortunately
30  // this should be impossible, because we can't receive datagrams
31  // before we call add_connection().
32  _available = false;
33 }
34 
35 ////////////////////////////////////////////////////////////////////
36 // Function: RecentConnectionReader::Destructor
37 // Access: Public, Virtual
38 // Description:
39 ////////////////////////////////////////////////////////////////////
40 RecentConnectionReader::
41 ~RecentConnectionReader() {
42  // We call shutdown() here to guarantee that all threads are gone
43  // before the RecentConnectionReader destructs.
44  shutdown();
45 }
46 
47 ////////////////////////////////////////////////////////////////////
48 // Function: RecentConnectionReader::data_available
49 // Access: Public
50 // Description: Returns true if a datagram is available on the queue;
51 // call get_data() to extract the datagram.
52 ////////////////////////////////////////////////////////////////////
55  return _available;
56 }
57 
58 ////////////////////////////////////////////////////////////////////
59 // Function: RecentConnectionReader::get_data
60 // Access: Public
61 // Description: If a previous call to data_available() returned
62 // true, this function will return the datagram that has
63 // become available.
64 //
65 // The return value is true if a datagram was
66 // successfully returned, or false if there was, in
67 // fact, no datagram available. (This may happen if
68 // there are multiple threads accessing the
69 // RecentConnectionReader).
70 ////////////////////////////////////////////////////////////////////
73  LightMutexHolder holder(_mutex);
74  if (!_available) {
75  // Huh. Nothing after all.
76  return false;
77  }
78 
79  result = _datagram;
80  _available = false;
81  return true;
82 }
83 
84 ////////////////////////////////////////////////////////////////////
85 // Function: RecentConnectionReader::get_data
86 // Access: Public
87 // Description: This flavor of RecentConnectionReader::get_data(),
88 // works like the other, except that it only fills a
89 // Datagram object, not a NetDatagram object. This
90 // means that the Datagram cannot be queried for its
91 // source Connection and/or NetAddress, but it is useful
92 // in all other respects.
93 ////////////////////////////////////////////////////////////////////
95 get_data(Datagram &result) {
96  NetDatagram nd;
97  if (!get_data(nd)) {
98  return false;
99  }
100  result = nd;
101  return true;
102 }
103 
104 ////////////////////////////////////////////////////////////////////
105 // Function: RecentConnectionReader::receive_datagram
106 // Access: Protected, Virtual
107 // Description: An internal function called by ConnectionReader()
108 // when a new datagram has become available. The
109 // RecentConnectionReader simply queues it up for later
110 // retrieval by get_data().
111 ////////////////////////////////////////////////////////////////////
112 void RecentConnectionReader::
113 receive_datagram(const NetDatagram &datagram) {
114  if (net_cat.is_debug()) {
115  net_cat.debug()
116  << "Received datagram of " << datagram.get_length()
117  << " bytes\n";
118  }
119 
120  LightMutexHolder holder(_mutex);
121  _datagram = datagram;
122  _available = true;
123 }
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.
void shutdown()
Terminates all threads cleanly.
size_t get_length() const
Returns the number of bytes in the datagram.
Definition: datagram.I:457
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...
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.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43