So it's early and I always have anxiety over things like this, so I was hoping someone could take a quick look and make sure this would actually work.
I've got a socket program that will transmit a Data object over the wire. I just want to make sure that my bytes are in order and nothing obviously wrong is going on. I haven't gotten far enough into it to test it, I just wanted to head off any errors at the pass.
This is the method that converts a object of type Data to a single byte array to be transmitted:
/// <summary>
/// Prepares a Data object for transmission over the wire.
/// </summary>
/// <param name="data">The Data object to prepare</param>
/// <returns>A byte array to send over the wire</returns>
public byte[] PrepareForWire(Data data)
{
using (MemoryStream DataArray = new MemoryStream())
{
// convert the data to byte arrays
byte[] dIsSecret = BitConverter.GetBytes(data.DIsSecret);
byte[] dCommand = BitConverter.GetBytes((int)data.DCommand);
// write the arrays to the memory stream
//skip the first 4 bytes, this is where the message length goes
DataArray.Seek(4, 0);
DataArray.Write(dCommand, 0, 4);
DataArray.Write(dIsSecret, 0, 1);
DataArray.Write(data.DMessage, 0, data.DMessage.Length);
// find the length of our message
int TransmissionLength = DataArray.ToArray().Length;
// write the length of the message minus 4 bytes to the start of the memory stream
DataArray.Seek(0, 0);
DataArray.Write(BitConverter.GetBytes(TransmissionLength - 4), 0, 4);
return DataArray.ToArray();
}
}
And here is a Data object and it's constructors:
/// <summary>
/// Data object
/// </summary>
public class Data
{
/// <summary>
/// The command requested
/// </summary>
public Command DCommand;
/// <summary>
/// Flag indicating if the message is secret or not
/// </summary>
public bool DIsSecret;
/// <summary>
/// The message
/// </summary>
public byte[] DMessage;
/// <summary>
/// Creates a new Data object
/// </summary>
public Data()
{
DCommand = Command.GoodBye;
DIsSecret = true;
}
/// <summary>
/// Creates a new Data object
/// </summary>
/// <param name="dCommand">Command to perform</param>
/// <param name="dMessage">The message</param>
public Data(Command dCommand, byte[] dMessage)
{
DCommand = dCommand;
DIsSecret = true;
DMessage = dMessage;
}
/// <summary>
/// Creates a new Data object
/// </summary>
/// <param name="dCommand">Command to perform</param>
/// <param name="dIsSecret">Flag indicating if the message is secret or not</param>
/// <param name="dMessage">The message</param>
public Data(Command dCommand, bool dIsSecret, byte[] dMessage)
{
DCommand = dCommand;
DIsSecret = dIsSecret;
DMessage = dMessage;
}
The array that the method returns should look like this:
| Position | Length | Data Type | Description |
----------------------------------------------------------------------------------------------------
| 0 | 4 | Int32 | The length of the following message |
| 4 | 4 | Int32 | The command to perform (See: Command Enumeration) |
| 8 | 1 | bool | Flag indicating if the receiver should use his private key |
| | | | when reading the message |
| 9 | Varies | string | If the IsSecret flag is "False" this is unicode text. |
| | | | If the IsSecret flag is "True" this is an encrypted object,|
| | | | the type of which is determined by the command. |
----------------------------------------------------------------------------------------------------