| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Globalization; |
| | | 4 | | |
| | | 5 | | namespace Ice.Internal; |
| | | 6 | | |
| | | 7 | | public 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 | | { |
| | 1 | 16 | | stringToMajorMinor(version, out byte major, out byte minor); |
| | 1 | 17 | | 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 | | { |
| | 1 | 27 | | stringToMajorMinor(version, out byte major, out byte minor); |
| | 1 | 28 | | 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> |
| | 1 | 36 | | 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> |
| | 1 | 43 | | internal static string encodingVersionToString(Ice.EncodingVersion v) => majorMinorToString(v.major, v.minor); |
| | | 44 | | |
| | 1 | 45 | | internal static readonly ProtocolVersion Protocol_1_0 = new(1, 0); |
| | | 46 | | |
| | 1 | 47 | | internal static readonly ProtocolVersion currentProtocol = new(protocolMajor, protocolMinor); |
| | | 48 | | |
| | 1 | 49 | | internal static readonly EncodingVersion currentProtocolEncoding = |
| | 1 | 50 | | 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 | | // |
| | 1 | 69 | | 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 | | |
| | 1 | 98 | | internal static readonly byte[] requestHdr = new byte[] |
| | 1 | 99 | | { |
| | 1 | 100 | | magic[0], magic[1], magic[2], magic[3], |
| | 1 | 101 | | protocolMajor, protocolMinor, |
| | 1 | 102 | | protocolEncodingMajor, protocolEncodingMinor, |
| | 1 | 103 | | requestMsg, |
| | 1 | 104 | | 0, // Compression status. |
| | 1 | 105 | | 0, 0, 0, 0, // Message size (placeholder). |
| | 1 | 106 | | 0, 0, 0, 0 // Request ID (placeholder). |
| | 1 | 107 | | }; |
| | | 108 | | |
| | 1 | 109 | | internal static readonly byte[] requestBatchHdr = new byte[] |
| | 1 | 110 | | { |
| | 1 | 111 | | magic[0], magic[1], magic[2], magic[3], |
| | 1 | 112 | | protocolMajor, protocolMinor, |
| | 1 | 113 | | protocolEncodingMajor, protocolEncodingMinor, |
| | 1 | 114 | | requestBatchMsg, |
| | 1 | 115 | | 0, // Compression status. |
| | 1 | 116 | | 0, 0, 0, 0, // Message size (placeholder). |
| | 1 | 117 | | 0, 0, 0, 0 // Number of requests in batch (placeholder). |
| | 1 | 118 | | }; |
| | | 119 | | |
| | 1 | 120 | | internal static readonly byte[] replyHdr = new byte[] |
| | 1 | 121 | | { |
| | 1 | 122 | | magic[0], magic[1], magic[2], magic[3], |
| | 1 | 123 | | protocolMajor, protocolMinor, |
| | 1 | 124 | | protocolEncodingMajor, protocolEncodingMinor, |
| | 1 | 125 | | replyMsg, |
| | 1 | 126 | | 0, // Compression status. |
| | 1 | 127 | | 0, 0, 0, 0 // Message size (placeholder). |
| | 1 | 128 | | }; |
| | | 129 | | |
| | | 130 | | internal static void checkSupportedEncoding(Ice.EncodingVersion v) |
| | | 131 | | { |
| | 1 | 132 | | if (v.major != Ice.Util.Encoding_1_1.major || v.minor > Ice.Util.Encoding_1_1.minor) |
| | | 133 | | { |
| | 1 | 134 | | throw new MarshalException( |
| | 1 | 135 | | $"This Ice runtime does not support encoding version {v.major}.{v.minor}"); |
| | | 136 | | } |
| | 1 | 137 | | } |
| | | 138 | | |
| | | 139 | | internal static bool |
| | | 140 | | isSupported(Ice.EncodingVersion version, Ice.EncodingVersion supported) => |
| | 1 | 141 | | version.major == supported.major && version.minor <= supported.minor; |
| | | 142 | | |
| | | 143 | | private static void stringToMajorMinor(string str, out byte major, out byte minor) |
| | | 144 | | { |
| | 1 | 145 | | int pos = str.IndexOf('.', StringComparison.Ordinal); |
| | 1 | 146 | | if (pos == -1) |
| | | 147 | | { |
| | 0 | 148 | | throw new ParseException($"malformed version value in '{str}'"); |
| | | 149 | | } |
| | | 150 | | |
| | 1 | 151 | | string majStr = str[..pos]; |
| | 1 | 152 | | string minStr = str[(pos + 1)..]; |
| | | 153 | | int majVersion; |
| | | 154 | | int minVersion; |
| | | 155 | | try |
| | | 156 | | { |
| | 1 | 157 | | majVersion = int.Parse(majStr, CultureInfo.InvariantCulture); |
| | 1 | 158 | | minVersion = int.Parse(minStr, CultureInfo.InvariantCulture); |
| | 1 | 159 | | } |
| | 0 | 160 | | catch (FormatException ex) |
| | | 161 | | { |
| | 0 | 162 | | throw new ParseException($"invalid version value in '{str}'", ex); |
| | | 163 | | } |
| | | 164 | | |
| | 1 | 165 | | if (majVersion < 1 || majVersion > 255 || minVersion < 0 || minVersion > 255) |
| | | 166 | | { |
| | 0 | 167 | | throw new ParseException($"range error in version '{str}'"); |
| | | 168 | | } |
| | | 169 | | |
| | 1 | 170 | | major = (byte)majVersion; |
| | 1 | 171 | | minor = (byte)minVersion; |
| | 1 | 172 | | } |
| | | 173 | | |
| | 1 | 174 | | private static string majorMinorToString(byte major, byte minor) => $"{major}.{minor}"; |
| | | 175 | | |
| | 0 | 176 | | private Protocol() |
| | | 177 | | { |
| | 0 | 178 | | } |
| | | 179 | | } |