]>
Dogcows Code - chaz/yoink/blob - src/moof/packet.hh
55dd96c7a201989ab1bf7458a47edad34152881a
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 #ifndef _MOOF_PACKET_HH_
13 #define _MOOF_PACKET_HH_
17 * Classes for building and interpreting datagram packets.
27 #define PAGE_SIZE 4096
35 * Represents a packet of serialized variables ready for transfer over the
36 * network. This method is most suitable for representing datagram
37 * packets, but it may also be useful for seralizing data for persistent
38 * state storage. The semantics are similar to that of a FIFO queue or
39 * stream where packets are written and read by inserting and extracting
40 * variables to and from the packet, although the actual order of the
41 * variables in the buffer may be different. At any time, a pointer to a
42 * buffer and the size of the buffer can be retrieved. This class also
43 * handles endian differences by serializing variables in network byte
51 * Construct a packet with an initial capacity.
52 * \param capacity Initial capacity of the packet.
54 explicit packet(size_t size
= PAGE_SIZE
);
57 * Construct a packet with some bytes from a buffer. The bytes will be
58 * copied into the packet, so you don't need to keep the original
60 * \param data The bytes.
61 * \param size The number of bytes.
63 packet(const char* data
, size_t size
);
66 * Insert a variable into the packet, serializing it. This usually
67 * increases the size of the packet by the size of the data type.
68 * \param value The value to insert.
71 packet
& operator << (bool value
);
72 packet
& operator << (int8_t value
);
73 packet
& operator << (int16_t value
);
74 packet
& operator << (int32_t value
);
75 packet
& operator << (int64_t value
);
76 packet
& operator << (uint8_t value
);
77 packet
& operator << (uint16_t value
);
78 packet
& operator << (uint32_t value
);
79 packet
& operator << (uint64_t value
);
80 packet
& operator << (float value
);
81 packet
& operator << (double value
);
84 * Write some bytes to the packet.
85 * \param bytes The bytes.
86 * \param size The number of bytes.
87 * \return The number of bytes actually written.
89 size_t write(const void* bytes
, size_t size
);
93 * Extract a variable from the packet. This usually decreases the size
94 * of the packet by the size of the data type.
95 * \param value Reference to the variable to extract.
98 packet
& operator >> (bool& value
);
99 packet
& operator >> (int8_t& value
);
100 packet
& operator >> (int16_t& value
);
101 packet
& operator >> (int32_t& value
);
102 packet
& operator >> (int64_t& value
);
103 packet
& operator >> (uint8_t& value
);
104 packet
& operator >> (uint16_t& value
);
105 packet
& operator >> (uint32_t& value
);
106 packet
& operator >> (uint64_t& value
);
107 packet
& operator >> (float& value
);
108 packet
& operator >> (double& value
);
111 * Read some bytes from the packet.
112 * \param bytes The buffer to hold the bytes read.
113 * \param size The size of the read buffer.
114 * \return The number of bytes actually read.
116 size_t read(void* bytes
, size_t size
);
120 * Clear the contents of the packet, setting the size of the packet to
127 * Save the current state internally, allowing it to be reverted to
128 * later using revert().
133 * Revert the packet to a previously saved state, or to that state
134 * immediately after construction if none other state has been
135 * explicitly saved using save().
141 * Get a pointer to an internal structure holding the serialized bytes
143 * \return The pointer.
145 const char* bytes() const
147 return buffer_
+ state_
.read_mark
;
151 * Get the size of the buffer holding the serialized bytes of the
153 * \return The number of bytes.
157 return state_
.write_mark
- state_
.read_mark
;
161 // The rest of this stuff is just to implement correct copy semantics.
163 packet(const packet
& copy
);
164 packet
& operator = (const packet
& copy
);
176 size_t read_bool_mark
;
177 size_t read_bool_num
;
179 size_t write_bool_mark
;
180 size_t write_bool_num
;
182 state(size_t size
= 0) :
197 inline packet
& operator << (packet
& packet
, const T
& value
)
204 inline packet
& operator >> (packet
& packet
, T
& value
)
206 value
.unpack(packet
);
211 inline packet
& operator << (packet
& packet
, const char* value
)
213 uint16_t length
= strlen(value
);
215 if (packet
.write(value
, length
) != length
)
217 throw std::length_error("out of memory");
223 inline packet
& operator << (packet
& packet
, const std::basic_string
<T
>& value
)
225 packet
<< static_cast<uint16_t>(value
.length());
226 size_t num_bytes
= value
.length() * sizeof(T
);
227 if (packet
.write(value
.data(), num_bytes
) != num_bytes
)
229 throw std::length_error("out of memory");
235 inline packet
& operator >> (packet
& packet
, std::basic_string
<T
>& value
)
241 size_t num_bytes
= length
* sizeof(T
);
242 if (packet
.read(str
, num_bytes
) != num_bytes
)
244 throw std::out_of_range("end of packet");
246 value
.assign(str
, length
);
252 inline packet
& operator << (packet
& packet
, const std::vector
<T
>& value
)
254 packet
<< static_cast<uint16_t>(value
.size());
255 typename
std::vector
<T
>::const_iterator it
;
256 for (it
= value
.begin(); it
!= value
.end(); ++it
)
264 inline packet
& operator >> (packet
& packet
, std::vector
<T
>& value
)
270 for (uint16_t i
= 0; i
< size
; ++i
)
274 value
.push_back(item
);
282 #endif // _MOOF_PACKET_HH_
This page took 0.06202 seconds and 4 git commands to generate.