forked from devcat-studio/VSCodeLuaDebug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGiderosMessageToSend.cs
More file actions
75 lines (64 loc) · 1.84 KB
/
Copy pathGiderosMessageToSend.cs
File metadata and controls
75 lines (64 loc) · 1.84 KB
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
using System.IO;
using System.Net.Sockets;
using System.Text;
namespace GiderosPlayerRemote
{
class GiderosMessageToSend
{
int seqId;
MemoryStream bodyStream;
BinaryWriter bodyWriter;
NetworkStream netStream;
public GiderosMessageToSend(int seqId, NetworkStream netStream)
{
this.seqId = seqId;
this.netStream = netStream;
this.bodyStream = new MemoryStream();
this.bodyWriter = new BinaryWriter(bodyStream);
}
public void Send()
{
var bytes = ToBytes();
netStream.Write(bytes, 0, bytes.Length);
}
byte[] ToBytes()
{
using (var stream = new MemoryStream())
using (var writer = new BinaryWriter(stream))
{
byte[] payload = bodyStream.ToArray();
writer.Write((int)payload.Length + 12);
writer.Write(seqId);
writer.Write((int)0); // 0=msg, 1=ack
writer.Write(payload);
return stream.ToArray();
}
}
public GiderosMessageToSend AppendByte(byte by)
{
bodyWriter.Write(by);
return this;
}
public GiderosMessageToSend AppendInt(int n)
{
bodyWriter.Write(n);
return this;
}
public GiderosMessageToSend AppendFloat(float n)
{
bodyWriter.Write(n);
return this;
}
public GiderosMessageToSend AppendString(string s)
{
bodyWriter.Write(Encoding.UTF8.GetBytes(s));
bodyWriter.Write((byte)0);
return this;
}
public GiderosMessageToSend AppendByteArray(byte[] ba)
{
bodyWriter.Write(ba);
return this;
}
}
}