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