Converted as much ugly code as I could find into appropriate C#-friendly versions. For example, all the manual ByteHelper stuff, buffer index tracking, etc. are now BinaryWriters/BinaryReaders. Also cleaned up a bunch of getter and setter methods to use C# properties. There's still more to be done here, but this greatly simplifies the code for reading and comprehension.
95 lines
2.4 KiB
C#
95 lines
2.4 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace ScreenLogicConnect.Messages
|
|
{
|
|
public class HLMessage
|
|
{
|
|
public const int HEADER_SIZE = 8;
|
|
|
|
protected byte[] data;
|
|
protected MemoryStream dataByteStream;
|
|
protected byte[] header = new byte[HEADER_SIZE];
|
|
protected MemoryStream headerByteStream;
|
|
protected int startIndex = 0;
|
|
|
|
public HLMessage(short sender, short id)
|
|
{
|
|
headerByteStream = new MemoryStream(header);
|
|
using (var mw = new BinaryWriter(headerByteStream))
|
|
{
|
|
mw.Write(sender);
|
|
mw.Write(id);
|
|
while (headerByteStream.Position < headerByteStream.Length)
|
|
{
|
|
mw.Write((byte)0xff);
|
|
}
|
|
}
|
|
}
|
|
|
|
public HLMessage(byte[] headerArray, byte[] dataArray)
|
|
{
|
|
headerByteStream = new MemoryStream(header);
|
|
using (var bw = new BinaryWriter(headerByteStream))
|
|
{
|
|
bw.Write(headerArray);
|
|
}
|
|
|
|
this.data = new byte[dataArray.Length];
|
|
dataByteStream = new MemoryStream(data);
|
|
using (var bw = new BinaryWriter(dataByteStream))
|
|
{
|
|
bw.Write(dataArray);
|
|
}
|
|
|
|
decode();
|
|
}
|
|
|
|
public HLMessage(HLMessage msg)
|
|
: this(msg.header, msg.data)
|
|
{
|
|
}
|
|
|
|
public virtual byte[] asByteArray()
|
|
{
|
|
var dataLength = this.data?.Length ?? 0;
|
|
byte[] result = new byte[dataLength + this.header.Length];
|
|
using (var bw = new BinaryWriter(new MemoryStream(result)))
|
|
{
|
|
bw.Write(this.header, 0, 4);
|
|
bw.Write(dataLength);
|
|
if (dataLength > 0)
|
|
{
|
|
bw.Write(this.data);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public static int extractDataSize(byte[] data)
|
|
{
|
|
return BitConverter.ToInt32(data, 4);
|
|
}
|
|
|
|
public short getMessageID()
|
|
{
|
|
return BitConverter.ToInt16(header, 2);
|
|
}
|
|
|
|
public short getMessageSender()
|
|
{
|
|
return BitConverter.ToInt16(header, 0);
|
|
}
|
|
|
|
public String getMessageIDasString()
|
|
{
|
|
return getMessageID().ToString();
|
|
}
|
|
|
|
protected virtual void decode()
|
|
{
|
|
}
|
|
}
|
|
}
|