Panda3D
datagramTCPHeader.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file datagramTCPHeader.cxx
10  * @author drose
11  * @date 2000-02-08
12  */
13 
14 #include "datagramTCPHeader.h"
15 #include "netDatagram.h"
16 #include "datagramIterator.h"
17 #include "config_net.h"
18 
19 #include "pnotify.h"
20 
21 /**
22  * This constructor creates a header based on an already-constructed
23  * NetDatagram.
24  */
26 DatagramTCPHeader(const NetDatagram &datagram, int header_size) {
27  size_t length = datagram.get_length();
28  switch (header_size) {
29  case 0:
30  break;
31 
32  case datagram_tcp16_header_size:
33  {
34  uint16_t size = (uint16_t)length;
35  nassertv((size_t)size == length);
36  _header.add_uint16(size);
37  }
38  break;
39 
40  case datagram_tcp32_header_size:
41  {
42  uint32_t size = (uint32_t)length;
43  nassertv((size_t)size == length);
44  _header.add_uint32(size);
45  }
46  break;
47 
48  default:
49  nassert_raise("invalid header size");
50  return;
51  }
52 
53  nassertv((int)_header.get_length() == header_size);
54 }
55 
56 /**
57  * This constructor decodes a header from a block of data of length
58  * datagram_tcp_header_size, presumably just read from a socket.
59  */
61 DatagramTCPHeader(const void *data, int header_size) :
62  _header(data, header_size)
63 {
64 }
65 
66 /**
67  * Returns the number of bytes in the associated datagram.
68  */
70 get_datagram_size(int header_size) const {
71  DatagramIterator di(_header);
72  switch (header_size) {
73  case 0:
74  return 0;
75 
76  case datagram_tcp16_header_size:
77  return di.get_uint16();
78 
79  case datagram_tcp32_header_size:
80  return di.get_uint32();
81  }
82 
83  return -1;
84 }
85 
86 /**
87  * Verifies that the indicated datagram has the appropriate length. Returns
88  * true if it matches, false otherwise.
89  */
91 verify_datagram(const NetDatagram &datagram, int header_size) const {
92  if (header_size == 0) {
93  // No way to validate without a header, so everything is valid.
94  return true;
95  }
96 
97  int actual_size = (int)datagram.get_length();
98  int expected_size = get_datagram_size(header_size);
99  if (actual_size == expected_size) {
100  return true;
101  }
102 
103  if (net_cat.is_debug()) {
104  net_cat.debug()
105  << "Invalid datagram! Size is " << actual_size
106  << " bytes, header reports " << expected_size << "\n";
107 
108  // We write the hex dump into a ostringstream first, to guarantee an
109  // atomic write to the output stream in case we're threaded.
110 
111  std::ostringstream hex;
112  datagram.dump_hex(hex);
113  hex << "\n";
114  net_cat.debug() << hex.str();
115  }
116 
117  return false;
118 }
A specific kind of Datagram, especially for sending across or receiving from a network.
Definition: netDatagram.h:40
void dump_hex(std::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:44
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void add_uint32(uint32_t value)
Adds an unsigned 32-bit integer to the datagram.
Definition: datagram.I:94
DatagramTCPHeader(const NetDatagram &datagram, int header_size)
This constructor creates a header based on an already-constructed NetDatagram.
void add_uint16(uint16_t value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:85
uint32_t get_uint32()
Extracts an unsigned 32-bit integer.
int get_datagram_size(int header_size) const
Returns the number of bytes in the associated datagram.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
uint16_t get_uint16()
Extracts an unsigned 16-bit integer.
A class to retrieve the individual data elements previously stored in a Datagram.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool verify_datagram(const NetDatagram &datagram, int header_size) const
Verifies that the indicated datagram has the appropriate length.
size_t get_length() const
Returns the number of bytes in the datagram.
Definition: datagram.I:335
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.