YOUSEFUL / DELPHI / COMPONENTS

TAtlasTcpListener

A listening TCP socket that accepts connections as TAtlasTcpTransport objects; the broker is built on it.

Class Unit Atlas.MqttTransport · Inherits TObject · Product Atlas MQTT

Description

TAtlasMqttBroker uses it to accept clients. Use it directly to write a scripted stand-in for a broker in a test: accept the client, then read and write packets byte by byte with TAtlasMqttCodec. IPv4 only. Errors raise EAtlasMqttTransportError.

Members

MemberKindWhat it does
CreateconstructorBinds and listens on APort (0 asks the system for a free port; read Port afterwards).
DestroydestructorCloses the listening socket.
ClosemethodStops listening.
AcceptmethodWaits up to TimeoutMs milliseconds for a connection and returns it as a connected TAtlasTcpTransport, which the caller owns and frees.
PortpropertyThe port actually bound.

Constructors

Create constructor

constructor Create(APort: Word; LoopbackOnly: Boolean);

Binds and listens on APort (0 asks the system for a free port; read Port afterwards). LoopbackOnly True binds 127.0.0.1, False every IPv4 interface. Raises EAtlasMqttTransportError when the port cannot be bound.

pascal
Listener := TAtlasTcpListener.Create(0, True);  // any free port, this machine only

Destructors

Destroy destructor

destructor Destroy; override;

Closes the listening socket. Connections already accepted are separate objects and stay open.

pascal
Listener.Free;

Properties

Port property

property Port: Word read FPort;

The port actually bound.

pascal
Client.Port := Listener.Port;

Methods

Close method

procedure Close;

Stops listening. Safe to call from another thread and safe to call twice. Accept then returns nil.

pascal
Listener.Close;

Accept method

function Accept(TimeoutMs: Integer): TAtlasTcpTransport;

Waits up to TimeoutMs milliseconds for a connection and returns it as a connected TAtlasTcpTransport, which the caller owns and frees. Returns nil when nobody connected in time or the listener is closed.

pascal
Transport := Listener.Accept(1000);  // nil when nobody came

Examples

A stand-in broker that refuses the next client

Answers a client's CONNECT with return code 5 ("Not authorised"), to test how an application handles a refusal. Create the listener, point a client at Listener.Port, call its Connect, then call this.

pascal
uses
  System.SysUtils, Atlas.MqttCodec, Atlas.MqttTransport;

function RefuseNextClient(Listener: TAtlasTcpListener): Boolean;
var
  Peer: TAtlasTcpTransport;
  Parser: TAtlasMqttParser;
  Packet: TMqttPacket;
  Buffer: TBytes;
  Count: Integer;
begin
  Peer := Listener.Accept(5000);
  Result := Peer <> nil;
  if not Result then
    Exit;
  Parser := TAtlasMqttParser.Create;
  try
    SetLength(Buffer, 1024);
    repeat
      Count := Peer.Receive(Buffer, 3000);
      if Count <= 0 then
        Exit(False);
      Parser.Feed(Buffer, Count);
    until Parser.Next(Packet);
    Result := Packet.Kind = mkConnect;
    Packet := TMqttPacket.Create(mkConnAck);
    Packet.ReturnCode := 5;
    Peer.Send(TAtlasMqttCodec.Encode(Packet));
  finally
    Parser.Free;
    Peer.Free;
  end;
end;

TAtlasTcpTransport · TAtlasMqttBroker

Also in Atlas MQTT

TAtlasMqttClient · TAtlasMqttBroker