summaryrefslogtreecommitdiff
path: root/Impostor-dev/src/Impostor.Benchmarks/Data/MessageReader_Bytes.cs
blob: 2aba724ebe13dba77632e3755cdb69cf15a8d03f (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
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;

namespace Impostor.Benchmarks.Data
{
    public class MessageReader_Bytes
    {
        public byte Tag { get; }
        public byte[] Buffer { get; }
        public int Position { get; set; }
        public int Length { get; set; }
        public int BytesRemaining => this.Length - this.Position;

        public MessageReader_Bytes(byte[] buffer, int position = 0, int length = 0)
        {
            Tag = byte.MaxValue;
            Buffer = buffer;
            Position = position;
            Length = length;
        }

        public MessageReader_Bytes(byte tag, byte[] buffer, int position = 0, int length = 0)
        {
            Tag = tag;
            Buffer = buffer;
            Position = position;
            Length = length;
        }

        public MessageReader_Bytes ReadMessage()
        {
            var length = ReadUInt16();
            var tag = FastByte();
            var pos = Position;

            Position += length;
            return new MessageReader_Bytes(tag, Buffer, pos, length);
        }

        public bool ReadBoolean()
        {
            byte val = FastByte();
            return val != 0;
        }

        public sbyte ReadSByte()
        {
            return (sbyte)FastByte();
        }

        public byte ReadByte()
        {
            return FastByte();
        }

        public ushort ReadUInt16()
        {
            return (ushort)(this.FastByte() |
                            this.FastByte() << 8);
        }

        public short ReadInt16()
        {
            return (short)(this.FastByte() |
                           this.FastByte() << 8);
        }

        public uint ReadUInt32()
        {
            return this.FastByte()
                   | (uint)this.FastByte() << 8
                   | (uint)this.FastByte() << 16
                   | (uint)this.FastByte() << 24;
        }

        public int ReadInt32()
        {
            return this.FastByte()
                   | this.FastByte() << 8
                   | this.FastByte() << 16
                   | this.FastByte() << 24;
        }

        public unsafe float ReadSingle()
        {
            float output = 0;
            fixed (byte* bufPtr = &this.Buffer[Position])
            {
                byte* outPtr = (byte*)&output;

                *outPtr = *bufPtr;
                *(outPtr + 1) = *(bufPtr + 1);
                *(outPtr + 2) = *(bufPtr + 2);
                *(outPtr + 3) = *(bufPtr + 3);
            }

            this.Position += 4;
            return output;
        }

        public string ReadString()
        {
            var len = this.ReadPackedInt32();

            if (this.BytesRemaining < len)
            {
                throw new InvalidDataException($"Read length is longer than message length: {len} of {this.BytesRemaining}");
            }

            var output = Encoding.UTF8.GetString(this.Buffer, Position, len);
            this.Position += len;
            return output;
        }

        public Span<byte> ReadBytesAndSize()
        {
            var len = ReadPackedInt32();
            return ReadBytes(len);
        }

        public Span<byte> ReadBytes(int length)
        {
            var output = Buffer.AsSpan(Position, length);
            Position += length;
            return output;
        }

        public int ReadPackedInt32()
        {
            return (int)ReadPackedUInt32();
        }

        public uint ReadPackedUInt32()
        {
            bool readMore = true;
            int shift = 0;
            uint output = 0;

            while (readMore)
            {
                byte b = FastByte();
                if (b >= 0x80)
                {
                    readMore = true;
                    b ^= 0x80;
                }
                else
                {
                    readMore = false;
                }

                output |= (uint)(b << shift);
                shift += 7;
            }

            return output;
        }

        public MessageReader_Bytes Slice(int start, int length)
        {
            return new MessageReader_Bytes(Tag, Buffer, start, length);
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private byte FastByte()
        {
            return Buffer[Position++];
        }
    }
}