language: C# (mono-2.8)
date: 438 days 8 hours ago
link:
visibility: public
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using System;
using System.Collections.Generic;
using System.Text;
 
public class Packet
{
        private List<byte> buffer = new List<byte>();
        private int byteIndex = 0;
        private int maxByteIndex = 0;
 
        public byte[] Buffer
        {
                get
                {
                        return buffer.ToArray();
                }
        }
 
        public int Length
        {
                get
                {
                        return buffer.Count;
                }
        }
 
        public int ByteIndex
        {
                get
                {
                        return byteIndex;
                }
                set
                {
                        if (value < 0 || value >= buffer.Count)
                        {
                                throw new ArgumentOutOfRangeException("ByteIndex must be an index in the Buffer");
                        }
                        byteIndex = value;
                }
        }
 
        public bool HasHeader
        {
                get
                {
                        return byteIndex >= 2;
                }
        }
 
        public bool HasData
        {
                get
                {
                        return byteIndex == maxByteIndex;
                }
        }
 
        public void Begin()
        {
                buffer.Add(0);
                buffer.Add(0);
                byteIndex += 2;
        }
 
        public void End()
        {
                buffer[0] = (byte)(buffer.Count >> 8);
                buffer[1] = (byte)buffer.Count;
        }
 
        public bool ReadHeader()
        {
                int previousByteIndex = byteIndex;
                byteIndex = 0;
                ushort length;
                ReadUInt16(out length);
                maxByteIndex = (int)length - 1;
                byteIndex = previousByteIndex;
                return maxByteIndex > 2;
        }
 
        public void WriteEventID(byte value)
        {
                buffer.Add(value);
                byteIndex++;
        }
 
        public void WriteByte(byte value)
        {
                buffer.Add(value);
                byteIndex++;
        }
 
        public void WriteUInt16(ushort value)
        {
                buffer.Add((byte)(value >> 8));
                buffer.Add((byte)value);
                byteIndex += 2;
        }
 
        public void WriteString(string value, int maxLength = 255)
        {
                if (maxLength < 0 || maxLength > ushort.MaxValue)
                {
                        throw new ArgumentOutOfRangeException("maxLength must be in the range [0, 65535]");
                }
                if (value.Length > maxLength)
                {
                        throw new ArgumentOutOfRangeException("String length was over the given max length");
                }
                WriteUInt16((ushort)value.Length);
                var characters = System.Text.Encoding.ASCII.GetBytes(value);
                foreach (var character in characters)
                {
                        WriteByte(character);
                }
        }
 
        public bool ReadEventID(out byte value)
        {
                return ReadByte(out value);
        }
 
        public bool ReadByte(out byte value)
        {
                value = 0;
                if (byteIndex >= buffer.Count)
                {
                        return false;
                }
                value = buffer[byteIndex];
                byteIndex++;
                return true;
        }
 
        public bool ReadUInt16(out ushort value)
        {
                value = 0;
                if (byteIndex + 1 >= buffer.Count)
                {
                        return false;
                }
                value = (ushort)(buffer[byteIndex] << 8);
                value |= (ushort)buffer[byteIndex + 1];
                byteIndex += 2;
                return true;
        }
 
        public bool ReadString(out string value, int maxLength = 255)
        {
                value = string.Empty;
                ushort length;
                if (!ReadUInt16(out length) || length > maxLength || byteIndex + length > buffer.Count)
                {
                        return false;
                }
                value = System.Text.Encoding.ASCII.GetString(buffer.ToArray(), byteIndex, (int)length);
                byteIndex += length;
                return true;
        }
}
 
public class Test
{
        public static void Main()
        {
                var packet = new Packet();
                packet.Begin();
                packet.WriteEventID(42);
                packet.WriteByte(43);
                packet.WriteUInt16(65535);
                packet.WriteString("Hello World", 20);
                packet.End();
 
                packet.ByteIndex = 2;
                byte eventID;
                if (!packet.ReadEventID(out eventID)) { Console.WriteLine("Error reading"); return; }
                Console.WriteLine(eventID);
                byte byteValue;
                if (!packet.ReadByte(out byteValue)) { Console.WriteLine("Error reading"); return; }
                Console.WriteLine(byteValue);
                ushort ushortValue;
                if (!packet.ReadUInt16(out ushortValue)) { Console.WriteLine("Error reading"); return; }
                Console.WriteLine(ushortValue);
                string stringValue;
                if (!packet.ReadString(out stringValue)) { Console.WriteLine("Error reading"); return; }
                Console.WriteLine(stringValue);
 
                /*
                To read from a TCP stream:
                // packet is a per end point object that persists through read callbacks
                for (int readByteIndex = 0; readByteIndex < receiveBufferSize; ++readByteIndex)
                {
                        packet.WriteByte(receiveBuffer[readByteIndex]);
                        if (!packet.HasHeader)
                        {
                                if (!packet.ReadHeader())
                                {
                                        DisconnectEndPoint();
                                }
                        }
                        if (!packet.HasData)
                        {
                                // Packets is a ConcurrentQueue
                                endPoint.Packets.Enqueue(packet);
                                packet = new Packet();
                        }
                }
                */
        }
}