You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+28-9Lines changed: 28 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -59,7 +59,7 @@ auto serialise(const UserPacket& packet) {
59
59
return buffer;
60
60
}
61
61
```
62
-
By default, Hexi will try to serialise basic structures such as our `UserPacket` if they meet requirements for being safe to directly copy the bytes. Now, for reasons of portability, it's not recommended that you do things this way unless you're positive that the data layout is identical on the system that wrote the data. Not to worry, this is easily solved. Plus, we didn't do any error handling. All in good time.
62
+
By default, Hexi will try to serialise basic structures such as our `UserPacket` if they meet requirements for being safe to directly copy the bytes. Now, for reasons of portability, it's not recommended that you do things this way unless you're positive that the data layout is identical on the system that wrote the data. Not to worry, this is easily solved. Plus, we didn't do any error or endianness handling. All in good time.
63
63
64
64
<img src="docs/assets/frog-remember.png" alt="Remember these two classes, if nothing else!">
65
65
@@ -146,15 +146,15 @@ struct UserPacket {
146
146
std::string username;
147
147
uint64_t timestamp;
148
148
uint8_t has_optional_field;
149
-
uint32_t optional_field; // pretend this is bigendian in the protocol
149
+
uint32_t optional_field; // pretend this is big-endian in the protocol
@@ -208,10 +216,21 @@ auto handle_user_packet(std::span<const char> buffer) {
208
216
}
209
217
```
210
218
211
-
Because `binary_stream` is a template, it's easiest to allow the compiler to perform type deduction magic.
219
+
This example is fully portable and is even independent of the platform byte order. By specifying the endianness of the stream, it'll automagically convert all endian-sensitive data to the requested byte order.
220
+
The default argument is `hexi::endian::native`, which will perform no conversions, while `hexi::endian::big` and `hexi::endian::little` will perform conversions if required.
221
+
222
+
If your protocol contains mixed endianness, you can use the endian adaptors to specify the desired byte order when streaming
223
+
the data, as shown in the above example.
224
+
225
+
Best of all, because this is handled by templates, there is zero runtime cost if no conversion is required
226
+
(i.e. that is the native byte order matches the requested byte order) and constant values can be converted
227
+
at compile-time. For example, specifying `hexi::endian::little` on a little-endian platform will generate zero
228
+
code.
229
+
230
+
`docs/examples/endian.cpp` provides examples for byte order handling functionality.
212
231
213
-
If you want the function bodies to be in a source file, it's recommended that you provide your own `using` alias for your `binary_stream` type.
214
-
The alternative is to use the polymorphic equivalents, `pmc::buffer_adaptor` and `pmc::binary_stream`, which allow you to change the underlying buffer type at runtime but at the cost of virtual call overhead and lacking some functionality that doesn't mesh well with polymorphism.
232
+
As for the serialisation functions, if you want the function bodies to be in a source file, it's recommended that you provide your own `using` alias for your `binary_stream` type.
233
+
The alternative is to use the polymorphic equivalents, `pmc::buffer_adaptor` and `pmc::binary_stream`, which allow you to change the underlying buffer type at runtime but at the potential cost of virtual call overhead (devirtualisation not withstanding) and lacking some functionality that doesn't mesh well with polymorphism.
215
234
216
235
How you structure your code is up to you, this is just one way of doing it.
0 commit comments