gpt4 book ai didi

delphi - 如何使用 IndyFtpServer (v10) 限制检索文件的速度

转载 作者:行者123 更新时间:2023-12-03 14:57:29 25 4
gpt4 key购买 nike

我正在使用 Delphi XE 6 和 Indy10 开发 FTP 服务器。问题是我需要限制下载速度(必须可配置,例如 1 KB/s、1 MB/s 等),但我无法使其工作。我知道一些像 BitsPerSec 等属性,但这只会影响协议(protocol)数据交换,而不影响使用 RETR 命令的文件交换。我在 IdFTPServer.pass 中看到,流被转换为字符串并以重复/直到循环(使用 IOHandler.Write())发送,但我需要某种形式的控制上传/下载过程并能够限制速度所有传入的客户端连接。请提供一些帮助来实现这一目标?

PD:抱歉我的英语不好。

我尝试使用以下代码为 RETR 命令实现 CommandHandler:

procedure TForm1.IdFTPServer1CommandHandlers0Command(ASender: TIdCommand);
var
LContext : TIdFTPServerContext;
vStream: TFileStream;
path: String;
vX: Integer;
LEncoding: IIdTextEncoding;
begin

LEncoding := IndyTextEncoding_8Bit;
LContext := ASender.Context as TIdFTPServerContext;
path := 'D:\indy_in_depth.pdf';


try
vStream := TFileStream.Create(path, fmOpenRead);
//LContext.DataChannel.FtpOperation := ftpRetr;
//LContext.DataChannel.Data := VStream;
LContext.DataChannel.OKReply.SetReply(226, RSFTPDataConnClosed);
LContext.DataChannel.ErrorReply.SetReply(426, RSFTPDataConnClosedAbnormally);

ASender.Reply.SetReply(150, RSFTPDataConnToOpen);
ASender.SendReply;

Memo1.Lines.Add('Sending a file with: ' + IntToStr(vStream.Size) + ' bytes');
//Make control of speed here !
for vX := 1 to vStream.Size do
begin
vStream.Position := vX;
LContext.Connection.IOHandler.Write(vStream, 1, False);
if vX mod 10000 = 0 then
Memo1.Lines.Add('Sended byte: ' + IntToStr(vX));
end;

//LContext.DataChannel.InitOperation(False);

//LContext.Connection.IOHandler.Write(vStream);

//LContext.KillDataChannel;
LContext.Connection.Socket.Close;

VStream.Free;
except
on E: Exception do
begin
ASender.Reply.SetReply(550, E.Message);
end;
end;

end;

但是 Filezilla FTP 客户端会像其他命令的一部分一样显示流数据。

Filezilla Client

最佳答案

The problem is that i need to limit the speed of download (must be configurable Ex. 1 KB/s, 1 MB/s, etc.) and i don't make it work. I know some props like BitsPerSec, etc. but this only affect the protocol data exchange, not the file exchange with RETR command.

BitsPerSecTIdInterceptThrottler 类的属性(以及 RecvBitsPerSecSendBitsPerSec 属性)。该类的实例可以通过其 Intercept 属性分配给任何 TIdTCPConnection 对象。这不仅限于命令连接,它也可以用于传输连接。

您可以通过TIdFTPServerContext.DataChannel.FDataChannel成员访问TIdFTPServer中文件传输的TIdTCPConnection对象,例如在OnRetrieveFile事件:

type
// the TIdDataChannel.FDataChannel member is 'protected',
// so use an accessor class to reach it...
TIdDataChannelAccess = class(TIdDataChannel)
end;

procedure TForm1.IdFTPServer1RetrieveFile(ASender: TIdFTPServerContext;
const AFileName: TIdFTPFileName; var VStream: TStream);
var
Conn: TIdTCPConnection;
begin
Conn := TIdDataChannelAccess(ASender.DataChannel).FDataChannel;
if Conn.Intercept = nil then
Conn.Intercept := TIdInterceptThrottler.Create(Conn);
TIdInterceptThrottler(Conn.Intercept).BitsPerSec := ...;
VStream := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyWrite);
end;

另一种方法是创建您自己的 T(File)Stream 派生类来重写虚拟 Read()Write() 方法根据需要执行您自己的限制,然后将该类的实例分配给 OnRetrieveFileOnStoreFile 事件的 VStream 参数。 p>

I see in IdFTPServer.pas, and the Stream is converted to string and send with a repeat/until loop (with IOHandler.Write())

不,流不会转换为字符串。您误读了代码。在对 IOHandler.Write(TStream) 方法的单次调用中,流的内容按原样作为二进制数据发送。

but i need some form of control the upload/download process and be able to limit the speed for all incoming client connections.

见上文。

I try to implement a CommandHandler for the RETR command

您不应该直接处理RETR命令。TIdFTPServer 已经为您做到了这一点。您应该使用 OnRetrieveFile 事件来准备下载,并使用 OnStoreFile 事件来准备上传。

关于delphi - 如何使用 IndyFtpServer (v10) 限制检索文件的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47028108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com