Measuring network traffic with Indy

六眼飞鱼酱① 提交于 2019-12-04 07:35:22

Look into the intercept classes of Indy. You should be able to create a custom intercept class that overrides the Receive() and Send() methods, and in addition to calling the methods of the base class implements your traffic calculation. There are already intercept classes to do logging, you would connect your custom class in the same way.

The documentation of TIdConnectionIntercept should be a good starting point. There is also a very simple example here on how to create and connect an intercept at runtime.

Wrap TCPCmdServer into class that logs traffic.

You can derive your class from TCPCmdServer and override send and receive methods if they are virtual.

Something like:

type
  TTcpCmdServerWithLogging = class(TTcpCmdServer)
    ...
    procedure SendReply; override;

implementation
    procedure SendReply;
    begin
      inherited SendReply;
      Inc (FTraffic, Sizeof (NormalReply.Code) +
           Sizeof (Char) * NormalReply.Text.Length)); 
    end;

If they aren't virtual, then create new class that instantiates TCPCmdServer and expose required methods and properties.

jpfollenius

Thank you both very much for your answers. I chose to implement it the way mghie described it - by implementing a custom interceptor class for my connections. Just for those interested in the solution, i will provide some source code here:

type
  TCountTrafficInterceptor = class (TIdConnectionIntercept)
  public
    type TIntPointer = ^Longint;
  private
    FTraffic : TIntPointer;
  public
    constructor Create (TrafficVar : TIntPointer);
    procedure Send (var ABuffer : TIdBytes); override;
    procedure Receive (var ABuffer : TIdBytes); override;
  end;

constructor TCountTrafficInterceptor.Create (TrafficVar : TIntPointer);
begin
  FTraffic := TrafficVar;
end;

procedure TCountTrafficInterceptor.Send (var ABuffer : TIdBytes);
begin
  inherited Send (ABuffer);
  FTraffic^ := FTraffic^ + Length (ABuffer);
end;

procedure TCountTrafficInterceptor.Receive (var ABuffer : TIdBytes);
begin
  inherited Receive (ABuffer);
  FTraffic^ := FTraffic^ + Length (ABuffer);
end;

And in the OnConnect method of the TIdTCPCmdServer:

AContext.Connection.IOHandler.Intercept := 
  TCountTrafficInterceptor.Create (@FNetworkTraffic);

Works great, just the kind of solution I was looking for. Thanks again for your answers.

Btw: How can I use the (at) sign in my posts? I always get a block quote when I try to type it...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!