/
PacketOut.cs
69 lines (58 loc) · 1.62 KB
/
PacketOut.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
using System;
using System.IO;
using System.Text;
namespace Emulator
{
/// <summary>
/// Send packages to the connected client
/// </summary>
public class PacketOut
{
/// <summary>
/// Packet Key
/// </summary>
protected MemoryStream stream;
protected BinaryWriter writer;
private ushort _length;
private byte _type;
private bool _compiled = false;
private uint _key = 56;
public uint Key {
get { return _key; }
set { _key = value; }
}
public MemoryStream Stream {
get { return stream; }
}
public PacketOut(byte type,ushort length)
{
_type = type;
_length = (ushort)(length + 3);
stream = new MemoryStream(_length);
writer = new BinaryWriter(stream);
writer.Write(_length);
writer.Write(_type);
}
public PacketOut(byte type)
{
_type = type;
}
public void SetCapacity(ushort newCapacity)
{
_length = (ushort)(newCapacity + 3);
stream = new MemoryStream(_length);
writer = new BinaryWriter(stream);
writer.Write(_length);
writer.Write(_type);
}
public byte[] Compile()
{
if(!_compiled) {
stream.Position = 0;
writer.Write(Crypter.EncodeString(stream.ToArray(),_key));
_compiled = true;
}
return stream.ToArray();
}
}
}