| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | namespace Ice.Internal; |
| | 4 | |
|
| | 5 | | public sealed class Protocol |
| | 6 | | { |
| | 7 | | // |
| | 8 | | // Size of the Ice protocol header |
| | 9 | | // |
| | 10 | | // Magic number (4 bytes) |
| | 11 | | // Protocol version major (Byte) |
| | 12 | | // Protocol version minor (Byte) |
| | 13 | | // Encoding version major (Byte) |
| | 14 | | // Encoding version minor (Byte) |
| | 15 | | // Message type (Byte) |
| | 16 | | // Compression status (Byte) |
| | 17 | | // Message size (Int) |
| | 18 | | // |
| | 19 | | internal const int headerSize = 14; |
| | 20 | |
|
| | 21 | | // |
| | 22 | | // The magic number at the front of each message |
| | 23 | | // |
| 1 | 24 | | internal static readonly byte[] magic = new byte[] { 0x49, 0x63, 0x65, 0x50 }; // 'I', 'c', 'e', 'P' |
| | 25 | |
|
| | 26 | | // |
| | 27 | | // The current Ice protocol and Slice encoding version |
| | 28 | | // |
| | 29 | | internal const byte protocolMajor = 1; |
| | 30 | | internal const byte protocolMinor = 0; |
| | 31 | | internal const byte protocolEncodingMajor = 1; |
| | 32 | | internal const byte protocolEncodingMinor = 0; |
| | 33 | |
|
| | 34 | | internal const byte encodingMajor = 1; |
| | 35 | | internal const byte encodingMinor = 1; |
| | 36 | |
|
| | 37 | | public const byte OPTIONAL_END_MARKER = 0xFF; |
| | 38 | |
|
| | 39 | | public const byte FLAG_HAS_TYPE_ID_STRING = 1 << 0; |
| | 40 | | public const byte FLAG_HAS_TYPE_ID_INDEX = 1 << 1; |
| | 41 | | public const byte FLAG_HAS_TYPE_ID_COMPACT = (1 << 1) | (1 << 0); |
| | 42 | | public const byte FLAG_HAS_OPTIONAL_MEMBERS = 1 << 2; |
| | 43 | | public const byte FLAG_HAS_INDIRECTION_TABLE = 1 << 3; |
| | 44 | | public const byte FLAG_HAS_SLICE_SIZE = 1 << 4; |
| | 45 | | public const byte FLAG_IS_LAST_SLICE = 1 << 5; |
| | 46 | |
|
| | 47 | | // |
| | 48 | | // The Ice protocol message types |
| | 49 | | // |
| | 50 | | internal const byte requestMsg = 0; |
| | 51 | | internal const byte requestBatchMsg = 1; |
| | 52 | | internal const byte replyMsg = 2; |
| | 53 | | internal const byte validateConnectionMsg = 3; |
| | 54 | | internal const byte closeConnectionMsg = 4; |
| | 55 | |
|
| 1 | 56 | | internal static readonly byte[] requestHdr = new byte[] |
| 1 | 57 | | { |
| 1 | 58 | | magic[0], magic[1], magic[2], magic[3], |
| 1 | 59 | | protocolMajor, protocolMinor, |
| 1 | 60 | | protocolEncodingMajor, protocolEncodingMinor, |
| 1 | 61 | | requestMsg, |
| 1 | 62 | | 0, // Compression status. |
| 1 | 63 | | 0, 0, 0, 0, // Message size (placeholder). |
| 1 | 64 | | 0, 0, 0, 0 // Request ID (placeholder). |
| 1 | 65 | | }; |
| | 66 | |
|
| 1 | 67 | | internal static readonly byte[] requestBatchHdr = new byte[] |
| 1 | 68 | | { |
| 1 | 69 | | magic[0], magic[1], magic[2], magic[3], |
| 1 | 70 | | protocolMajor, protocolMinor, |
| 1 | 71 | | protocolEncodingMajor, protocolEncodingMinor, |
| 1 | 72 | | requestBatchMsg, |
| 1 | 73 | | 0, // Compression status. |
| 1 | 74 | | 0, 0, 0, 0, // Message size (placeholder). |
| 1 | 75 | | 0, 0, 0, 0 // Number of requests in batch (placeholder). |
| 1 | 76 | | }; |
| | 77 | |
|
| 1 | 78 | | internal static readonly byte[] replyHdr = new byte[] |
| 1 | 79 | | { |
| 1 | 80 | | magic[0], magic[1], magic[2], magic[3], |
| 1 | 81 | | protocolMajor, protocolMinor, |
| 1 | 82 | | protocolEncodingMajor, protocolEncodingMinor, |
| 1 | 83 | | replyMsg, |
| 1 | 84 | | 0, // Compression status. |
| 1 | 85 | | 0, 0, 0, 0 // Message size (placeholder). |
| 1 | 86 | | }; |
| | 87 | |
|
| | 88 | | internal static void checkSupportedEncoding(Ice.EncodingVersion v) |
| | 89 | | { |
| 1 | 90 | | if (v.major != encodingMajor || v.minor > encodingMinor) |
| | 91 | | { |
| 1 | 92 | | throw new MarshalException( |
| 1 | 93 | | $"This Ice runtime does not support encoding version {v.major}.{v.minor}"); |
| | 94 | | } |
| 1 | 95 | | } |
| | 96 | |
|
| | 97 | | // |
| | 98 | | // Either return the given encoding if not compatible, or the greatest |
| | 99 | | // supported encoding otherwise. |
| | 100 | | // |
| | 101 | | internal static Ice.EncodingVersion |
| | 102 | | getCompatibleEncoding(Ice.EncodingVersion v) |
| | 103 | | { |
| 1 | 104 | | if (v.major != Ice.Util.currentEncoding.major) |
| | 105 | | { |
| 1 | 106 | | return v; // Unsupported encoding, return as is. |
| | 107 | | } |
| 1 | 108 | | else if (v.minor < Ice.Util.currentEncoding.minor) |
| | 109 | | { |
| 1 | 110 | | return v; // Supported encoding. |
| | 111 | | } |
| | 112 | | else |
| | 113 | | { |
| | 114 | | // |
| | 115 | | // Unsupported but compatible, use the currently supported |
| | 116 | | // encoding, that's the best we can do. |
| | 117 | | // |
| 1 | 118 | | return Ice.Util.currentEncoding; |
| | 119 | | } |
| | 120 | | } |
| | 121 | |
|
| | 122 | | internal static bool |
| | 123 | | isSupported(Ice.EncodingVersion version, Ice.EncodingVersion supported) => |
| 1 | 124 | | version.major == supported.major && version.minor <= supported.minor; |
| | 125 | |
|
| 0 | 126 | | private Protocol() |
| | 127 | | { |
| 0 | 128 | | } |
| | 129 | | } |