forked from andremussche/DelphiWebsockets
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Journeyman.Portable.WSMultiReadThread.pas
431 lines (372 loc) · 12.5 KB
/
Journeyman.Portable.WSMultiReadThread.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
unit Journeyman.Portable.WSMultiReadThread;
interface
uses
Journeyman.WebSocket.MultiReadThread, IdStack, IdStackConsts;
type
TPortableMultiReadThread = class(TIdWebSocketMultiReadThread)
protected
FReadSet, FExceptionSet: TIdSocketList;
FInterval: Integer;
{$IF DEFINED(ANDROID)}
FConnectIP: string;
FConnectHandle, FListenHandle: TIdStackSocketHandle;
procedure Disconnect(var AHandle: TIdStackSocketHandle); inline;
{$ELSEIF DEFINED(POSIX)}
FPosixWritePipe: TIdStackSocketHandle;
FConnectIP: string;
FConnectHandle, FListenHandle: TIdStackSocketHandle;
procedure Disconnect(var AHandle: TIdStackSocketHandle); inline;
{$ENDIF}
procedure BreakSelectWait; override;
procedure CloseSpecialEventSocket; override;
procedure InitSpecialEventSocket; override;
procedure InitComponents; override;
procedure FreeComponents; override;
procedure ReadFromAllChannels; override;
end;
implementation
{$WARN UNIT_PLATFORM OFF}
uses
IdStackBSDBase, IdGlobal, System.SysUtils,
Journeyman.WebSocket.Interfaces,
System.DateUtils, Journeyman.WebSocket.Consts,
{$IF DEFINED(MSWINDOWS)}
Winapi.Windows,
{$ELSEIF DEFINED(LINUX64) OR DEFINED(POSIX)}
Posix.Fcntl, Posix.Unistd, Posix.StrOpts,
{$IF DEFINED(LINUX64) OR DEFINED(LINUX)}
Linuxapi.KernelIoctl,
{$ENDIF}
{$ENDIF}
System.Generics.Collections, Journeyman.WebSocket.Client, Journeyman.WebSocket.Types,
Journeyman.WebSocket.DebugUtils;
{$IF DEFINED(ANDROID)}
type
TIdStackAndroid = class helper for TIdStackBSDBase
public
function SendTo(ASocket: TIdStackSocketHandle; const ABuffer: TIdBytes;
const AOffset: Integer = 0; const ASize: Integer = -1; Flags: Integer = 0): Integer; overload; inline;
function SendTo(ASocket: TIdStackSocketHandle; const ABuffer: TArray<Byte>;
const AOffset: Integer = 0; const ASize: Integer = -1; Flags: Integer = 0): Integer; overload; inline;
end;
function TIdStackAndroid.SendTo(ASocket: TIdStackSocketHandle;
const ABuffer: TIdBytes; const AOffset: Integer = 0;
const ASize: Integer = -1; Flags: Integer = 0): Integer;
begin
Result := WSSend(ASocket, ABuffer[AOffset], Length(ABuffer), Flags);
end;
function TIdStackAndroid.SendTo(ASocket: TIdStackSocketHandle;
const ABuffer: TArray<Byte>; const AOffset: Integer = 0;
const ASize: Integer = -1; Flags: Integer = 0): Integer;
begin
Result := WSSend(ASocket, ABuffer[AOffset], Length(ABuffer), Flags);
end;
{$ENDIF}
{$IF DEFINED(ANDROID) OR DEFINED(POSIX)}
procedure TPortableMultiReadThread.Disconnect(var AHandle: TIdStackSocketHandle);
begin
if AHandle <> 0 then
begin
GStack.Disconnect(AHandle);
AHandle := 0;
end;
end;
{$ENDIF}
procedure TPortableMultiReadThread.BreakSelectWait;
begin
// Test if this speeds up the 2s delay
Exit;
// End test
if FTempHandle = 0 then
Exit;
FPendingBreak := True;
{$IF DECLARED(TIdStackAndroid)}
var LBuffer: TIdBytes := [$99]; // send any data... this value arbitrary.
// Send an OOB message, necessary to cause Select to exit immediately
// on the Exception set.
GBSDStack.SendTo(FConnectHandle, LBuffer, 0, Length(LBuffer),
MSG_OOB);
{$IF DEFINED(DEBUG_WS)}
OutputDebugString('Socket', 'OOB data, no disconnection');
{$ENDIF}
{$ELSEIF DEFINED(POSIX)}
// pipe approach, which works by writing to the write part of the pipe
// which then causes data to be available in the read part of the pipe
var nRead: Integer := 0;
var Dummy: Byte := 0;
var LIOCtlResult := ioctl(FTempHandle, FIONREAD, @nRead);
if (LIOCtlResult = 0) and (nRead = 0) then
begin
// Writes to the write pipe
FileWrite(FPosixWritePipe, Dummy, SizeOf(Dummy));
end;
{$ELSE}
// connect to non-existing address to stop "select" from waiting
// Note: this is some kind of "hack" because there is no nice way to stop it
// The only(?) other possibility is to make a "socket pair" and send a byte to it,
// but this requires a dynamic server socket (which can trigger a firewall
// exception/question popup in WindowsXP+)
// https://docs.microsoft.com/en-us/windows/desktop/api/winsock2/nf-winsock2-select
// If a socket is processing a connect call (nonblocking), failure of the
// connect attempt is indicated in exceptfds (application must then call
// getsockopt SO_ERROR to determine the error value to describe why the
// failure occurred). This document does not define which other
// errors will be included.
try
{$IF DEFINED(DEBUG_WS)}
OutputDebugString('WSChat', 'Portable Breaking SelectWait');
{$ENDIF}
// Hopefully nothing is listening on port 1
GStack.Connect(FTempHandle, '0.0.0.0', 1, Id_IPv4);
except
on E: EIdSocketError do
begin
// Debug
OutputDebugString(E.Message);
end; // do nothing
else
raise;
end;
{$ENDIF}
end;
procedure TPortableMultiReadThread.CloseSpecialEventSocket;
begin
{$IF DEFINED(POSIX) OR DEFINED(ANDROID)}
Disconnect(FListenHandle);
Disconnect(FConnectHandle);
Disconnect(FTempHandle);
//{$ELSEIF DEFINED(POSIX)}
// FileClose(FTempHandle);
// FTempHandle := 0;
// FileClose(FPosixWritePipe);
// FPosixWritePipe := 0;
{$ELSE}
inherited;
{$ENDIF}
end;
procedure TPortableMultiReadThread.InitComponents;
begin
FReadSet := TIdSocketList.CreateSocketList;
FExceptionSet := TIdSocketList.CreateSocketList;
end;
procedure TPortableMultiReadThread.FreeComponents;
begin
FExceptionSet.Free;
FReadSet.Free;
end;
procedure TPortableMultiReadThread.InitSpecialEventSocket;
// FIONBIO is $8004667E
const
//{$IF DEFINED(PORTABLE)}
PortableIOC_IN = $80000000;
PortableIOCPARM_MASK = $7F;
PortableFIONBIO = PortableIOC_IN or ((SizeOf(Longint) and PortableIOCPARM_MASK) shl 16)
or (Ord('f') shl 8) or 126; {Do not Localize}
//{$ENDIF}
var
LTempHandle: THandle;
begin
if GStack = nil then
Exit; // finalized?
{$REGION 'Enable non-blocking mode'}
{$IF DEFINED(ANDROID) OR DEFINED(LINUX)}
LTempHandle := GStack.NewSocketHandle(Id_SOCK_STREAM, Id_IPPROTO_IP,
Id_IPv4, False);
// FTempHandle := LTempHandle;
//
// var LFlags := fcntl(LTempHandle, F_GETFL, 0);
// LFlags := LFlags or O_NONBLOCK;
// fcntl(LTempHandle, F_SETFL, LFlags);
GStack.Bind(LTempHandle, '0.0.0.0', 0, Id_IPv4);
var LIP: string; var LPort: Word;
GStack.GetSocketName(LTempHandle, LIP, LPort);
// Can't connect to 0.0.0.0, so need to connect to a valid address...
if LIP = '0.0.0.0' then
FConnectIP := '127.0.0.1' else
FConnectIP := LIP;
GStack.Listen(LTempHandle, 1);
FListenHandle := LTempHandle;
FConnectHandle := GStack.NewSocketHandle(Id_SOCK_STREAM, Id_IPPROTO_IP, Id_IPv4, False);
GStack.Connect(FConnectHandle, FConnectIP, LPort, Id_IPv4);
var LAcceptedHandle := GStack.Accept(FListenHandle, FConnectIP, LPort);
FTempHandle := LAcceptedHandle;
{$ELSEIF DEFINED(POSIX)}
// LFlags := fcntl(LTempHandle, F_GETFL, 0);
// LFlags := LFlags or O_NONBLOCK;
// iResult := fcntl(LTempHandle, F_SETFL, LFlags);
// pipe approach, which works by writing to the write part of the pipe
// which then causes data to be available in the read part of the pipe
var LSyncEvent: TPipeDescriptors;
if pipe(LSyncEvent) >= 0 then
begin
FTempHandle := LSyncEvent.ReadDes; // read pipe
FPosixWritePipe := LSyncEvent.WriteDes; // write pipe
end;
{$ELSE}
// alloc socket
LTempHandle := GStack.NewSocketHandle(Id_SOCK_STREAM, Id_IPPROTO_IP, Id_IPv4, False);
FTempHandle := LTempHandle;
Assert(LTempHandle <> THandle(Id_INVALID_SOCKET));
var LFlags: Cardinal := 1; // enable NON blocking mode
var iResult := GStack.IOControl(LTempHandle, PortableFIONBIO, LFlags);
GStack.CheckForSocketError(iResult);
{$ENDIF}
{$ENDREGION}
end;
procedure TPortableMultiReadThread.ReadFromAllChannels;
var
LList: TList<TIdHTTPWebSocketClient>;
LClient: TIdHTTPWebSocketClient;
/// <summary>Number of WebSockets with data.</summary>
LWebSocketCount, I: Integer;
LSelectorsHaveData: Boolean;
LEnterSleep: Boolean;
LHandler: IIOHandlerWebSocket;
begin
{$IF DEFINED(DEBUG_WS)}
OutputDebugString('ReadFromAllChannels');
{$ENDIF}
LList := FChannels.LockList;
try
{$IF DEFINED(DEBUG_WS)}
OutputDebugString('ReadFromAllChannels, channel count: '+ LList.Count.ToString);
{$ENDIF}
LWebSocketCount := 0;
LSelectorsHaveData := False;
FReadSet.Clear;
for I := 0 to LList.Count - 1 do
begin
LClient := TIdHTTPWebSocketClient(LList.Items[I]);
if LClient.NoAsyncRead then
Continue;
LHandler := LClient.IOHandler;
// valid?
if //not chn.Busy and also take busy channels (will be ignored later),
// otherwise we have to break/reset for each RO function execution
(LHandler <> nil) and (LHandler.IsWebSocket) and
(LClient.Socket <> nil) and (LClient.Socket.Binding <> nil) and
(LClient.Socket.Binding.Handle > 0) and
(LClient.Socket.Binding.Handle <> Id_INVALID_SOCKET) then
begin
if LHandler.HasData then
begin
LSelectorsHaveData := True;
Break;
end;
FReadSet.Add(LClient.Socket.Binding.Handle);
Inc(LWebSocketCount);
end;
end;
if FPendingBreak then
ResetSpecialEventSocket;
finally
FChannels.UnlockList;
end;
LEnterSleep := not LSelectorsHaveData;
// nothing to wait for? then sleep some time to prevent 100% CPU
if LEnterSleep then
begin
{$IF DEFINED(DEBUG_WS)}
var LStart := Now;
{$ENDIF}
// special helper socket to be able to stop "select" from waiting
FExceptionSet.Clear;
FExceptionSet.Add(FTempHandle);
FInterval := ReadTimeout;
var LExceptionSet: TIdSocketList;
if LWebSocketCount = 0 then
begin
try
// Returns true if there's data
// Returns false if timeout
// Throws an exception if the exception handle has been connected
LExceptionSet := FExceptionSet;
LSelectorsHaveData := FReadSet.Select(nil, nil, LExceptionSet, FInterval);
except
LSelectorsHaveData := True;
end;
end else
begin
// wait till a socket has some data (or a signal via exceptionset is fired)
try
LSelectorsHaveData := FReadSet.Select(FReadSet, nil, FExceptionSet, FInterval);
except
// an error should be the equivalent of iResult = SOCKET_ERROR,
// so set LSelectorsHaveData = false, so that it'll exit...
LSelectorsHaveData := False;
end;
end;
{$IF DEFINED(DEBUG_WS)}
var LStopped := Now;
if MilliSecondsBetween(LStopped, LStart) < ReadTimeout then
begin
OutputDebugString('WSChat', 'Select Broken before Timeout');
end else
begin
OutputDebugString('WSChat', 'Select Broken ON Timeout');
end;
{$ENDIF}
if not LSelectorsHaveData then
Exit;
end;
if Terminated then
Exit;
// Is there some data?
if LSelectorsHaveData then
begin
// make sure the thread is created outside a lock
TIdWebSocketDispatchThread.Instance;
LList := FChannels.LockList;
{$REGION 'LOOP'}
if LList = nil then
Exit;
try
// check for data for all channels
for I := 0 to LList.Count - 1 do
begin
if LList = nil then
Exit;
LClient := TIdHTTPWebSocketClient(LList.Items[I]);
if LClient.NoAsyncRead then
Continue;
if LClient.TryLock then
try
LHandler := LClient.IOHandler as IIOHandlerWebSocket;
if (LHandler = nil) then
Continue;
if LHandler.TryLock then // IOHandler.Readable cannot be done during pending action!
try
try
// If the client got disconnecte, it won't be able to read any data
// and the SSL connection will throw an exceptino
// so need to protect it with a try except block
LClient.ReadAndProcessData;
except
// usually an EAccessViolation from the SSL layer if it's disconnected
on E: Exception do
begin
LList := nil;
FChannels.UnlockList;
LClient.ResetChannel;
// raise;
end;
end;
finally
LHandler.Unlock;
end;
finally
LClient.Unlock;
end;
end;
if FPendingBreak then
ResetSpecialEventSocket;
finally
if LList <> nil then
FChannels.UnlockList;
end;
{$ENDREGION}
end;
end;
initialization
TIdWebSocketMultiReadThread.GMultiReadClass := TPortableMultiReadThread;
end.