Add traits that enable object like parsing/serializing of game query packets #148
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a draft implementation of what #104 could look like.
It adds 3 traits:
ToPacket
- to encode a struct as aVec<u8>
for sending over the networkFromPacket
- to create a struct from&[u8]
ExtendFromPacket
- to extend the data in a struct from&[u8]
, this is useful when responses come in multiple packets e.g. player responsesTo help with implementing the
ToPacket
traitWriteBuffer
was also added, this functions very similarly toBuffer
but is instead for writing data into a buffer.These traits can be used by the query implementations to create packets:
In unreal 2 the old send request was implemented as
this becomes
where PacketRequest is an exact representation of the raw packet bytes:
They can also be used to parse packets:
For the unreal2 implementation the packets all have a common outer header part, using these traits the can be represented using an outer
ResponsePacket
type that is generic over the inner body. I think this is quite a nice abstraction as it allows for a blanket implementation for encoding/decoding all packets where the inner body implements theToPacket
orFromPacket
traits. Although there is a bit of added complexity.Here the Packet structs are also raw representation of the bytes.
Positives of moving to this architecture:
Downsides:
My thoughts:
#[repr(C)]
) in these cases it could be a lot faster to just cast the struct to a slice of bytes rather than using aWriteBuffer
.ToPacket
could takeWriteBuffer
as a parameter directly to avoid allocating aVec
.I'll probably think of more benefits/downsides in the future but I think this is in a place where its ready for other people to give their thoughts on.