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