Skip to content

Commit

Permalink
Refactor EndpointInfo in C# (#3185)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardnormier authored Nov 26, 2024
1 parent 77599a1 commit 750f781
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 245 deletions.
154 changes: 115 additions & 39 deletions csharp/src/Ice/Endpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,94 +19,170 @@ public interface Endpoint
/// <summary>
/// Base class providing access to the endpoint details.
/// </summary>
public abstract class EndpointInfo
public class EndpointInfo
{
/// <summary>
/// The information of the underlying endpoint or null if there's no underlying endpoint.
/// Gets the information for the underlying endpoint, or null if there's no underlying endpoint.
/// </summary>
public EndpointInfo? underlying;
public readonly EndpointInfo? underlying;

/// <summary>
/// The timeout for the endpoint in milliseconds. -1 means no timeout.
/// Gets the timeout of the endpoint in milliseconds. -1 means no timeout.
/// </summary>
public int timeout;
public readonly int timeout;

/// <summary>
/// Specifies whether or not compression should be used if available when using this endpoint.
/// Gets a value indicating whether or not compression should be used if available when using this endpoint.
/// </summary>
public bool compress;
public readonly bool compress;

/// <summary>Returns the type of the endpoint.</summary>
/// <summary>Gets a 16-bit integer that identifies the transport of this endpoint.</summary>
/// <returns>The endpoint type.</returns>
public abstract short type();

/// <summary>Returns true if this endpoint is a datagram endpoint.</summary>
/// <returns>True for a datagram endpoint.</returns>
public abstract bool datagram();

/// <summary>Returns true if this endpoint is secure; otherwise false.</summary>
/// <returns>True if the endpoint is secure.</returns>
public abstract bool secure();
/// <remarks>The type of an underlying endpoint is always the same as the type its enclosing endpoint.</remarks>
public virtual short type() => underlying?.type() ?? -1;

/// <summary>Checks if this endpoint's transport is a datagram transport such as UDP.</summary>
/// <returns>True for a datagram endpoint; otherwise, false.</returns>
public virtual bool datagram() => underlying?.datagram() ?? false;

/// <summary>Checks if this endpoint's transport is secure.</summary>
/// <returns>True if the endpoint's transport is secure; otherwise, false.</returns>
/// <remarks>The value returned for an underlying endpoint is the same as the value returned for the enclosing
/// endpoint.</remarks>
public virtual bool secure() => underlying?.secure() ?? false;

protected EndpointInfo(EndpointInfo underlying)
{
this.underlying = underlying;
timeout = underlying.timeout;
compress = underlying.compress;
}

protected EndpointInfo(int timeout, bool compress)
{
this.timeout = timeout;
this.compress = compress;
}
}

/// <summary>
/// Provides access to the address details of a IP endpoint.
/// Provides access to the details of an IP endpoint.
/// </summary>
public abstract class IPEndpointInfo : EndpointInfo
public class IPEndpointInfo : EndpointInfo
{
/// <summary>
/// The host or address configured with the endpoint.
/// Gets the host or address configured with the endpoint.
/// </summary>
public string host = "";
public readonly string host;

/// <summary>
/// The endpoint's port number.
/// Gets the endpoint's port number.
/// </summary>
public int port;
public readonly int port;

/// <summary>
/// The source IP address.
/// Gets the source IP address.
/// </summary>
public string sourceAddress = "";
public readonly string sourceAddress;

protected IPEndpointInfo(int timeout, bool compress, string host, int port, string sourceAddress)
: base(timeout, compress)
{
this.host = host;
this.port = port;
this.sourceAddress = sourceAddress;
}
}

public abstract class TCPEndpointInfo : IPEndpointInfo
public sealed class TCPEndpointInfo : IPEndpointInfo
{
private readonly bool _secure;
private readonly short _type;

public override short type() => _type;

public override bool secure() => _secure;

internal TCPEndpointInfo(
int timeout,
bool compress,
string host,
int port,
string sourceAddress,
short type,
bool secure)
: base(timeout, compress, host, port, sourceAddress)
{
_type = type;
_secure = secure;
}
}

/// <summary>
/// Provides access to a TCP endpoint information.
/// Provides access to a UDP endpoint information.
/// </summary>
public abstract class UDPEndpointInfo : IPEndpointInfo
public sealed class UDPEndpointInfo : IPEndpointInfo
{
public string mcastInterface = "";

public int mcastTtl;
public readonly string mcastInterface;

public readonly int mcastTtl;

public override short type() => UDPEndpointType.value;

public override bool datagram() => true;

internal UDPEndpointInfo(
bool compress,
string host,
int port,
string sourceAddress,
string mcastInterface,
int mcastTtl)
: base(timeout: -1, compress, host, port, sourceAddress)
{
this.mcastInterface = mcastInterface;
this.mcastTtl = mcastTtl;
}
}

/// <summary>
/// Provides access to a WebSocket endpoint information.
/// </summary>
public abstract class WSEndpointInfo : EndpointInfo
public sealed class WSEndpointInfo : EndpointInfo
{
/// <summary>
/// The URI configured with the endpoint.
/// Gets the URI configured for this endpoint.
/// </summary>
public string resource = "";
public readonly string resource;

internal WSEndpointInfo(EndpointInfo underlying, string resource)
: base(underlying) => this.resource = resource;
}

