-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCONClient.cs
306 lines (244 loc) · 9.61 KB
/
SCONClient.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
using System;
using System.Collections.Generic;
using Aragas.Network.IO;
using Aragas.Network.Data;
using Aragas.Network.Packets;
using PCLExt.Network;
using PokeD.Core;
using PokeD.Core.Packets.SCON;
using PokeD.Core.Packets.SCON.Authorization;
using PokeD.Core.Packets.SCON.Chat;
using PokeD.Core.Packets.SCON.Logs;
using PokeD.Core.Packets.SCON.Status;
using PokeD.SCON.Extensions;
using PokeD.SCON.UILibrary;
namespace PokeD.SCON
{
public partial class SCONClient : IUpdatable, IDisposable
{
BasicUIViewModel BasicUIVM { get; }
ISocketClient Client { get; }
ProtobufStream Stream { get; }
PasswordStorage Password { get; set; }
#if DEBUG
// -- Debug -- //
List<ProtobufPacket> Received { get; } = new List<ProtobufPacket>();
List<ProtobufPacket> Sended { get; } = new List<ProtobufPacket>();
public object FileStorageExtension { get; private set; }
// -- Debug -- //
#endif
public SCONClient(BasicUIViewModel basicUIvm)
{
BasicUIVM = basicUIvm;
BasicUIVM.OnConnect += BasicUIViewModel_OnConnect;
BasicUIVM.OnDisconnect += BasicUIViewModel_OnDisconnect;
BasicUIVM.TabChanged += BasicUIViewModel_TabChanged;
BasicUIVM.OnGetLog += BasicUIViewModel_GetLog;
BasicUIVM.OnGetCrashLog += BasicUIViewModel_GetCrashLog;
BasicUIVM.OnSaveLog += BasicUIViewModel_SaveLog;
BasicUIVM.OnChatStateChanged += BasicUIViewModel_OnChatStateChanged;
Client = SocketClient.CreateTCP();
Stream = new ProtobufStream(Client);
}
private bool BasicUIViewModel_OnConnect(string ip, ushort port, string password, bool autoReconnect)
{
try
{
Password = new PasswordStorage(password);
if (Stream.IsConnected)
Disconnect();
Stream.Connect(ip, port);
SendPacket(new AuthorizationRequestPacket());
return true;
}
catch (Exception) { return false; }
}
private bool BasicUIViewModel_OnDisconnect()
{
try
{
if (Stream.IsConnected)
Disconnect();
return true;
}
catch (Exception) { return false; }
}
private void BasicUIViewModel_TabChanged(string tabName)
{
if(!Stream.IsConnected)
return;
switch (tabName)
{
case "Players":
SendPacket(new PlayerInfoListRequestPacket());
break;
case "Bans":
SendPacket(new BanListRequestPacket());
break;
case "Player Database":
SendPacket(new PlayerDatabaseListRequestPacket());
break;
case "Logs":
SendPacket(new LogListRequestPacket());
break;
case "Crash Logs":
SendPacket(new CrashLogListRequestPacket());
break;
case "Settings":
break;
}
}
private void BasicUIViewModel_GetLog(int index)
{
if (!Stream.IsConnected)
DisplayMessage("Not connected to server!");
else
SendPacket(new LogFileRequestPacket { LogFilename = BasicUIVM.LogsGridDataList[index].LogFilename });
}
private void BasicUIViewModel_GetCrashLog(int index)
{
if (!Stream.IsConnected)
DisplayMessage("Not connected to server!");
else
SendPacket(new CrashLogFileRequestPacket { CrashLogFilename = BasicUIVM.CrashLogsGridDataList[index].LogFilename });
}
private void BasicUIViewModel_SaveLog(string logname, string log)
{
FileStorageExtensions.SaveLog(logname, log);
}
private void BasicUIViewModel_OnChatStateChanged(bool value)
{
if (value)
SendPacket(new StartChatReceivingPacket());
else
SendPacket(new StopChatReceivingPacket());
}
public void Update()
{
if (Stream.IsConnected && Stream.DataAvailable > 0)
{
var dataLength = Stream.ReadVarInt();
if (dataLength > 0)
{
var data = Stream.Receive(dataLength);
HandleData(data);
}
else
{
Logger.Log(LogType.Error, $"Protobuf Reading Error: Packet Length size is 0. Disconnecting.");
SendPacket(new AuthorizationDisconnectPacket { Reason = "Packet Length size is 0!" });
Dispose();
}
}
}
private void HandleData(byte[] data)
{
if (data != null)
{
using (var reader = new ProtobufDataReader(data))
{
var id = reader.Read<VarInt>();
Func<SCONPacket> func;
if (SCONPacketResponses.TryGetPacketFunc(id, out func))
{
if (func != null)
{
var packet = func().ReadPacket(reader);
if (packet != null)
{
HandlePacket(packet);
#if DEBUG
Received.Add(packet);
#endif
}
else
{
Logger.Log(LogType.Error, $"SCON Reading Error: packet is null. Packet ID {id}");
SendPacket(new AuthorizationDisconnectPacket { Reason = $"Packet ID {id} is not correct!" });
Dispose();
}
}
else
{
Logger.Log(LogType.Error, $"SCON Reading Error: Packet ID {id} is not correct, Packet Data: {data}. Disconnecting.");
SendPacket(new AuthorizationDisconnectPacket {Reason = $"Packet ID {id} is not correct!"});
Dispose();
}
}
else
{
Logger.Log(LogType.Error, $"SCON Reading Error: Packet ID {id} is not correct, Packet Data: {data}. Disconnecting.");
SendPacket(new AuthorizationDisconnectPacket {Reason = $"Packet ID {id} is not correct!"});
Dispose();
}
}
}
else
Logger.Log(LogType.Error, $"SCON Reading Error: Packet Data is null.");
}
private void HandlePacket(ProtobufPacket packet)
{
switch ((SCONPacketTypes)(int) packet.ID)
{
case SCONPacketTypes.AuthorizationResponse:
HandleAuthorizationResponse((AuthorizationResponsePacket) packet);
break;
case SCONPacketTypes.EncryptionRequest:
HandleEncryptionRequest((EncryptionRequestPacket) packet);
break;
case SCONPacketTypes.AuthorizationComplete:
HandleAuthorizationComplete((AuthorizationCompletePacket) packet);
break;
case SCONPacketTypes.AuthorizationDisconnect:
HandleAuthorizationDisconnect((AuthorizationDisconnectPacket) packet);
break;
case SCONPacketTypes.ChatMessage:
HandleChatMessage((ChatMessagePacket) packet);
break;
case SCONPacketTypes.PlayerInfoListResponse:
HandlePlayerInfoListResponse((PlayerInfoListResponsePacket) packet);
break;
case SCONPacketTypes.LogListResponse:
HandleLogListResponse((LogListResponsePacket) packet);
break;
case SCONPacketTypes.LogFileResponse:
HandleLogFileResponse((LogFileResponsePacket) packet);
break;
case SCONPacketTypes.CrashLogListResponse:
HandleCrashLogListResponse((CrashLogListResponsePacket) packet);
break;
case SCONPacketTypes.CrashLogFileResponse:
HandleCrashLogFileResponse((CrashLogFileResponsePacket) packet);
break;
case SCONPacketTypes.PlayerDatabaseListResponse:
HandlePlayerDatabaseListResponse((PlayerDatabaseListResponsePacket) packet);
break;
case SCONPacketTypes.BanListResponse:
HandleBanListResponse((BanListResponsePacket) packet);
break;
}
}
private void SendPacket(SCONPacket packet)
{
if (Stream.IsConnected)
{
Stream.SendPacket(packet);
#if DEBUG
Sended.Add(packet);
#endif
}
}
private void DisplayMessage(string message)
{
BasicUIVM.DisplayMessage(message);
}
private void Disconnect()
{
Stream.Disconnect();
}
public void Dispose()
{
BasicUIVM.Dispose();
}
}
}