-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAniDB.cs
More file actions
87 lines (70 loc) · 3.18 KB
/
AniDB.cs
File metadata and controls
87 lines (70 loc) · 3.18 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
using System;
using System.Threading.Tasks;
using AniDBCore.Commands;
using AniDBCore.Commands.Auth;
using AniDBCore.Commands.Misc;
using AniDBCore.Events;
using AniDBCore.Utils;
namespace AniDBCore {
public static class AniDB {
public const string ClientName = "AniDBCore";
public const string ClientVersion = "1";
public static event EventHandler<ClientErrorArgs> ClientError;
public static event EventHandler<ServerErrorArgs> ServerError;
public static event EventHandler<CommandSentArgs> CommandSent;
public static event EventHandler<CommandResultReceivedArgs> CommandResultReceived;
public static event EventHandler KeepAlivePing;
public static event EventHandler KeepAlivePong;
public static bool Connect(string host, int port, bool cache = true) {
bool clientConnected = Client.Connect(host, port);
if (clientConnected == false)
return false;
Client.Cache = cache;
PingCommand command = new PingCommand();
bool setParameter = command.SetOptionalParameter("nat", "1", out string error);
if (setParameter == false)
throw new Exception($"Failed to set parameter ({error})");
Task<ICommandResult> result = command.Send();
result.Wait(2500);
return result.Result.ReturnCode == ReturnCode.Pong;
}
public static void Disconnect() {
Client.Disconnect();
}
public static async Task<ICommandResult> SendCommand(ICommand command) {
return await command.Send();
}
public static async Task<ICommandResult> Auth(string username, string password, bool encryption = false,
string apiKey = "") {
if (encryption) {
Session.SetApiKey(apiKey);
ICommandResult result = await SendCommand(new EncryptCommand(username));
if (result.ReturnCode != ReturnCode.EncryptionEnabled)
return result;
}
return await SendCommand(new AuthCommand(username, password));
}
public static async Task<ICommandResult> Logout() {
return await SendCommand(new LogoutCommand());
}
// Dunno if sender should really be null for the event invocations, but I can't use this 'cause static class, so....
internal static void InvokeClientError(ClientErrorArgs args) {
ClientError?.Invoke(null, args);
}
internal static void InvokeServerError(ServerErrorArgs args) {
ServerError?.Invoke(null, args);
}
internal static void InvokeCommandSent(CommandSentArgs args) {
CommandSent?.Invoke(null, args);
}
internal static void InvokeCommandResultReceived(CommandResultReceivedArgs args) {
CommandResultReceived?.Invoke(null, args);
}
internal static void InvokeKeepAlivePing() {
KeepAlivePing?.Invoke(null, EventArgs.Empty);
}
internal static void InvokeKeepAlivePong() {
KeepAlivePong?.Invoke(null, EventArgs.Empty);
}
}
}