< Summary

Information
Class: Ice.Internal.Protocol
Assembly: Ice
File(s): /_/csharp/src/Ice/Internal/Protocol.cs
Tag: 91_21789722663
Line coverage
90%
Covered lines: 57
Uncovered lines: 6
Coverable lines: 63
Total lines: 179
Line coverage: 90.4%
Branch coverage
62%
Covered branches: 10
Total branches: 16
Branch coverage: 62.5%
Method coverage
90%
Covered methods: 9
Total methods: 10
Method coverage: 90%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
stringToProtocolVersion(...)100%11100%
stringToEncodingVersion(...)100%11100%
protocolVersionToString(...)100%11100%
encodingVersionToString(...)100%11100%
.cctor()100%11100%
checkSupportedEncoding(...)100%44100%
isSupported(...)50%22100%
stringToMajorMinor(...)50%11.91073.33%
majorMinorToString(...)100%11100%
.ctor()100%210%

File(s)

/_/csharp/src/Ice/Internal/Protocol.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using System.Globalization;
 4
 5namespace Ice.Internal;
 6
 7public sealed class Protocol
 8{
 9    /// <summary>
 10    /// Converts a string to a protocol version.
 11    /// </summary>
 12    /// <param name="version">The string to convert.</param>
 13    /// <returns>The converted protocol version.</returns>
 14    internal static ProtocolVersion stringToProtocolVersion(string version)
 15    {
 116        stringToMajorMinor(version, out byte major, out byte minor);
 117        return new ProtocolVersion(major, minor);
 18    }
 19
 20    /// <summary>
 21    /// Converts a string to an encoding version.
 22    /// </summary>
 23    /// <param name="version">The string to convert.</param>
 24    /// <returns>The converted encoding version.</returns>
 25    internal static EncodingVersion stringToEncodingVersion(string version)
 26    {
 127        stringToMajorMinor(version, out byte major, out byte minor);
 128        return new EncodingVersion(major, minor);
 29    }
 30
 31    /// <summary>
 32    /// Converts a protocol version to a string.
 33    /// </summary>
 34    /// <param name="v">The protocol version to convert.</param>
 35    /// <returns>The converted string.</returns>
 136    internal static string protocolVersionToString(Ice.ProtocolVersion v) => majorMinorToString(v.major, v.minor);
 37
 38    /// <summary>
 39    /// Converts an encoding version to a string.
 40    /// </summary>
 41    /// <param name="v">The encoding version to convert.</param>
 42    /// <returns>The converted string.</returns>
 143    internal static string encodingVersionToString(Ice.EncodingVersion v) => majorMinorToString(v.major, v.minor);
 44
 145    internal static readonly ProtocolVersion Protocol_1_0 = new(1, 0);
 46
 147    internal static readonly ProtocolVersion currentProtocol = new(protocolMajor, protocolMinor);
 48
 149    internal static readonly EncodingVersion currentProtocolEncoding =
 150        new(protocolEncodingMajor, protocolEncodingMinor);
 51
 52    //
 53    // Size of the Ice protocol header
 54    //
 55    // Magic number (4 bytes)
 56    // Protocol version major (Byte)
 57    // Protocol version minor (Byte)
 58    // Encoding version major (Byte)
 59    // Encoding version minor (Byte)
 60    // Message type (Byte)
 61    // Compression status (Byte)
 62    // Message size (Int)
 63    //
 64    internal const int headerSize = 14;
 65
 66    //
 67    // The magic number at the front of each message
 68    //
 169    internal static readonly byte[] magic = [0x49, 0x63, 0x65, 0x50]; // 'I', 'c', 'e', 'P'
 70
 71    //
 72    // The current Ice protocol and Slice encoding version
 73    //
 74    internal const byte protocolMajor = 1;
 75    internal const byte protocolMinor = 0;
 76    internal const byte protocolEncodingMajor = 1;
 77    internal const byte protocolEncodingMinor = 0;
 78
 79    public const byte OPTIONAL_END_MARKER = 0xFF;
 80
 81    public const byte FLAG_HAS_TYPE_ID_STRING = 1 << 0;
 82    public const byte FLAG_HAS_TYPE_ID_INDEX = 1 << 1;
 83    public const byte FLAG_HAS_TYPE_ID_COMPACT = (1 << 1) | (1 << 0);
 84    public const byte FLAG_HAS_OPTIONAL_MEMBERS = 1 << 2;
 85    public const byte FLAG_HAS_INDIRECTION_TABLE = 1 << 3;
 86    public const byte FLAG_HAS_SLICE_SIZE = 1 << 4;
 87    public const byte FLAG_IS_LAST_SLICE = 1 << 5;
 88
 89    //
 90    // The Ice protocol message types
 91    //
 92    internal const byte requestMsg = 0;
 93    internal const byte requestBatchMsg = 1;
 94    internal const byte replyMsg = 2;
 95    internal const byte validateConnectionMsg = 3;
 96    internal const byte closeConnectionMsg = 4;
 97
 198    internal static readonly byte[] requestHdr = new byte[]
 199    {
 1100        magic[0], magic[1], magic[2], magic[3],
 1101        protocolMajor, protocolMinor,
 1102        protocolEncodingMajor, protocolEncodingMinor,
 1103        requestMsg,
 1104        0, // Compression status.
 1105        0, 0, 0, 0, // Message size (placeholder).
 1106        0, 0, 0, 0  // Request ID (placeholder).
 1107    };
 108
 1109    internal static readonly byte[] requestBatchHdr = new byte[]
 1110    {
 1111        magic[0], magic[1], magic[2], magic[3],
 1112        protocolMajor, protocolMinor,
 1113        protocolEncodingMajor, protocolEncodingMinor,
 1114        requestBatchMsg,
 1115        0, // Compression status.
 1116        0, 0, 0, 0, // Message size (placeholder).
 1117        0, 0, 0, 0  // Number of requests in batch (placeholder).
 1118    };
 119
 1120    internal static readonly byte[] replyHdr = new byte[]
 1121    {
 1122        magic[0], magic[1], magic[2], magic[3],
 1123        protocolMajor, protocolMinor,
 1124        protocolEncodingMajor, protocolEncodingMinor,
 1125        replyMsg,
 1126        0, // Compression status.
 1127        0, 0, 0, 0 // Message size (placeholder).
 1128    };
 129
 130    internal static void checkSupportedEncoding(Ice.EncodingVersion v)
 131    {
 1132        if (v.major != Ice.Util.Encoding_1_1.major || v.minor > Ice.Util.Encoding_1_1.minor)
 133        {
 1134            throw new MarshalException(
 1135                $"This Ice runtime does not support encoding version {v.major}.{v.minor}");
 136        }
 1137    }
 138
 139    internal static bool
 140    isSupported(Ice.EncodingVersion version, Ice.EncodingVersion supported) =>
 1141        version.major == supported.major && version.minor <= supported.minor;
 142
 143    private static void stringToMajorMinor(string str, out byte major, out byte minor)
 144    {
 1145        int pos = str.IndexOf('.', StringComparison.Ordinal);
 1146        if (pos == -1)
 147        {
 0148            throw new ParseException($"malformed version value in '{str}'");
 149        }
 150
 1151        string majStr = str[..pos];
 1152        string minStr = str[(pos + 1)..];
 153        int majVersion;
 154        int minVersion;
 155        try
 156        {
 1157            majVersion = int.Parse(majStr, CultureInfo.InvariantCulture);
 1158            minVersion = int.Parse(minStr, CultureInfo.InvariantCulture);
 1159        }
 0160        catch (FormatException ex)
 161        {
 0162            throw new ParseException($"invalid version value in '{str}'", ex);
 163        }
 164
 1165        if (majVersion < 1 || majVersion > 255 || minVersion < 0 || minVersion > 255)
 166        {
 0167            throw new ParseException($"range error in version '{str}'");
 168        }
 169
 1170        major = (byte)majVersion;
 1171        minor = (byte)minVersion;
 1172    }
 173
 1174    private static string majorMinorToString(byte major, byte minor) => $"{major}.{minor}";
 175
 0176    private Protocol()
 177    {
 0178    }
 179}