-
Notifications
You must be signed in to change notification settings - Fork 0
/
Protocol.AsyncSending.cs
74 lines (56 loc) · 2.46 KB
/
Protocol.AsyncSending.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using MineLib.Core;
using ProtocolTrueCraft.Packets;
using ProtocolTrueCraft.Packets.Client.Login;
namespace ProtocolTrueCraft
{
public sealed partial class Protocol
{
private Dictionary<Type, Func<ISendingAsyncArgs, Task>> SendingAsyncHandlers { get; set; }
public void RegisterSending(Type sendingAsyncType, Func<ISendingAsyncArgs, Task> func)
{
var any = sendingAsyncType.GetTypeInfo().ImplementedInterfaces.Any(p => p == typeof(ISendingAsync));
if (!any)
throw new InvalidOperationException("AsyncSending type must implement MineLib.Network.IAsyncSending");
SendingAsyncHandlers[sendingAsyncType] = func;
}
private void RegisterSupportedSendings()
{
RegisterSending(typeof(ConnectToServerAsync), ConnectToServerAsync);
RegisterSending(typeof(KeepAliveAsync), KeepAliveAsync);
}
public Task DoSendingAsync(Type sendingAsyncType, ISendingAsyncArgs args)
{
var any = sendingAsyncType.GetTypeInfo().ImplementedInterfaces.Any(p => p == typeof(ISendingAsync));
if (!any)
throw new InvalidOperationException("AsyncSending type must implement MineLib.Network.IAsyncSending");
return SendingAsyncHandlers[sendingAsyncType](args);
}
public void DoSending(Type sendingType, ISendingAsyncArgs args)
{
var any = sendingType.GetTypeInfo().ImplementedInterfaces.Any(p => p == typeof(ISendingAsync));
if (!any)
throw new InvalidOperationException("AsyncSending type must implement MineLib.Network.IAsyncSending");
SendingAsyncHandlers[sendingType](args).Wait();
}
private async Task ConnectToServerAsync(ISendingAsyncArgs args)
{
var data = (ConnectToServerAsyncArgs) args;
State = ConnectionState.Joining;
await SendPacketAsync(new HandshakePacket
{
Username = _minecraft.ClientUsername
});
await SendPacketAsync(new LoginRequestPacket { ProtocolVersion = 14, Username = "-" });
}
private Task KeepAliveAsync(ISendingAsyncArgs args)
{
var data = (KeepAliveAsyncArgs) args;
return SendPacketAsync(new KeepAlivePacket());
}
}
}