sources/network/bytestream.cpp

changeset 18
56a1ac7d931b
parent 13
09dcaeaa216b
child 58
d175243ad169
equal deleted inserted replaced
17:50341dec533e 18:56a1ac7d931b
63 { 63 {
64 init (other.data(), other.written_length()); 64 init (other.data(), other.written_length());
65 } 65 }
66 66
67 // ------------------------------------------------------------------------------------------------- 67 // -------------------------------------------------------------------------------------------------
68 METHOD Bytestream::operator= (const Bytestream& other) -> Bytestream& 68 //
69 Bytestream::~Bytestream()
70 {
71 delete m_data;
72 }
73
74 // -------------------------------------------------------------------------------------------------
75 METHOD
76 Bytestream::operator= (const Bytestream& other) -> Bytestream&
69 { 77 {
70 init (other.data(), other.written_length()); 78 init (other.data(), other.written_length());
71 return *this; 79 return *this;
72 } 80 }
73 81
74 // ------------------------------------------------------------------------------------------------- 82 // -------------------------------------------------------------------------------------------------
75 // 83 //
76 Bytestream::~Bytestream() 84 METHOD
77 { 85 Bytestream::resize (unsigned long newsize) -> void
78 delete m_data;
79 }
80
81 // -------------------------------------------------------------------------------------------------
82 //
83 void Bytestream::resize (unsigned long newsize)
84 { 86 {
85 Vector<unsigned char> olddata; 87 Vector<unsigned char> olddata;
86 unsigned long oldsize = 0L; 88 unsigned long oldsize = 0L;
87 89
88 if (m_data != nullptr) 90 if (m_data != nullptr)
100 memcpy (m_data, olddata, min (oldsize, newsize)); 102 memcpy (m_data, olddata, min (oldsize, newsize));
101 } 103 }
102 104
103 // ------------------------------------------------------------------------------------------------- 105 // -------------------------------------------------------------------------------------------------
104 // 106 //
105 void Bytestream::init (const unsigned char* data, unsigned long length) 107 METHOD
108 Bytestream::init (const unsigned char* data, unsigned long length) -> void
106 { 109 {
107 resize (length); 110 resize (length);
108 memcpy (m_data, data, length); 111 memcpy (m_data, data, length);
109 m_cursor = &m_data[0]; 112 m_cursor = &m_data[0];
110 m_writtenLength = length; 113 m_writtenLength = length;
111 } 114 }
112 115
113 // ------------------------------------------------------------------------------------------------- 116 // -------------------------------------------------------------------------------------------------
114 // 117 //
115 void Bytestream::clear() 118 METHOD
119 Bytestream::clear() -> void
116 { 120 {
117 m_cursor = &m_data[0]; 121 m_cursor = &m_data[0];
118 m_writtenLength = 0; 122 m_writtenLength = 0;
119 } 123 }
120 124
130 } 134 }
131 } 135 }
132 136
133 // ------------------------------------------------------------------------------------------------- 137 // -------------------------------------------------------------------------------------------------
134 // 138 //
135 char Bytestream::read_byte() 139 METHOD
140 Bytestream::read_byte() -> char
136 { 141 {
137 ensure_read_space (1); 142 ensure_read_space (1);
138 return *m_cursor++; 143 return *m_cursor++;
139 } 144 }
140 145
141 // ------------------------------------------------------------------------------------------------- 146 // -------------------------------------------------------------------------------------------------
142 // 147 //
143 short int Bytestream::read_short() 148 METHOD
149 Bytestream::read_short() -> short int
144 { 150 {
145 ensure_read_space (2); 151 ensure_read_space (2);
146 short int result = 0; 152 short int result = 0;
147 153
148 for (int i = 0; i < 2; ++i) 154 for (int i = 0; i < 2; ++i)
152 return result; 158 return result;
153 } 159 }
154 160
155 // ------------------------------------------------------------------------------------------------- 161 // -------------------------------------------------------------------------------------------------
156 // 162 //
157 long int Bytestream::read_long() 163 METHOD
164 Bytestream::read_long() -> long int
158 { 165 {
159 ensure_read_space (4); 166 ensure_read_space (4);
160 long int result = 0; 167 long int result = 0;
161 168
162 for (int i = 0; i < 4; ++i) 169 for (int i = 0; i < 4; ++i)
166 return result; 173 return result;
167 } 174 }
168 175
169 // ------------------------------------------------------------------------------------------------- 176 // -------------------------------------------------------------------------------------------------
170 // 177 //
171 float Bytestream::read_float() 178 METHOD
179 Bytestream::read_float() -> float
172 { 180 {
173 int value = read_long(); 181 int value = read_long();
174 return reinterpret_cast<float&> (value); 182 return reinterpret_cast<float&> (value);
175 } 183 }
176 184
177 // ------------------------------------------------------------------------------------------------- 185 // -------------------------------------------------------------------------------------------------
178 // 186 //
179 String Bytestream::read_string() 187 METHOD
188 Bytestream::read_string() -> String
180 { 189 {
181 // Zandronum sends strings of maximum 2048 characters, though it only 190 // Zandronum sends strings of maximum 2048 characters, though it only
182 // reads 2047-character long ones so I guess we can follow up and do 191 // reads 2047-character long ones so I guess we can follow up and do
183 // the same :-) 192 // the same :-)
184 static char buffer[MAX_NETWORK_STRING]; 193 static char buffer[MAX_NETWORK_STRING];
217 m_cursor += length; 226 m_cursor += length;
218 } 227 }
219 228
220 // ------------------------------------------------------------------------------------------------- 229 // -------------------------------------------------------------------------------------------------
221 // 230 //
222 void Bytestream::write (unsigned char val) 231 METHOD
232 Bytestream::write (unsigned char val) -> void
223 { 233 {
224 *m_cursor++ = val; 234 *m_cursor++ = val;
225 m_writtenLength++; 235 m_writtenLength++;
226 } 236 }
227 237
228 // ------------------------------------------------------------------------------------------------- 238 // -------------------------------------------------------------------------------------------------
229 // 239 //
230 void Bytestream::write (const unsigned char* val, unsigned int length) 240 METHOD
241 Bytestream::write (const unsigned char* val, unsigned int length) -> void
231 { 242 {
232 grow_to_fit (length); 243 grow_to_fit (length);
233 memcpy (m_cursor, val, length); 244 memcpy (m_cursor, val, length);
234 m_cursor += length; 245 m_cursor += length;
235 m_writtenLength += length; 246 m_writtenLength += length;
236 } 247 }
237 248
238 // ------------------------------------------------------------------------------------------------- 249 // -------------------------------------------------------------------------------------------------
239 // 250 //
240 void Bytestream::grow_to_fit (unsigned long bytes) 251 METHOD
252 Bytestream::grow_to_fit (unsigned long bytes) -> void
241 { 253 {
242 if (space_left() < bytes) 254 if (space_left() < bytes)
243 resize (allocated_size() + bytes + 128); 255 resize (allocated_size() + bytes + 128);
244 } 256 }
245 257
246 // ------------------------------------------------------------------------------------------------- 258 // -------------------------------------------------------------------------------------------------
247 // 259 //
248 void Bytestream::write_byte (char val) 260 METHOD
261 Bytestream::write_byte (char val) -> void
249 { 262 {
250 grow_to_fit (1); 263 grow_to_fit (1);
251 write (val); 264 write (val);
252 } 265 }
253 266
254 // ------------------------------------------------------------------------------------------------- 267 // -------------------------------------------------------------------------------------------------
255 // 268 //
256 void Bytestream::write_short (short int val) 269 METHOD
270 Bytestream::write_short (short int val) -> void
257 { 271 {
258 grow_to_fit (2); 272 grow_to_fit (2);
259 273
260 for (int i = 0; i < 2; ++i) 274 for (int i = 0; i < 2; ++i)
261 write ((val >> (i * 8)) & 0xFF); 275 write ((val >> (i * 8)) & 0xFF);
262 } 276 }
263 277
264 // ------------------------------------------------------------------------------------------------- 278 // -------------------------------------------------------------------------------------------------
265 // 279 //
266 void Bytestream::write_long (long int val) 280 METHOD
281 Bytestream::write_long (long int val) -> void
267 { 282 {
268 grow_to_fit (4); 283 grow_to_fit (4);
269 284
270 for (int i = 0; i < 4; ++i) 285 for (int i = 0; i < 4; ++i)
271 write ((val >> (i * 8)) & 0xFF); 286 write ((val >> (i * 8)) & 0xFF);
272 } 287 }
273 288
274 // ------------------------------------------------------------------------------------------------- 289 // -------------------------------------------------------------------------------------------------
275 // 290 //
276 void Bytestream::write_float (float val) 291 METHOD
292 Bytestream::write_float (float val) -> void
277 { 293 {
278 // I know this is probably dangerous but this is what Zandronum does so yeah 294 // I know this is probably dangerous but this is what Zandronum does so yeah
279 write_long (reinterpret_cast<int&> (val)); 295 write_long (reinterpret_cast<int&> (val));
280 } 296 }
281 297
282 // ------------------------------------------------------------------------------------------------- 298 // -------------------------------------------------------------------------------------------------
283 // 299 //
284 void Bytestream::write_string (const String& val) 300 METHOD
301 Bytestream::write_string (const String& val) -> void
285 { 302 {
286 grow_to_fit (val.length() + 1); 303 grow_to_fit (val.length() + 1);
287 write (reinterpret_cast<const unsigned char*> (val.chars()), val.length()); 304 write (reinterpret_cast<const unsigned char*> (val.chars()), val.length());
288 write (0); 305 write (0);
289 } 306 }
290 307
291 // ------------------------------------------------------------------------------------------------- 308 // -------------------------------------------------------------------------------------------------
292 // 309 //
293 void Bytestream::write_buffer (const Bytestream& other) 310 METHOD
311 Bytestream::write_buffer (const Bytestream& other) -> void
294 { 312 {
295 write (other.data(), other.written_length()); 313 write (other.data(), other.written_length());
296 } 314 }

mercurial