|
| 1 | +/*---------------------------------------------------------- |
| 2 | +This Source Code Form is subject to the terms of the |
| 3 | +Mozilla Public License, v.2.0. If a copy of the MPL |
| 4 | +was not distributed with this file, You can obtain one |
| 5 | +at http://mozilla.org/MPL/2.0/. |
| 6 | +----------------------------------------------------------*/ |
| 7 | +using System; |
| 8 | + |
| 9 | +namespace OneScript.DebugProtocol.TcpServer |
| 10 | +{ |
| 11 | + public static class FormatReconcileUtils |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Массив байт, который вызовет SerializationException при чтении его через BinaryFormatter |
| 15 | + /// И при этом оставит поток пустым, для следующего валидного сообщения |
| 16 | + /// <see>[MS-NRBF] .NET Remoting: Binary Format Data Structure</see> |
| 17 | + /// </summary> |
| 18 | + public static readonly byte[] FORMAT_RECONCILE_MAGIC = |
| 19 | + { |
| 20 | + 0x11, // RecordTypeEnum |
| 21 | + 0,0,0,0, // RootId |
| 22 | + 0,0,0,0, // HeaderId |
| 23 | + 0x00, 0x0A, 0x00, 0x0A, // Version Major |
| 24 | + 0xAA, 0xAA, 0xAA, 0xAA // Version Minor, |
| 25 | + }; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Ответ на запрос формата |
| 29 | + /// </summary> |
| 30 | + public static readonly byte[] FORMAT_RECONCILE_RESPONSE_PREFIX = |
| 31 | + { |
| 32 | + 0x1C, |
| 33 | + 0x1C, |
| 34 | + 0x1C, |
| 35 | + 0x1C |
| 36 | + }; |
| 37 | + |
| 38 | + public static readonly TimeSpan FORMAT_RECONCILE_TIMEOUT = TimeSpan.FromMilliseconds(500); |
| 39 | + |
| 40 | + public static bool CheckReconcilePrefix(byte[] data) |
| 41 | + { |
| 42 | + for (int i = 0; i < FORMAT_RECONCILE_RESPONSE_PREFIX.Length; i++) |
| 43 | + { |
| 44 | + if (data[i] != FORMAT_RECONCILE_RESPONSE_PREFIX[i]) |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + return true; |
| 49 | + } |
| 50 | + |
| 51 | + public static bool CheckReconcileRequest(byte[] data) |
| 52 | + { |
| 53 | + for (int i = 0; i < FORMAT_RECONCILE_MAGIC.Length; i++) |
| 54 | + { |
| 55 | + if (data[i] != FORMAT_RECONCILE_MAGIC[i]) |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + public static int EncodeFormatMarker(short transport, short dataVersion) |
| 63 | + { |
| 64 | + var marker = transport << 16; |
| 65 | + return marker | dataVersion; |
| 66 | + } |
| 67 | + |
| 68 | + public static (int, int) DecodeFormatMarker(int marker) |
| 69 | + { |
| 70 | + var transport = marker >> 16; |
| 71 | + var dataVersion = marker & 0x0000FFFF; |
| 72 | + |
| 73 | + return (transport, dataVersion); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments