Panda3D
datagramIterator.cxx
1 // Filename: datagramIterator.cxx
2 // Created by: jns (07Feb00)
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 "datagramIterator.h"
16 #include "pnotify.h"
17 
18 TypeHandle DatagramIterator::_type_handle;
19 
20 ////////////////////////////////////////////////////////////////////
21 // Function: DatagramIterator::get_string
22 // Access: Public
23 // Description: Extracts a variable-length string.
24 ////////////////////////////////////////////////////////////////////
25 string DatagramIterator::
27  // First, get the length of the string
28  PN_uint16 s_len = get_uint16();
29 
30  nassertr(_datagram != (const Datagram *)NULL, "");
31  nassertr(_current_index + s_len <= _datagram->get_length(), "");
32 
33  const char *ptr = (const char *)_datagram->get_data();
34  int last_index = _current_index;
35 
36  _current_index += s_len;
37 
38  return string(ptr + last_index, s_len);
39 }
40 
41 ////////////////////////////////////////////////////////////////////
42 // Function: DatagramIterator::get_string32
43 // Access: Public
44 // Description: Extracts a variable-length string with a 32-bit
45 // length field.
46 ////////////////////////////////////////////////////////////////////
47 string DatagramIterator::
49  // First, get the length of the string
50  PN_uint32 s_len = get_uint32();
51 
52  nassertr(_datagram != (const Datagram *)NULL, "");
53  nassertr(_current_index + s_len <= _datagram->get_length(), "");
54 
55  const char *ptr = (const char *)_datagram->get_data();
56  int last_index = _current_index;
57 
58  _current_index += s_len;
59 
60  return string(ptr + last_index, s_len);
61 }
62 
63 ////////////////////////////////////////////////////////////////////
64 // Function: DatagramIterator::get_z_string
65 // Access: Public
66 // Description: Extracts a variable-length string, as a
67 // NULL-terminated string.
68 ////////////////////////////////////////////////////////////////////
69 string DatagramIterator::
71  nassertr(_datagram != (const Datagram *)NULL, "");
72 
73  // First, determine the length of the string.
74  const char *ptr = (const char *)_datagram->get_data();
75  size_t length = _datagram->get_length();
76  size_t p = _current_index;
77  while (p < length && ptr[p] != '\0') {
78  ++p;
79  }
80  nassertr(p < length, ""); // no NULL character?
81 
82  int last_index = _current_index;
83  _current_index = p + 1;
84 
85  return string(ptr + last_index, p - last_index);
86 }
87 
88 ////////////////////////////////////////////////////////////////////
89 // Function: DatagramIterator::get_fixed_string
90 // Access: Public
91 // Description: Extracts a fixed-length string. However, if a zero
92 // byte occurs within the string, it marks the end of
93 // the string.
94 ////////////////////////////////////////////////////////////////////
95 string DatagramIterator::
96 get_fixed_string(size_t size) {
97  nassertr(_datagram != (const Datagram *)NULL, "");
98  nassertr(_current_index + size <= _datagram->get_length(), "");
99 
100  const char *ptr = (const char *)_datagram->get_data();
101  string s(ptr + _current_index, size);
102 
103  _current_index += size;
104 
105  size_t zero_byte = s.find('\0');
106  return s.substr(0, zero_byte);
107 }
108 
109 ////////////////////////////////////////////////////////////////////
110 // Function: DatagramIterator::get_wstring
111 // Access: Public
112 // Description: Extracts a variable-length wstring (with a 32-bit
113 // length field).
114 ////////////////////////////////////////////////////////////////////
115 wstring DatagramIterator::
117  // First, get the length of the string
118  PN_uint32 s_len = get_uint32();
119 
120  nassertr(_datagram != (const Datagram *)NULL, wstring());
121  nassertr(_current_index + s_len * 2 <= _datagram->get_length(), wstring());
122 
123  wstring result;
124  result.reserve(s_len);
125  while (s_len > 0) {
126  result += wchar_t(get_uint16());
127  --s_len;
128  }
129 
130  return result;
131 }
132 
133 ////////////////////////////////////////////////////////////////////
134 // Function: DatagramIterator::extract_bytes
135 // Access: Public
136 // Description: Extracts the indicated number of bytes in the
137 // datagram and returns them as a string.
138 ////////////////////////////////////////////////////////////////////
139 string DatagramIterator::
140 extract_bytes(size_t size) {
141  nassertr((int)size >= 0, "");
142  nassertr(_datagram != (const Datagram *)NULL, "");
143  nassertr(_current_index + size <= _datagram->get_length(), "");
144 
145  const char *ptr = (const char *)_datagram->get_data();
146  int last_index = _current_index;
147 
148  _current_index += size;
149 
150  return string(ptr + last_index, size);
151 }
152 
153 ////////////////////////////////////////////////////////////////////
154 // Function: DatagramIterator::extract_bytes
155 // Access: Published
156 // Description: Extracts the indicated number of bytes in the
157 // datagram into the given character buffer. Assumes
158 // that the buffer is big enough to hold the requested
159 // number of bytes. Returns the number of bytes
160 // that were successfully written.
161 ////////////////////////////////////////////////////////////////////
162 size_t DatagramIterator::
163 extract_bytes(unsigned char *into, size_t size) {
164  nassertr((int)size >= 0, 0);
165  nassertr(_datagram != (const Datagram *)NULL, 0);
166  nassertr(_current_index + size <= _datagram->get_length(), 0);
167 
168  const char *ptr = (const char *)_datagram->get_data();
169  memcpy(into, ptr + _current_index, size);
170 
171  _current_index += size;
172  return size;
173 }
174 
175 ////////////////////////////////////////////////////////////////////
176 // Function : output
177 // Access : Public
178 // Description : Write a string representation of this instance to
179 // <out>.
180 ////////////////////////////////////////////////////////////////////
182 output(ostream &out) const {
183  #ifndef NDEBUG //[
184  out<<""<<"DatagramIterator";
185  #endif //] NDEBUG
186 }
187 
188 ////////////////////////////////////////////////////////////////////
189 // Function : write
190 // Access : Public
191 // Description : Write a string representation of this instance to
192 // <out>.
193 ////////////////////////////////////////////////////////////////////
195 write(ostream &out, unsigned int indent) const {
196  #ifndef NDEBUG //[
197  out.width(indent); out<<""<<"DatagramIterator:\n";
198  out.width(indent+2); out<<""<<"_current_index "<<_current_index;
199  if (_datagram) {
200  out<<""<<" (of "<<(get_datagram().get_length())<<")";
201  out<<""<<" / 0x"<<(void*)_current_index<<" (of 0x"
202  <<(void*)(get_datagram().get_length())<<")\n";
203  get_datagram().write(out, indent+2);
204  } else {
205  out<<""<<" (_datagram is null)\n";
206  }
207  #endif //] NDEBUG
208 }
209 
const Datagram & get_datagram() const
Return the datagram of this iterator.
PN_uint32 get_uint32()
Extracts an unsigned 32-bit integer.
string get_string()
Extracts a variable-length string.
PN_uint16 get_uint16()
Extracts an unsigned 16-bit integer.
string extract_bytes(size_t size)
Extracts the indicated number of bytes in the datagram and returns them as a string.
void write(ostream &out, unsigned int indent=0) const
Write a string representation of this instance to <out>.
Definition: datagram.cxx:206
void write(ostream &out, unsigned int indent=0) const
Write a string representation of this instance to <out>.
void output(ostream &out) const
Write a string representation of this instance to <out>.
string get_z_string()
Extracts a variable-length string, as a NULL-terminated string.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
wstring get_wstring()
Extracts a variable-length wstring (with a 32-bit length field).
string get_string32()
Extracts a variable-length string with a 32-bit length field.
size_t get_length() const
Returns the number of bytes in the datagram.
Definition: datagram.I:457
const void * get_data() const
Returns a pointer to the beginning of the datagram&#39;s data.
Definition: datagram.I:447
string get_fixed_string(size_t size)
Extracts a fixed-length string.