]>
Dogcows Code - chaz/yoink/blob - src/Moof/Packet.cc
8862f29bc9ba918ef23fb4608d539681fbb4f90a
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
)),
110 Packet::Packet(const char* data
, size_t size
) :
111 mBuffer((char*)malloc(size
)),
120 memcpy(mBuffer
, data
, size
);
130 Packet
& Packet::operator<<(bool value
)
132 int bit
= mBoolNumW
% 8;
137 unsigned char byte
= 0;
138 if (write(&byte
, 1) == 0) return *this;
141 if (value
) mBuffer
[mBoolW
] |= (1 << bit
);
148 Packet
& Packet::operator<<(int8_t value
)
150 return *this << (uint8_t)value
;
153 Packet
& Packet::operator<<(int16_t value
)
155 return *this << (uint16_t)value
;
158 Packet
& Packet::operator<<(int32_t value
)
160 return *this << (uint32_t)value
;
163 Packet
& Packet::operator<<(int64_t value
)
165 return *this << (uint64_t)value
;
169 Packet
& Packet::operator<<(uint8_t value
)
171 write(&value
, sizeof(value
));
175 Packet
& Packet::operator<<(uint16_t value
)
177 value
= htons(value
);
178 write(&value
, sizeof(value
));
182 Packet
& Packet::operator<<(uint32_t value
)
184 value
= htonl(value
);
185 write(&value
, sizeof(value
));
189 Packet
& Packet::operator<<(uint64_t value
)
191 value
= htonll(value
);
192 write(&value
, sizeof(value
));
196 Packet
& Packet::operator<<(float value
)
198 // XXX: assumes the ieee-754
199 uint32_t* integer
= reinterpret_cast<uint32_t*>(&value
);
200 *integer
= htonl(*integer
);
201 write(integer
, sizeof(value
));
205 Packet
& Packet::operator<<(double value
)
207 // XXX: assumes the ieee-754
208 uint64_t* integer
= reinterpret_cast<uint64_t*>(&value
);
209 *integer
= htonll(*integer
);
210 write(integer
, sizeof(value
));
214 size_t Packet::write(const void* bytes
, size_t size
)
216 size_t nBytes
= std::min(size
, mSize
- mW
);
217 if (!mBuffer
|| nBytes
< size
)
219 int numPages
= 1 + size
/ PAGE_SIZE
;
220 int newSize
= mSize
+ numPages
* PAGE_SIZE
;
221 char* newBuffer
= (char*)realloc(mBuffer
, newSize
);
228 memcpy(&mBuffer
[mW
], bytes
, nBytes
);
234 Packet
& Packet::operator>>(bool& value
)
236 int bit
= mBoolNumR
% 8;
241 unsigned char byte
= 0;
242 if (read(&byte
, 1) == 0) return *this;
245 value
= 1 & (mBuffer
[mBoolR
] >> bit
);
251 Packet
& Packet::operator>>(int8_t& value
)
253 return *this >> (uint8_t&)value
;
256 Packet
& Packet::operator>>(int16_t& value
)
258 return *this >> (uint16_t&)value
;
261 Packet
& Packet::operator>>(int32_t& value
)
263 return *this >> (uint32_t&)value
;
266 Packet
& Packet::operator>>(int64_t& value
)
268 return *this >> (uint64_t&)value
;
271 Packet
& Packet::operator>>(uint8_t& value
)
273 read(&value
, sizeof(value
));
277 Packet
& Packet::operator>>(uint16_t& value
)
279 read(&value
, sizeof(value
));
280 value
= ntohs(value
);
284 Packet
& Packet::operator>>(uint32_t& value
)
286 read(&value
, sizeof(value
));
287 value
= ntohl(value
);
291 Packet
& Packet::operator>>(uint64_t& value
)
293 read(&value
, sizeof(value
));
294 value
= ntohll(value
);
298 Packet
& Packet::operator>>(float& value
)
300 // XXX: assumes the ieee-754
301 uint32_t* integer
= reinterpret_cast<uint32_t*>(&value
);
302 read(integer
, sizeof(value
));
303 *integer
= htonl(*integer
);
307 Packet
& Packet::operator>>(double& value
)
309 // XXX: assumes the ieee-754
310 uint64_t* integer
= reinterpret_cast<uint64_t*>(&value
);
311 read(integer
, sizeof(value
));
312 *integer
= htonll(*integer
);
316 size_t Packet::read(void* bytes
, size_t size
)
318 size_t nBytes
= std::min(size
, mW
- mR
);
319 memcpy(bytes
, &mBuffer
[mR
], nBytes
);
This page took 0.047241 seconds and 4 git commands to generate.