summaryrefslogtreecommitdiff
path: root/csharp/src/Ice/Protocol.cs
blob: d8144552b0f0a876b0732bc2d62400bd1ac02428 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

namespace IceInternal
{

    public sealed class Protocol
    {
        //
        // Size of the Ice protocol header
        //
        // Magic number (4 bytes)
        // Protocol version major (Byte)
        // Protocol version minor (Byte)
        // Encoding version major (Byte)
        // Encoding version minor (Byte)
        // Message type (Byte)
        // Compression status (Byte)
        // Message size (Int)
        //
        internal const int headerSize = 14;
        
        //
        // The magic number at the front of each message
        //
        internal static readonly byte[] magic
            = new byte[] { (byte)0x49, (byte)0x63, (byte)0x65, (byte)0x50 }; // 'I', 'c', 'e', 'P'
        
        //
        // The current Ice protocol and encoding version
        //
        internal const byte protocolMajor = 1;
        internal const byte protocolMinor = 0;
        internal const byte protocolEncodingMajor = 1;
        internal const byte protocolEncodingMinor = 0;

        internal const byte encodingMajor = 1;
        internal const byte encodingMinor = 1;

        public const byte OPTIONAL_END_MARKER           = 0xFF;

        public const byte FLAG_HAS_TYPE_ID_STRING       = (byte)(1<<0);
        public const byte FLAG_HAS_TYPE_ID_INDEX        = (byte)(1<<1);
        public const byte FLAG_HAS_TYPE_ID_COMPACT      = (byte)(1<<1 | 1<<0);
        public const byte FLAG_HAS_OPTIONAL_MEMBERS     = (byte)(1<<2);
        public const byte FLAG_HAS_INDIRECTION_TABLE    = (byte)(1<<3);
        public const byte FLAG_HAS_SLICE_SIZE           = (byte)(1<<4);
        public const byte FLAG_IS_LAST_SLICE            = (byte)(1<<5);
        
        //
        // The Ice protocol message types
        //
        internal const byte requestMsg = 0;
        internal const byte requestBatchMsg = 1;
        internal const byte replyMsg = 2;
        internal const byte validateConnectionMsg = 3;
        internal const byte closeConnectionMsg = 4;

        internal static readonly byte[] requestHdr = new byte[]
        {
            IceInternal.Protocol.magic[0], IceInternal.Protocol.magic[1], IceInternal.Protocol.magic[2], 
            IceInternal.Protocol.magic[3],
            IceInternal.Protocol.protocolMajor, IceInternal.Protocol.protocolMinor,
            IceInternal.Protocol.protocolEncodingMajor, IceInternal.Protocol.protocolEncodingMinor,
            IceInternal.Protocol.requestMsg,
            (byte)0, // Compression status.
            (byte)0, (byte)0, (byte)0, (byte)0, // Message size (placeholder).
            (byte)0, (byte)0, (byte)0, (byte)0  // Request ID (placeholder).
        };

        internal static readonly byte[] requestBatchHdr = new byte[]
        {
            IceInternal.Protocol.magic[0], IceInternal.Protocol.magic[1], IceInternal.Protocol.magic[2],
            IceInternal.Protocol.magic[3],
            IceInternal.Protocol.protocolMajor, IceInternal.Protocol.protocolMinor,
            IceInternal.Protocol.protocolEncodingMajor, IceInternal.Protocol.protocolEncodingMinor,
            IceInternal.Protocol.requestBatchMsg,
            (byte)0, // Compression status.
            (byte)0, (byte)0, (byte)0, (byte)0, // Message size (placeholder).
            (byte)0, (byte)0, (byte)0, (byte)0  // Number of requests in batch (placeholder).
        };
        
        internal static readonly byte[] replyHdr = new byte[]
        {
            IceInternal.Protocol.magic[0], IceInternal.Protocol.magic[1], IceInternal.Protocol.magic[2], 
            IceInternal.Protocol.magic[3],
            IceInternal.Protocol.protocolMajor, IceInternal.Protocol.protocolMinor,
            IceInternal.Protocol.protocolEncodingMajor, IceInternal.Protocol.protocolEncodingMinor,
            IceInternal.Protocol.replyMsg,
            (byte)0, // Compression status.
            (byte)0, (byte)0, (byte)0, (byte)0 // Message size (placeholder).
        };

        internal static void
        checkSupportedProtocol(Ice.ProtocolVersion v)
        {
            if(v.major != protocolMajor || v.minor > protocolMinor)
            {
                throw new Ice.UnsupportedProtocolException("", v, Ice.Util.currentProtocol);
            }
        }

        public static void
        checkSupportedProtocolEncoding(Ice.EncodingVersion v)
        {
            if(v.major != protocolEncodingMajor || v.minor > protocolEncodingMinor)
            {
                throw new Ice.UnsupportedEncodingException("", v, Ice.Util.currentProtocolEncoding);
            }
        }

        internal static void
        checkSupportedEncoding(Ice.EncodingVersion v)
        {
            if(v.major != encodingMajor || v.minor > encodingMinor)
            {
                throw new Ice.UnsupportedEncodingException("", v, Ice.Util.currentEncoding);
            }
        }
        
        //
        // Either return the given protocol if not compatible, or the greatest
        // supported protocol otherwise.
        //
        internal static Ice.ProtocolVersion
        getCompatibleProtocol(Ice.ProtocolVersion v)
        {
            if(v.major != Ice.Util.currentProtocol.major)
            {
                return v; // Unsupported protocol, return as is.
            }
            else if(v.minor < Ice.Util.currentProtocol.minor)
            {
                return v; // Supported protocol.
            }
            else
            {
                //
                // Unsupported but compatible, use the currently supported
                // protocol, that's the best we can do.
                //
                return Ice.Util.currentProtocol; 
            }
        }

        //
        // Either return the given encoding if not compatible, or the greatest
        // supported encoding otherwise.
        //
        internal static Ice.EncodingVersion
        getCompatibleEncoding(Ice.EncodingVersion v)
        {
            if(v.major != Ice.Util.currentEncoding.major)
            {
                return v; // Unsupported encoding, return as is.
            }
            else if(v.minor < Ice.Util.currentEncoding.minor)
            {
                return v; // Supported encoding.
            }
            else
            {
                //
                // Unsupported but compatible, use the currently supported
                // encoding, that's the best we can do.
                //
                return Ice.Util.currentEncoding; 
            }
        }

        internal static bool
        isSupported(Ice.ProtocolVersion version, Ice.ProtocolVersion supported)
        {
            return version.major == supported.major && version.minor <= supported.minor;
        }

        internal static bool
        isSupported(Ice.EncodingVersion version, Ice.EncodingVersion supported)
        {
            return version.major == supported.major && version.minor <= supported.minor;
        }
        
        private Protocol()
        {
        }
    }

}