Skip to content

Commit

Permalink
Refactor ConnectionInfo in C# (#3187)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardnormier authored Nov 26, 2024
1 parent 750f781 commit d9f2ba4
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 98 deletions.
130 changes: 115 additions & 15 deletions csharp/src/Ice/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,48 +138,148 @@ public class ConnectionInfo
/// <summary>
/// The underlying connection information.
/// </summary>
public ConnectionInfo? underlying;
public readonly ConnectionInfo? underlying;

/// <summary>
/// Whether the connection is an incoming connection (<c>true</c>) or an outgoing connection (<c>false</c>).
/// </summary>
public bool incoming;
public readonly bool incoming;

/// <summary>
/// The name of the adapter associated with the connection.
/// </summary>
public string adapterName = "";
public readonly string adapterName;

/// <summary>
/// The connection id.
/// </summary>
public string connectionId = "";
public readonly string connectionId;

protected ConnectionInfo(ConnectionInfo underlying)
{
this.underlying = underlying;
incoming = underlying.incoming;
adapterName = underlying.adapterName;
connectionId = underlying.connectionId;
}

protected ConnectionInfo(bool incoming, string adapterName, string connectionId)
{
this.incoming = incoming;
this.adapterName = adapterName;
this.connectionId = connectionId;
}
}

public class IPConnectionInfo : ConnectionInfo
{
public string localAddress = "";
public int localPort = -1;
public string remoteAddress = "";
public int remotePort = -1;
public readonly string localAddress;
public readonly int localPort;
public readonly string remoteAddress;
public readonly int remotePort;

protected IPConnectionInfo(
bool incoming,
string adapterName,
string connectionId,
string localAddress,
int localPort,
string remoteAddress,
int remotePort)
: base(incoming, adapterName, connectionId)
{
this.localAddress = localAddress;
this.localPort = localPort;
this.remoteAddress = remoteAddress;
this.remotePort = remotePort;
}
}

public sealed class TCPConnectionInfo : IPConnectionInfo
{
public int rcvSize;
public int sndSize;
public readonly int rcvSize;
public readonly int sndSize;

internal TCPConnectionInfo(
bool incoming,
string adapterName,
string connectionId,
string localAddress,
int localPort,
string remoteAddress,
int remotePort,
int rcvSize,
int sndSize)
: base(incoming, adapterName, connectionId, localAddress, localPort, remoteAddress, remotePort)
{
this.rcvSize = rcvSize;
this.sndSize = sndSize;
}

internal TCPConnectionInfo(bool incoming, string adapterName, string connectionId)
: this(
incoming,
adapterName,
connectionId,
localAddress: "",
localPort: -1,
remoteAddress: "",
remotePort: -1,
rcvSize: 0,
sndSize: 0)
{
}
}

public sealed class UDPConnectionInfo : IPConnectionInfo
{
public string mcastAddress = "";
public int mcastPort = -1;
public int rcvSize;
public int sndSize;
public readonly string mcastAddress;
public readonly int mcastPort;
public readonly int rcvSize;
public readonly int sndSize;

internal UDPConnectionInfo(
bool incoming,
string adapterName,
string connectionId,
string localAddress,
int localPort,
string remoteAddress,
int remotePort,
string mcastAddress,
int mcastPort,
int rcvSize,
int sndSize)
: base(incoming, adapterName, connectionId, localAddress, localPort, remoteAddress, remotePort)
{
this.mcastAddress = mcastAddress;
this.mcastPort = mcastPort;
this.rcvSize = rcvSize;
this.sndSize = sndSize;
}

internal UDPConnectionInfo(bool incoming, string adapterName, string connectionId)
: this(
incoming,
adapterName,
connectionId,
localAddress: "",
localPort: -1,
remoteAddress: "",
remotePort: -1,
mcastAddress: "",
mcastPort: -1,
rcvSize: 0,
sndSize: 0)
{
}
}

