谢谢您的建议,但我不倾向于在基础TIdContext
类别中添加这些类型的补充。他们根本不属于那里。在这种情况下,派生自定义类并将其分配给服务器的ContextClass
属性是正确和合适的解决方案。这就是为什么该财产首先存在。这实在是不算多的编码,如:
type
TMyContext = class(TIdServerContext)
protected
FUserFlag: Boolean;
FUserID: Integer;
...
public
Property UserFlag: Boolean Read FUserFlag Write FUserFlag;
Property UserID: Integer Read FUserID Write FUserID;
end;
procedure TMyForm.FormCreate(Sender: TObject);
begin
// must do this before activating the server...
IdTCPServer1.ContextClass := TMyContext;
end;
,然后在需要的时候可以类型转换TIdContext
对象指针TMyContext
。
各种Indy服务器在内部完成此操作。例如,TIdFTPServer
使用TIdFTPServerContext
类,该类具有登录的会话的Account
和Username
属性。
话虽这么说,如果你不希望派生自定义类,基TIdContext
类也已经有公开的Data
属性(或DataObject
和DataValue
性能在基于ARC的Delphi编译器),可用于存储用户定义的数据,例如:
type
TMyData = class
protected
FUserFlag: Boolean;
FUserID: Integer;
...
public
Property UserFlag: Boolean Read FUserFlag Write FUserFlag;
Property UserID: Integer Read FUserID Write FUserID;
end;
procedure TMyForm.IdTCPServer1Connect(AContext: TIdContext);
begin
AContext.Data := TMyData.Create;
...
end;
然后你就可以简单的类型转换AContext.Data
到TMyData
需要的时候。
错误和功能请求可以提交给[Indy的问题跟踪器](http://indy.codeplex.com),或在Embarcadero互联网/套接字论坛中,或者私下提交给我。但是这个功能并不是我想要实现的功能。 –