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.
43 lines
1.2 KiB
C#
43 lines
1.2 KiB
C#
using ScreenLogicConnect.Messages;
|
|
using System.IO;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
|
|
namespace ScreenLogicConnect
|
|
{
|
|
static class ExtensionMethods
|
|
{
|
|
public static void SendHLMessage(this NetworkStream stream, Messages.HLMessage msg)
|
|
{
|
|
var arr = msg.asByteArray();
|
|
System.Diagnostics.Debug.WriteLine($" sent {arr.Length}");
|
|
stream.Write(arr, 0, arr.Length);
|
|
}
|
|
|
|
public static void WritePrefixLength(this BinaryWriter bw, string val)
|
|
{
|
|
bw.Write(val.Length);
|
|
bw.Write(Encoding.ASCII.GetBytes(val));
|
|
bw.Write(new byte[HLMessageTypeHelper.alignToNext4Boundary(val.Length)]);
|
|
}
|
|
|
|
public static void WritePrefixLength(this BinaryWriter bw, byte[] val)
|
|
{
|
|
bw.Write(val.Length);
|
|
bw.Write(val);
|
|
}
|
|
|
|
public static void Write(this BinaryWriter bw, HLTime hlTime)
|
|
{
|
|
bw.Write(hlTime.year);
|
|
bw.Write(hlTime.month);
|
|
bw.Write(hlTime.dayOfWeek);
|
|
bw.Write(hlTime.day);
|
|
bw.Write(hlTime.hour);
|
|
bw.Write(hlTime.minute);
|
|
bw.Write(hlTime.second);
|
|
bw.Write(hlTime.millisecond);
|
|
}
|
|
}
|
|
}
|