TsgcWebTransportStream › Methods › Close

Close Method

Unregisters the stream from its session and raises OnClose, once. A local signal only: the peer is not told.

Syntax

procedure Close;

Remarks

Close marks the stream closed, unregisters it from its session by calling Session.RemoveStream, and then raises OnClose. The order matters: the session already reflects the new StreamCount while your OnClose handler runs, and FindStreamById no longer returns the stream, so no inbound data can be routed to it once it is closed.

It is worth being explicit about what it does not do, because the name promises more:

It is guarded against repeat calls. The closed flag is checked first, so calling Close twice raises OnClose exactly once, and TsgcWebTransportSession.Close is guarded the same way. This holds on Delphi, C++Builder and .NET alike, and on every stack Close unregisters the stream from its session before raising the event.

Use it as your own end-of-stream marker and hang cleanup off OnClose. Because nothing is sent, a peer that needs to know a stream is finished must be told by your protocol: send a sentinel message, or agree that a given exchange ends the stream. Nothing arrives at the far end when you call this.

The stream remains fully valid after Close returns, and stays alive until its session is destroyed, so any reference you kept to it is safe to read. There is no need to call Session.RemoveStream afterwards, Close has already done it. Closed streams are only released with the session, so if you open them continuously over a long-lived session, prefer fewer and longer-lived streams.

On .NET the managed class additionally exposes a public IsClosed property, set by Close, which lets you test whether a stream has been closed. Delphi and C++Builder keep the same flag privately and expose no equivalent.

Example


procedure TfrmClient.FinishExchange;
begin
  if not Assigned(FStream) then
    Exit;

  // The peer is NOT told, so if it needs to know, our protocol must say so
  FStream.WriteData('BYE');

  FStream.Close;   // raises OnClose once; safe to call again
  FStream := nil;  // owned by the session, so do not free it
end;

procedure TfrmClient.OnStreamClose(Sender: TObject);
var
  oStream: TsgcWebTransportStream;
begin
  // The stream is still valid here, so its state can be read
  oStream := TsgcWebTransportStream(Sender);
  memoLog.Lines.Add('stream ' + IntToStr(oStream.StreamId) + ' closed');
end;

Back to Methods