Skip to content
This repository has been archived by the owner on Jan 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #3 from monde-sistemas/async
Browse files Browse the repository at this point in the history
Changes TMonitor for a TCriticalSection because of the RSP-10197 bug
  • Loading branch information
RobertoSchneiders committed Sep 30, 2015
2 parents c7b1962 + 2a7c023 commit cf14def
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions delphi/PusherServer.pas
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ interface
System.Generics.Collections,
System.Threading,
System.Classes,
SysUtils;
SysUtils,
System.SyncObjs;

type
TOptions = set of (UseSSL);
Expand All @@ -24,10 +25,12 @@ TPusherServer = class
TOnErrorEvent = reference to procedure(Error: Exception);
TAsyncPusherServer = class(TPusherServer)
private
FTaskList: TArray<ITask>;
FOnError: TOnErrorEvent;
FLock: TObject;
FLock: TCriticalSection;
function WithLock(Proc: TProc): TProc;
procedure WithErrorHandling(Proc: TProc);
procedure AddToTaskList(Task: ITask);
public
property OnError: TOnErrorEvent read FOnError write FOnError;
constructor Create(AppID, AppKey, AppSecret: string; CustomHost: string = '';
Expand All @@ -53,37 +56,43 @@ procedure TPusherServer.Trigger(Channel, EventName, Message: string);

{ TAsyncPusherServer }

procedure TAsyncPusherServer.AddToTaskList(Task: ITask);
begin
SetLength(FTaskList, Length(FTaskList) +1);
FTaskList[High(FTaskList)] := Task;
end;

constructor TAsyncPusherServer.Create(AppID, AppKey, AppSecret,
CustomHost: string; Options: TOptions);
begin
inherited;
FLock := TObject.Create;
FLock := TCriticalSection.Create;
end;

destructor TAsyncPusherServer.Destroy;
begin
TMonitor.Wait(FLock, INFINITE);
TTask.WaitForAll(FTaskList);
FLock.Free;
inherited;
end;

procedure TAsyncPusherServer.Trigger(Channel, EventName, Message: string);
begin
TTask.Run(WithLock(procedure
AddToTaskList(TTask.Run(WithLock(procedure
begin
inherited;
end));
end)));
end;

function TAsyncPusherServer.WithLock(Proc: TProc): TProc;
begin
Result := procedure
begin
FLock.Acquire;
try
TMonitor.Enter(FLock);
WithErrorHandling(Proc);
finally
TMonitor.Exit(FLock);
FLock.Release;
end;
end;
end;
Expand Down

0 comments on commit cf14def

Please sign in to comment.