]> Dogcows Code - chaz/yoink/blob - src/Moof/Packet.cc
6923e7cb06e5d4a507ec1d8cbe2064e784503a4b
[chaz/yoink] / src / Moof / Packet.cc
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #include "../config.h"
13
14 #include <algorithm>
15 #if HAVE_BYTESWAP_H
16 #include <byteswap.h>
17 #endif
18 #include <cstdlib>
19
20 #if HAVE_ARPA_INET_H
21 #include <arpa/inet.h>
22 #endif
23
24 #include <SDL/SDL.h>
25
26 #include "Packet.hh"
27
28
29 #ifndef bswap_16
30 #define bswap_16(x) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
31 #endif
32
33 #ifndef bswap_32
34 #define bswap_32(x) ((((x) & 0xff000000) >> 24) | \
35 (((x) & 0x00ff0000) >> 8) | \
36 (((x) & 0x0000ff00) << 8) | \
37 (((x) & 0x000000ff) << 24))
38 #endif
39
40 #ifndef bswap_64
41 #define bswap_64(x) (((x) << 56) | \
42 (((x) << 40) & 0xff000000000000ULL) | \
43 (((x) << 24) & 0xff0000000000ULL) | \
44 (((x) << 8) & 0xff00000000ULL) | \
45 (((x) >> 8) & 0xff000000ULL) | \
46 (((x) >> 24) & 0xff0000ULL) | \
47 (((x) >> 40) & 0xff00ULL) | \
48 ((x) >> 56))
49 #endif
50
51
52 #if !HAVE_ARPA_INET_H
53 static uint16_t htons(uint16_t x)
54 {
55 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
56 return bswap_16(x);
57 #else
58 return x;
59 #endif
60 }
61 static uint16_t ntohs(uint16_t x)
62 {
63 return htons(x);
64 }
65
66 static uint32_t htonl(uint32_t x)
67 {
68 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
69 return bswap_32(x);
70 #else
71 return x;
72 #endif
73 }
74 static uint32_t ntohl(uint32_t x)
75 {
76 return htonl(x);
77 }
78 #endif
79
80
81 static uint64_t htonll(uint64_t x)
82 {
83 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
84 return bswap_64(x);
85 #else
86 return x;
87 #endif
88 }
89 static uint64_t ntohll(uint64_t x)
90 {
91 return htonll(x);
92 }
93
94
95 namespace Mf {
96
97
98 Packet::Packet(size_t size) :
99 mBuffer((char*)malloc(size)),
100 mSize(size),
101 mR(0),
102 mW(0),
103 mBoolR(0),
104 mBoolW(0),
105 mBoolNumR(0),
106 mBoolNumW(0) {}
107
108 Packet::Packet(const char* data, size_t size) :
109 mBuffer((char*)malloc(size)),
110 mSize(size),
111 mR(0),
112 mW(size),
113 mBoolR(0),
114 mBoolW(0),
115 mBoolNumR(0),
116 mBoolNumW(0)
117 {
118 if (mBuffer) memcpy(mBuffer, data, size);
119 }
120
121
122 Packet::Packet(const Packet& copy) :
123 mBuffer((char*)malloc(copy.mSize)),
124 mSize(copy.mSize),
125 mR(copy.mR),
126 mW(copy.mW),
127 mBoolR(copy.mBoolR),
128 mBoolW(copy.mBoolW),
129 mBoolNumR(copy.mBoolNumR),
130 mBoolNumW(copy.mBoolNumW)
131 {
132 if (mBuffer) memcpy(mBuffer, copy.mBuffer, mSize);
133 }
134
135 Packet& Packet::operator=(const Packet& copy)
136 {
137 free(mBuffer);
138
139 mBuffer = (char*)malloc(copy.mSize);
140 mSize = copy.mSize;
141 mR = copy.mR;
142 mW = copy.mW;
143 mBoolR = copy.mBoolR;
144 mBoolW = copy.mBoolW;
145 mBoolNumR = copy.mBoolNumR;
146 mBoolNumW = copy.mBoolNumW;
147 return *this;
148 }
149
150
151 Packet::~Packet()
152 {
153 free(mBuffer);
154 }
155
156
157 Packet& Packet::operator<<(bool value)
158 {
159 int bit = mBoolNumW % 8;
160 if (bit == 0)
161 {
162 mBoolW = mW;
163
164 unsigned char byte = 0;
165 if (write(&byte, 1) == 0) return *this;
166 }
167
168 if (value) mBuffer[mBoolW] |= (1 << bit);
169 ++mBoolNumW;
170
171 return *this;
172 }
173
174
175 Packet& Packet::operator<<(int8_t value)
176 {
177 return *this << (uint8_t)value;
178 }
179
180 Packet& Packet::operator<<(int16_t value)
181 {
182 return *this << (uint16_t)value;
183 }
184
185 Packet& Packet::operator<<(int32_t value)
186 {
187 return *this << (uint32_t)value;
188 }
189
190 Packet& Packet::operator<<(int64_t value)
191 {
192 return *this << (uint64_t)value;
193 }
194
195
196 Packet& Packet::operator<<(uint8_t value)
197 {
198 write(&value, sizeof(value));
199 return *this;
200 }
201
202 Packet& Packet::operator<<(uint16_t value)
203 {
204 value = htons(value);
205 write(&value, sizeof(value));
206 return *this;
207 }
208
209 Packet& Packet::operator<<(uint32_t value)
210 {
211 value = htonl(value);
212 write(&value, sizeof(value));
213 return *this;
214 }
215
216 Packet& Packet::operator<<(uint64_t value)
217 {
218 value = htonll(value);
219 write(&value, sizeof(value));
220 return *this;
221 }
222
223 Packet& Packet::operator<<(float value)
224 {
225 // XXX: assumes the ieee-754
226 uint32_t* integer = reinterpret_cast<uint32_t*>(&value);
227 *integer = htonl(*integer);
228 write(integer, sizeof(value));
229 return *this;
230 }
231
232 Packet& Packet::operator<<(double value)
233 {
234 // XXX: assumes the ieee-754
235 uint64_t* integer = reinterpret_cast<uint64_t*>(&value);
236 *integer = htonll(*integer);
237 write(integer, sizeof(value));
238 return *this;
239 }
240
241 size_t Packet::write(const void* bytes, size_t size)
242 {
243 size_t nBytes = std::min(size, mSize - mW);
244 if (!mBuffer || nBytes < size)
245 {
246 int nPages = 1 + size / PAGE_SIZE;
247 int newSize = mSize + nPages * PAGE_SIZE;
248 char* newBuffer = (char*)realloc(mBuffer, newSize);
249 if (newBuffer)
250 {
251 mBuffer = newBuffer;
252 mSize = newSize;
253 nBytes = size;
254 }
255 if (!mBuffer) return 0;
256 }
257 memcpy(&mBuffer[mW], bytes, nBytes);
258 mW += nBytes;
259 return nBytes;
260 }
261
262
263 Packet& Packet::operator>>(bool& value)
264 {
265 int bit = mBoolNumR % 8;
266 if (bit == 0)
267 {
268 mBoolR = mR;
269
270 unsigned char byte = 0;
271 if (read(&byte, 1) == 0) return *this;
272 }
273
274 value = 1 & (mBuffer[mBoolR] >> bit);
275 ++mBoolNumR;
276
277 return *this;
278 }
279
280 Packet& Packet::operator>>(int8_t& value)
281 {
282 return *this >> (uint8_t&)value;
283 }
284
285 Packet& Packet::operator>>(int16_t& value)
286 {
287 return *this >> (uint16_t&)value;
288 }
289
290 Packet& Packet::operator>>(int32_t& value)
291 {
292 return *this >> (uint32_t&)value;
293 }
294
295 Packet& Packet::operator>>(int64_t& value)
296 {
297 return *this >> (uint64_t&)value;
298 }
299
300 Packet& Packet::operator>>(uint8_t& value)
301 {
302 read(&value, sizeof(value));
303 return *this;
304 }
305
306 Packet& Packet::operator>>(uint16_t& value)
307 {
308 read(&value, sizeof(value));
309 value = ntohs(value);
310 return *this;
311 }
312
313 Packet& Packet::operator>>(uint32_t& value)
314 {
315 read(&value, sizeof(value));
316 value = ntohl(value);
317 return *this;
318 }
319
320 Packet& Packet::operator>>(uint64_t& value)
321 {
322 read(&value, sizeof(value));
323 value = ntohll(value);
324 return *this;
325 }
326
327 Packet& Packet::operator>>(float& value)
328 {
329 // XXX: assumes the ieee-754
330 uint32_t* integer = reinterpret_cast<uint32_t*>(&value);
331 read(integer, sizeof(value));
332 *integer = htonl(*integer);
333 return *this;
334 }
335
336 Packet& Packet::operator>>(double& value)
337 {
338 // XXX: assumes the ieee-754
339 uint64_t* integer = reinterpret_cast<uint64_t*>(&value);
340 read(integer, sizeof(value));
341 *integer = htonll(*integer);
342 return *this;
343 }
344
345 size_t Packet::read(void* bytes, size_t size)
346 {
347 size_t nBytes = std::min(size, mW - mR);
348 memcpy(bytes, &mBuffer[mR], nBytes);
349 mR += nBytes;
350 return nBytes;
351 }
352
353
354 void Packet::clear()
355 {
356 mR = 0;
357 mW = 0;
358 mBoolR = 0;
359 mBoolW = 0;
360 mBoolNumR = 0;
361 mBoolNumW = 0;
362 }
363
364
365 } // namespace Mf
366
This page took 0.041497 seconds and 3 git commands to generate.