forked from NewLifeX/X
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebSocketMessage.cs
203 lines (170 loc) · 5.16 KB
/
WebSocketMessage.cs
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using NewLife.Data;
namespace NewLife.Http;
/// <summary>WebSocket消息类型</summary>
public enum WebSocketMessageType
{
/// <summary>附加数据</summary>
Data = 0,
/// <summary>文本数据</summary>
Text = 1,
/// <summary>二进制数据</summary>
Binary = 2,
/// <summary>连接关闭</summary>
Close = 8,
/// <summary>心跳</summary>
Ping = 9,
/// <summary>心跳响应</summary>
Pong = 10,
}
/// <summary>WebSocket消息</summary>
public class WebSocketMessage
{
#region 属性
/// <summary>消息是否结束</summary>
public Boolean Fin { get; set; }
/// <summary>消息类型</summary>
public WebSocketMessageType Type { get; set; }
/// <summary>加密数据的掩码</summary>
public Byte[]? MaskKey { get; set; }
/// <summary>负载数据</summary>
public Packet? Payload { get; set; }
/// <summary>关闭状态。仅用于Close消息</summary>
public Int32 CloseStatus { get; set; }
/// <summary>关闭状态描述。仅用于Close消息</summary>
public String? StatusDescription { get; set; }
#endregion
#region 方法
/// <summary>读取消息</summary>
/// <param name="pk"></param>
/// <returns></returns>
public Boolean Read(Packet pk)
{
if (pk.Count < 2) return false;
var ms = pk.GetStream();
var b = ms.ReadByte();
Type = (WebSocketMessageType)(b & 0x7F);
// 仅处理一个包
Fin = (b & 0x80) == 0x80;
if (!Fin) return false;
var len = ms.ReadByte();
var mask = (len & 0x80) == 0x80;
/*
* 数据长度
* len < 126 单字节表示长度
* len = 126 后续2字节表示长度,大端
* len = 127 后续8字节表示长度
*/
len &= 0x7F;
if (len == 126)
len = (ms.ReadByte() << 8) | ms.ReadByte();
else if (len == 127)
{
var buf = new Byte[8];
ms.Read(buf, 0, buf.Length);
// 没有人会传输超大数据
len = (Int32)BitConverter.ToUInt64(buf, 0);
}
// 如果mask,剩下的就是数据,避免拷贝,提升性能
if (!mask)
{
Payload = pk.Slice((Int32)ms.Position, len);
}
else
{
var masks = new Byte[4];
if (mask) ms.Read(masks, 0, masks.Length);
MaskKey = masks;
if (mask)
{
// 直接在数据缓冲区修改,避免拷贝
var data = Payload = pk.Slice((Int32)ms.Position, len);
for (var i = 0; i < len; i++)
{
data[i] = (Byte)(data[i] ^ masks[i % 4]);
}
}
}
// 特殊处理关闭消息
if (Type == WebSocketMessageType.Close)
{
var data = Payload;
if (data != null)
{
CloseStatus = data.ReadBytes(0, 2).ToUInt16(0, false);
StatusDescription = data.Slice(2).ToStr();
}
}
return true;
}
/// <summary>把消息转为封包</summary>
/// <returns></returns>
public virtual Packet ToPacket()
{
var pk = Payload;
var len = pk == null ? 0 : pk.Total;
// 特殊处理关闭消息
if (len == 0 && Type == WebSocketMessageType.Close)
{
pk = new Packet(((UInt16)CloseStatus).GetBytes(false))
{
Next = StatusDescription.GetBytes()
};
len = pk.Total;
}
var ms = new MemoryStream();
ms.WriteByte((Byte)(0x80 | (Byte)Type));
var masks = MaskKey;
/*
* 数据长度
* len < 126 单字节表示长度
* len = 126 后续2字节表示长度,大端
* len = 127 后续8字节表示长度
*/
if (masks == null)
{
if (len < 126)
{
ms.WriteByte((Byte)len);
}
else if (len < 0xFFFF)
{
ms.WriteByte(126);
ms.Write(((Int16)len).GetBytes(false));
}
else
{
ms.WriteByte(127);
ms.Write(((Int64)len).GetBytes(false));
}
}
else
{
if (len < 126)
{
ms.WriteByte((Byte)(len | 0x80));
}
else if (len < 0xFFFF)
{
ms.WriteByte(126 | 0x80);
ms.Write(((Int16)len).GetBytes(false));
}
else
{
ms.WriteByte(127 | 0x80);
ms.Write(((Int64)len).GetBytes(false));
}
ms.Write(masks);
// 掩码混淆数据。直接在数据缓冲区修改,避免拷贝
var data = Payload;
if (data != null)
{
for (var i = 0; i < len; i++)
{
data[i] = (Byte)(data[i] ^ masks[i % 4]);
}
}
}
return new Packet(ms.ToArray()) { Next = pk };
}
#endregion
}