]>
Dogcows Code - chaz/yoink/blob - src/Moof/Packet.cc
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
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.
10 **************************************************************************/
12 #include "../config.h"
21 #include <arpa/inet.h>
30 #define bswap_16(x) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
34 #define bswap_32(x) ((((x) & 0xff000000) >> 24) | \
35 (((x) & 0x00ff0000) >> 8) | \
36 (((x) & 0x0000ff00) << 8) | \
37 (((x) & 0x000000ff) << 24))
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) | \
53 static uint16_t htons(uint16_t x
)
55 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
61 static uint16_t ntohs(uint16_t x
)
66 static uint32_t htonl(uint32_t x
)
68 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
74 static uint32_t ntohl(uint32_t x
)
81 static uint64_t htonll(uint64_t x
)
83 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
89 static uint64_t ntohll(uint64_t x
)
98 Packet::Packet(size_t size
) :
99 mBuffer((char*)malloc(size
)),
108 Packet::Packet(const char* data
, size_t size
) :
109 mBuffer((char*)malloc(size
)),
118 if (mBuffer
) memcpy(mBuffer
, data
, size
);
122 Packet::Packet(const Packet
& copy
) :
123 mBuffer((char*)malloc(copy
.mSize
)),
129 mBoolNumR(copy
.mBoolNumR
),
130 mBoolNumW(copy
.mBoolNumW
)
132 if (mBuffer
) memcpy(mBuffer
, copy
.mBuffer
, mSize
);
135 Packet
& Packet::operator=(const Packet
& copy
)
139 mBuffer
= (char*)malloc(copy
.mSize
);
143 mBoolR
= copy
.mBoolR
;
144 mBoolW
= copy
.mBoolW
;
145 mBoolNumR
= copy
.mBoolNumR
;
146 mBoolNumW
= copy
.mBoolNumW
;
157 Packet
& Packet::operator<<(bool value
)
159 int bit
= mBoolNumW
% 8;
164 unsigned char byte
= 0;
165 if (write(&byte
, 1) == 0) return *this;
168 if (value
) mBuffer
[mBoolW
] |= (1 << bit
);
175 Packet
& Packet::operator<<(int8_t value
)
177 return *this << (uint8_t)value
;
180 Packet
& Packet::operator<<(int16_t value
)
182 return *this << (uint16_t)value
;
185 Packet
& Packet::operator<<(int32_t value
)
187 return *this << (uint32_t)value
;
190 Packet
& Packet::operator<<(int64_t value
)
192 return *this << (uint64_t)value
;
196 Packet
& Packet::operator<<(uint8_t value
)
198 write(&value
, sizeof(value
));
202 Packet
& Packet::operator<<(uint16_t value
)
204 value
= htons(value
);
205 write(&value
, sizeof(value
));
209 Packet
& Packet::operator<<(uint32_t value
)
211 value
= htonl(value
);
212 write(&value
, sizeof(value
));
216 Packet
& Packet::operator<<(uint64_t value
)
218 value
= htonll(value
);
219 write(&value
, sizeof(value
));
223 Packet
& Packet::operator<<(float value
)
225 // XXX: assumes the ieee-754
226 uint32_t* integer
= reinterpret_cast<uint32_t*>(&value
);
227 *integer
= htonl(*integer
);
228 write(integer
, sizeof(value
));
232 Packet
& Packet::operator<<(double value
)
234 // XXX: assumes the ieee-754
235 uint64_t* integer
= reinterpret_cast<uint64_t*>(&value
);
236 *integer
= htonll(*integer
);
237 write(integer
, sizeof(value
));
241 size_t Packet::write(const void* bytes
, size_t size
)
243 size_t nBytes
= std::min(size
, mSize
- mW
);
244 if (!mBuffer
|| nBytes
< size
)
246 int nPages
= 1 + size
/ PAGE_SIZE
;
247 int newSize
= mSize
+ nPages
* PAGE_SIZE
;
248 char* newBuffer
= (char*)realloc(mBuffer
, newSize
);
255 if (!mBuffer
) return 0;
257 memcpy(&mBuffer
[mW
], bytes
, nBytes
);
263 Packet
& Packet::operator>>(bool& value
)
265 int bit
= mBoolNumR
% 8;
270 unsigned char byte
= 0;
271 if (read(&byte
, 1) == 0) return *this;
274 value
= 1 & (mBuffer
[mBoolR
] >> bit
);
280 Packet
& Packet::operator>>(int8_t& value
)
282 return *this >> (uint8_t&)value
;
285 Packet
& Packet::operator>>(int16_t& value
)
287 return *this >> (uint16_t&)value
;
290 Packet
& Packet::operator>>(int32_t& value
)
292 return *this >> (uint32_t&)value
;
295 Packet
& Packet::operator>>(int64_t& value
)
297 return *this >> (uint64_t&)value
;
300 Packet
& Packet::operator>>(uint8_t& value
)
302 read(&value
, sizeof(value
));
306 Packet
& Packet::operator>>(uint16_t& value
)
308 read(&value
, sizeof(value
));
309 value
= ntohs(value
);
313 Packet
& Packet::operator>>(uint32_t& value
)
315 read(&value
, sizeof(value
));
316 value
= ntohl(value
);
320 Packet
& Packet::operator>>(uint64_t& value
)
322 read(&value
, sizeof(value
));
323 value
= ntohll(value
);
327 Packet
& Packet::operator>>(float& value
)
329 // XXX: assumes the ieee-754
330 uint32_t* integer
= reinterpret_cast<uint32_t*>(&value
);
331 read(integer
, sizeof(value
));
332 *integer
= htonl(*integer
);
336 Packet
& Packet::operator>>(double& value
)
338 // XXX: assumes the ieee-754
339 uint64_t* integer
= reinterpret_cast<uint64_t*>(&value
);
340 read(integer
, sizeof(value
));
341 *integer
= htonll(*integer
);
345 size_t Packet::read(void* bytes
, size_t size
)
347 size_t nBytes
= std::min(size
, mW
- mR
);
348 memcpy(bytes
, &mBuffer
[mR
], nBytes
);
This page took 0.047039 seconds and 4 git commands to generate.