forked from andremussche/DelphiWebsockets
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
IdWebsocketServer.pas
351 lines (306 loc) · 10.6 KB
/
IdWebsocketServer.pas
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
unit IdWebSocketServer;
interface
{$I wsdefines.pas}
uses
System.Classes, IdStreamVCL, IdGlobal, IdWinsock2, IdHTTPServer, IdContext,
IdCustomHTTPServer, IdHTTPWebBrokerBridge, IdIOHandlerWebSocket,
IdServerIOHandlerWebSocket, IdServerWebSocketContext,
IdServerWebSocketHandling, IdServerSocketIOHandling, IdWebSocketTypes,
IdIIOHandlerWebSocket;
type
TWebSocketMessageText = procedure(const AContext: TIdServerWSContext;
const aText: string; const AResponse: TStream) of object;
TWebSocketMessageBin = procedure(const AContext: TIdServerWSContext;
const aData: TStream; const AResponse: TStream) of object;
{$IFDEF WEBSOCKETBRIDGE}
TMyIdHttpWebBrokerBridge = class(TidHttpWebBrokerBridge)
published
property OnCreatePostStream;
property OnDoneWithPostStream;
property OnCommandGet;
end;
{$ENDIF}
{$IFDEF WEBSOCKETBRIDGE}
TIdWebSocketServer = class(TMyIdHttpWebBrokerBridge)
{$ELSE}
TIdWebSocketServer = class(TIdHTTPServer)
{$ENDIF}
private
FSocketIO: TIdServerSocketIOHandling_Ext;
FOnMessageText: TWebSocketMessageText;
FOnMessageBin: TWebSocketMessageBin;
FOnWebSocketClosing: TOnWebSocketClosing;
FWriteTimeout: Integer;
FConnectionEvents: TWebSocketConnectionEvents;
function GetSocketIO: TIdServerSocketIOHandling;
procedure SetWriteTimeout(const Value: Integer);
protected
function WebSocketCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo): Boolean;
procedure DoCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); override;
procedure ContextCreated(AContext: TIdContext); override;
procedure ContextDisconnected(AContext: TIdContext); override;
procedure InternalDisconnect(const AHandler: IIOHandlerWebsocket;
ANotifyPeer: Boolean; const AReason: string='');
procedure WebSocketUpgradeRequest(const AContext: TIdServerWSContext;
const ARequestInfo: TIdHTTPRequestInfo; var Accept: Boolean); virtual;
procedure WebSocketChannelRequest(const AContext: TIdServerWSContext;
var aType:TWSDataType; const aStrmRequest, aStrmResponse: TMemoryStream); virtual;
procedure SetOnWebSocketClosing(const AValue: TOnWebSocketClosing);
// Fix for AV when changing Active from True to False;
procedure DoTerminateContext(AContext: TIdContext); override;
public
procedure AfterConstruction; override;
destructor Destroy; override;
procedure DisconnectAll(const AReason: string='');
procedure DisconnectWithReason(const AHandler: IIOHandlerWebSocket;
const AReason: string; ANotifyPeer: Boolean = True);
procedure SendMessageToAll(const aBinStream: TStream); overload;
/// <summary> Sends the indicated message to all users except the AExceptTarget </summary>
procedure SendMessageToAll(const AText: string;
const AExceptTarget: TIdServerWSContext=nil); overload;
property OnConnected: TWebSocketConnected read FConnectionEvents.ConnectedEvent
write FConnectionEvents.ConnectedEvent;
property OnDisconnected: TWebSocketDisconnected read FConnectionEvents.DisconnectedEvent
write FConnectionEvents.DisconnectedEvent;
property OnMessageText: TWebSocketMessageText read FOnMessageText write FOnMessageText;
property OnMessageBin : TWebSocketMessageBin read FOnMessageBin write FOnMessageBin;
property OnWebSocketClosing: TOnWebSocketClosing read FOnWebSocketClosing write SetOnWebSocketClosing;
property SocketIO: TIdServerSocketIOHandling read GetSocketIO;
published
property WriteTimeout: Integer read FWriteTimeout write SetWriteTimeout default 2000;
end;
implementation
uses
System.SysUtils, WSDebugger, IdHTTPWebSocketClient;
{ TIdWebSocketServer }
procedure TIdWebSocketServer.AfterConstruction;
begin
inherited;
FSocketIO := TIdServerSocketIOHandling_Ext.Create;
ContextClass := TIdServerWSContext;
if IOHandler = nil then
IOHandler := TIdServerIOHandlerWebSocket.Create(Self);
FWriteTimeout := 2 * 1000; //2s
end;
procedure TIdWebSocketServer.ContextCreated(AContext: TIdContext);
begin
inherited ContextCreated(AContext);
(AContext as TIdServerWSContext).OnCustomChannelExecute := Self.WebSocketChannelRequest;
// default 2s write timeout
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms740532(v=vs.85).aspx
try
AContext.Connection.Socket.Binding.SetSockOpt(SOL_SOCKET, SO_SNDTIMEO, WriteTimeout);
except
// Some Lunux, eg Ubuntu, can't accept setting of WriteTimeout
end;
end;
procedure TIdWebSocketServer.ContextDisconnected(AContext: TIdContext);
begin
FSocketIO.FreeConnection(AContext);
inherited;
end;
destructor TIdWebSocketServer.Destroy;
begin
FSocketIO.Free;
inherited;
end;
procedure TIdWebSocketServer.DisconnectAll(const AReason: string);
var
LList: TList;
LContext: TIdServerWSContext;
I: Integer;
LHandler: IIOHandlerWebSocket;
begin
LList := Self.Contexts.LockList;
try
for I := LList.Count - 1 downto 0 do
begin
LContext := TIdServerWSContext(LList[I]);
Assert(LContext is TIdServerWSContext);
LHandler := LContext.IOHandler;
try
if LHandler.IsWebSocket and not LContext.IsSocketIO then
begin
DisconnectWithReason(LHandler, AReason);
// LHandler.Close;
end;
except
{$IF DEFINED(DEBUG_WS)}
on E: Exception do
WSDebugger.OutputDebugString('Disconnect All: '+E.Message);
{$ENDIF}
end;
end;
finally
Self.Contexts.UnlockList;
end;
end;
procedure TIdWebSocketServer.DisconnectWithReason(
const AHandler: IIOHandlerWebSocket; const AReason: string;
ANotifyPeer: Boolean);
begin
InternalDisconnect(AHandler, ANotifyPeer, AReason);
end;
function TIdWebSocketServer.WebSocketCommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo): Boolean;
var
LHandler: IIOHandlerWebSocket;
LServerContext: TIdServerWSContext absolute AContext;
begin
LServerContext.OnWebSocketUpgrade := Self.WebSocketUpgradeRequest;
LServerContext.OnCustomChannelExecute := Self.WebSocketChannelRequest;
LServerContext.SocketIO := FSocketIO;
Result := True;
try // try to see if server becomes non-responsive by removing the try-except
LHandler := LServerContext.IOHandler;
LHandler.OnNotifyClosed := procedure begin
LHandler.ClosedGracefully := True;
end;
Result := TIdServerWebSocketHandling.ProcessServerCommandGet(
AContext as TIdServerWSContext, FConnectionEvents,
ARequestInfo, AResponseInfo);
{$IF DEFINED(DEBUG_WS)}
WSDebugger.OutputDebugString('Completed ProcessServerCommandGet');
{$ENDIF}
except
// chuacw fix, CloseConnection when return????
on E: Exception do
begin
{$IF DEFINED(DEBUG_WS)}
WSDebugger.OutputDebugString('Exception in ProcessServerCommandGet: '+E.Message);
{$ENDIF}
SetIOHandler(nil);
end;
end;
end;
procedure TIdWebSocketServer.DoCommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
if not WebSocketCommandGet(AContext,ARequestInfo,AResponseInfo) then
inherited DoCommandGet(AContext, ARequestInfo, AResponseInfo);
end;
function TIdWebSocketServer.GetSocketIO: TIdServerSocketIOHandling;
begin
Result := FSocketIO;
end;
procedure TIdWebSocketServer.InternalDisconnect(
const AHandler: IIOHandlerWebSocket; ANotifyPeer: Boolean;
const AReason: string);
var
LHandler: IIOHandlerWebSocket;
begin
LHandler := AHandler;
// See if we can get the reason from the server by using the read thread
if LHandler <> nil then
begin
if AReason = '' then
LHandler.Close else
LHandler.CloseWithReason(AReason);
LHandler.Lock;
try
LHandler.IsWebSocket := False;
LHandler.Clear;
finally
LHandler.Unlock;
end;
end;
end;
procedure TIdWebSocketServer.SendMessageToAll(const AText: string;
const AExceptTarget: TIdServerWSContext=nil);
var
LList: TList;
LContext: TIdServerWSContext;
I: Integer;
begin
LList := Contexts.LockList;
try
for I := 0 to LList.Count - 1 do
begin
LContext := TIdServerWSContext(LList.Items[I]);
Assert(LContext is TIdServerWSContext);
if LContext = AExceptTarget then
Continue; // skip sending to this target
if LContext.IOHandler.IsWebSocket and not LContext.IsSocketIO then
LContext.IOHandler.Write(AText);
end;
finally
Self.Contexts.UnlockList;
end;
end;
procedure TIdWebSocketServer.SetOnWebSocketClosing(
const AValue: TOnWebSocketClosing);
var
LList: TList;
LContext: TIdServerWSContext;
I: Integer;
LHandler: IIOHandlerWebSocket;
LSetWebSocketClosing: ISetWebSocketClosing;
begin
FOnWebSocketClosing := AValue;
LList := Contexts.LockList;
try
for I := LList.Count - 1 downto 0 do
begin
LContext := TIdServerWSContext(LList[I]);
Assert(LContext is TIdServerWSContext);
LHandler := LContext.IOHandler;
if Supports(LHandler, ISetWebSocketClosing, LSetWebSocketClosing) then
LSetWebSocketClosing.SetWebSocketClosing(AValue);
end;
finally
Contexts.UnlockList;
end;
end;
procedure TIdWebSocketServer.DoTerminateContext(AContext: TIdContext);
begin
AContext.Binding.CloseSocket;
end;
procedure TIdWebSocketServer.SetWriteTimeout(const Value: Integer);
begin
FWriteTimeout := Value;
end;
procedure TIdWebSocketServer.WebSocketUpgradeRequest(const AContext: TIdServerWSContext;
const ARequestInfo: TIdHTTPRequestInfo; var Accept: Boolean);
begin
Accept := True;
end;
procedure TIdWebSocketServer.WebSocketChannelRequest(
const AContext: TIdServerWSContext; var aType: TWSDataType;
const aStrmRequest, aStrmResponse: TMemoryStream);
var s: string;
begin
if aType = wdtText then
begin
with TStreamReader.Create(aStrmRequest) do
begin
s := ReadToEnd;
Free;
end;
if Assigned(OnMessageText) then
OnMessageText(AContext, s, aStrmResponse)
end
else if Assigned(OnMessageBin) then
OnMessageBin(AContext, aStrmRequest, aStrmResponse)
end;
procedure TIdWebSocketServer.SendMessageToAll(const aBinStream: TStream);
var
LList: TList;
LContext: TIdServerWSContext;
I: Integer;
bytes: TIdBytes;
begin
LList := Contexts.LockList;
try
TIdStreamHelperVCL.ReadBytes(aBinStream, bytes);
for I := 0 to LList.Count - 1 do
begin
LContext := TIdServerWSContext(LList.Items[I]);
Assert(LContext is TIdServerWSContext);
if LContext.IOHandler.IsWebSocket and not LContext.IsSocketIO then
LContext.IOHandler.Write(bytes);
end;
finally
Contexts.UnlockList;
end;
end;
end.