2017-02-24 55 views
0

在設置TCPServer和FTPServer時,我最注意的事情是在IdContext中需要一個UserID字段和一個UserFlag字段。這些簡單的添加將極大地方便爲多個客戶端設置組件。你可以創建一個後代,但是對於那些很容易添加到源代碼中的東西需要很多不必要的編碼。我修改IdContext.pas如下:Indy 10.6 IdContext需要一個ID字段

Protected 
    FUserFlag: Boolean; 
    FUserID: Integer; 
... 
Public 
    Property UserFlag: Boolean Read FUserFlag Write FUserFlag Default False; 
    Property UserID: Integer Read FUserID Write FUserID Default -1; 

通過使用這些我能信號事件之間的狀態,我有參考一應俱全每當一個事件被觸發。我試圖在Indy項目中說些什麼,但是我找不到任何地方說:/

+0

錯誤和功能請求可以提交給[Indy的問題跟蹤器](http://indy.codeplex.com),或在Embarcadero互聯網/套接字論壇中,或者私下提交給我。但是這個功能並不是我想要實現的功能。 –

回答

0

謝謝您的建議,但我不傾向於在基礎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類,該類具有登錄的會話的AccountUsername屬性。

話雖這麼說,如果你不希望派生自定義類,基TIdContext類也已經有公開的Data屬性(或DataObjectDataValue性能在基於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.DataTMyData需要的時候。

+0

謝謝雷米,很棒的建議:) – user7475089