Skip to content

Commit

Permalink
Birds Lock List
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusvicente100 committed Jul 6, 2020
1 parent 17fdd2c commit 21393c0
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 14 deletions.
96 changes: 88 additions & 8 deletions src/Bird.Socket.Connection.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

interface

uses IdContext, Bird.Socket.Consts, Bird.Socket.Helpers, System.JSON, System.Generics.Collections;
uses IdContext, Bird.Socket.Consts, Bird.Socket.Helpers, System.JSON, System.Generics.Collections, System.SysUtils;

type
TBirdSocketConnection = class
Expand All @@ -15,24 +15,41 @@ TBirdSocketConnection = class
function Id: Integer;
function CheckForDataOnSource(const ATimeOut: Integer): Boolean;
function Connected: Boolean;
function IsEquals(const AIdContext: TIdContext): Boolean;
procedure Send(const AMessage: string); overload;
procedure Send(const ACode: Integer; const AMessage: string); overload;
procedure Send(const ACode: Integer; const AMessage: string; const AValues: array of const); overload;
procedure Send(const AJSONObject: TJSONObject; const AOwns: Boolean = True); overload;
procedure SendFile(const AFile: string); overload;
end;

TBirds = TList<TBirdSocketConnection>;
TBirds = class
private
FListLocked: Boolean;
FItems: TList<TBirdSocketConnection>;
public
constructor Create;
function LockList: TList<TBirdSocketConnection>;
function Last: TBirdSocketConnection;
procedure Add(const ABird: TBirdSocketConnection);
procedure Remove(const ABird: TBirdSocketConnection);
procedure UnLockList;
destructor Destroy; override;
end;

implementation

function TBirdSocketConnection.CheckForDataOnSource(const ATimeOut: Integer): Boolean;
begin
if (not Assigned(FIdContext)) or (not Assigned(FIdContext.Connection)) or (not Assigned(FIdContext.Connection.IOHandler)) then
Exit(False);
Result := FIdContext.Connection.IOHandler.CheckForDataOnSource(ATimeOut);
end;

function TBirdSocketConnection.Connected: Boolean;
begin
if (not Assigned(FIdContext)) or (not Assigned(FIdContext.Connection)) then
Exit(False);
Result := FIdContext.Connection.Connected;
end;

Expand All @@ -41,45 +58,108 @@ constructor TBirdSocketConnection.Create(const AIdContext: TIdContext);
FIdContext := AIdContext;
end;

function TBirdSocketConnection.IsEquals(const AIdContext: TIdContext): Boolean;
begin
if (not Assigned(FIdContext)) then
Exit(False);
Result := (FIdContext = AIdContext);
end;

procedure TBirdSocketConnection.Send(const AMessage: string);
begin
FIdContext.Connection.IOHandler.Send(AMessage);
if Assigned(FIdContext) and Assigned(FIdContext.Connection) and Assigned(FIdContext.Connection.IOHandler) then
FIdContext.Connection.IOHandler.Send(AMessage);
end;

procedure TBirdSocketConnection.Send(const ACode: Integer; const AMessage: string);
begin
FIdContext.Connection.IOHandler.Send(ACode, AMessage);
if Assigned(FIdContext) and Assigned(FIdContext.Connection) and Assigned(FIdContext.Connection.IOHandler) then
FIdContext.Connection.IOHandler.Send(ACode, AMessage);
end;

procedure TBirdSocketConnection.Send(const ACode: Integer; const AMessage: string; const AValues: array of const);
begin
FIdContext.Connection.IOHandler.Send(ACode, AMessage, AValues);
if Assigned(FIdContext) and Assigned(FIdContext.Connection) and Assigned(FIdContext.Connection.IOHandler) then
FIdContext.Connection.IOHandler.Send(ACode, AMessage, AValues);
end;

function TBirdSocketConnection.ID: Integer;
begin
Result := FIdContext.Binding.ID;
Result := Integer(@FIdContext);
end;

