forked from LuchunPen/BytePacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BytePacketReader.cs
189 lines (182 loc) · 6.92 KB
/
BytePacketReader.cs
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
/*
Copyright (c) Luchunpen (bwolf88). All rights reserved.
Date: 17/01/2016 09:17
*/
using System;
using System.Text;
namespace Nano3
{
public class BytePacketReader
{
private byte[] _items;
private int _bufferSize;
private int _ptr;
public int CurrentPosition { get { return _ptr; } }
public BytePacketReader(byte[] packet)
{
if (packet == null) { new ArgumentNullException("Source buffer is null)"); }
_items = packet;
_bufferSize = _items.Length;
}
public bool ReadBoolean()
{
return _items[_ptr++] == 1;
}
public byte ReadByte()
{
return _items[_ptr++];
}
public sbyte ReadSbyte()
{
return (sbyte)_items[_ptr++];
}
public short ReadShort()
{
return (short)(_items[_ptr++] | _items[_ptr++] << 8);
}
public ushort ReadUshort()
{
return (ushort)(_items[_ptr++] | _items[_ptr++] << 8);
}
public int ReadInt()
{
return (int)(_items[_ptr++] | _items[_ptr++] << 8 | _items[_ptr++] << 16 | _items[_ptr++] << 24);
}
public uint ReadUint()
{
return (uint)(_items[_ptr++] | _items[_ptr++] << 8 | _items[_ptr++] << 16 | _items[_ptr++] << 24);
}
public long ReadLong()
{
return (_items[_ptr++] | (long)_items[_ptr++] << 8
| (long)_items[_ptr++] << 16 | (long)_items[_ptr++] << 24
| (long)_items[_ptr++] << 32 | (long)_items[_ptr++] << 40
| (long)_items[_ptr++] << 48 | (long)_items[_ptr++] << 56);
}
public ulong ReadUlong()
{
return (_items[_ptr++] | (ulong)_items[_ptr++] << 8
| (ulong)_items[_ptr++] << 16 | (ulong)_items[_ptr++] << 24
| (ulong)_items[_ptr++] << 32 | (ulong)_items[_ptr++] << 40
| (ulong)_items[_ptr++] << 48 | (ulong)_items[_ptr++] << 56);
}
public float ReadSingle()
{
FloatInt fi = new FloatInt();
fi.ivalue = (int)(_items[_ptr++] | _items[_ptr++] << 8 | _items[_ptr++] << 16 | _items[_ptr++] << 24);
return fi.fvalue;
}
public double ReadDouble()
{
DoubleLong dl = new DoubleLong();
dl.lvalue = (_items[_ptr++] | (long)_items[_ptr++] << 8
| (long)_items[_ptr++] << 16 | (long)_items[_ptr++] << 24
| (long)_items[_ptr++] << 32 | (long)_items[_ptr++] << 40
| (long)_items[_ptr++] << 48 | (long)_items[_ptr++] << 56);
return dl.dvalue;
}
public string ReadString()
{
int size = ReadInt();
if (size > 0 && _ptr + size <= _items.Length)
{
int n = _ptr;
_ptr += size;
return Encoding.UTF8.GetString(_items, n, size);
}
return "";
}
public byte[] ReadPacket()
{
int size = ReadInt();
if (size > 0 && _ptr + size <= _items.Length)
{
int n = _ptr;
_ptr += size;
byte[] result = new byte[size];
Buffer.BlockCopy(_items, n, result, 0, size);
return result;
}
return new byte[0];
}
#region StaticFunc
public static bool ReadBoolean(byte[] packet, ref int position)
{
return packet[position++] == 1;
}
public static byte ReadByte(byte[] packet, ref int position)
{
return packet[position++];
}
public static sbyte ReadSbyte(byte[] packet, ref int position)
{
return (sbyte)packet[position++];
}
public static short ReadShort(byte[] packet, ref int position)
{
return (short)(packet[position++] | packet[position++] << 8);
}
public static ushort ReadUshort(byte[] packet, ref int position)
{
return (ushort)(packet[position++] | packet[position++] << 8);
}
public static int ReadInt(byte[] packet, ref int position)
{
return (int)(packet[position++] | packet[position++] << 8 | packet[position++] << 16 | packet[position++] << 24);
}
public static uint ReadUint(byte[] packet, ref int position)
{
return (uint)(packet[position++] | packet[position++] << 8 | packet[position++] << 16 | packet[position++] << 24);
}
public static long ReadLong(byte[] packet, ref int position)
{
return (packet[position++] | (long)packet[position++] << 8
| (long)packet[position++] << 16 | (long)packet[position++] << 24
| (long)packet[position++] << 32 | (long)packet[position++] << 40
| (long)packet[position++] << 48 | (long)packet[position++] << 56);
}
public static ulong ReadUlong(byte[] packet, ref int position)
{
return (packet[position++] | (ulong)packet[position++] << 8
| (ulong)packet[position++] << 16 | (ulong)packet[position++] << 24
| (ulong)packet[position++] << 32 | (ulong)packet[position++] << 40
| (ulong)packet[position++] << 48 | (ulong)packet[position++] << 56);
}
public static float ReadSingle(byte[] packet, ref int position)
{
FloatInt fi = new FloatInt();
fi.ivalue = (int)(packet[position++] | packet[position++] << 8 | packet[position++] << 16 | packet[position++] << 24);
return fi.fvalue;
}
public static double ReadDouble(byte[] packet, ref int position)
{
DoubleLong dl = new DoubleLong();
dl.lvalue = (packet[position++] | (long)packet[position++] << 8
| (long)packet[position++] << 16 | (long)packet[position++] << 24
| (long)packet[position++] << 32 | (long)packet[position++] << 40
| (long)packet[position++] << 48 | (long)packet[position++] << 56);
return dl.dvalue;
}
public static string ReadString(byte[] packet, ref int position)
{
int size = ReadInt(packet, ref position);
if (size > 0 && position + size <= packet.Length)
{
int pos = position;
position += size;
return Encoding.UTF8.GetString(packet, pos, size);
}
return "";
}
public static byte[] ReadPacket(byte[] packet, ref int position)
{
int size = ReadInt(packet, ref position);
byte[] result = new byte[size];
if (result.Length == 0) return result;
Buffer.BlockCopy(packet, position, result, 0, size);
position += size;
return result;
}
#endregion StaticFunc
}
}