pStatClientControlMessage.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Filename: pStatClientControlMessage.cxx
  2. // Created by: drose (09Jul00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://www.panda3d.org/license.txt .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #include "config_pstats.h"
  19. #include "pStatClientControlMessage.h"
  20. #include "pStatClientVersion.h"
  21. #include <datagram.h>
  22. #include <datagramIterator.h>
  23. ////////////////////////////////////////////////////////////////////
  24. // Function: PStatClientControlMessage::Constructor
  25. // Access: Public
  26. // Description:
  27. ////////////////////////////////////////////////////////////////////
  28. PStatClientControlMessage::
  29. PStatClientControlMessage() {
  30. _type = T_invalid;
  31. }
  32. ////////////////////////////////////////////////////////////////////
  33. // Function: PStatClientControlMessage::encode
  34. // Access: Public
  35. // Description: Writes the message into the indicated datagram.
  36. ////////////////////////////////////////////////////////////////////
  37. void PStatClientControlMessage::
  38. encode(Datagram &datagram) const {
  39. datagram.clear();
  40. datagram.add_uint8(_type);
  41. switch (_type) {
  42. case T_hello:
  43. datagram.add_string(_client_hostname);
  44. datagram.add_string(_client_progname);
  45. datagram.add_uint16(_major_version);
  46. datagram.add_uint16(_minor_version);
  47. break;
  48. case T_define_collectors:
  49. {
  50. datagram.add_uint16(_collectors.size());
  51. for (int i = 0; i < (int)_collectors.size(); i++) {
  52. _collectors[i]->write_datagram(datagram);
  53. }
  54. }
  55. break;
  56. case T_define_threads:
  57. {
  58. datagram.add_uint16(_first_thread_index);
  59. datagram.add_uint16(_names.size());
  60. for (int i = 0; i < (int)_names.size(); i++) {
  61. datagram.add_string(_names[i]);
  62. }
  63. }
  64. break;
  65. default:
  66. pstats_cat.error()
  67. << "Invalid PStatClientControlMessage::Type " << (int)_type << "\n";
  68. }
  69. }
  70. ////////////////////////////////////////////////////////////////////
  71. // Function: PStatClientControlMessage::decode
  72. // Access: Public
  73. // Description: Extracts the message from the indicated datagram.
  74. // Returns true on success, false on error.
  75. ////////////////////////////////////////////////////////////////////
  76. bool PStatClientControlMessage::
  77. decode(const Datagram &datagram, PStatClientVersion *version) {
  78. DatagramIterator source(datagram);
  79. _type = (Type)source.get_uint8();
  80. switch (_type) {
  81. case T_hello:
  82. _client_hostname = source.get_string();
  83. _client_progname = source.get_string();
  84. if (source.get_remaining_size() == 0) {
  85. _major_version = 1;
  86. _minor_version = 0;
  87. } else {
  88. _major_version = source.get_uint16();
  89. _minor_version = source.get_uint16();
  90. }
  91. break;
  92. case T_define_collectors:
  93. {
  94. int num = source.get_uint16();
  95. _collectors.clear();
  96. for (int i = 0; i < num; i++) {
  97. PStatCollectorDef *def = new PStatCollectorDef;
  98. def->read_datagram(source, version);
  99. _collectors.push_back(def);
  100. }
  101. }
  102. break;
  103. case T_define_threads:
  104. {
  105. _first_thread_index = source.get_uint16();
  106. int num = source.get_uint16();
  107. _names.clear();
  108. for (int i = 0; i < num; i++) {
  109. _names.push_back(source.get_string());
  110. }
  111. }
  112. break;
  113. case T_datagram:
  114. // Not, strictly speaking, a control message.
  115. return false;
  116. default:
  117. pstats_cat.error()
  118. << "Read invalid PStatClientControlMessage type: " << (int)_type << "\n";
  119. _type = T_invalid;
  120. return false;
  121. }
  122. return true;
  123. }