c# - Struct for binary message serializiation/deserialization -
i'm new binary in c# , have question on best way this. have application i'm trying communicate has specific binary message format. has begin b8 hex code, , end bb hex code, binary message in between. best way of being able take byte buffer , cast message easy access message properties? i'd imagine struct, in honesty don't know.
edit:
the reason don't want in binary can easliy use data in application. example, i'd convert binary bits represent command type enum. (just representation of i'd do):
struct commandmessage { public commandtype command { get; set; } public object data { get; set; } } enum commandtype { userjoined, messagereceived }
i suggest use protobuf-net dto serialization.
so, define entity e.g package (commandmessage in sample) has
public command command; public byte[] data; (serialized protobuf)
based on command able deserialize data concrete dto type using protobuf.
if message should start special prefix, can handle in package well. also, package should handle writing/reading to/from binary stream or buffers (that's pretty strait forward).
e.g package.writeto(buffer) produces [bb,command,data,b8]. same package.readfrom()
Comments
Post a Comment