forked from chronoxor/NetCoreServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWssServer.cs
More file actions
180 lines (152 loc) · 6.91 KB
/
Copy pathWssServer.cs
File metadata and controls
180 lines (152 loc) · 6.91 KB
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
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace NetCoreServer
{
/// <summary>
/// WebSocket secure server
/// </summary>
/// <remarks> WebSocket secure server is used to communicate with clients using WebSocket protocol. Thread-safe.</remarks>
public class WssServer : HttpsServer, IWebSocket
{
internal readonly WebSocket WebSocket;
/// <summary>
/// Initialize WebSocket server with a given IP address and port number
/// </summary>
/// <param name="context">SSL context</param>
/// <param name="address">IP address</param>
/// <param name="port">Port number</param>
public WssServer(SslContext context, IPAddress address, int port) : base(context, address, port) { WebSocket = new WebSocket(this); }
/// <summary>
/// Initialize WebSocket server with a given IP address and port number
/// </summary>
/// <param name="context">SSL context</param>
/// <param name="address">IP address</param>
/// <param name="port">Port number</param>
public WssServer(SslContext context, string address, int port) : base(context, address, port) { WebSocket = new WebSocket(this); }
/// <summary>
/// Initialize WebSocket server with a given IP endpoint
/// </summary>
/// <param name="context">SSL context</param>
/// <param name="endpoint">IP endpoint</param>
public WssServer(SslContext context, IPEndPoint endpoint) : base(context, endpoint) { WebSocket = new WebSocket(this); }
public virtual bool CloseAll(int status)
{
lock (WebSocket.WsSendLock)
{
WebSocket.PrepareSendFrame(WebSocket.WS_FIN | WebSocket.WS_CLOSE, false, null, 0, 0, status);
if (!Multicast(WebSocket.WsSendBuffer.ToArray()))
return false;
return base.DisconnectAll();
}
}
public override bool Multicast(byte[] buffer, long offset, long size)
{
if (!IsStarted)
return false;
if (size == 0)
return true;
// Multicast data to all WebSocket sessions
foreach (var session in Sessions.Values)
{
if (session is WssSession wssSession)
{
if (wssSession.WebSocket.WsHandshaked)
wssSession.SendAsync(buffer, offset, size);
}
}
return true;
}
#region WebSocket multicast text methods
public bool MulticastText(byte[] buffer, long offset, long size)
{
lock (WebSocket.WsSendLock)
{
WebSocket.PrepareSendFrame(WebSocket.WS_FIN | WebSocket.WS_TEXT, false, buffer, offset, size);
return Multicast(WebSocket.WsSendBuffer.ToArray());
}
}
public bool MulticastText(string text)
{
lock (WebSocket.WsSendLock)
{
var data = Encoding.UTF8.GetBytes(text);
WebSocket.PrepareSendFrame(WebSocket.WS_FIN | WebSocket.WS_TEXT, false, data, 0, data.Length);
return Multicast(WebSocket.WsSendBuffer.ToArray());
}
}
#endregion
#region WebSocket multicast binary methods
public bool MulticastBinary(byte[] buffer, long offset, long size)
{
lock (WebSocket.WsSendLock)
{
WebSocket.PrepareSendFrame(WebSocket.WS_FIN | WebSocket.WS_BINARY, false, buffer, offset, size);
return Multicast(WebSocket.WsSendBuffer.ToArray());
}
}
public bool MulticastBinary(string text)
{
lock (WebSocket.WsSendLock)
{
var data = Encoding.UTF8.GetBytes(text);
WebSocket.PrepareSendFrame(WebSocket.WS_FIN | WebSocket.WS_BINARY, false, data, 0, data.Length);
return Multicast(WebSocket.WsSendBuffer.ToArray());
}
}
#endregion
#region WebSocket multicast ping methods
public bool SendPing(byte[] buffer, long offset, long size)
{
lock (WebSocket.WsSendLock)
{
WebSocket.PrepareSendFrame(WebSocket.WS_FIN | WebSocket.WS_PING, false, buffer, offset, size);
return Multicast(WebSocket.WsSendBuffer.ToArray());
}
}
public bool SendPing(string text)
{
lock (WebSocket.WsSendLock)
{
var data = Encoding.UTF8.GetBytes(text);
WebSocket.PrepareSendFrame(WebSocket.WS_FIN | WebSocket.WS_PING, false, data, 0, data.Length);
return Multicast(WebSocket.WsSendBuffer.ToArray());
}
}
#endregion
#region WebSocket multicast pong methods
public bool SendPong(byte[] buffer, long offset, long size)
{
lock (WebSocket.WsSendLock)
{
WebSocket.PrepareSendFrame(WebSocket.WS_FIN | WebSocket.WS_PONG, false, buffer, offset, size);
return Multicast(WebSocket.WsSendBuffer.ToArray());
}
}
public bool SendPong(string text)
{
lock (WebSocket.WsSendLock)
{
var data = Encoding.UTF8.GetBytes(text);
WebSocket.PrepareSendFrame(WebSocket.WS_FIN | WebSocket.WS_PONG, false, data, 0, data.Length);
return Multicast(WebSocket.WsSendBuffer.ToArray());
}
}
#endregion
#region IWebSocket implementation
public void OnWsConnecting(HttpRequest request) { WebSocket.OnWsConnecting(request); }
public void OnWsConnected(HttpResponse response) { WebSocket.OnWsConnected(response); }
public bool OnWsConnecting(HttpRequest request, HttpResponse response) { return WebSocket.OnWsConnecting(request, response); }
public void OnWsConnected(HttpRequest request) { WebSocket.OnWsConnected(request); }
public void OnWsDisconnected() { WebSocket.OnWsDisconnected(); }
public void OnWsReceived(byte[] buffer, long offset, long size) { WebSocket.OnWsReceived(buffer, offset, size); }
public void OnWsClose(byte[] buffer, long offset, long size) { WebSocket.OnWsClose(buffer, offset, size); }
public void OnWsPing(byte[] buffer, long offset, long size) { WebSocket.OnWsPing(buffer, offset, size); }
public void OnWsPong(byte[] buffer, long offset, long size) { WebSocket.OnWsPong(buffer, offset, size); }
public void OnWsError(string error) { WebSocket.OnWsError(error); }
public void OnWsError(SocketError error) { WebSocket.OnWsError(error); }
public void SendResponse(HttpResponse response) { WebSocket.SendResponse(response); }
#endregion
protected override SslSession CreateSession() { return new WssSession(this); }
}
}