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

📄 main.pas

📁 一个SNOOP控件,各位看看可以对网络包进行分析.
💻 PAS
字号:
unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Snoop, ExtCtrls, Global, Menus,
  SnoopTrace;

const
  MAX_MEMORY_CNT = 1500; // gilgil temp 2003.08.19
  GRID_SIZE = 16;

type
  TMainForm = class(TForm)
	imgStatistics: TImage;
	SnoopStatistics1: TSnoopStatistics;
	PopupMenu1: TPopupMenu;
	miOpenCapture: TMenuItem;
	miCloseCapture: TMenuItem;
	miOption: TMenuItem;
	procedure FormCreate(Sender: TObject);
	procedure FormShow(Sender: TObject);
	procedure FormClose(Sender: TObject; var Action: TCloseAction);
	procedure miOpenCaptureClick(Sender: TObject);
	procedure miCloseCaptureClick(Sender: TObject);
	procedure miOptionClick(Sender: TObject);
	procedure SnoopStatistics1CaptureStatistics(Sender: TObject;
	  PacketHeader: PPCAP_PKTHDR; Packets, Bytes: Int64);
    procedure FormResize(Sender: TObject);
    procedure imgStatisticsMouseMove(Sender: TObject; Shift: TShiftState;
      X, Y: Integer);
  private
	{ Private declarations }
	GridOffset: Integer;
	PacketsList: TList;
	BytesList: TList;
	function CalcY(Value: Integer; Max: Integer): Integer;
	procedure SetControl;
	procedure OpenCapture;
	procedure CloseCapture;
	procedure DrawStatistics;
  public
	{ Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

uses Option;

{$R *.DFM}

function TMainForm.CalcY(Value: Integer; Max: Integer): Integer;
begin
	Result := imgStatistics.Height -
		(imgStatistics.Height * Value div Max) * 1000 div _Global.ReadTimeOut;
end;

procedure TMainForm.SetControl;
var
	Active: Boolean;
begin
	Active := SnoopStatistics1.Active;
	miOpenCapture.Enabled := not Active;
	miCloseCapture.Enabled := Active;
	miOption.Enabled := not Active;
end;

procedure TMainForm.OpenCapture;
begin
	SnoopStatistics1.AdapterIndex := _Global.AdapterIndex;
	SnoopStatistics1.Filter := _Global.Filter;
	SnoopStatistics1.ReadTimeOut := _Global.ReadTimeOut;
	SnoopStatistics1.SnapLen := _Global.SnapLen;
	SnoopStatistics1.Open;
	if not SnoopStatistics1.Active then
		ShowMessage(SnoopStatistics1.Error);
	SetControl;
end;

procedure TMainForm.CloseCapture;
begin
	SnoopStatistics1.Close;
	SetControl;
end;

procedure TMainForm.DrawStatistics;
var
	i: Integer;
	Canvas: TCanvas;
	aRect: TRect;
	Width: Integer;
	Height: Integer;
	Index: Integer;
	x, y: Integer;
begin
	Width := imgStatistics.Width;
	Height := imgStatistics.Height;
	imgStatistics.Picture.Bitmap.Width := Width;
	imgStatistics.Picture.Bitmap.Height := Height;
	// Fill Black Backgroung
	Canvas := imgStatistics.Picture.Bitmap.Canvas;
	Canvas.Brush.Color := clBlack;
	aRect := Rect(0, 0, imgStatistics.Width, imgStatistics.Height);
	Canvas.FillRect(aRect);

	// Draw Grid
	Canvas.Pen.Color := clGreen;
	x := GridOffset;
	while x < Width do
	begin
		if x <> 0 then
		begin
			Canvas.MoveTo(x, 0);
			Canvas.LineTo(x, Height);
		end;
		inc (x, GRID_SIZE);
	end;
	y := GRID_SIZE;
	while y < Height do
	begin
		Canvas.MoveTo(0, y);
		Canvas.LineTo(Width, y);
		inc(y, GRID_SIZE);
	end;

	// Draw Bytes;
	Canvas.Pen.Color := clLime;
	Index := 0;
	for i := Width - 1 downto 0 do // gilgil temp 2003.08.19
	begin
		if Index >= BytesList.Count then break;
		y := CalcY(Integer(BytesList.Items[Index]), _Global.MaxBytes);
		if y < 0 then y := 0;
		if y >= Height then y := Height - 1;
		if Index = 0 then
			Canvas.MoveTo(i, y)
		else
			Canvas.LineTo(i, y);
		inc(Index);
	end;

end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
	GridOffset := 0;
	PacketsList := TList.Create;
	BytesList := TList.Create;

	imgStatistics.Align := alClient;
	Constraints.MaxWidth := MAX_MEMORY_CNT;
end;

procedure TMainForm.FormShow(Sender: TObject);
begin
	if _Global.AdapterIndex = -1 then // if invalid AdapterIndex
		OptionForm.ShowModal;

	_Global.Load;
	// Coordination
	Left := _Global.Left;
	Top := _Global.Top;
	Width := _Global.Width;
	Height := _Global.Height;

	DrawStatistics;
	miOpenCapture.Click;
end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
	// Coordination
	_Global.Left := Left;
	_Global.Top := Top;
	_Global.Width := Width;
	_Global.Height := Height;

	_Global.Save;	
end;

procedure TMainForm.miOpenCaptureClick(Sender: TObject);
begin
	OpenCapture;
	SetControl;
end;

procedure TMainForm.miCloseCaptureClick(Sender: TObject);
begin
	CloseCapture;
	SetControl;
end;

procedure TMainForm.miOptionClick(Sender: TObject);
begin
	OptionForm.ShowModal;
	DrawStatistics;
end;

procedure TMainForm.SnoopStatistics1CaptureStatistics(Sender: TObject;
  PacketHeader: PPCAP_PKTHDR; Packets, Bytes: Int64);
var
	_Packets: Integer;
	_Bytes: Integer;
begin
	_Packets := Integer(Packets);
	_Bytes := Integer(Bytes);
	PacketsList.Insert(0, Pointer(_Packets));
	while PacketsList.Count > MAX_MEMORY_CNT do
		PacketsList.Delete(PacketsList.Count - 1);
	BytesList.Insert(0, Pointer(_Bytes));
	while BytesList.Count > MAX_MEMORY_CNT do
		BytesList.Delete(BytesList.Count - 1);
	dec(GridOffset);
	if GridOffset < 0 then GridOffset := GRID_SIZE - 1;

	DrawStatistics;
end;

procedure TMainForm.FormResize(Sender: TObject);
begin
	DrawStatistics;
end;

procedure TMainForm.imgStatisticsMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
var
	Index: Integer;
	Packets: Integer;
	Bytes: Integer;
begin
	Index := - X + imgStatistics.Width - 1;
	if (Index >= 0) and (Index < PacketsList.Count) then
	begin
		Packets := Integer(PacketsList[Index]);
		Bytes := Integer(BytesList[Index]);
		Caption := Format('Statistics - Packet : %7d Bytes : %10d', [Packets, Bytes]);
	end else
		Caption := 'Statistics';
end;

end.

⌨️ 快捷键说明

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