Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset connection #7

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions Source/AMQP.Connection.pas
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ THeartbeatThread = class;
procedure SetPort(const Value: Word);
function GetTimeOut: LongWord;
//Heartbeat handling
Procedure StartWriteHeartbeat;
Procedure CloseWriteHeartbeat;
Procedure HeartbeatReceived;
//Helpers
function ThreadRunning: Boolean;
Expand Down Expand Up @@ -323,7 +325,7 @@ procedure TAMQPConnection.Connect;
FServerProperties.ReadConnectionOpenOK( Frame.Payload.AsMethod );
FIsOpen := True;
FServerDisconnected := False;
FHeartbeatThread.Start;
StartWriteHeartbeat;
end;

constructor TAMQPConnection.Create;
Expand Down Expand Up @@ -355,7 +357,6 @@ constructor TAMQPConnection.Create;
FMaxFrameSize := 131072;
FMaxChannel := 1;
FServerDisconnected := False;
FHeartbeatThread := THeartbeatThread.Create(Self);
end;

function TAMQPConnection.DefaultMessageProperties: IAMQPMessageProperties;
Expand All @@ -376,7 +377,7 @@ destructor TAMQPConnection.Destroy;
FTCP.Free;
FSendLock.Free;
FDebugLock.Free;
FHeartbeatThread.Free;
CloseWriteHeartbeat;
inherited;
End;
end;
Expand Down Expand Up @@ -427,6 +428,15 @@ procedure TAMQPConnection.CloseConnection;
End;
end;

procedure TAMQPConnection.CloseWriteHeartbeat;
begin
if Assigned(FHeartbeatThread) then begin
FHeartbeatThread.Terminate;
FHeartbeatThread.WaitFor;
FreeAndNil(FHeartbeatThread);
end;
end;

procedure TAMQPConnection.Disconnect;
begin
if not IsOpen then
Expand Down Expand Up @@ -539,7 +549,7 @@ procedure TAMQPConnection.HeartbeatReceived;

procedure TAMQPConnection.InternalDisconnect(ACloseConnection: Boolean);
begin
FHeartbeatThread.Terminate;
CloseWriteHeartbeat;
Try
CloseAllChannels;
if ACloseConnection then
Expand Down Expand Up @@ -585,6 +595,9 @@ function TAMQPConnection.MakeChannel: IAMQPChannel;
Begin
Channels := FChannels.LockList;
Try
if Channels.Count = FMaxChannel then
raise AMQPException.Create('Exceeded maximum channel number');

Result := TAMQPChannel.Create( Self, GetNewChannelID( Channels ) );
Channels.Add( Result );
Finally
Expand Down Expand Up @@ -683,6 +696,14 @@ procedure TAMQPConnection.SetTimeout(const Value: LongWord);
FTimeout := Value;
end;

procedure TAMQPConnection.StartWriteHeartbeat;
begin
if not Assigned(FHeartbeatThread) then begin
FHeartbeatThread := THeartbeatThread.Create(Self);
FHeartbeatThread.Start;
end;
end;

function TAMQPConnection.ThreadRunning: Boolean;
begin
Result := Assigned(FThread) and not FThread.Terminated;
Expand Down
Loading