initial network stuff
[chaz/yoink] / src / Moof / Packet.cc
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
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.
9 *
10 **************************************************************************/
11
12 #include "../config.h"
13
14 #include <algorithm>
15 #if HAVE_BYTESWAP_H
16 #include <byteswap.h>
17 #endif
18
19 #include <arpa/inet.h>
20
21 #include <SDL/SDL.h>
22
23 #include "Packet.hh"
24
25
26 #ifndef bswap_64
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))
35 #endif
36
37 static uint64_t htonll(uint64_t x)
38 {
39 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
40 return bswap_64(x);
41 #endif
42 }
43
44 static uint64_t ntohll(uint64_t x)
45 {
46 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
47 return bswap_64(x);
48 #endif
49 }
50
51
52 namespace Mf {
53
54
55 Packet::Packet(size_t size) :
56 mR(0),
57 mW(0),
58 mBoolR(0),
59 mBoolW(0),
60 mBoolNumR(0),
61 mBoolNumW(0)
62 {
63 mBuffer = boost::shared_array<char>(new char[size]);
64 mSize = size;
65 }
66
67 Packet::Packet(const char* data, size_t size) :
68 mR(0),
69 mBoolR(0),
70 mBoolW(0),
71 mBoolNumR(0),
72 mBoolNumW(0)
73 {
74 mBuffer = boost::shared_array<char>(new char[size]);
75 mSize = size;
76 mW = size;
77 memcpy(mBuffer.get(), data, mSize);
78 }
79
80
81 Packet& Packet::operator<<(bool value)
82 {
83 int bit = mBoolNumW % 8;
84 if (bit == 0)
85 {
86 mBoolW = mW;
87
88 unsigned char byte = 0;
89 if (write(&byte, 1) == 0) return *this;
90 }
91
92 if (value) mBuffer[mBoolW] |= (1 << bit);
93 ++mBoolNumW;
94
95 return *this;
96 }
97
98
99 Packet& Packet::operator<<(int8_t value)
100 {
101 return *this << (uint8_t)value;
102 }
103
104 Packet& Packet::operator<<(int16_t value)
105 {
106 return *this << (uint16_t)value;
107 }
108
109 Packet& Packet::operator<<(int32_t value)
110 {
111 return *this << (uint32_t)value;
112 }
113
114 Packet& Packet::operator<<(int64_t value)
115 {
116 return *this << (uint64_t)value;
117 }
118
119
120 Packet& Packet::operator<<(uint8_t value)
121 {
122 write(&value, sizeof(value));
123 return *this;
124 }
125
126 Packet& Packet::operator<<(uint16_t value)
127 {
128 value = htons(value);
129 write(&value, sizeof(value));
130 return *this;
131 }
132
133 Packet& Packet::operator<<(uint32_t value)
134 {
135 value = htonl(value);
136 write(&value, sizeof(value));
137 return *this;
138 }
139
140 Packet& Packet::operator<<(uint64_t value)
141 {
142 value = htonll(value);
143 write(&value, sizeof(value));
144 return *this;
145 }
146
147 //Packet& Packet::operator<<(float value)
148 //{
149 //}
150
151 //Packet& Packet::operator<<(double value)
152 //{
153 //}
154
155 //Packet& Packet::operator<<(long double value)
156 //{
157 //}
158
159
160 size_t Packet::write(const void* bytes, size_t size)
161 {
162 size_t nBytes = std::min(size, mSize - mW);
163 memcpy(&mBuffer[mW], bytes, nBytes);
164 mW += nBytes;
165 return nBytes;
166 }
167
168
169 Packet& Packet::operator>>(bool& value)
170 {
171 int bit = mBoolNumR % 8;
172 if (bit == 0)
173 {
174 mBoolR = mR;
175
176 unsigned char byte = 0;
177 if (read(&byte, 1) == 0) return *this;
178 }
179
180 value = 1 & (mBuffer[mBoolR] >> bit);
181 ++mBoolNumR;
182
183 return *this;
184 }
185
186 Packet& Packet::operator>>(int8_t& value)
187 {
188 return *this >> (uint8_t&)value;
189 }
190
191 Packet& Packet::operator>>(int16_t& value)
192 {
193 return *this >> (uint16_t&)value;
194 }
195
196 Packet& Packet::operator>>(int32_t& value)
197 {
198 return *this >> (uint32_t&)value;
199 }
200
201 Packet& Packet::operator>>(int64_t& value)
202 {
203 return *this >> (uint64_t&)value;
204 }
205
206 Packet& Packet::operator>>(uint8_t& value)
207 {
208 read(&value, sizeof(value));
209 return *this;
210 }
211
212 Packet& Packet::operator>>(uint16_t& value)
213 {
214 read(&value, sizeof(value));
215 value = ntohs(value);
216 return *this;
217 }
218
219 Packet& Packet::operator>>(uint32_t& value)
220 {
221 read(&value, sizeof(value));
222 value = ntohl(value);
223 return *this;
224 }
225
226 Packet& Packet::operator>>(uint64_t& value)
227 {
228 read(&value, sizeof(value));
229 value = ntohll(value);
230 return *this;
231 }
232
233
234 size_t Packet::read(void* bytes, size_t size)
235 {
236 size_t nBytes = std::min(size, mW - mR);
237 memcpy(bytes, &mBuffer[mR], nBytes);
238 mR += nBytes;
239 return nBytes;
240 }
241
242
243 } // namespace Mf
244
This page took 0.039032 seconds and 4 git commands to generate.