public sealed class WSConnectionInfo : ConnectionInfo
{
public Dictionary<string, string> headers = [];
public readonly Dictionary<string, string> headers;

internal WSConnectionInfo(ConnectionInfo underlying, Dictionary<string, string> headers)
: base(underlying) => this.headers = headers;
}
}
17 changes: 3 additions & 14 deletions csharp/src/Ice/ConnectionI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2660,25 +2660,14 @@ private void closeTimedOut(System.Threading.Timer closeTimer)

private ConnectionInfo initConnectionInfo()
{
// Called with _mutex locked.

if (_state > StateNotInitialized && _info is not null) // Update the connection info until it's initialized
{
return _info;
}

try
{
_info = _transceiver.getInfo();
}
catch (LocalException)
{
_info = new ConnectionInfo();
}
for (ConnectionInfo info = _info; info is not null; info = info.underlying)
{
info.connectionId = _endpoint.connectionId();
info.adapterName = _adapter?.getName() ?? "";
info.incoming = _connector is null;
}
_info = _transceiver.getInfo(incoming: _connector is null, _adapter?.getName() ?? "", _endpoint.connectionId());
return _info;
}

Expand Down
3 changes: 2 additions & 1 deletion csharp/src/Ice/Internal/IdleTimeoutTransceiverDecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public void finishRead(Buffer buf)
// We call write after finishWrite, so no need to do anything here.
public void finishWrite(Buffer buf) => _decoratee.finishWrite(buf);

public ConnectionInfo getInfo() => _decoratee.getInfo();
public ConnectionInfo getInfo(bool incoming, string adapterName, string connectionId) =>
_decoratee.getInfo(incoming, adapterName, connectionId);

public int initialize(Buffer readBuffer, Buffer writeBuffer, ref bool hasMoreData) =>
_decoratee.initialize(readBuffer, writeBuffer, ref hasMoreData);
Expand Down
27 changes: 17 additions & 10 deletions csharp/src/Ice/Internal/TcpTransceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,28 @@ public string protocol()
return _instance.protocol();
}

public Ice.ConnectionInfo getInfo()
public ConnectionInfo getInfo(bool incoming, string adapterName, string connectionId)
{
Ice.TCPConnectionInfo info = new Ice.TCPConnectionInfo();
if (_stream.fd() != null)
if (_stream.fd() is null)
{
return new TCPConnectionInfo(incoming, adapterName, connectionId);
}
else
{
EndPoint localEndpoint = Network.getLocalAddress(_stream.fd());
info.localAddress = Network.endpointAddressToString(localEndpoint);
info.localPort = Network.endpointPort(localEndpoint);
EndPoint remoteEndpoint = Network.getRemoteAddress(_stream.fd());
info.remoteAddress = Network.endpointAddressToString(remoteEndpoint);
info.remotePort = Network.endpointPort(remoteEndpoint);
info.rcvSize = Network.getRecvBufferSize(_stream.fd());
info.sndSize = Network.getSendBufferSize(_stream.fd());

return new TCPConnectionInfo(
incoming,
adapterName,
connectionId,
Network.endpointAddressToString(localEndpoint),
Network.endpointPort(localEndpoint),
Network.endpointAddressToString(remoteEndpoint),
Network.endpointPort(remoteEndpoint),
Network.getRecvBufferSize(_stream.fd()),
Network.getSendBufferSize(_stream.fd()));
}
return info;
}

public void checkSendSize(Buffer buf)
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/Ice/Internal/Transceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public interface Transceiver

string toDetailedString();

Ice.ConnectionInfo getInfo();
ConnectionInfo getInfo(bool incoming, string adapterName, string connectionId);

void checkSendSize(Buffer buf);

Expand Down
60 changes: 34 additions & 26 deletions csharp/src/Ice/Internal/UdpTransceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,41 +544,49 @@ public string protocol()
return _instance.protocol();
}

