Skip to content

Commit 09eb71a

Browse files
author
Arnaud Bouchez
committed
raise exception on setting TRestServer.OnNotifyCallback twice
- previous behavior was to allow several WS servers on the same TRestServer instance, silently disabling the precedent WS servers
1 parent e20140c commit 09eb71a

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

src/mormot.commit.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
'2.0.4624'
1+
'2.0.4625'

src/rest/mormot.rest.http.server.pas

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ TRestHttpServer = class(TSynPersistent)
427427
// so that AJAX applications would be able to connect to this server
428428
// - this method raise an EHttpServer if the associated server class does not
429429
// support WebSockets, i.e. this instance isn't useBidirSocket/useBidirAsync
430+
// - warning: only a single WebSockets server could be used in TRestServer
431+
// callback at the same time to avoid confusion between the WS connections
430432
function WebSocketsEnable(
431433
const aWSURI, aWSEncryptionKey: RawUtf8; aWSAjax: boolean = false;
432434
aWSBinaryOptions: TWebSocketProtocolBinaryOptions = [pboSynLzCompress];
@@ -928,7 +930,7 @@ procedure TRestHttpServer.Shutdown(noRestServerShutdown: boolean);
928930
if not noRestServerShutdown then
929931
fDBServers[i].Server.Shutdown;
930932
if TMethod(fDBServers[i].Server.OnNotifyCallback).Data = self then
931-
// avoid unexpected GPF
933+
// avoid unexpected GPF, and proper TRestServer reuse
932934
fDBServers[i].Server.OnNotifyCallback := nil;
933935
end;
934936
finally

src/rest/mormot.rest.server.pas

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,7 @@ TRestServer = class(TRest)
17991799
fServer: IRestOrmServer;
18001800
fRouter: TRestRouter;
18011801
fRouterSafe: TRWLightLock;
1802+
fOnNotifyCallback: TOnRestServerClientCallback;
18021803
procedure SetNoAjaxJson(const Value: boolean);
18031804
function GetNoAjaxJson: boolean;
18041805
{$ifdef HASINLINE}inline;{$endif}
@@ -1817,6 +1818,7 @@ TRestServer = class(TRest)
18171818
function GetServiceMethodStat(const aMethod: RawUtf8): TSynMonitorInputOutput;
18181819
procedure SetRoutingClass(aServicesRouting: TRestServerUriContextClass);
18191820
procedure SetOptions(rso: TRestServerOptions);
1821+
procedure SetOnNotifyCallback(const event: TOnRestServerClientCallback);
18201822
/// add a new session to the internal session list
18211823
// - do not use this method directly: this callback is to be used by
18221824
// TRestServerAuthentication* classes
@@ -1868,10 +1870,6 @@ TRestServer = class(TRest)
18681870
// - Ctxt is nil if the session is closed due to a timeout
18691871
// - Ctxt is not nil if the session is closed explicitly by the client
18701872
OnSessionClosed: TOnOrmSession;
1871-
/// this event will be executed to push notifications from the server to
1872-
// a remote client, using a (fake) interface parameter
1873-
// - is nil by default, but may point e.g. to TRestHttpServer.NotifyCallback
1874-
OnNotifyCallback: TOnRestServerClientCallback;
18751873
/// this event will be executed by TServiceFactoryServer.CreateInstance
18761874
// - you may set a callback to customize a server-side service instance,
18771875
// i.e. inject class-level dependencies:
@@ -1925,6 +1923,12 @@ TRestServer = class(TRest)
19251923
// - is set by default to PAGINGPARAMETERS_YAHOO constant by
19261924
// TRestServer.Create() constructor
19271925
UriPagingParameters: TRestServerUriPagingParameters;
1926+
/// this event will be executed to push notifications from the WebSockets
1927+
// server to a remote client, using a (fake) interface parameter
1928+
// - is nil by default, but may point e.g. to TRestHttpServer.NotifyCallback
1929+
// - only a single WS server can be assigned to a TRestServer instance
1930+
property OnNotifyCallback: TOnRestServerClientCallback
1931+
read fOnNotifyCallback write SetOnNotifyCallback;
19281932

19291933
/// Server initialization with a specified Database Model
19301934
// - if HandleUserAuthentication is false, will set URI access rights to
@@ -6687,6 +6691,16 @@ function TRestServer.GetServiceMethodStat(
66876691
result := nil;
66886692
end;
66896693

6694+
procedure TRestServer.SetOnNotifyCallback(const event: TOnRestServerClientCallback);
6695+
begin
6696+
if Assigned(fOnNotifyCallback) and
6697+
Assigned(event) then
6698+
raise ERestException.CreateUtf8(
6699+
'%.OnNotifyCallback(%) set twice: only a single WS server can be assigned',
6700+
[self, ClassNameShort(TObject(TMethod(event).Data))^]);
6701+
fOnNotifyCallback := event;
6702+
end;
6703+
66906704
procedure TRestServer.SetRoutingClass(
66916705
aServicesRouting: TRestServerUriContextClass);
66926706
begin

0 commit comments

Comments
 (0)