]> Dogcows Code - chaz/yoink/blob - src/Moof/Packet.hh
096f112ca063289f502dc7b5f60395188a1f8d70
[chaz/yoink] / src / Moof / Packet.hh
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 #ifndef _MOOF_PACKET_HH_
13 #define _MOOF_PACKET_HH_
14
15 #include <cstring>
16 #include <string>
17 #include <vector>
18
19
20 #ifndef PAGE_SIZE
21 #define PAGE_SIZE 4096
22 #endif
23
24
25 namespace Mf {
26
27
28 class Packet
29 {
30 public:
31
32 Packet(size_t size = PAGE_SIZE);
33 Packet(const char* data, size_t size);
34
35 ~Packet();
36
37 Packet& operator<<(bool value);
38 Packet& operator<<(int8_t value);
39 Packet& operator<<(int16_t value);
40 Packet& operator<<(int32_t value);
41 Packet& operator<<(int64_t value);
42 Packet& operator<<(uint8_t value);
43 Packet& operator<<(uint16_t value);
44 Packet& operator<<(uint32_t value);
45 Packet& operator<<(uint64_t value);
46 Packet& operator<<(float value);
47 Packet& operator<<(double value);
48
49 size_t write(const void* bytes, size_t size);
50
51 Packet& operator>>(bool& value);
52 Packet& operator>>(int8_t& value);
53 Packet& operator>>(int16_t& value);
54 Packet& operator>>(int32_t& value);
55 Packet& operator>>(int64_t& value);
56 Packet& operator>>(uint8_t& value);
57 Packet& operator>>(uint16_t& value);
58 Packet& operator>>(uint32_t& value);
59 Packet& operator>>(uint64_t& value);
60 Packet& operator>>(float& value);
61 Packet& operator>>(double& value);
62
63 size_t read(void* bytes, size_t size);
64
65
66 const char* bytes() const
67 {
68 return mBuffer + mR;
69 }
70
71 size_t size() const
72 {
73 return mW - mR;
74 }
75
76
77 private:
78
79 char* mBuffer;
80 size_t mSize;
81
82 size_t mR;
83 size_t mW;
84
85 size_t mBoolR;
86 size_t mBoolW;
87 size_t mBoolNumR;
88 size_t mBoolNumW;
89 };
90
91
92 inline Packet& operator<<(Packet& packet, const char* value)
93 {
94 uint16_t length = strlen(value);
95 packet << length;
96 packet.write(value, length);
97 return packet;
98 }
99
100 inline Packet& operator<<(Packet& packet, const std::string& value)
101 {
102 packet << (uint16_t)value.length();
103 packet.write(value.c_str(), value.length());
104 return packet;
105 }
106
107 template <class T>
108 inline Packet& operator<<(Packet& packet, const std::vector<T>& value)
109 {
110 packet << (uint8_t)value.size();
111 typename std::vector<T>::const_iterator it;
112 for (it = value.begin(); it != value.end(); ++it)
113 {
114 packet << *it;
115 }
116 return packet;
117 }
118
119
120 inline Packet& operator>>(Packet& packet, std::string& value)
121 {
122 uint8_t length = 0;
123 packet >> length;
124
125 char str[256];
126 size_t charsRead = packet.read(str, length);
127 value.assign(str, charsRead);
128 return packet;
129 }
130
131 template <class T>
132 inline Packet& operator>>(Packet& packet, std::vector<T>& value)
133 {
134 uint8_t size = 0;
135 packet >> size;
136
137 value.clear();
138 for (uint8_t i = 0; i < size; ++i)
139 {
140 T item;
141 packet >> item;
142 value.push_back(item);
143 }
144 return packet;
145 }
146
147
148
149 } // namespace Mf
150
151 #endif // _MOOF_PACKET_HH_
152
This page took 0.036851 seconds and 4 git commands to generate.