简体   繁体   中英

How do I close all Connections on my idHTTPServer?

Having idHTTPServer and want it to stop receiveing requests. What is the right way of doing this ?

I am not sure in my code below

function TRPSystem.CloseAllConnections: boolean;
var
  i: Integer;
  l: TList;
  c: TIdThreadSafeObjectList;

begin
  Result := false;
  c := Main.Server.Contexts;
  if c = nil then
    Exit();

  l := c.LockList();

  try
    for i := 0 to  l.Count - 1 do
      TIdContext(l.Items[i]).Connection.Disconnect(False);
    Result := true;
  finally
    c.UnlockList;
  end;
end;

procedure TRPSystem.ServerStop;
begin
  CloseAllConnections();
  Main.Server.Scheduler.ActiveYarns.Clear; 
  Main.Server.IsActive := false;
end;

To fully deactivate the server, you need to only set the server's Active property to False. That stops the server from listening for new connections AND closes down any currently active connections. There is no need to close client connections manually, or clear the active Yarns list. The Active setter handles everything for you:

procedure TRPSystem.ServerStop;
begin
  Main.Server.Active := false;
end;

Otherwise, if you do not want to fully deactivate the server, just take it offline temporarily, you can reject new HTTP requests as they arrive by using the OnHeadersAvailable and OnHeadersBlocked events. Return False from OnHeadersAvailable , and then return a suitable status code from OnHeadersBlocked , such as 503 Service Unavailable (the default is 403 Forbidden ), eg:

procedure TMain.ServerHeadersAvailable(AContext: TIdContext; const AUri: string; AHeaders: TIdHeaderList; var VContinueProcessing: Boolean);
begin
  if Offline then
    VContinueProcessing := False;
end;

procedure TMain.ServerHeadersBlocked(AContext: TIdContext; AHeaders: TIdHeaderList; var VResponseNo: Integer; var VResponseText, VContentText: String);
begin
  VResponseNo := 503;
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM