Panda3D
 All Classes Functions Variables Enumerations
datagramTCPHeader.cxx
1 // Filename: datagramTCPHeader.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 "datagramTCPHeader.h"
16 #include "netDatagram.h"
17 #include "datagramIterator.h"
18 #include "config_net.h"
19 
20 #include "pnotify.h"
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: DatagramTCPHeader::Constructor
24 // Access: Public
25 // Description: This constructor creates a header based on an
26 // already-constructed NetDatagram.
27 ////////////////////////////////////////////////////////////////////
29 DatagramTCPHeader(const NetDatagram &datagram, int header_size) {
30  const string &str = datagram.get_message();
31  switch (header_size) {
32  case 0:
33  break;
34 
35  case datagram_tcp16_header_size:
36  {
37  PN_uint16 size = str.length();
38  nassertv(size == str.length());
39  _header.add_uint16(size);
40  }
41  break;
42 
43  case datagram_tcp32_header_size:
44  {
45  PN_uint32 size = str.length();
46  nassertv(size == str.length());
47  _header.add_uint32(size);
48  }
49  break;
50 
51  default:
52  nassertv(false);
53  }
54 
55  nassertv((int)_header.get_length() == header_size);
56 }
57 
58 ////////////////////////////////////////////////////////////////////
59 // Function: DatagramTCPHeader::Constructor
60 // Access: Public
61 // Description: This constructor decodes a header from a block of
62 // data of length datagram_tcp_header_size, presumably
63 // just read from a socket.
64 ////////////////////////////////////////////////////////////////////
66 DatagramTCPHeader(const void *data, int header_size) :
67  _header(data, header_size)
68 {
69 }
70 
71 ////////////////////////////////////////////////////////////////////
72 // Function: DatagramTCPHeader::get_datagram_size
73 // Access: Public
74 // Description: Returns the number of bytes in the associated
75 // datagram.
76 ////////////////////////////////////////////////////////////////////
78 get_datagram_size(int header_size) const {
79  DatagramIterator di(_header);
80  switch (header_size) {
81  case 0:
82  return 0;
83 
84  case datagram_tcp16_header_size:
85  return di.get_uint16();
86 
87  case datagram_tcp32_header_size:
88  return di.get_uint32();
89  }
90 
91  return -1;
92 }
93 
94 ////////////////////////////////////////////////////////////////////
95 // Function: DatagramTCPHeader::verify_datagram
96 // Access: Public
97 // Description: Verifies that the indicated datagram has the
98 // appropriate length. Returns true if it matches,
99 // false otherwise.
100 ////////////////////////////////////////////////////////////////////
102 verify_datagram(const NetDatagram &datagram, int header_size) const {
103  if (header_size == 0) {
104  // No way to validate without a header, so everything is valid.
105  return true;
106  }
107 
108  const string &str = datagram.get_message();
109  int actual_size = str.length();
110  int expected_size = get_datagram_size(header_size);
111  if (actual_size == expected_size) {
112  return true;
113  }
114 
115  if (net_cat.is_debug()) {
116  net_cat.debug()
117  << "Invalid datagram! Size is " << actual_size
118  << " bytes, header reports " << expected_size << "\n";
119 
120  // We write the hex dump into a ostringstream first, to guarantee
121  // an atomic write to the output stream in case we're threaded.
122 
123  ostringstream hex;
124  datagram.dump_hex(hex);
125  hex << "\n";
126  net_cat.debug() << hex.str();
127  }
128 
129  return false;
130 }
int get_datagram_size(int header_size) const
Returns the number of bytes in the associated datagram.
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
PN_uint32 get_uint32()
Extracts an unsigned 32-bit integer.
size_t get_length() const
Returns the number of bytes in the datagram.
Definition: datagram.I:457
PN_uint16 get_uint16()
Extracts an unsigned 16-bit integer.
DatagramTCPHeader(const NetDatagram &datagram, int header_size)
This constructor creates a header based on an already-constructed NetDatagram.
bool verify_datagram(const NetDatagram &datagram, int header_size) const
Verifies that the indicated datagram has the appropriate length.
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
void add_uint16(PN_uint16 value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:181
void add_uint32(PN_uint32 value)
Adds an unsigned 32-bit integer to the datagram.
Definition: datagram.I:192
A class to retrieve the individual data elements previously stored in a Datagram. ...