]>
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"
19 #include <arpa/inet.h>
27 #define bswap_64(x) (((uint64_t)(x) << 56) | \
28 (((uint64_t)(x) << 40) & 0xff000000000000ULL) | \
29 (((uint64_t)(x) << 24) & 0xff0000000000ULL) | \
30 (((uint64_t)(x) << 8) & 0xff00000000ULL) | \
31 (((uint64_t)(x) >> 8) & 0xff000000ULL) | \
32 (((uint64_t)(x) >> 24) & 0xff0000ULL) | \
33 (((uint64_t)(x) >> 40) & 0xff00ULL) | \
34 ((uint64_t)(x) >> 56))
37 static uint64_t htonll(uint64_t x
)
39 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
44 static uint64_t ntohll(uint64_t x
)
46 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
55 Packet::Packet(size_t size
) :
63 mBuffer
= boost::shared_array
<char>(new char[size
]);
67 Packet::Packet(const char* data
, size_t size
) :
74 mBuffer
= boost::shared_array
<char>(new char[size
]);
77 memcpy(mBuffer
.get(), data
, mSize
);
81 Packet
& Packet::operator<<(bool value
)
83 int bit
= mBoolNumW
% 8;
88 unsigned char byte
= 0;
89 if (write(&byte
, 1) == 0) return *this;
92 if (value
) mBuffer
[mBoolW
] |= (1 << bit
);
99 Packet
& Packet::operator<<(int8_t value
)
101 return *this << (uint8_t)value
;
104 Packet
& Packet::operator<<(int16_t value
)
106 return *this << (uint16_t)value
;
109 Packet
& Packet::operator<<(int32_t value
)
111 return *this << (uint32_t)value
;
114 Packet
& Packet::operator<<(int64_t value
)
116 return *this << (uint64_t)value
;
120 Packet
& Packet::operator<<(uint8_t value
)
122 write(&value
, sizeof(value
));
126 Packet
& Packet::operator<<(uint16_t value
)
128 value
= htons(value
);
129 write(&value
, sizeof(value
));
133 Packet
& Packet::operator<<(uint32_t value
)
135 value
= htonl(value
);
136 write(&value
, sizeof(value
));
140 Packet
& Packet::operator<<(uint64_t value
)
142 value
= htonll(value
);
143 write(&value
, sizeof(value
));
147 //Packet& Packet::operator<<(float value)
151 //Packet& Packet::operator<<(double value)
155 //Packet& Packet::operator<<(long double value)
160 size_t Packet::write(const void* bytes
, size_t size
)
162 size_t nBytes
= std::min(size
, mSize
- mW
);
163 memcpy(&mBuffer
[mW
], bytes
, nBytes
);
169 Packet
& Packet::operator>>(bool& value
)
171 int bit
= mBoolNumR
% 8;
176 unsigned char byte
= 0;
177 if (read(&byte
, 1) == 0) return *this;
180 value
= 1 & (mBuffer
[mBoolR
] >> bit
);
186 Packet
& Packet::operator>>(int8_t& value
)
188 return *this >> (uint8_t&)value
;
191 Packet
& Packet::operator>>(int16_t& value
)
193 return *this >> (uint16_t&)value
;
196 Packet
& Packet::operator>>(int32_t& value
)
198 return *this >> (uint32_t&)value
;
201 Packet
& Packet::operator>>(int64_t& value
)
203 return *this >> (uint64_t&)value
;
206 Packet
& Packet::operator>>(uint8_t& value
)
208 read(&value
, sizeof(value
));
212 Packet
& Packet::operator>>(uint16_t& value
)
214 read(&value
, sizeof(value
));
215 value
= ntohs(value
);
219 Packet
& Packet::operator>>(uint32_t& value
)
221 read(&value
, sizeof(value
));
222 value
= ntohl(value
);
226 Packet
& Packet::operator>>(uint64_t& value
)
228 read(&value
, sizeof(value
));
229 value
= ntohll(value
);
234 size_t Packet::read(void* bytes
, size_t size
)
236 size_t nBytes
= std::min(size
, mW
- mR
);
237 memcpy(bytes
, &mBuffer
[mR
], nBytes
);
This page took 0.045663 seconds and 5 git commands to generate.