diff --git a/Example1/BinaryMessage.cs b/Example1/BinaryMessage.cs new file mode 100644 index 00000000..eafa7aa9 --- /dev/null +++ b/Example1/BinaryMessage.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using WebSocketSharp; + +namespace Example1 +{ + internal class BinaryMessage + { + public uint UserID { + get; set; + } + + public byte ChannelNumber { + get; set; + } + + public uint BufferLength { + get; set; + } + + public float[,] BufferArray { + get; set; + } + + public static BinaryMessage Parse (byte[] data) + { + var id = data.SubArray (0, 4).To (ByteOrder.Big); + var num = data.SubArray (4, 1)[0]; + var len = data.SubArray (5, 4).To (ByteOrder.Big); + var arr = new float[num, len]; + + var offset = 9; + ((uint) num).Times ( + i => + len.Times ( + j => { + arr[i, j] = data.SubArray (offset, 4).To (ByteOrder.Big); + offset += 4; + } + ) + ); + + return new BinaryMessage { + UserID = id, + ChannelNumber = num, + BufferLength = len, + BufferArray = arr + }; + } + + public byte[] ToArray () + { + var buff = new List (); + + var id = UserID; + var num = ChannelNumber; + var len = BufferLength; + var arr = BufferArray; + + buff.AddRange (id.ToByteArray (ByteOrder.Big)); + buff.Add (num); + buff.AddRange (len.ToByteArray (ByteOrder.Big)); + + ((uint) num).Times ( + i => + len.Times ( + j => buff.AddRange (arr[i, j].ToByteArray (ByteOrder.Big)) + ) + ); + + return buff.ToArray (); + } + } +} diff --git a/Example1/Example1.csproj b/Example1/Example1.csproj index 903f5b04..1e3425ae 100644 --- a/Example1/Example1.csproj +++ b/Example1/Example1.csproj @@ -66,6 +66,7 @@ +