YOUSEFUL / DELPHI / COMPONENTS
TAtlasTcpListener
A listening TCP socket that accepts connections as TAtlasTcpTransport objects; the broker is built on it.
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
| Member | Kind | What it does |
|---|---|---|
Create | constructor | Binds and listens on APort (0 asks the system for a free port; read Port afterwards). |
Destroy | destructor | Closes the listening socket. |
Close | method | Stops listening. |
Accept | method | Waits up to TimeoutMs milliseconds for a connection and returns it as a connected TAtlasTcpTransport, which the caller owns and frees. |
Port | property | The 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.
Listener := TAtlasTcpListener.Create(0, True); // any free port, this machine onlyDestructors
Destroy destructor
destructor Destroy; override;Closes the listening socket. Connections already accepted are separate objects and stay open.
Listener.Free;Properties
Port property
property Port: Word read FPort;The port actually bound.
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.
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.
Transport := Listener.Accept(1000); // nil when nobody cameExamples
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.
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;Related
TAtlasTcpTransport · TAtlasMqttBroker