]>
Dogcows Code - chaz/yoink/blob - Packet.cc
437438effd73803b814123f3bfbea4ab32c45be4
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
)),
109 Packet::Packet(const char* data
, size_t size
) :
110 mBuffer((char*)malloc(size
)),
120 if (mBuffer
) memcpy(mBuffer
, data
, size
);
121 else throw std::length_error("out of memory");
125 Packet::Packet(const Packet
& copy
) :
126 mBuffer((char*)malloc(copy
.mSize
)),
130 mOriginalW(copy
.mOriginalW
),
133 mBoolNumR(copy
.mBoolNumR
),
134 mBoolNumW(copy
.mBoolNumW
)
136 if (mBuffer
) memcpy(mBuffer
, copy
.mBuffer
, mSize
);
137 else throw std::length_error("out of memory");
140 Packet
& Packet::operator=(const Packet
& copy
)
144 mBuffer
= (char*)malloc(copy
.mSize
);
148 mOriginalW
= copy
.mOriginalW
;
149 mBoolR
= copy
.mBoolR
;
150 mBoolW
= copy
.mBoolW
;
151 mBoolNumR
= copy
.mBoolNumR
;
152 mBoolNumW
= copy
.mBoolNumW
;
153 if (mBuffer
) memcpy(mBuffer
, copy
.mBuffer
, mSize
);
154 else throw std::length_error("out of memory");
165 Packet
& Packet::operator<<(bool value
)
167 int bit
= mBoolNumW
% 8;
172 unsigned char byte
= 0;
173 if (write(&byte
, 1) == 0) throw std::length_error("out of memory");
176 if (value
) mBuffer
[mBoolW
] |= (1 << bit
);
183 Packet
& Packet::operator<<(int8_t value
)
185 return *this << reinterpret_cast<uint8_t&>(value
);
188 Packet
& Packet::operator<<(int16_t value
)
190 return *this << reinterpret_cast<uint16_t&>(value
);
193 Packet
& Packet::operator<<(int32_t value
)
195 return *this << reinterpret_cast<uint32_t&>(value
);
198 Packet
& Packet::operator<<(int64_t value
)
200 return *this << reinterpret_cast<uint64_t&>(value
);
204 Packet
& Packet::operator<<(uint8_t value
)
206 if (write(&value
, sizeof(value
)) != sizeof(value
))
208 throw std::length_error("out of memory");
213 Packet
& Packet::operator<<(uint16_t value
)
215 value
= htons(value
);
216 if (write(&value
, sizeof(value
)) != sizeof(value
))
218 throw std::length_error("out of memory");
223 Packet
& Packet::operator<<(uint32_t value
)
225 value
= htonl(value
);
226 if (write(&value
, sizeof(value
)) != sizeof(value
))
228 throw std::length_error("out of memory");
233 Packet
& Packet::operator<<(uint64_t value
)
235 value
= htonll(value
);
236 if (write(&value
, sizeof(value
)) != sizeof(value
))
238 throw std::length_error("out of memory");
243 Packet
& Packet::operator<<(float value
)
245 // XXX: assumes the ieee-754
246 uint32_t* integer
= reinterpret_cast<uint32_t*>(&value
);
247 *integer
= htonl(*integer
);
248 if (write(integer
, sizeof(value
)) != sizeof(value
))
250 throw std::length_error("out of memory");
255 Packet
& Packet::operator<<(double value
)
257 // XXX: assumes the ieee-754
258 uint64_t* integer
= reinterpret_cast<uint64_t*>(&value
);
259 *integer
= htonll(*integer
);
260 if (write(integer
, sizeof(value
)) != sizeof(value
))
262 throw std::length_error("out of memory");
267 size_t Packet::write(const void* bytes
, size_t size
)
269 size_t nBytes
= std::min(size
, mSize
- mW
);
270 if (!mBuffer
|| nBytes
< size
)
272 int nPages
= 1 + size
/ PAGE_SIZE
;
273 int newSize
= mSize
+ nPages
* PAGE_SIZE
;
274 char* newBuffer
= (char*)realloc(mBuffer
, newSize
);
281 if (!mBuffer
) return 0;
283 memcpy(mBuffer
+ mW
, bytes
, nBytes
);
289 Packet
& Packet::operator>>(bool& value
)
291 int bit
= mBoolNumR
% 8;
296 unsigned char byte
= 0;
297 if (read(&byte
, 1) == 0) throw std::out_of_range("end of packet");
300 value
= 1 & (mBuffer
[mBoolR
] >> bit
);
306 Packet
& Packet::operator>>(int8_t& value
)
308 return *this >> reinterpret_cast<uint8_t&>(value
);
311 Packet
& Packet::operator>>(int16_t& value
)
313 return *this >> reinterpret_cast<uint16_t&>(value
);
316 Packet
& Packet::operator>>(int32_t& value
)
318 return *this >> reinterpret_cast<uint32_t&>(value
);
321 Packet
& Packet::operator>>(int64_t& value
)
323 return *this >> reinterpret_cast<uint64_t&>(value
);
326 Packet
& Packet::operator>>(uint8_t& value
)
328 if (read(&value
, sizeof(value
)) != sizeof(value
))
330 throw std::out_of_range("end of packet");
335 Packet
& Packet::operator>>(uint16_t& value
)
337 if (read(&value
, sizeof(value
)) != sizeof(value
))
339 throw std::out_of_range("end of packet");
341 value
= ntohs(value
);
345 Packet
& Packet::operator>>(uint32_t& value
)
347 if (read(&value
, sizeof(value
)) != sizeof(value
))
349 throw std::out_of_range("end of packet");
351 value
= ntohl(value
);
355 Packet
& Packet::operator>>(uint64_t& value
)
357 if (read(&value
, sizeof(value
)) != sizeof(value
))
359 throw std::out_of_range("end of packet");
361 value
= ntohll(value
);
365 Packet
& Packet::operator>>(float& value
)
367 // XXX: assumes the ieee-754
368 uint32_t* integer
= reinterpret_cast<uint32_t*>(&value
);
369 if (read(integer
, sizeof(value
)) != sizeof(value
))
371 throw std::out_of_range("end of packet");
373 *integer
= htonl(*integer
);
377 Packet
& Packet::operator>>(double& value
)
379 // XXX: assumes the ieee-754
380 uint64_t* integer
= reinterpret_cast<uint64_t*>(&value
);
381 if (read(integer
, sizeof(value
)) != sizeof(value
))
383 throw std::out_of_range("end of packet");
385 *integer
= htonll(*integer
);
389 size_t Packet::read(void* bytes
, size_t size
)
391 size_t nBytes
= std::min(size
, mW
- mR
);
392 memcpy(bytes
, mBuffer
+ mR
, nBytes
);
This page took 0.054603 seconds and 3 git commands to generate.