-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.cs
More file actions
285 lines (230 loc) · 11.3 KB
/
Client.cs
File metadata and controls
285 lines (230 loc) · 11.3 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
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
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using AniDBCore.Commands;
using AniDBCore.Commands.Auth;
using AniDBCore.Commands.Misc;
using AniDBCore.Events;
using AniDBCore.Utils;
namespace AniDBCore {
internal static class Client {
private const int LocalPort = 4257;
public static bool Cache;
public static bool Connected { get; private set; }
private static readonly Dictionary<CommandTag, ICommandResult> CommandsWithResponse =
new Dictionary<CommandTag, ICommandResult>();
private static readonly Dictionary<CommandTag, Command> CommandsWaitingForResponse =
new Dictionary<CommandTag, Command>();
private static readonly Queue<Command> CommandQueue = new Queue<Command>();
private static readonly Session Session;
private static int _timeOutsSinceLastResponse;
private static CommandTag _keepAliveTag;
private static UdpClient _connection;
private static bool _rateLimited;
private static object rlLock = new object();
private static void ReceiverLoop() {
IPEndPoint any = new IPEndPoint(IPAddress.Any, LocalPort);
// Always receive data
while (Connected) {
// TODO we'll have to change this when we start handling things like notifications.
// Current thoughts is to have the `WaitForResponse` method determine if it's a response or a notification
// then handle it appropriately
APIResponse response = WaitForResponse(ref any);
Command command = ParseResponse(response);
if (command == null)
continue;
ICommandResult result = PreReturnResult(response, command);
CommandsWithResponse.Add(command.Tag, result);
}
// Clean up any loose commands that never got a reply
foreach (KeyValuePair<CommandTag, Command> kvp in CommandsWaitingForResponse) {
ICommandResult result =
Activator.CreateInstance(kvp.Value.ResultType, ReturnCode.ConnectionClosed) as ICommandResult;
CommandsWithResponse.Add(kvp.Key, result);
}
}
private static APIResponse WaitForResponse(ref IPEndPoint any) {
// this should probably get renamed since a notification isn't a response to a request, but we don't handle that yet
byte[] rawData = _connection.Receive(ref any);
string dataString = rawData.DecodeBytesToContent();
_rateLimited = false;
//TEMP
Console.WriteLine($"{DateTime.Now:hh:mm:ss} - Got data: {dataString}");
return new APIResponse(dataString);
}
private static Command ParseResponse(APIResponse response) {
// Make sure there was a tag on the response
if (response.Tag == null) {
Console.WriteLine($"No tag ({response})");
return null;
}
// Find command using tag
Command command = CommandsWaitingForResponse[response.Tag];
// Don't need to proceed if this is the response to our KeepAlive ping
if (response.Tag == _keepAliveTag) {
// TODO do NAT stuff here instead of where we sent this ping, since we have the port the API sees
AniDB.InvokeKeepAlivePong();
return null;
}
if (command != null)
return command;
// We should always have a command here, since we set a tag on every command we send
// Raise an error saying we're missing a command
AniDB.InvokeClientError(
new ClientErrorArgs($"Command is null; tag is probably not set on reply\nData: {response}")
);
return null;
}
private static ICommandResult PreReturnResult(APIResponse response, Command command) {
// Handle all cases where we need to handle something in this class, otherwise just return a response to the queue
switch (response.ReturnCode) {
case ReturnCode.LoginAccepted:
case ReturnCode.LoginAcceptedNewVersion: {
Session.StartSession(response.Data[0]);
return new AuthResult(response.ReturnCode);
}
case ReturnCode.EncryptionEnabled: {
string salt = response.Data[0];
Session.EnableEncryption(StaticUtils.MD5Hash(Session.ApiKey + salt));
return new EncryptResult(response.ReturnCode);
}
case ReturnCode.ServerBusy:
case ReturnCode.OutOfService: {
// TODO make WriterLoop() wait until API is back (should we delay different amounts of time for Busy vs Out of Service?)
return new CommandResult(response.ReturnCode);
}
case ReturnCode.Timeout: {
// TODO add the command back to the queue to be sent again
return new CommandResult(response.ReturnCode);
}
default:
return Activator.CreateInstance(command.ResultType, response.ReturnCode, response.Data[0]) as
ICommandResult;
}
}
private static void WriterLoop() {
// local bool so we don't lock for 2 seconds. If the value changes between the time we lock, and the time we start sleeping, oh well, it won't break anything, just delay it.
bool wait = false;
DateTime lastCommandSentTime = DateTime.MinValue;
while (Connected) {
lock (rlLock)
if (_rateLimited)
wait = true;
if (wait) {
// TODO exponential backoff
Thread.Sleep(2000); // sleep and hope we aren't rate limited after we're done
continue;
}
// Send any queued commands, otherwise keep connection alive (ping if last command was sent more than 5m ago)
if (CommandQueue.Count != 0) {
SendCommand(CommandQueue.Dequeue());
lastCommandSentTime = DateTime.Now;
} else
KeepConnectionAlive(lastCommandSentTime);
// Sleep for 2.1 seconds just to make sure API don't get mad (limit is 1 per 2 seconds)
Thread.Sleep(2100); // TODO this needs to wait longer when we receive a response code that tells us to
}
}
private static ICommandResult WaitForResponse(CommandTag tag) {
int waitTime = 0;
while (CommandsWithResponse.ContainsKey(tag) == false) {
if (waitTime > 20_000) {
// Note: I *think* that this should fix itself it it encounters a race condition.
lock (rlLock) {
_timeOutsSinceLastResponse++;
if (_timeOutsSinceLastResponse > 5)
// we're probably rate limited
_rateLimited = true;
}
return new CommandResult(ReturnCode.RequestTimedOut);
}
Thread.Sleep(50);
waitTime += 50;
}
lock (rlLock) {
_timeOutsSinceLastResponse = 0;
}
AniDB.InvokeCommandResultReceived(new CommandResultReceivedArgs());
tag.Release();
return CommandsWithResponse[tag];
}
private static void KeepConnectionAlive(DateTime lastCommandSentTime) {
// Check if last command we sent was more than 5 minutes ago so we can keep connection alive (for NAT)
if (lastCommandSentTime < DateTime.Now.AddMinutes(-5) && lastCommandSentTime != DateTime.MinValue) {
PingCommand pingCommand = new PingCommand();
bool parameterSet = pingCommand.SetOptionalParameter("nat", "1", out string error);
if (parameterSet == false) {
AniDB.InvokeClientError(new ClientErrorArgs($"Setting parameter failed ({error})"));
return;
}
_keepAliveTag = pingCommand.Tag;
Task<ICommandResult> _ = pingCommand.Send();
AniDB.InvokeKeepAlivePing();
}
Thread.Sleep(150); // small timeout
}
private static void SendCommand(Command command) {
if (ShouldSendCommand(command) == false)
return;
string commandString = $"{command.CommandBase} {command.GetParameters()}".Trim();
if (command.RequiresSession)
commandString += $"&{Session.SessionKey}";
// TEMP
Console.WriteLine("Sending data: " + commandString);
AniDB.InvokeCommandSent(new CommandSentArgs(command));
byte[] bytes = Encoding.ASCII.GetBytes(commandString);
_connection.Send(bytes, bytes.Length);
CommandsWaitingForResponse.Add(command.Tag, command);
}
private static bool ShouldSendCommand(Command command) {
// Don't login again
if (Session.LoggedIn && command.CommandBase == "AUTH")
return false;
// Don't encrypt again
if (Session.EncryptionEnabled && command.CommandBase == "ENCRYPT")
return false;
// Don't set connection to be encrypted when api key is not set
if (string.IsNullOrEmpty(Session.ApiKey) && command.CommandBase == "ENCRYPT")
return false;
// Don't encrypt if a session is already in progress (maybe close the current session and start a new one?)
if (Session.LoggedIn && command.CommandBase == "ENCRYPT")
return false;
// Don't logout if not logged in
if (Session.LoggedIn == false && command.CommandBase == "LOGOUT")
return false;
return true;
}
public static bool Connect(string host, int port) {
if (Connected)
return false;
_connection = new UdpClient(new IPEndPoint(IPAddress.Any, LocalPort));
_connection.Connect(host, port);
Connected = true;
Task.Run(ReceiverLoop);
Task.Run(WriterLoop);
return true;
}
public static void Disconnect() {
if (Connected == false)
return;
// Logout first
SendCommand(new LogoutCommand());
Session.EndSession();
_connection.Close();
Connected = false;
// Tasks should clean themselves up since we're only doing while Connected
}
public static Task<ICommandResult> QueueCommand(Command command) {
if (Connected == false)
AniDB.InvokeClientError(
new ClientErrorArgs(
$"Connection must be initialized before queueing commands! {command.CommandBase}"));
CommandQueue.Enqueue(command);
return Task.Run(() => WaitForResponse(command.Tag));
}
}
}