Panda3D
 All Classes Functions Variables Enumerations
pStatServerControlMessage.cxx
1 // Filename: pStatServerControlMessage.cxx
2 // Created by: drose (09Jul00)
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 "config_pstats.h"
16 #include "pStatServerControlMessage.h"
17 
18 #include "datagram.h"
19 #include "datagramIterator.h"
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: PStatServerControlMessage::Constructor
23 // Access: Public
24 // Description:
25 ////////////////////////////////////////////////////////////////////
26 PStatServerControlMessage::
27 PStatServerControlMessage() {
28  _type = T_invalid;
29 }
30 
31 ////////////////////////////////////////////////////////////////////
32 // Function: PStatServerControlMessage::encode
33 // Access: Public
34 // Description: Writes the message into the indicated datagram.
35 ////////////////////////////////////////////////////////////////////
37 encode(Datagram &datagram) const {
38  datagram.clear();
39  datagram.add_uint8(_type);
40  switch (_type) {
41  case T_hello:
42  datagram.add_string(_server_hostname);
43  datagram.add_string(_server_progname);
44  datagram.add_uint16(_udp_port);
45  break;
46 
47  default:
48  pstats_cat.error()
49  << "Invalid PStatServerControlMessage::Type " << (int)_type << "\n";
50  }
51 }
52 
53 ////////////////////////////////////////////////////////////////////
54 // Function: PStatServerControlMessage::decode
55 // Access: Public
56 // Description: Extracts the message from the indicated datagram.
57 // Returns true on success, false on error.
58 ////////////////////////////////////////////////////////////////////
60 decode(const Datagram &datagram) {
61  DatagramIterator source(datagram);
62  _type = (Type)source.get_uint8();
63 
64  switch (_type) {
65  case T_hello:
66  _server_hostname = source.get_string();
67  _server_progname = source.get_string();
68  _udp_port = source.get_uint16();
69  break;
70 
71  default:
72  pstats_cat.error()
73  << "Read invalid PStatServerControlMessage type: " << (int)_type << "\n";
74  _type = T_invalid;
75  return false;
76  }
77 
78  return true;
79 }
void add_uint8(PN_uint8 value)
Adds an unsigned 8-bit integer to the datagram.
Definition: datagram.I:138
void add_string(const string &str)
Adds a variable-length string to the datagram.
Definition: datagram.I:351
bool decode(const Datagram &datagram)
Extracts the message from the indicated datagram.
virtual void clear()
Resets the datagram to empty, in preparation for building up a new datagram.
Definition: datagram.cxx:41
PN_uint8 get_uint8()
Extracts an unsigned 8-bit integer.
void encode(Datagram &datagram) const
Writes the message into the indicated datagram.
string get_string()
Extracts a variable-length string.
PN_uint16 get_uint16()
Extracts an unsigned 16-bit integer.
void add_uint16(PN_uint16 value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:181
A class to retrieve the individual data elements previously stored in a Datagram. ...
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43