-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from cayque10/automatic-reconnection
Automatic reconnection
- Loading branch information
Showing
8 changed files
with
499 additions
and
2,325 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
unit Bird.Socket.Client.ConnectionMonitor.Interfaces; | ||
|
||
interface | ||
|
||
type | ||
|
||
/// <summary> | ||
/// Interface representing the connection monitor parameters. | ||
/// </summary> | ||
IBirdSocketClientConnectionMonitorParams = interface | ||
['{CDEF540C-E21C-46B6-8551-894CF00661ED}'] | ||
/// <summary> | ||
/// Sets the active status of the connection monitor. | ||
/// </summary> | ||
/// <param name="AValue">Boolean value indicating if the monitor should be active.</param> | ||
/// <returns>The updated connection monitor parameters interface.</returns> | ||
function Active(const AValue: Boolean): IBirdSocketClientConnectionMonitorParams; | ||
|
||
/// <summary> | ||
/// Sets the interval for the connection check. | ||
/// </summary> | ||
/// <param name="AValue">Interval value in milliseconds.</param> | ||
/// <returns>The updated connection monitor parameters interface.</returns> | ||
function Interval(const AValue: Integer): IBirdSocketClientConnectionMonitorParams; | ||
end; | ||
|
||
/// <summary> | ||
/// Interface representing the connection monitor. | ||
/// </summary> | ||
IBirdSocketClientConnectionMonitor = interface | ||
['{933093C3-20AB-4A31-8DA0-31F60EC57189}'] | ||
/// <summary> | ||
/// Sets up auto-reconnect parameters for the connection monitor. | ||
/// </summary> | ||
/// <returns>The connection monitor parameters interface.</returns> | ||
function AutoReconnect: IBirdSocketClientConnectionMonitorParams; | ||
end; | ||
|
||
implementation | ||
|
||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
{ | ||
Class to monitor the connection status of a Bird Socket Client and attempt reconnection if the connection is lost. | ||
} | ||
|
||
unit Bird.Socket.Client.ConnectionMonitor; | ||
|
||
interface | ||
|
||
uses | ||
System.Classes, | ||
System.SysUtils; | ||
|
||
type | ||
TBirdSocketClientConnectionMonitor = class(TThread) | ||
private | ||
/// <summary> | ||
/// Interval between connection status checks, in milliseconds. | ||
/// </summary> | ||
FInterval: Integer; | ||
/// <summary> | ||
/// Function to check the current connection status. Should return True if connected, False otherwise. | ||
/// </summary> | ||
FOnStatusCurrentConnection: TFunc<Boolean>; | ||
/// <summary> | ||
/// Procedure to attempt reconnection if the connection is lost. | ||
/// </summary> | ||
FOnReconnect: TProc; | ||
/// <summary> | ||
/// Indicates if the monitor is currently trying to reconnect. | ||
/// </summary> | ||
FTryingConnect: Boolean; | ||
protected | ||
/// <summary> | ||
/// Main execution method for the thread. Periodically checks the connection status and attempts reconnection if necessary. | ||
/// </summary> | ||
procedure Execute; override; | ||
public | ||
/// <summary> | ||
/// Creates an instance of the connection monitor thread. | ||
/// </summary> | ||
/// <param name="ACreatedSuspended">Indicates if the thread should be created in a suspended state.</param> | ||
/// <param name="AInterval">Interval between connection status checks, in milliseconds.</param> | ||
/// <param name="AOnStatusCurrentConnection">Function to check the current connection status.</param> | ||
/// <param name="AOnReconnect">Procedure to attempt reconnection if the connection is lost.</param> | ||
constructor Create(const ACreatedSuspended: Boolean; const AInterval: Integer; | ||
const AOnStatusCurrentConnection: TFunc<Boolean>; const AOnReconnect: TProc); | ||
end; | ||
|
||
implementation | ||
|
||
uses | ||
System.DateUtils; | ||
|
||
{ TBirdSocketClientConnectionMonitor } | ||
|
||
constructor TBirdSocketClientConnectionMonitor.Create(const ACreatedSuspended: Boolean; const AInterval: Integer; | ||
const AOnStatusCurrentConnection: TFunc<Boolean>; const AOnReconnect: TProc); | ||
begin | ||
inherited Create(ACreatedSuspended); | ||
FInterval := AInterval; | ||
FOnStatusCurrentConnection := AOnStatusCurrentConnection; | ||
FOnReconnect := AOnReconnect; | ||
FTryingConnect := False; | ||
FreeOnTerminate := False; | ||
end; | ||
|
||
procedure TBirdSocketClientConnectionMonitor.Execute; | ||
var | ||
LStatusConnection: Boolean; | ||
lStart: TDateTime; | ||
begin | ||
|
||
while not terminated do | ||
begin | ||
lStart := Now; | ||
|
||
while (not terminated) and (MilliSecondsBetween(Now, lStart) < FInterval) do | ||
Sleep(100); | ||
|
||
if not Assigned(FOnStatusCurrentConnection) then | ||
raise Exception.Create('The connection status check event has not been set!'); | ||
|
||
if not Assigned(FOnReconnect) then | ||
raise Exception.Create('The reconnect event has not been set!'); | ||
|
||
LStatusConnection := FOnStatusCurrentConnection; | ||
|
||
if not terminated and not FTryingConnect and not LStatusConnection then | ||
begin | ||
FTryingConnect := True; | ||
try | ||
TThread.Synchronize(TThread.Current, | ||
procedure | ||
begin | ||
FOnReconnect; | ||
end); | ||
finally | ||
FTryingConnect := False; | ||
end; | ||
end; | ||
|
||
end; | ||
|
||
end; | ||
|
||
end. |
Oops, something went wrong.