The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,262 other subscribers

Indy Sockets and getting a description on the connection depends on the direction

Posted by jpluimers on 2018/05/29

From my Indy gitter archive:

@jpluimers
From a TIdIOHandlerSocket or TIdSocketHandle: is it possible to see who has initiated the connection? i.e. if it’s Binding.Peer that initiated to Binding.IP or the other way around?

@rlebeau
A socket is bidirectional, it doesn’t know or care which direction the connection was initially established. You will have to keep track of that yourself based on whether the socket is coming from a client component or a server component.

@jpluimers
I was afraid so. No problem: thanks for confirming.

So I made a helper class for TIdSocketHandle that gets you a SummaryString based on a direction enumeration: TIdSocketHandleHelper.

Notes:

–jeroen


unit IdSocketHandlerHelperUnit;
interface
uses
IdSocketHandle;
type
TConnectionDirection = (cdIncoming, cdOutgoing);
TConnectionDirectionStrings = array[TConnectionDirection] of string;
TIdSocketHandleHelper = class helper for TIdSocketHandle
function LocalAndPort: string; inline;
function LocalSummaryString: string; inline;
function PeerIpAndPort: string; inline;
function PeerSummaryString: string; inline;
function SummaryString(const aConnectionDirection: TConnectionDirection = cdIncoming): string; inline;
const
//1 Point of view: Peer cConnectionStrings[] Local
cConnectionDirectionStrings: TConnectionDirectionStrings = ('->', '<-');
end;
function IpColonPort(const aIp: string; const aPort: Integer): string; inline;
implementation
uses
System.SysUtils;
function IpColonPort(const aIp: string; const aPort: Integer): string; inline;
begin
Result := Format('%s:%d', [aIp, aPort]);
end;
function TIdSocketHandleHelper.LocalAndPort: string;
begin
Result := IpColonPort(IP, Port);
end;
function TIdSocketHandleHelper.LocalSummaryString: string;
begin
Result := Format('Local %s', [LocalAndPort]);
end;
function TIdSocketHandleHelper.PeerIpAndPort: string;
begin
Result := IpColonPort(PeerIP, PeerPort);
end;
function TIdSocketHandleHelper.PeerSummaryString: string;
begin
Result := Format('Peer %s', [PeerIpAndPort]);
end;
function TIdSocketHandleHelper.SummaryString(const aConnectionDirection: TConnectionDirection = cdIncoming): string;
begin
Result := Format('%s %s %s', [PeerSummaryString, cConnectionDirectionStrings[aConnectionDirection], LocalSummaryString]);
end;
end.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.