Skip to content

Remove Indy dependency and replace connectivity check with System.Net.HttpClient #8

@jimmckeeth

Description

@jimmckeeth

Overview

The training uses Indy (IdTCPClient) solely to check internet connectivity by opening a TCP connection to www.embarcadero.com:80. Indy is a legacy networking library; Delphi has included the modern System.Net.HttpClient stack since XE8. The Indy dependency should be removed and replaced with a native connectivity check.

Background

Current Pattern

uses IdComponent, IdTCPClient;

function IsConnected: Boolean;
var
  TCP: TIdTCPClient;
begin
  TCP := TIdTCPClient.Create(nil);
  try
    TCP.Host := 'www.embarcadero.com';
    TCP.Port := 80;
    TCP.Connect;
    Result := TCP.Connected;
  except
    Result := False;
  finally
    TCP.Free;
  end;
end;

This is wrapped in a TTask.Future<Boolean> for async execution.

Problems

  • Indy is installed with Delphi but is increasingly unmaintained and flagged by static analysis tools.
  • TCP port 80 connections are often blocked by corporate firewalls; HTTP/HTTPS is more reliable.
  • System.Net.HttpClient (already used elsewhere in the project for the Maps API) achieves the same goal with a HEAD request and no extra dependency.

Replacement Strategy

Use TNetHTTPClient with a HEAD request to a reliable endpoint (e.g., https://www.google.com or https://connectivitycheck.gstatic.com/generate_204). The async wrapper via TTask can remain unchanged.

Files Affected

lab-src/Lab*/forms/formMain.pas  (connectivity check call site)
lab-src/Lab*/units/  (wherever Indy units are referenced)
All FieldLogger.dproj files (remove Indy package references if explicit)

Steps to Address

  1. Locate all uses clauses referencing IdComponent, IdTCPClient, IdBaseComponent and remove them.
  2. Replace the connectivity check function body with:
    uses System.Net.HttpClient;
    
    function IsConnected: Boolean;
    var
      HTTP: THTTPClient;
      Resp: IHTTPResponse;
    begin
      HTTP := THTTPClient.Create;
      try
        try
          Resp := HTTP.Head('https://connectivitycheck.gstatic.com/generate_204');
          Result := Resp.StatusCode < 500;
        except
          Result := False;
        end;
      finally
        HTTP.Free;
      end;
    end;
  3. Wrap the call in TTask.Run (or keep the existing TTask.Future<Boolean> wrapper) for async execution.
  4. Verify no other Indy units (IdSSLOpenSSL, etc.) remain in the codebase.
  5. Remove explicit Indy package references from .dproj files if present.

Test Plan

  • Code compiles without any Id*.pas unit in the uses clause.
  • On Android emulator with Wi-Fi enabled: connectivity check returns True and the Maps API image loads.
  • On Android emulator with Wi-Fi disabled (airplane mode): connectivity check returns False and the app shows the "no internet" message gracefully.
  • Same two scenarios verified on iOS simulator.
  • No Indy-related packages appear in the project's package list.
  • Connectivity check completes within 5 seconds (no blocking the UI thread).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions