Skip to content

Commit 47d53f0

Browse files
[Networking] Added StapleNetworking;
1 parent fafcc78 commit 47d53f0

16 files changed

+2174
-0
lines changed

Engine/Engine.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StapleOpenALAudio", "Staple
5151
{97D22183-838A-63D0-EC91-3C2CD868FEC0} = {97D22183-838A-63D0-EC91-3C2CD868FEC0}
5252
EndProjectSection
5353
EndProject
54+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StapleNetworking", "StapleNetworking\StapleNetworking.csproj", "{AB3DE773-FDBC-4760-8967-1B5B052F9B1B}"
55+
EndProject
5456
Global
5557
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5658
Debug|Any CPU = Debug|Any CPU
@@ -97,6 +99,10 @@ Global
9799
{B1931566-065A-4ED4-A87D-CF6F9CE3413A}.Debug|Any CPU.Build.0 = Debug|Any CPU
98100
{B1931566-065A-4ED4-A87D-CF6F9CE3413A}.Release|Any CPU.ActiveCfg = Release|Any CPU
99101
{B1931566-065A-4ED4-A87D-CF6F9CE3413A}.Release|Any CPU.Build.0 = Release|Any CPU
102+
{AB3DE773-FDBC-4760-8967-1B5B052F9B1B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
103+
{AB3DE773-FDBC-4760-8967-1B5B052F9B1B}.Debug|Any CPU.Build.0 = Debug|Any CPU
104+
{AB3DE773-FDBC-4760-8967-1B5B052F9B1B}.Release|Any CPU.ActiveCfg = Release|Any CPU
105+
{AB3DE773-FDBC-4760-8967-1B5B052F9B1B}.Release|Any CPU.Build.0 = Release|Any CPU
100106
EndGlobalSection
101107
GlobalSection(SolutionProperties) = preSolution
102108
HideSolutionNode = FALSE
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
using System;
2+
using System.Linq;
3+
using System.Text;
4+
5+
namespace Staple.Networking;
6+
7+
/// <summary>
8+
/// Reads packet data from a byte array
9+
/// </summary>
10+
public class ByteArrayPacketReader(byte[] data) : INetworkReader
11+
{
12+
private readonly byte[] data = data;
13+
private int position;
14+
15+
public int Length => data?.Length ?? 0;
16+
17+
public int Position => position;
18+
19+
public bool ReadBool()
20+
{
21+
if(position + 1 > Length)
22+
{
23+
throw new Exception("Exceeded packet length");
24+
}
25+
26+
return data[position++] != 0;
27+
}
28+
29+
public byte ReadByte()
30+
{
31+
if (position + 1 > Length)
32+
{
33+
throw new Exception("Exceeded packet length");
34+
}
35+
36+
return data[position++];
37+
}
38+
39+
public byte[] ReadBytes()
40+
{
41+
var length = ReadUInt32();
42+
43+
return ReadBytesAndSize((int)length);
44+
}
45+
46+
public byte[] ReadBytesAndSize(int length)
47+
{
48+
if (position + length > Length)
49+
{
50+
throw new Exception("Exceeded packet length");
51+
}
52+
53+
var outValue = data.Skip(position).Take(length).ToArray();
54+
55+
position += length;
56+
57+
return outValue;
58+
}
59+
60+
public char ReadChar()
61+
{
62+
var size = ReadUInt16();
63+
64+
if (position + size > Length)
65+
{
66+
throw new Exception("Exceeded packet length");
67+
}
68+
69+
var inValue = data.Skip(position).Take(size).ToArray();
70+
71+
position += size;
72+
73+
var character = BitConverter.ToChar(inValue);
74+
75+
return character;
76+
}
77+
78+
public double ReadDouble()
79+
{
80+
if (position + sizeof(double) > Length)
81+
{
82+
throw new Exception("Exceeded packet length");
83+
}
84+
85+
var t = data.Skip(position).Take(sizeof(double)).ToArray();
86+
87+
position += sizeof(double);
88+
89+
return BitConverter.ToDouble(t);
90+
}
91+
92+
public float ReadFloat()
93+
{
94+
if (position + sizeof(float) > Length)
95+
{
96+
throw new Exception("Exceeded packet length");
97+
}
98+
99+
var t = data.Skip(position).Take(sizeof(float)).ToArray();
100+
101+
position += sizeof(float);
102+
103+
return BitConverter.ToSingle(t);
104+
}
105+
106+
public short ReadInt16()
107+
{
108+
if (position + sizeof(short) > Length)
109+
{
110+
throw new Exception("Exceeded packet length");
111+
}
112+
113+
var t = data.Skip(position).Take(sizeof(short)).ToArray();
114+
115+
position += sizeof(short);
116+
117+
return BitConverter.ToInt16(t);
118+
}
119+
120+
public int ReadInt32()
121+
{
122+
if (position + sizeof(int) > Length)
123+
{
124+
throw new Exception("Exceeded packet length");
125+
}
126+
127+
var t = data.Skip(position).Take(sizeof(int)).ToArray();
128+
129+
position += sizeof(int);
130+
131+
return BitConverter.ToInt32(t);
132+
}
133+
134+
public long ReadInt64()
135+
{
136+
if (position + sizeof(long) > Length)
137+
{
138+
throw new Exception("Exceeded packet length");
139+
}
140+
141+
var t = data.Skip(position).Take(sizeof(long)).ToArray();
142+
143+
position += sizeof(long);
144+
145+
return BitConverter.ToInt64(t);
146+
}
147+
148+
public sbyte ReadSByte()
149+
{
150+
if (position + 1 > Length)
151+
{
152+
throw new Exception("Exceeded packet length");
153+
}
154+
155+
return (sbyte)data[position++];
156+
}
157+
158+
public string ReadString()
159+
{
160+
var length = ReadUInt16();
161+
162+
if(position + length > Length)
163+
{
164+
throw new Exception("Exceeded packet length");
165+
}
166+
167+
var str = Encoding.UTF8.GetString(data, position, length);
168+
169+
position += length;
170+
171+
return str;
172+
}
173+
174+
public ushort ReadUInt16()
175+
{
176+
if (position + sizeof(ushort) > Length)
177+
{
178+
throw new Exception("Exceeded packet length");
179+
}
180+
181+
var t = data.Skip(position).Take(sizeof(ushort)).ToArray();
182+
183+
position += sizeof(ushort);
184+
185+
return BitConverter.ToUInt16(t);
186+
}
187+
188+
public uint ReadUInt32()
189+
{
190+
if (position + sizeof(uint) > Length)
191+
{
192+
throw new Exception("Exceeded packet length");
193+
}
194+
195+
var t = data.Skip(position).Take(sizeof(uint)).ToArray();
196+
197+
position += sizeof(uint);
198+
199+
return BitConverter.ToUInt32(t);
200+
}
201+
202+
public ulong ReadUInt64()
203+
{
204+
if (position + sizeof(ulong) > Length)
205+
{
206+
throw new Exception("Exceeded packet length");
207+
}
208+
209+
var t = data.Skip(position).Take(sizeof(ulong)).ToArray();
210+
211+
position += sizeof(ulong);
212+
213+
return BitConverter.ToUInt64(t);
214+
}
215+
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Staple.Networking;
7+
8+
/// <summary>
9+
/// Writes packet data to a byte array
10+
/// </summary>
11+
public class ByteArrayPacketWriter : INetworkWriter
12+
{
13+
private readonly List<byte> buffer = [];
14+
15+
private int position;
16+
17+
public int Length => buffer.Count;
18+
19+
public int Position => position;
20+
21+
public byte[] ToArray()
22+
{
23+
return buffer.ToArray();
24+
}
25+
26+
public void WriteBool(bool value)
27+
{
28+
position++;
29+
30+
buffer.Add((byte)(value ? 1 : 0));
31+
}
32+
33+
public void WriteByte(byte value)
34+
{
35+
position++;
36+
37+
buffer.Add(value);
38+
}
39+
40+
public void WriteBytes(byte[] value)
41+
{
42+
WriteUInt32((ushort)value.Length);
43+
44+
position += value.Length;
45+
46+
buffer.AddRange(value);
47+
}
48+
49+
public void WriteBytesAndSize(byte[] value, int length)
50+
{
51+
position += length;
52+
53+
buffer.AddRange(value.Take(length));
54+
}
55+
56+
public void WriteChar(char value)
57+
{
58+
var t = Encoding.UTF8.GetBytes(new char[] { value });
59+
60+
WriteUInt16((ushort)t.Length);
61+
62+
position += t.Length;
63+
64+
buffer.AddRange(t);
65+
}
66+
67+
public void WriteDouble(double value)
68+
{
69+
var bytes = BitConverter.GetBytes(value);
70+
71+
position += bytes.Length;
72+
73+
buffer.AddRange(bytes);
74+
}
75+
76+
public void WriteFloat(float value)
77+
{
78+
var bytes = BitConverter.GetBytes(value);
79+
80+
position += bytes.Length;
81+
82+
buffer.AddRange(bytes);
83+
}
84+
85+
public void WriteInt16(short value)
86+
{
87+
var bytes = BitConverter.GetBytes(value);
88+
89+
position += bytes.Length;
90+
91+
buffer.AddRange(bytes);
92+
}
93+
94+
public void WriteInt32(int value)
95+
{
96+
var bytes = BitConverter.GetBytes(value);
97+
98+
position += bytes.Length;
99+
100+
buffer.AddRange(bytes);
101+
}
102+
103+
public void WriteInt64(long value)
104+
{
105+
var bytes = BitConverter.GetBytes(value);
106+
107+
position += bytes.Length;
108+
109+
buffer.AddRange(bytes);
110+
}
111+
112+
public void WriteSByte(sbyte value)
113+
{
114+
position++;
115+
116+
buffer.Add((byte)value);
117+
}
118+
119+
public void WriteString(string value)
120+
{
121+
if(value == null)
122+
{
123+
WriteUInt16(0);
124+
125+
return;
126+
}
127+
128+
var t = Encoding.UTF8.GetBytes(value);
129+
130+
WriteUInt16((ushort)t.Length);
131+
132+
WriteBytesAndSize(t, t.Length);
133+
}
134+
135+
public void WriteUInt16(ushort value)
136+
{
137+
var bytes = BitConverter.GetBytes(value);
138+
139+
position += bytes.Length;
140+
141+
buffer.AddRange(bytes);
142+
}
143+
144+
public void WriteUInt32(uint value)
145+
{
146+
var bytes = BitConverter.GetBytes(value);
147+
148+
position += bytes.Length;
149+
150+
buffer.AddRange(bytes);
151+
}
152+
153+
public void WriteUInt64(ulong value)
154+
{
155+
var bytes = BitConverter.GetBytes(value);
156+
157+
position += bytes.Length;
158+
159+
buffer.AddRange(bytes);
160+
}
161+
}

0 commit comments

Comments
 (0)