⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 psicomponent.pas

📁 一个delphi的p2p控件的源代码
💻 PAS
字号:
unit PsiComponent;

//******************************************************************************
// The original software is under
// Copyright (c) 1993 - 2000, Chad Z. Hower (Kudzu)
//   and the Indy Pit Crew - http://www.nevrona.com/Indy/
//
// Amended : November 2000, by Michael M. Michalak MACS for use with
// MorphTek.com Inc Peer to Peer Open Source Components - http://www.morphtek.com
//
//******************************************************************************

interface

uses
  Classes,
  PsiAntiFreezeBase, PsiBaseComponent, PsiGlobal, PsiStack, PsiResourceStrings,
  SysUtils;

type
  TPsiStatus = ( hsResolving,
                hsConnecting,
                hsConnected,
                hsDisconnecting,
                hsDisconnected);
const
  PsiStati: array[TPsiStatus] of string = (
                RSStatusResolving,
                RSStatusConnecting,
                RSStatusConnected,
                RSStatusDisconnecting,
                RSStatusDisconnected);

type
  TPsiStatusEvent = procedure(axSender: TObject; const axStatus: TPsiStatus;
   const asStatusText: string) of object;

  TWorkMode = (wmRead, wmWrite);
  TWorkInfo = record
    Current: Integer;
    Max: Integer;
    Level: Integer;
  end;

  TWorkBeginEvent = procedure(Sender: TComponent; AWorkMode: TWorkMode;
   const AWorkCountMax: Integer) of object;
  TWorkEndEvent = procedure(Sender: TComponent; AWorkMode: TWorkMode) of object;
  TWorkEvent = procedure(Sender: TComponent; AWorkMode: TWorkMode; const AWorkCount: Integer)
   of object;

  TPsiComponent = class(TPsiBaseComponent)
  protected
    FOnStatus: TPsiStatusEvent;
    FOnWork: TWorkEvent;
    FOnWorkBegin: TWorkBeginEvent;
    FOnWorkEnd: TWorkEndEvent;
    FWorkInfos: array[TWorkMode] of TWorkInfo;
    //
    procedure DoStatus(axStatus: TPsiStatus); overload;
    procedure DoStatus(axStatus: TPsiStatus; const aaArgs: array of const); overload;
    // GetLocalName cannot be static/class method.
    // CBuilder doesnt handle it correctly for a prop accessor
    function GetLocalName: string;
    //
    property OnWork: TWorkEvent read FOnWork write FOnWork;
    property OnWorkBegin: TWorkBeginEvent read FOnWorkBegin write FOnWorkBegin;
    property OnWorkEnd: TWorkEndEvent read FOnWorkEnd write FOnWorkEnd;
  public
    procedure BeginWork(AWorkMode: TWorkMode; const ASize: Integer = 0); virtual;
  	constructor Create(axOwner: TComponent); override;
    destructor Destroy; override;
    procedure DoWork(AWorkMode: TWorkMode; const ACount: Integer); virtual;
    procedure EndWork(AWorkMode: TWorkMode); virtual;
    //
    property LocalName: string read GetLocalName;
  published
    property OnStatus: TPsiStatusEvent read FOnStatus write FOnStatus;
  end;

implementation

var
	gnInstanceCount: Integer = 0;

{ TPsiComponent }

constructor TPsiComponent.Create(axOwner: TComponent);
begin
  inherited;
  if GStack = nil then begin
    GStack := TPsiStack.CreateStack;
	end;
  Inc(gnInstanceCount);
end;

destructor TPsiComponent.Destroy;
begin
  Dec(gnInstanceCount);
  if gnInstanceCount = 0 then begin
		GStack.free;
    GStack := nil;
  end;
  inherited;
end;

procedure TPsiComponent.DoStatus(axStatus: TPsiStatus);
begin
	DoStatus(axStatus, []);
end;

procedure TPsiComponent.DoStatus(axStatus: TPsiStatus; const aaArgs: array of const);
begin
  if assigned(OnStatus) then begin
    OnStatus(Self, axStatus, Format(PsiStati[axStatus], aaArgs));
  end;
end;

function TPsiComponent.GetLocalName: string;
begin
  result := GStack.WSGetHostName;
end;

procedure TPsiComponent.BeginWork(AWorkMode: TWorkMode; const ASize: Integer = 0);
begin
  Inc(FWorkInfos[AWorkMode].Level);
  if FWorkInfos[AWorkMode].Level = 1 then begin
    FWorkInfos[AWorkMode].Max := ASize;
    FWorkInfos[AWorkMode].Current := 0;
    if assigned(OnWorkBegin) then begin
      OnWorkBegin(Self, AWorkMode, ASize);
    end;
  end;
end;

procedure TPsiComponent.DoWork(AWorkMode: TWorkMode; const ACount: Integer);
begin
  if FWorkInfos[AWorkMode].Level > 0 then begin
    Inc(FWorkInfos[AWorkMode].Current, ACount);
    if assigned(OnWork) then begin
      OnWork(Self, AWorkMode, FWorkInfos[AWorkMode].Current);
    end;
  end;
end;

procedure TPsiComponent.EndWork(AWorkMode: TWorkMode);
begin
  if FWorkInfos[AWorkMode].Level = 1 then begin
    if assigned(OnWorkEnd) then begin
      OnWorkEnd(Self, AWorkMode);
    end;
  end;
  Dec(FWorkInfos[AWorkMode].Level);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -