Skip to content

Commit

Permalink
enhanced TStreamRedirect remaining time accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud Bouchez committed Aug 11, 2021
1 parent 7cb6823 commit 261554b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/core/mormot.core.base.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3547,6 +3547,7 @@ TFakeWriterStream = class(TStream)
function Seek(Offset: Longint; Origin: Word): Longint; override;
end;

{$M+}
/// TStream with a protected fPosition field
TStreamWithPosition = class(TStream)
protected
Expand All @@ -3560,6 +3561,7 @@ TStreamWithPosition = class(TStream)
/// call the 64-bit Seek() overload
function Seek(Offset: Longint; Origin: Word): Longint; override;
end;
{$M-}

/// TStream using a RawByteString as internal storage
// - default TStringStream uses UTF-16 WideChars since Delphi 2009, so it is
Expand Down
18 changes: 13 additions & 5 deletions src/core/mormot.core.buffers.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1779,7 +1779,7 @@ TStreamRedirect = class(TStreamWithPosition)
fConsoleLen: byte;
fMode: (mUnknown, mRead, mWrite);
function GetSize: Int64; override;
procedure DoReport;
procedure DoReport(ReComputeElapsed: boolean);
procedure DoHash(data: pointer; len: integer); virtual; // do nothing
procedure SetExpectedSize(Value: Int64);
procedure ReadWriteHash(const Buffer; Count: Longint); virtual;
Expand Down Expand Up @@ -8290,6 +8290,12 @@ function TStreamRedirect.GetProgress: RawUtf8;
var
ctx: shortstring;
begin
if (self = nil) or
fTerminated then
begin
result := '';
exit;
end;
Ansi7StringToShortString(Context, ctx);
if ctx[0] > #30 then
begin
Expand Down Expand Up @@ -8340,8 +8346,10 @@ class procedure TStreamRedirect.ProgressToConsole(Sender: TStreamRedirect);
end;
{$I+}

procedure TStreamRedirect.DoReport;
procedure TStreamRedirect.DoReport(ReComputeElapsed: boolean);
begin
if ReComputeElapsed then
fElapsed := GetTickCount64 - fStartTix; // may have changed in-between
if (fCurrentSize <> fExpectedSize) and
(fElapsed < fReportTix) then
exit;
Expand Down Expand Up @@ -8450,7 +8458,7 @@ procedure TStreamRedirect.Ended;
fExpectedSize := fCurrentSize; // reached 100%
if Assigned(fOnProgress) or
Assigned(fOnLog) then
DoReport; // notify finished
DoReport(true); // notify finished
end;

procedure TStreamRedirect.Terminate;
Expand Down Expand Up @@ -8495,7 +8503,7 @@ procedure TStreamRedirect.ReadWriteReport(const Caller: ShortString);
SleepHiRes(300); // show progress on very low bandwidth
if Assigned(fOnProgress) or
Assigned(fOnLog) then
DoReport;
DoReport(true);
dec(tosleep, 300);
if fTerminated then
raise ESynException.CreateUtf8('%.%(%) Terminated',
Expand All @@ -8509,7 +8517,7 @@ procedure TStreamRedirect.ReadWriteReport(const Caller: ShortString);
end;
if Assigned(fOnProgress) or
Assigned(fOnLog) then
DoReport;
DoReport(false);
if fTerminated then
raise ESynException.CreateUtf8('%.%(%) Terminated',
[self, Caller, fContext]);
Expand Down

0 comments on commit 261554b

Please sign in to comment.