TsgcWebTransportStream › Properties › Session
The WebTransport session that owns this stream.
property Session: TsgcWebTransportSession read FSession;
Set by the constructor to the session that created the stream. It never changes and is never nil for a stream the library handed you.
Read-only. A back-reference to the owning session, useful from inside a stream event handler, where the session is often not otherwise in scope. From OnData or OnClose, Sender is the stream, and Session gets you from there to the session, its SessionId, its StreamCount, or OpenStream to answer on a fresh stream.
Ownership runs the other way. The session owns the stream, not the reverse. The session's stream list frees every stream when the session is destroyed, so never free a stream through this reference, and never keep a stream pointer alive past its session. Reading Session does not extend anything's lifetime.
All streams in a session share one QUIC connection, so Session is also the way to check that two streams belong together: compare the references, or compare Session.SessionId.
procedure TfrmServer.OnWTStreamData(Sender: TObject; const aData: TBytes);
var
oStream: TsgcWebTransportStream;
oSession: TsgcWebTransportSession;
begin
if not (Sender is TsgcWebTransportStream) then
Exit;
oStream := TsgcWebTransportStream(Sender);
// The session is not in scope here, so reach it through the stream
oSession := oStream.Session;
memoLog.Lines.Add(Format('session %d, stream %d, %d bytes',
[oSession.SessionId, oStream.StreamId, Length(aData)]));
// Answer on a NEW unidirectional stream in the same session, rather than
// on the stream the request arrived on
with oSession.OpenStream(True) do
WriteData('ack');
end;