public Ice.ConnectionInfo getInfo()
public ConnectionInfo getInfo(bool incoming, string adapterName, string connectionId)
{
Ice.UDPConnectionInfo info = new Ice.UDPConnectionInfo();
if (_fd != null)
if (_fd is null)
{
return new UDPConnectionInfo(incoming, adapterName, connectionId);
}
else
{
EndPoint localEndpoint = Network.getLocalAddress(_fd);
info.localAddress = Network.endpointAddressToString(localEndpoint);
info.localPort = Network.endpointPort(localEndpoint);
if (_state == StateNotConnected)
{
if (_peerAddr != null)
{
info.remoteAddress = Network.endpointAddressToString(_peerAddr);
info.remotePort = Network.endpointPort(_peerAddr);
}

if (_state == StateNotConnected) // TODO: eliminate all these states
{
return new UDPConnectionInfo(
incoming,
adapterName,
connectionId,
Network.endpointAddressToString(localEndpoint),
Network.endpointPort(localEndpoint),
_peerAddr is not null ? Network.endpointAddressToString(_peerAddr) : "",
_peerAddr is not null ? Network.endpointPort(_peerAddr) : -1,
_mcastAddr is not null ? Network.endpointAddressToString(_mcastAddr) : "",
_mcastAddr is not null ? Network.endpointPort(_mcastAddr) : -1,
_rcvSize,
_sndSize);
}
else
{
EndPoint remoteEndpoint = Network.getRemoteAddress(_fd);
if (remoteEndpoint != null)
{
info.remoteAddress = Network.endpointAddressToString(remoteEndpoint);
info.remotePort = Network.endpointPort(remoteEndpoint);
}
}
info.rcvSize = Network.getRecvBufferSize(_fd);
info.sndSize = Network.getSendBufferSize(_fd);
}

if (_mcastAddr != null)
{
info.mcastAddress = Network.endpointAddressToString(_mcastAddr);
info.mcastPort = Network.endpointPort(_mcastAddr);
return new UDPConnectionInfo(
incoming,
adapterName,
connectionId,
Network.endpointAddressToString(localEndpoint),
Network.endpointPort(localEndpoint),
remoteEndpoint is not null ? Network.endpointAddressToString(remoteEndpoint) : "",
remoteEndpoint is not null ? Network.endpointPort(remoteEndpoint) : -1,
_mcastAddr is not null ? Network.endpointAddressToString(_mcastAddr) : "",
_mcastAddr is not null ? Network.endpointPort(_mcastAddr) : -1,
_rcvSize,
_sndSize);
}
}
return info;
}

public void checkSendSize(Buffer buf)
Expand Down
9 changes: 2 additions & 7 deletions csharp/src/Ice/Internal/WSTransceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -655,13 +655,8 @@ public string protocol()
return _instance.protocol();
}

public Ice.ConnectionInfo getInfo()
{
Ice.WSConnectionInfo info = new Ice.WSConnectionInfo();
info.headers = _parser.getHeaders();
info.underlying = _delegate.getInfo();
return info;
}
public ConnectionInfo getInfo(bool incoming, string adapterName, string connectionId) =>
new WSConnectionInfo(_delegate.getInfo(incoming, adapterName, connectionId), _parser.getHeaders());

public void checkSendSize(Buffer buf)
{
Expand Down
14 changes: 11 additions & 3 deletions csharp/src/Ice/SSL/ConnectionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ namespace Ice.SSL;

public sealed class ConnectionInfo : Ice.ConnectionInfo
{
public string cipher = "";
public X509Certificate2[] certs = [];
public bool verified;
public readonly string cipher;
public readonly X509Certificate2[] certs;
public readonly bool verified;

internal ConnectionInfo(Ice.ConnectionInfo underlying, string cipher, X509Certificate2[] certs, bool verified)
: base(underlying)
{
this.cipher = cipher;
this.certs = certs;
this.verified = verified;
}
}
Loading

0 comments on commit d9f2ba4

Please sign in to comment.