QUIC Client and Server Components for Delphi and C++ Builder | eSeGeCe Blog

QUIC Client and Server Components for Delphi and C++ Builder

· Components
sgcQUIC QUIC client and server components

sgcQUIC brings the QUIC transport protocol (RFC 9000) to Delphi, C++ Builder and .NET. It ships two new components, TsgcQUICClient and TsgcQUICServer, both built on the native QUIC engine in OpenSSL 3.5, so the transport and the TLS 1.3 handshake run as a single flight.

QUIC is the protocol underneath HTTP/3, but it is useful on its own. It runs over UDP, multiplexes many independent streams on one connection, resumes with 0-RTT, and survives a change of network thanks to connection migration. This post covers the raw QUIC layer. The next post covers the HTTP/3 components that sit on top.

Why QUIC

A classic TLS-over-TCP connection needs a TCP handshake and then a TLS handshake before the first byte of application data travels. QUIC folds both into one exchange, so a fresh connection is ready in a single round trip, and a resumed one can send data with 0-RTT.

Because each QUIC stream is delivered on its own, a lost packet on one stream does not stall the others. That removes the head-of-line blocking you get when you multiplex over a single TCP socket. A QUIC connection is identified by a connection ID rather than by the IP and port pair, so it can migrate from Wi-Fi to mobile data without dropping.

The QUIC Client

Set Host and Port, assign the events, then set Active to True. The handshake completes in one flight, and WriteData sends bytes on a QUIC stream.

uses
  sgcQUIC_Client;

var
  oClient: TsgcQUICClient;
begin
  oClient := TsgcQUICClient.Create(nil);
  oClient.Host := 'www.example.com';
  oClient.Port := 443;
  oClient.OnQUICConnect    := OnQUICConnect;
  oClient.OnQUICStreamData := OnQUICStreamData;
  oClient.OnQUICDisconnect := OnQUICDisconnect;

  oClient.Active := True;          // TLS 1.3 handshake in a single flight
  oClient.WriteData('ping');       // send bytes on a QUIC stream
end;

Inbound bytes arrive in the OnQUICStreamData event, which reports the stream id and the payload:

procedure TForm1.OnQUICStreamData(Sender: TObject; aStreamId: Int64;
  aUnidirectional: Boolean; const aData: TBytes);
begin
  Memo1.Lines.Add('stream ' + IntToStr(aStreamId) + ': ' +
    TEncoding.UTF8.GetString(aData));
end;

The QUIC Server

The server listens on a UDP port and needs a certificate and private key in PEM format. QUIC always runs over TLS 1.3, so the certificate is mandatory. Client bytes arrive in OnQUICData, and you reply with WriteData, passing the connection you want to answer.

uses
  sgcQUIC_Server;

var
  oServer: TsgcQUICServer;
begin
  oServer := TsgcQUICServer.Create(nil);
  oServer.Port := 443;
  oServer.QUICOptions.SSLOptions.CertFile := 'cert.pem';
  oServer.QUICOptions.SSLOptions.KeyFile  := 'key.pem';
  oServer.OnQUICConnect := OnQUICConnect;
  oServer.OnQUICData    := OnQUICData;
  oServer.Active := True;
end;

procedure TForm1.OnQUICData(Sender: TObject;
  aConnection: TsgcQUICServerConnection; const aData: TBytes);
begin
  // echo the message back to the client that sent it
  oServer.WriteData(aConnection, aData);
end;

Multiplexed Streams

A single QUIC connection can carry many streams at once. Call EnableMultiStream before you connect, then open as many streams as you need. A bidirectional stream carries a request and its reply; a unidirectional stream pushes data one way.

oClient.EnableMultiStream;
oClient.OpenStream;                 // a new bidirectional stream
oClient.OpenStream(True);           // a unidirectional stream

Multi-stream mode uses the builtin OpenSSL 3.5 QUIC engine. Each stream is flow-controlled on its own, so a slow or stalled stream never blocks the rest.

0-RTT and Connection Migration

After a first successful handshake, the client can keep the session ticket and reconnect with 0-RTT, sending application data in the very first packet. Connection migration keeps a session alive across a network change, which matters for mobile clients that move between Wi-Fi and cellular. Both are handled by the underlying QUIC engine.

Availability

sgcQUIC is a separate package, licensed as an add-on to sgcWebSockets Enterprise. Every license includes full Pascal source code and one year of updates. The components install on the SGC QUIC palette and support Delphi and C++ Builder 7 through 13, plus a .NET port with the same Tsgc class names.

The HTTP/3 client and server components are covered in the next post. You can download the trial and read the full component reference on the sgcQUIC product page.

Questions or feedback? Get in touch, you will get a reply from the people who wrote the code.