Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measuring network traffic with Indy

I'm using a TIdTCPCmdServer to handle all communication with client applications. I would like my server to log all kind of stuff including network traffic. Is there any easy or clever way to see how much bytes the TCPCmdServer actually received and sent? I can only think of code like

ASender.SendReply;
Inc (FTraffic, Sizeof (ASender.NormalReply.Code) +
               Sizeof (Char) * ASender.NormalReply.Text.Length));

which is extremely ugly in my opinion because these traffic updates would be spreaded out all over my code and are fairly complicated as well.

Any suggestions?

Thanks for your help.

like image 778
jpfollenius Avatar asked Feb 18 '09 09:02

jpfollenius


3 Answers

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.

like image 103
mghie Avatar answered Nov 15 '22 03:11

mghie


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.

like image 45
zendar Avatar answered Nov 15 '22 04:11

zendar


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...

like image 34
jpfollenius Avatar answered Nov 15 '22 04:11

jpfollenius