File tree 2 files changed +75
-0
lines changed
2 files changed +75
-0
lines changed Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using WebSocketSharp ;
4
+
5
+ namespace Example1
6
+ {
7
+ internal class BinaryMessage
8
+ {
9
+ public uint UserID {
10
+ get ; set ;
11
+ }
12
+
13
+ public byte ChannelNumber {
14
+ get ; set ;
15
+ }
16
+
17
+ public uint BufferLength {
18
+ get ; set ;
19
+ }
20
+
21
+ public float [ , ] BufferArray {
22
+ get ; set ;
23
+ }
24
+
25
+ public static BinaryMessage Parse ( byte [ ] data )
26
+ {
27
+ var id = data . SubArray ( 0 , 4 ) . To < uint > ( ByteOrder . Big ) ;
28
+ var num = data . SubArray ( 4 , 1 ) [ 0 ] ;
29
+ var len = data . SubArray ( 5 , 4 ) . To < uint > ( ByteOrder . Big ) ;
30
+ var arr = new float [ num , len ] ;
31
+
32
+ var offset = 9 ;
33
+ ( ( uint ) num ) . Times (
34
+ i =>
35
+ len . Times (
36
+ j => {
37
+ arr [ i , j ] = data . SubArray ( offset , 4 ) . To < float > ( ByteOrder . Big ) ;
38
+ offset += 4 ;
39
+ }
40
+ )
41
+ ) ;
42
+
43
+ return new BinaryMessage {
44
+ UserID = id ,
45
+ ChannelNumber = num ,
46
+ BufferLength = len ,
47
+ BufferArray = arr
48
+ } ;
49
+ }
50
+
51
+ public byte [ ] ToArray ( )
52
+ {
53
+ var buff = new List < byte > ( ) ;
54
+
55
+ var id = UserID ;
56
+ var num = ChannelNumber ;
57
+ var len = BufferLength ;
58
+ var arr = BufferArray ;
59
+
60
+ buff . AddRange ( id . ToByteArray ( ByteOrder . Big ) ) ;
61
+ buff . Add ( num ) ;
62
+ buff . AddRange ( len . ToByteArray ( ByteOrder . Big ) ) ;
63
+
64
+ ( ( uint ) num ) . Times (
65
+ i =>
66
+ len . Times (
67
+ j => buff . AddRange ( arr [ i , j ] . ToByteArray ( ByteOrder . Big ) )
68
+ )
69
+ ) ;
70
+
71
+ return buff . ToArray ( ) ;
72
+ }
73
+ }
74
+ }
Original file line number Diff line number Diff line change 66
66
<Compile Include =" TextMessage.cs" />
67
67
<Compile Include =" NotificationMessage.cs" />
68
68
<Compile Include =" Notifier.cs" />
69
+ <Compile Include =" BinaryMessage.cs" />
69
70
</ItemGroup >
70
71
<Import Project =" $(MSBuildBinPath)\Microsoft.CSharp.targets" />
71
72
<ItemGroup >
You can’t perform that action at this time.
0 commit comments