0
votes

Here's how the receiver will receive the stream.

 Ms := TMemoryStream.Create;

    try
      Ms.Position := 0;
      Connection.IOHandler.LargeStream := True;
      Connection.IOHandler.ReadStream(Ms, -1,false);
    finally
      Ms.Free;
    end; 

If the receiver wants to cancel it

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  try Ms.Free; except end; 
end;

This doesn't work well, the application sometimes closes.

Also for the sender...

    Ms := TMemoryStream.Create;

    try
      Ms.Position := 0;
      Connection.IOHandler.LargeStream := True;
      Connection.IOHandler.Write(Ms, 0, True);
    finally
      Ms.Free;
    end; 

The same with the sender in canceling the stream.

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  try Ms.Free; except end; 
end;

What is the proper way to cancel the stream that is being send or receive ?

1
No, that won't work well, pulling the rug from under Indy's feet. - David Heffernan

1 Answers

1
votes

Forcing a memory access error is a horrible way to abort a send/receive operation. There are three other ways I can think of to abort it:

  1. Raise an exception in the context of Indy's own send/receive logic. Either raise an exception in Indy's OnWork event, or derive a new TStream class that raises an exception in its Write() and Read() implementations.

  2. Break up the data into smaller chunks, each with its own send/receive call, then check for your termination condition in between each chunk.

  3. Close the socket.