Panda3D
 All Classes Functions Variables Enumerations
datagramUDPHeader.cxx
1 // Filename: datagramUDPHeader.cxx
2 // Created by: drose (08Feb00)
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 "datagramUDPHeader.h"
16 #include "netDatagram.h"
17 #include "datagramIterator.h"
18 #include "config_net.h"
19 
20 #include "pnotify.h"
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: DatagramUDPHeader::Constructor
24 // Access: Public
25 // Description: This constructor creates a header based on an
26 // already-constructed NetDatagram.
27 ////////////////////////////////////////////////////////////////////
29 DatagramUDPHeader(const NetDatagram &datagram) {
30  const string &str = datagram.get_message();
31  PN_uint16 checksum = 0;
32  for (size_t p = 0; p < str.size(); p++) {
33  checksum += (PN_uint16)(PN_uint8)str[p];
34  }
35 
36  // Now pack the header.
37  _header.add_uint16(checksum);
38  nassertv((int)_header.get_length() == datagram_udp_header_size);
39 }
40 
41 ////////////////////////////////////////////////////////////////////
42 // Function: DatagramUDPHeader::Constructor
43 // Access: Public
44 // Description: This constructor decodes a header from a block of
45 // data of length datagram_udp_header_size, presumably
46 // just read from a socket.
47 ////////////////////////////////////////////////////////////////////
49 DatagramUDPHeader(const void *data) : _header(data, datagram_udp_header_size) {
50 }
51 
52 ////////////////////////////////////////////////////////////////////
53 // Function: DatagramUDPHeader::verify_datagram
54 // Access: Public
55 // Description: Verifies that the indicated datagram has the
56 // appropriate length and checksum. Returns true if it
57 // matches, false otherwise.
58 ////////////////////////////////////////////////////////////////////
60 verify_datagram(const NetDatagram &datagram) const {
61  const string &str = datagram.get_message();
62 
63  PN_uint16 checksum = 0;
64  for (size_t p = 0; p < str.size(); p++) {
65  checksum += (PN_uint16)(PN_uint8)str[p];
66  }
67 
68  if (checksum == get_datagram_checksum()) {
69  return true;
70  }
71 
72  if (net_cat.is_debug()) {
73  net_cat.debug()
74  << "Invalid datagram!\n";
75  if (checksum != get_datagram_checksum()) {
76  net_cat.debug()
77  << " checksum is " << checksum << ", header reports "
78  << get_datagram_checksum() << "\n";
79  }
80 
81  // We write the hex dump into a ostringstream first, to guarantee
82  // an atomic write to the output stream in case we're threaded.
83 
84  ostringstream hex;
85  datagram.dump_hex(hex);
86  hex << "\n";
87  net_cat.debug(false) << hex.str();
88  }
89 
90  return false;
91 }
string get_message() const
Returns the datagram&#39;s data as a string.
Definition: datagram.I:431
A specific kind of Datagram, especially for sending across or receiving from a network.
Definition: netDatagram.h:43
DatagramUDPHeader(const NetDatagram &datagram)
This constructor creates a header based on an already-constructed NetDatagram.
size_t get_length() const
Returns the number of bytes in the datagram.
Definition: datagram.I:457
void dump_hex(ostream &out, unsigned int indent=0) const
Writes a representation of the entire datagram contents, as a sequence of hex (and ASCII) values...
Definition: datagram.cxx:52
int get_datagram_checksum() const
Returns the checksum appropriate for the indicated datagram.
void add_uint16(PN_uint16 value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:181
bool verify_datagram(const NetDatagram &datagram) const
Verifies that the indicated datagram has the appropriate length and checksum.
static int size()
Returns 3: the number of components of a LVecBase3.
Definition: lvecBase3.h:452