]>
Dogcows Code - chaz/yoink/blob - src/Moof/Packet.cc
62da00b17f98141d307573232c2d23ca07444fa8
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"
20 #include <arpa/inet.h>
29 #define bswap_16(x) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
33 #define bswap_32(x) ((((x) & 0xff000000) >> 24) | \
34 (((x) & 0x00ff0000) >> 8) | \
35 (((x) & 0x0000ff00) << 8) | \
36 (((x) & 0x000000ff) << 24))
40 #define bswap_64(x) (((x) << 56) | \
41 (((x) << 40) & 0xff000000000000ULL) | \
42 (((x) << 24) & 0xff0000000000ULL) | \
43 (((x) << 8) & 0xff00000000ULL) | \
44 (((x) >> 8) & 0xff000000ULL) | \
45 (((x) >> 24) & 0xff0000ULL) | \
46 (((x) >> 40) & 0xff00ULL) | \
52 static uint16_t htons(uint16_t x
)
54 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
60 static uint16_t ntohs(uint16_t x
)
65 static uint32_t htonl(uint32_t x
)
67 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
73 static uint32_t ntohl(uint32_t x
)
80 static uint64_t htonll(uint64_t x
)
82 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
88 static uint64_t ntohll(uint64_t x
)
97 Packet::Packet(size_t size
) :
105 mBuffer
= boost::shared_array
<char>(new char[size
]);
109 Packet::Packet(const char* data
, size_t size
) :
116 mBuffer
= boost::shared_array
<char>(new char[size
]);
119 memcpy(mBuffer
.get(), data
, mSize
);
123 Packet
& Packet::operator<<(bool value
)
125 int bit
= mBoolNumW
% 8;
130 unsigned char byte
= 0;
131 if (write(&byte
, 1) == 0) return *this;
134 if (value
) mBuffer
[mBoolW
] |= (1 << bit
);
141 Packet
& Packet::operator<<(int8_t value
)
143 return *this << (uint8_t)value
;
146 Packet
& Packet::operator<<(int16_t value
)
148 return *this << (uint16_t)value
;
151 Packet
& Packet::operator<<(int32_t value
)
153 return *this << (uint32_t)value
;
156 Packet
& Packet::operator<<(int64_t value
)
158 return *this << (uint64_t)value
;
162 Packet
& Packet::operator<<(uint8_t value
)
164 write(&value
, sizeof(value
));
168 Packet
& Packet::operator<<(uint16_t value
)
170 value
= htons(value
);
171 write(&value
, sizeof(value
));
175 Packet
& Packet::operator<<(uint32_t value
)
177 value
= htonl(value
);
178 write(&value
, sizeof(value
));
182 Packet
& Packet::operator<<(uint64_t value
)
184 value
= htonll(value
);
185 write(&value
, sizeof(value
));
189 Packet
& Packet::operator<<(float value
)
191 // XXX: assumes the ieee-754
192 uint32_t* integer
= reinterpret_cast<uint32_t*>(&value
);
193 *integer
= htonl(*integer
);
194 write(integer
, sizeof(value
));
198 Packet
& Packet::operator<<(double value
)
200 // XXX: assumes the ieee-754
201 uint64_t* integer
= reinterpret_cast<uint64_t*>(&value
);
202 *integer
= htonll(*integer
);
203 write(integer
, sizeof(value
));
207 size_t Packet::write(const void* bytes
, size_t size
)
209 size_t nBytes
= std::min(size
, mSize
- mW
);
210 memcpy(&mBuffer
[mW
], bytes
, nBytes
);
216 Packet
& Packet::operator>>(bool& value
)
218 int bit
= mBoolNumR
% 8;
223 unsigned char byte
= 0;
224 if (read(&byte
, 1) == 0) return *this;
227 value
= 1 & (mBuffer
[mBoolR
] >> bit
);
233 Packet
& Packet::operator>>(int8_t& value
)
235 return *this >> (uint8_t&)value
;
238 Packet
& Packet::operator>>(int16_t& value
)
240 return *this >> (uint16_t&)value
;
243 Packet
& Packet::operator>>(int32_t& value
)
245 return *this >> (uint32_t&)value
;
248 Packet
& Packet::operator>>(int64_t& value
)
250 return *this >> (uint64_t&)value
;
253 Packet
& Packet::operator>>(uint8_t& value
)
255 read(&value
, sizeof(value
));
259 Packet
& Packet::operator>>(uint16_t& value
)
261 read(&value
, sizeof(value
));
262 value
= ntohs(value
);
266 Packet
& Packet::operator>>(uint32_t& value
)
268 read(&value
, sizeof(value
));
269 value
= ntohl(value
);
273 Packet
& Packet::operator>>(uint64_t& value
)
275 read(&value
, sizeof(value
));
276 value
= ntohll(value
);
280 Packet
& Packet::operator>>(float& value
)
282 // XXX: assumes the ieee-754
283 uint32_t* integer
= reinterpret_cast<uint32_t*>(&value
);
284 read(integer
, sizeof(value
));
285 *integer
= htonl(*integer
);
289 Packet
& Packet::operator>>(double& value
)
291 // XXX: assumes the ieee-754
292 uint64_t* integer
= reinterpret_cast<uint64_t*>(&value
);
293 read(integer
, sizeof(value
));
294 *integer
= htonll(*integer
);
298 size_t Packet::read(void* bytes
, size_t size
)
300 size_t nBytes
= std::min(size
, mW
- mR
);
301 memcpy(bytes
, &mBuffer
[mR
], nBytes
);
This page took 0.04588 seconds and 3 git commands to generate.