/// <summary>
/// Provides access to the details of an opaque endpoint.
/// </summary>
public abstract class OpaqueEndpointInfo : EndpointInfo
public sealed class OpaqueEndpointInfo : EndpointInfo
{
/// <summary>
/// The encoding version of the opaque endpoint (to decode or encode the rawBytes).
/// Gets the raw encoding (to decode the rawBytes).
/// </summary>
public EncodingVersion rawEncoding;
public readonly EncodingVersion rawEncoding;

/// <summary>
/// The raw encoding of the opaque endpoint.
/// Gets the raw bytes of the opaque endpoint.
/// </summary>
public byte[] rawBytes = [];
public readonly byte[] rawBytes;

private readonly short _type;

public override short type() => _type;

internal OpaqueEndpointInfo(short type, EncodingVersion rawEncoding, byte[] rawBytes)
: base(compress: false, timeout: -1)
{
_type = type;
this.rawEncoding = rawEncoding;
this.rawBytes = rawBytes;
}
}
39 changes: 0 additions & 39 deletions csharp/src/Ice/Internal/IPEndpointI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,6 @@ protected IPEndpointI(ProtocolInstance instance, Ice.InputStream s)
connectionId_ = "";
}

private sealed class InfoI : Ice.IPEndpointInfo
{
public InfoI(IPEndpointI e)
{
_endpoint = e;
}

public override short type()
{
return _endpoint.type();
}

public override bool datagram()
{
return _endpoint.datagram();
}

public override bool secure()
{
return _endpoint.secure();
}

private IPEndpointI _endpoint;
}

public override Ice.EndpointInfo getInfo()
{
InfoI info = new InfoI(this);
fillEndpointInfo(info);
return info;
}

public override short type()
{
return instance_.type();
Expand Down Expand Up @@ -270,13 +238,6 @@ public override void streamWriteImpl(Ice.OutputStream s)
s.writeInt(port_);
}

public virtual void fillEndpointInfo(Ice.IPEndpointInfo info)
{
info.host = host_;
info.port = port_;
info.sourceAddress = Network.endpointAddressToString(sourceAddr_);
}

public virtual void initWithOptions(List<string> args, bool oaEndpoint)
{
initWithOptions(args);
Expand Down
32 changes: 1 addition & 31 deletions csharp/src/Ice/Internal/OpaqueEndpointI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,40 +59,10 @@ public override string ToString()
return "opaque -t " + _type + " -e " + Ice.Util.encodingVersionToString(_rawEncoding) + " -v " + val;
}

private sealed class InfoI : OpaqueEndpointInfo
{
public InfoI(short type, EncodingVersion rawEncoding, byte[] rawBytes)
{
_type = type;
this.rawEncoding = rawEncoding;
this.rawBytes = rawBytes;
}

public override short type()
{
return _type;
}

public override bool datagram()
{
return false;
}

public override bool secure()
{
return false;
}

private readonly short _type;
}

//
// Return the endpoint information.
//
public override Ice.EndpointInfo getInfo()
{
return new InfoI(_type, _rawEncoding, _rawBytes);
}
public override EndpointInfo getInfo() => new OpaqueEndpointInfo(_type, _rawEncoding, _rawBytes);

//
// Return the endpoint type
Expand Down
47 changes: 9 additions & 38 deletions csharp/src/Ice/Internal/TcpEndpointI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,44 +39,22 @@ public TcpEndpointI(ProtocolInstance instance, Ice.InputStream s)
_compress = s.readBool();
}

private sealed class InfoI : Ice.TCPEndpointInfo
{
public InfoI(IPEndpointI e)
{
_endpoint = e;
}

public override short type()
{
return _endpoint.type();
}

public override bool datagram()
{
return _endpoint.datagram();
}

public override bool secure()
{
return _endpoint.secure();
}

private IPEndpointI _endpoint;
}

public override void streamWriteImpl(Ice.OutputStream s)
{
base.streamWriteImpl(s);
s.writeInt(_timeout);
s.writeBool(_compress);
}

public override Ice.EndpointInfo getInfo()
{
InfoI info = new InfoI(this);
fillEndpointInfo(info);
return info;
}
public override EndpointInfo getInfo() =>
new TCPEndpointInfo(
_timeout,
_compress,
host_,
port_,
Network.endpointAddressToString(sourceAddr_),
type(),
secure());

public override int timeout()
{
Expand Down Expand Up @@ -205,13 +183,6 @@ public override int CompareTo(EndpointI obj)

public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), _timeout, _compress);

public override void fillEndpointInfo(Ice.IPEndpointInfo info)
{
base.fillEndpointInfo(info);
info.timeout = _timeout;
info.compress = _compress;
}

public override EndpointI toPublishedEndpoint(string publishedHost)
{
// A server endpoint can't have a source address or connection ID.
Expand Down
Loading

0 comments on commit 750f781

Please sign in to comment.