Panda3D
|
00001 // Filename: recentConnectionReader.cxx 00002 // Created by: drose (23Jun00) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "recentConnectionReader.h" 00016 #include "config_net.h" 00017 #include "lightMutexHolder.h" 00018 00019 //////////////////////////////////////////////////////////////////// 00020 // Function: RecentConnectionReader::Constructor 00021 // Access: Public 00022 // Description: 00023 //////////////////////////////////////////////////////////////////// 00024 RecentConnectionReader:: 00025 RecentConnectionReader(ConnectionManager *manager) : 00026 ConnectionReader(manager, 1) 00027 { 00028 // We should not receive any datagrams before the constructor is 00029 // done initializing, or our thread may get confused. Fortunately 00030 // this should be impossible, because we can't receive datagrams 00031 // before we call add_connection(). 00032 _available = false; 00033 } 00034 00035 //////////////////////////////////////////////////////////////////// 00036 // Function: RecentConnectionReader::Destructor 00037 // Access: Public, Virtual 00038 // Description: 00039 //////////////////////////////////////////////////////////////////// 00040 RecentConnectionReader:: 00041 ~RecentConnectionReader() { 00042 // We call shutdown() here to guarantee that all threads are gone 00043 // before the RecentConnectionReader destructs. 00044 shutdown(); 00045 } 00046 00047 //////////////////////////////////////////////////////////////////// 00048 // Function: RecentConnectionReader::data_available 00049 // Access: Public 00050 // Description: Returns true if a datagram is available on the queue; 00051 // call get_data() to extract the datagram. 00052 //////////////////////////////////////////////////////////////////// 00053 bool RecentConnectionReader:: 00054 data_available() { 00055 return _available; 00056 } 00057 00058 //////////////////////////////////////////////////////////////////// 00059 // Function: RecentConnectionReader::get_data 00060 // Access: Public 00061 // Description: If a previous call to data_available() returned 00062 // true, this function will return the datagram that has 00063 // become available. 00064 // 00065 // The return value is true if a datagram was 00066 // successfully returned, or false if there was, in 00067 // fact, no datagram available. (This may happen if 00068 // there are multiple threads accessing the 00069 // RecentConnectionReader). 00070 //////////////////////////////////////////////////////////////////// 00071 bool RecentConnectionReader:: 00072 get_data(NetDatagram &result) { 00073 LightMutexHolder holder(_mutex); 00074 if (!_available) { 00075 // Huh. Nothing after all. 00076 return false; 00077 } 00078 00079 result = _datagram; 00080 _available = false; 00081 return true; 00082 } 00083 00084 //////////////////////////////////////////////////////////////////// 00085 // Function: RecentConnectionReader::get_data 00086 // Access: Public 00087 // Description: This flavor of RecentConnectionReader::get_data(), 00088 // works like the other, except that it only fills a 00089 // Datagram object, not a NetDatagram object. This 00090 // means that the Datagram cannot be queried for its 00091 // source Connection and/or NetAddress, but it is useful 00092 // in all other respects. 00093 //////////////////////////////////////////////////////////////////// 00094 bool RecentConnectionReader:: 00095 get_data(Datagram &result) { 00096 NetDatagram nd; 00097 if (!get_data(nd)) { 00098 return false; 00099 } 00100 result = nd; 00101 return true; 00102 } 00103 00104 //////////////////////////////////////////////////////////////////// 00105 // Function: RecentConnectionReader::receive_datagram 00106 // Access: Protected, Virtual 00107 // Description: An internal function called by ConnectionReader() 00108 // when a new datagram has become available. The 00109 // RecentConnectionReader simply queues it up for later 00110 // retrieval by get_data(). 00111 //////////////////////////////////////////////////////////////////// 00112 void RecentConnectionReader:: 00113 receive_datagram(const NetDatagram &datagram) { 00114 if (net_cat.is_debug()) { 00115 net_cat.debug() 00116 << "Received datagram of " << datagram.get_length() 00117 << " bytes\n"; 00118 } 00119 00120 LightMutexHolder holder(_mutex); 00121 _datagram = datagram; 00122 _available = true; 00123 }