function TBirdSocketConnection.IPAdress: string;
begin
if (not Assigned(FIdContext)) or (not Assigned(FIdContext.Connection)) or (not Assigned(FIdContext.Connection.Socket)) or
(not Assigned(FIdContext.Connection.Socket.Binding)) then
Exit(EmptyStr);
Result := FIdContext.Connection.Socket.Binding.PeerIP;
end;

procedure TBirdSocketConnection.Send(const AJSONObject: TJSONObject; const AOwns: Boolean);
begin
FIdContext.Connection.IOHandler.Send(AJSONObject, AOwns);
if Assigned(FIdContext) and Assigned(FIdContext.Connection) and Assigned(FIdContext.Connection.IOHandler) then
FIdContext.Connection.IOHandler.Send(AJSONObject, AOwns);
end;

procedure TBirdSocketConnection.SendFile(const AFile: string);
begin
FIdContext.Connection.IOHandler.SendFile(AFile);
if Assigned(FIdContext) and Assigned(FIdContext.Connection) and Assigned(FIdContext.Connection.IOHandler) then
FIdContext.Connection.IOHandler.SendFile(AFile);
end;

function TBirdSocketConnection.WaitMessage: string;
begin
if (not Assigned(FIdContext)) or (not Assigned(FIdContext.Connection)) or (not Assigned(FIdContext.Connection.IOHandler)) then
Exit(EmptyStr);
FIdContext.Connection.IOHandler.CheckForDataOnSource(TIMEOUT_DATA_ON_SOURCE);
Result := FIdContext.Connection.IOHandler.ReadString;
end;

procedure TBirds.Add(const ABird: TBirdSocketConnection);
begin
LockList.Add(ABird);
UnLockList;
end;

constructor TBirds.Create;
begin
FItems := TList<TBirdSocketConnection>.Create;
end;

destructor TBirds.Destroy;
begin
FItems.Free;
inherited;
end;

function TBirds.Last: TBirdSocketConnection;
begin
Result := LockList.Last;
UnLockList;
end;

function TBirds.LockList: TList<TBirdSocketConnection>;
begin
while FListLocked do
begin
Sleep(100);
Continue;
end;
FListLocked := True;
Result := FItems;
end;

procedure TBirds.Remove(const ABird: TBirdSocketConnection);
begin
LockList;
FItems.Remove(ABird);
UnLockList;
end;

procedure TBirds.UnLockList;
begin
FListLocked := False;
end;

end.
18 changes: 12 additions & 6 deletions src/Bird.Socket.Server.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
interface

uses IdCustomTCPServer, IdHashSHA, IdSSLOpenSSL, IdContext, IdSSL, IdIOHandler, IdGlobal, IdCoderMIME, System.SysUtils,
Bird.Socket.Helpers, Bird.Socket.Consts, Bird.Socket.Types, Bird.Socket.Connection;
Bird.Socket.Helpers, Bird.Socket.Consts, Bird.Socket.Types, Bird.Socket.Connection, System.Generics.Collections;

type
TBirdSocketServer = class(TIdCustomTCPServer)
Expand Down Expand Up @@ -141,13 +141,19 @@ procedure TBirdSocketServer.DoOnExecute(AContext: TIdContext);
function TBirdSocketServer.GetBirdFromContext(const AContext: TIdContext): TBirdSocketConnection;
var
LBird: TBirdSocketConnection;
LBirds: TList<TBirdSocketConnection>;
begin
for LBird in FBirds do
begin
if LBird.Id = AContext.Binding.ID then
Exit(LBird);
LBirds := FBirds.LockList;
try
for LBird in LBirds do
begin
if LBird.IsEquals(AContext) then
Exit(LBird);
end;
Result := TBirdSocketConnection.Create(AContext);
finally
FBirds.UnLockList
end;
Result := TBirdSocketConnection.Create(AContext);
end;

function TBirdSocketServer.GetEncodedHash(const ASecretKey: string): string;
Expand Down

0 comments on commit 21393c0

Please sign in to comment.