1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
using System;
using System.IO;
using XUtliPoolLib;
namespace XMainClient
{
public class ProtocolHead
{
public bool IsPtc
{
get
{
return !this.TestBit(this.flag, 0);
}
}
public bool IsRpc
{
get
{
return this.TestBit(this.flag, 0);
}
}
public bool IsRpcReply
{
get
{
return !this.TestBit(this.flag, 1);
}
}
public bool IsRpcRequest
{
get
{
return this.TestBit(this.flag, 1);
}
}
public bool IsCompressed
{
get
{
return this.TestBit(this.flag, 2);
}
}
public bool IsRpcNull
{
get
{
return this.TestBit(this.flag, 3);
}
}
public int Size
{
get
{
bool isRpc = this.IsRpc;
int result;
if (isRpc)
{
result = 16;
}
else
{
result = 12;
}
return result;
}
}
public static ProtocolHead SharedHead = new ProtocolHead();
public static byte[] sharedUIntBuffer = new byte[4];
public uint len;
public uint type;
public uint flag;
public uint tagID;
public const uint MinSize = 12u;
public bool TestBit(uint value, int bit)
{
return ((ulong)value & (ulong)(1L << (bit & 31))) > 0UL;
}
public ProtocolHead()
{
this.len = 0u;
this.type = 0u;
this.flag = 0u;
}
public void Reset()
{
this.len = 0u;
this.type = 0u;
this.flag = 0u;
this.tagID = 0u;
}
public void Deserialize(byte[] bytes)
{
this.len = BitConverter.ToUInt32(bytes, 0);
this.type = BitConverter.ToUInt32(bytes, 4);
this.flag = BitConverter.ToUInt32(bytes, 8);
bool isRpc = this.IsRpc;
if (isRpc)
{
this.tagID = BitConverter.ToUInt32(bytes, 12);
}
}
private uint ToUInt32(ref SmallBuffer<byte> sb, int startIndex)
{
uint num = (uint)sb[startIndex];
uint num2 = (uint)sb[startIndex + 1];
uint num3 = (uint)sb[startIndex + 2];
uint num4 = (uint)sb[startIndex + 3];
return num | num2 << 8 | num3 << 16 | num4 << 24;
}
public void Deserialize(ref SmallBuffer<byte> sb)
{
this.len = this.ToUInt32(ref sb, 0);
this.type = this.ToUInt32(ref sb, 4);
this.flag = this.ToUInt32(ref sb, 8);
bool isRpc = this.IsRpc;
if (isRpc)
{
this.tagID = this.ToUInt32(ref sb, 12);
}
}
private byte[] GetBytes(uint value)
{
ProtocolHead.sharedUIntBuffer[0] = (byte)(value & 255u);
ProtocolHead.sharedUIntBuffer[1] = (byte)((value & 65280u) >> 8);
ProtocolHead.sharedUIntBuffer[2] = (byte)((value & 16711680u) >> 16);
ProtocolHead.sharedUIntBuffer[3] = (byte)((value & 4278190080u) >> 24);
return ProtocolHead.sharedUIntBuffer;
}
public void Serialize(MemoryStream stream)
{
stream.Write(this.GetBytes(this.len), 0, 4);
stream.Write(this.GetBytes(this.type), 0, 4);
stream.Write(this.GetBytes(this.flag), 0, 4);
bool isRpc = this.IsRpc;
if (isRpc)
{
stream.Write(this.GetBytes(this.tagID), 0, 4);
}
}
}
}
|