-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cs
137 lines (117 loc) · 3.68 KB
/
Utils.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
using System;
using System.IO;
namespace Terralite
{
public class Utils
{
/// <summary>
/// Takes <paramref name="buffer"/> and returns an array of byte[]
/// that each holds <paramref name="head"/> plus up to MAX_SIZE (1400 bytes) of data.
/// </summary>
/// <param name="head">Header to be appended</param>
/// <param name="buffer">Data to be split</param>
/// <returns></returns>
public static byte[][] SplitBuffer(byte[] head, byte[] buffer)
{
byte[][] result = new byte[buffer.Length / Packet.MAX_SIZE][];
int size;
MemoryStream s;
byte[] tmp;
byte[] header;
byte pid = 1;
for (uint i = 0; i < result.GetLength(0); i++)
{
s = new MemoryStream(5);
s.WriteByte(Packet.MULTI);
s.WriteByte((byte)result.GetLength(0));
s.WriteByte(pid++);
s.Write(head, 0, head.Length);
header = new byte[s.Length];
s.Read(header, 0, header.Length);
size = i == result.GetLength(0) - 1 ? buffer.Length % Packet.MAX_SIZE : Packet.MAX_SIZE;
tmp = new byte[header.Length + size];
Array.Copy(buffer, i * (Packet.MAX_SIZE + header.Length), tmp, 0, size);
result[i] = Combine(header, tmp);
}
return result;
}
/// <summary>
/// Splits <paramref name="buffer"/> into two byte[] at <paramref name="index"/>.
/// If index is -1, it calculates where to split using the packet type.
/// </summary>
/// <param name="buffer">Byte[] to be split</param>
/// <param name="index">Index to split at</param>
/// <returns>Two byte arrays</returns>
public static byte[][] Split(byte[] buffer, int index = -1)
{
byte[][] result = new byte[2][];
//calculate index from header
if (index == -1)
{
switch (buffer[0])
{
case Packet.INIT:
case Packet.INIT_ACK:
case Packet.INIT_FIN:
case Packet.NON_RELIABLE:
case Packet.PING:
case Packet.PING_ACK:
case Packet.DISCONNECT:
index = 1;
break;
case Packet.RELIABLE:
case Packet.ACK:
index = 2;
break;
}
}
result[0] = new byte[index];
result[1] = new byte[buffer.Length - index];
Array.Copy(buffer, result[0], result[0].Length);
Array.Copy(buffer, index, result[1], 0, result[1].Length);
return result;
}
/// <summary>
/// Takes two byte arrays and returns their combination
/// </summary>
/// <param name="buffer1">The first byte array</param>
/// <param name="buffer2">The second byte array</param>
/// <returns><paramref name="buffer1"/> + <paramref name="buffer2"/></returns>
/// <remarks>
/// If only one buffer is null, it returns that buffer.
/// </remarks>
public static byte[] Combine(byte[] buffer1, byte[] buffer2, int count1 = -1, int count2 = -1)
{
if (buffer1 == null && buffer2 != null)
return buffer2;
if (buffer1 != null && buffer2 == null)
return buffer1;
int size1 = count1 == -1 ? buffer1.Length : count1;
int size2 = count2 == -1 ? buffer2.Length : count2;
byte[] result = new byte[size1 + size2];
Array.Copy(buffer1, 0, result, 0, size1);
Array.Copy(buffer2, 0, result, size1, size2);
return result;
}
/// <summary>
/// Used to compare two byte arrays.
/// </summary>
/// <param name="buffer1">Byte array one</param>
/// <param name="buffer2">Byte array two</param>
/// <returns>Returns whether or not their data is equal</returns>
public static bool Compare(byte[] buffer1, byte[] buffer2)
{
if (buffer1 == buffer2)
return true;
if ((buffer1 == null && buffer2 != null) ||
(buffer1 != null && buffer2 == null))
return false;
if (buffer1.Length != buffer2.Length)
return false;
for (int i = 0; i < buffer1.Length; i++)
if (buffer1[i] != buffer2[i])
return false;
return true;
}
}
}