-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProtocol.cs
222 lines (160 loc) · 6.44 KB
/
Protocol.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using MineLib.Core;
using MineLib.Core.IO;
using ProtocolTrueCraft.IO;
using ProtocolTrueCraft.Packets;
namespace ProtocolTrueCraft
{
public sealed partial class Protocol : IProtocol
{
#region Properties
public string Name { get { return "TrueCraft"; } }
public string Version { get { return "b1.7.3"; } }
public ConnectionState State { get; set; }
public bool Connected { get { return _stream != null && _stream.Connected; } }
public bool UseLogin { get { return _minecraft.UseLogin; } }
// -- Debugging
public bool SavePackets { get; private set; }
public List<IPacket> PacketsReceived { get; private set; }
public List<IPacket> PacketsSended { get; private set; }
public List<IPacket> LastPackets { get { return PacketsReceived != null ? PacketsReceived.GetRange(PacketsReceived.Count - 50, 50) : null; } }
public IPacket LastPacket { get { return PacketsReceived[PacketsReceived.Count - 1]; } }
// -- Debugging
#endregion
private Task _readTask;
private IMinecraftClient _minecraft;
private IProtocolStream _stream;
public IProtocol Initialize(IMinecraftClient client, INetworkTCP tcp, bool debugPackets = false)
{
_minecraft = client;
_stream = new TrueCraftStream(tcp);
SavePackets = debugPackets;
PacketsReceived = new List<IPacket>();
PacketsSended = new List<IPacket>();
SendingAsyncHandlers = new Dictionary<Type, Func<ISendingAsyncArgs, Task>>();
RegisterSupportedSendings();
return this;
}
private async void ReadCycle()
{
while (PacketReceiver())
await Task.Delay(50);
}
private bool PacketReceiver()
{
if (!Connected)
return false; // -- Terminate cycle
if (_stream.Available)
{
var packetId = _stream.ReadByte();
HandlePacket(packetId, _stream);
}
return true;
}
/// <summary>
/// Packets are handled here. Compression and encryption are handled here too
/// </summary>
/// <param name="id">Packet ID</param>
/// <param name="stream"></param>
private void HandlePacket(int id, IProtocolStream stream)
{
using (var reader = new TrueCraftDataReader(stream as Stream))
{
IPacket packet = null;
switch (State)
{
#region Login
case ConnectionState.Joining:
if (ServerResponse.JoiningServer[id] == null)
throw new ProtocolException("Reading error: Wrong Login packet ID.");
packet = ServerResponse.JoiningServer[id]().ReadPacket(reader);
OnPacketHandled(id, packet, ConnectionState.Joining);
break;
#endregion Login
#region Play
case ConnectionState.Joined:
if (ServerResponse.JoinedServer[id] == null)
throw new ProtocolException("Reading error: Wrong Play packet ID.");
packet = ServerResponse.JoinedServer[id]().ReadPacket(reader);
OnPacketHandled(id, packet, ConnectionState.Joined);
break;
#endregion Play
}
if (SavePackets)
PacketsReceived.Add(packet);
}
}
#region Network
public void Connect(string ip, ushort port)
{
if (Connected)
throw new ProtocolException("Connection error: Already connected to server.");
// -- Connect to server.
_stream.Connect(ip, port);
// -- Begin data reading.
if (_readTask != null && _readTask.Status == TaskStatus.Running)
throw new ProtocolException("Connection error: Task already running.");
else
_readTask = Task.Factory.StartNew(ReadCycle);
}
public void Disconnect()
{
if (!Connected)
throw new ProtocolException("Connection error: Not connected to server.");
_stream.Disconnect(false);
}
public void SendPacket(IPacket packet)
{
if (!Connected)
throw new ProtocolException("Connection error: Not connected to server.");
_stream.SendPacket(ref packet);
if (SavePackets)
PacketsSended.Add(packet);
}
public void SendPacket(ref IPacket packet)
{
if (!Connected)
throw new ProtocolException("Connection error: Not connected to server.");
_stream.SendPacket(ref packet);
if (SavePackets)
PacketsSended.Add(packet);
}
public async Task ConnectAsync(string ip, ushort port)
{
if (Connected)
throw new ProtocolException("Connection error: Already connected to server.");
await _stream.ConnectAsync(ip, port);
if (_readTask != null &&_readTask.Status == TaskStatus.Running)
throw new ProtocolException("Connection error: Task already running.");
else
_readTask = Task.Factory.StartNew(ReadCycle);
}
public bool DisconnectAsync()
{
if (!Connected)
throw new ProtocolException("Connection error: Not connected to server.");
return _stream.DisconnectAsync(false);
}
public async Task SendPacketAsync(IPacket packet)
{
if (!Connected)
throw new ProtocolException("Connection error: Not connected to server.");
await _stream.SendPacketAsync(packet);
if (SavePackets)
PacketsSended.Add(packet);
}
#endregion
public void Dispose()
{
if (_stream != null)
_stream.Dispose();
if (PacketsReceived != null)
PacketsReceived.Clear();
if (PacketsSended != null)
PacketsSended.Clear();
}
}
}