recentConnectionReader.cxx 4.4 KB

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