5

I'm new to binary in C# and have a question on the best way to do this. I have an application I'm trying to communicate that has a specific binary message format. It has to begin with a B8 hex code, and end with a BB hex code, with the binary message in between. What is the best way of being able to take a byte buffer and cast it to the message for easy access to the message properties? I'd imagine a struct, but in all honesty i don't really know.

EDIT:

The reason i don't want it in binary is so that I can easliy use the data in my application. For example, i'd like to convert the binary bits that represent a command type to an enum. Like this (just a representation of what I'd like to do):

struct CommandMessage
{
    public CommandType Command { get; set; }
    public object Data { get; set; }
}

enum CommandType
{
    UserJoined,
    MessageReceived
}
svick
  • 236,525
  • 50
  • 385
  • 514
LordZardeck
  • 7,953
  • 19
  • 62
  • 119
  • 1
    This is very unclear. Can you post an example of what you are trying to do? Why do you need this binary message? What do you do with it? Why do you want a C# representation that isn't a `byte[]`? – Oded Apr 06 '13 at 08:56
  • To answer that, we would need to understand exactly how the message is laid out in the byte[] - i.e. *can* it be logically unpacked as a struct (which frankly I don't really recommend anyway). In particular, any `string` content is going to mean "no". You also need to understand the endianness of any data, etc. Also : in binary there is nothing magic about `BB` / `B8` - what do you intend to do if a `BB` or `B8` happens naturally inside a float / int / string / guid / etc? – Marc Gravell Apr 06 '13 at 08:57
  • The example does nothing to explain the "specific binary message format", nor to explain what you're doing about the BB/B8 issue. Is the "specific binary message format" already defined? or are you free to choose your own layout? – Marc Gravell Apr 06 '13 at 09:14
  • See this: http://stackoverflow.com/questions/3278827/how-to-convert-a-structure-to-a-byte-array-in-c – DasKrümelmonster Apr 06 '13 at 09:34
  • 1
    K; it is already defined, but you haven't told us what that definition is. So how can we say whether you can map it directly to a struct, or advise on the best route forward? The details really matter here. – Marc Gravell Apr 06 '13 at 13:10
  • Sorry. `B8` precedes the command type, while `BB` follows the command and precedes the data. Then a `B8` closes the message – LordZardeck Apr 06 '13 at 19:22

1 Answers1

1

I would suggest to use protobuf-net for DTO serialization.

So, define some entity e.g Package (CommandMessage in your sample) which has

public Command Command;

public byte[] Data; (serialized with protobuf)

Based on Command you will be able to deserialize Data to concrete DTO type using protobuf.

If your message should start with special prefix, you can handle this in Package as well. Also, Package should handle writing/reading itself to/from binary stream or buffers (that's pretty strait forward).

e.g package.WriteTo(buffer) produces [BB,Command,Data,B8]. Same for package.ReadFrom()

illegal-immigrant
  • 8,089
  • 9
  • 51
  • 84
  • @MarcGravell as far as I understood from comments, it should work. Let me explain: DTOs are serialized with protobuf, but Package (wrapper around DTO+Command) handles all the read/write logic. So say we have package.WriteTo(...) which writes [BB, Command, Data(serialized DTO), B8]. Same approach for reading. – illegal-immigrant Apr 06 '13 at 13:16
  • until you get a naturally occurring B8 in the payload – Marc Gravell Apr 06 '13 at 16:02