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:
- [Archive.is] TIdTCPConnection.Socket Property is of type [Archive.is] TIdIOHandlerSocket Class
- [Archive.is] TIdIOHandlerSocket.Binding property is of type [Archive.is] TIdSocketHandle Class
–jeroen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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