gpt4 book ai didi

delphi - 使用Delphi和Indy 10.6,我找不到TIdServerContext的OnWork

转载 作者:行者123 更新时间:2023-12-02 14:07:04 25 4
gpt4 key购买 nike

在设置TCP Client / Server系统时,我将TIdServerContext子类化以添加索引标志,但是似乎没有OnWork事件。我需要重置一个计时器,比如说在上传/下载大文件时,很多Indy组件都有一个OnWork事件用于此操作,但我找不到用于TIdServerContextTIdTCPServer的事件,根据其他帖子,TIdTCPServer.OnStatus事件似乎未实现。

这是我实现TIdServerContext的方法:

TUserContext = Class(TIdServerContext)
Protected
FUserID: Integer;
Public
Property UserID: Integer Read FUserID Write FUserID Default 0;
End;

Procedure FormCreate(Sender: TObject);
Begin
Server.ContextClass := TUserContext;
End;

Procedure ServerExecute(AContext: TIdContext);
Var
I: Integer;
Begin
I := TUserContext(AContext).UserID;
...
End;

最佳答案

OnWork...事件在TIdComponent类中,而TIdServerContext并非此类来源。但是,TIdTCPConnectionTIdIOHandler可以。 TIdContext具有公共的Connection属性,因此在服务器的事件(OnConnectOnExecute等)中,可以根据需要将处理程序分配给TIdConnection.OnWork...事件:

procedure TMyForm.ServerConnect(AContext: TIdContext);
begin
AContext.Connection.Tag := NativeInt(AContext);
AContext.Connection.OnWork := WorkHandler;
end;

procedure TMyForm.WorkHandler(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
var
Ctx: TUserContext;
begin
Ctx := TUserContext(TIdTCPConnection(ASender).Tag);
// use Ctx members as needed...
end;


或者,另一种无需使用 AContext即可将 WorkHandler传递给 Tag的方法:

procedure TMyForm.ServerConnect(AContext: TIdContext);
var
Handler: TWorkEvent;
begin
Handler := WorkHandler;
TMethod(Handler).Data := AContext;
AContext.Connection.OnWork := Handler;
end;

procedure TMyForm.WorkHandler(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
var
Ctx: TUserContext;
begin
Ctx := TUserContext(Self);
// use Ctx members as needed...
end;


解决此问题的另一种方法是重写 TIdServerContext构造函数,该构造函数接收 TIdTCPConnection对象作为参数。您可以使处理程序成为 TUserContext类本身的方法,并让其构造函数立即分配 OnWork...事件,而不是等待服务器的 On(Connect|OnExecute)事件首先触发:

type
TUserContext = Class(TIdServerContext)
protected
FUserID: Integer;
procedure WorkHandler(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
public
constructor Create(AConnection: TIdTCPConnection; AYarn: TIdYarn; AList: TIdContextThreadList = nil); override;
property UserID: Integer read FUserID write FUserID;
end;

constructor TUserContext.Create(AConnection: TIdTCPConnection; AYarn: TIdYarn; AList: TIdContextThreadList = nil);
begin
inherited;
AConnection.OnWork := WorkHandler;
end;

procedure TUserContext.WorkHandler(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
begin
// use Self members as needed...
end;


或者,如果您只需要根据需要进行活动:

type
TUserContext = Class(TIdServerContext)
protected
FUserID: Integer;
procedure WorkHandler(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
public
procedure StartWorkTimer;
procedure StopWorkTimer;
property UserID: Integer read FUserID write FUserID;
end;

procedure TUserContext.StartWorkTimer;
begin
Connection.OnWork := WorkHandler;
end;

procedure TUserContext.StopWorkTimer;
begin
Connection.OnWork := nil;
end;

procedure TMyForm.ServerExecute(AContext: TIdContext);
var
Ctx: TUserContext;
begin
Ctx := TUserContext(AContext);

...

if (some condition) then
Ctx.StartWorkTimer;

...

if (some other condition) then
Ctx.StopWorkTimer;

...
end;

关于delphi - 使用Delphi和Indy 10.6,我找不到TIdServerContext的OnWork,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43902374/

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