]> Dogcows Code - chaz/yoink/blob - src/Moof/Packet.hh
better string encoding in packets
[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 template <class T>
101 inline Packet& operator<<(Packet& packet, const std::basic_string<T>& value)
102 {
103 packet << (uint16_t)value.length();
104 packet.write(value.data(), value.length() * sizeof(T));
105 return packet;
106 }
107
108 template <class T>
109 inline Packet& operator>>(Packet& packet, std::basic_string<T>& value)
110 {
111 uint16_t length = 0;
112 packet >> length;
113
114 T str[length];
115 size_t charsRead = packet.read(str, length * sizeof(T));
116 value.assign(str, charsRead);
117 return packet;
118 }
119
120
121 template <class T>
122 inline Packet& operator<<(Packet& packet, const std::vector<T>& value)
123 {
124 packet << (uint16_t)value.size();
125 typename std::vector<T>::const_iterator it;
126 for (it = value.begin(); it != value.end(); ++it)
127 {
128 packet << *it;
129 }
130 return packet;
131 }
132
133 template <class T>
134 inline Packet& operator>>(Packet& packet, std::vector<T>& value)
135 {
136 uint16_t size = 0;
137 packet >> size;
138
139 value.clear();
140 for (uint16_t i = 0; i < size; ++i)
141 {
142 T item;
143 packet >> item;
144 value.push_back(item);
145 }
146 return packet;
147 }
148
149
150 } // namespace Mf
151
152 #endif // _MOOF_PACKET_HH_
153
This page took 0.035805 seconds and 4 git commands to generate.