This repository has been archived by the owner on Nov 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatusClient.cs
150 lines (109 loc) · 4.4 KB
/
StatusClient.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
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Aragas.Core.Packets;
using Aragas.Core.Wrappers;
using MineLib.Core.Data;
using MineLib.Core.Events;
using MineLib.Core.Interfaces;
using MineLib.Core.IO;
using MineLib.PacketBuilder.Client.Status;
using MineLib.PacketBuilder.Server.Handshaking;
using MineLib.PacketBuilder.Server.Status;
using Newtonsoft.Json;
using ProtocolModern.Enum;
namespace ProtocolModern
{
public sealed class Handshake : SendingEvent { }
public sealed class HandshakeArgs : SendingEventArgs
{
public string IP { get; }
public ushort Port { get; }
public int ProtocolVersion { get; }
public HandshakeArgs(string ip, ushort port, int protocolVersion)
{
IP = ip;
Port = port;
ProtocolVersion = protocolVersion;
}
}
public sealed class SendRequest : SendingEvent { }
public sealed class SendRequestArgs : SendingEventArgs { }
public sealed class StatusClient : IStatusClient
{
private event Action<ProtobufPacket> OnResponsePacket;
public ResponseData GetResponseData(string ip, ushort port, int protocolVersion)
{
var responseData = new ResponseData();
var response = false;
var protocol = new Protocol(null, true);
protocol.Connect(ip, port);
protocol.RegisterReceiving(typeof(ResponsePacket), SendResponse);
OnResponsePacket += packet => { responseData.Info = ParseResponse(packet); response = true; };
protocol.RegisterSending(typeof(Handshake), Handshake);
protocol.RegisterSending(typeof(SendRequest), SendRequest);
protocol.DoSending(typeof(Handshake), new HandshakeArgs(ip, port, protocolVersion));
protocol.DoSending(typeof(SendRequest), new SendRequestArgs());
var watch = Stopwatch.StartNew();
while (!response) { Task.Delay(100).Wait(); if(watch.ElapsedMilliseconds > 2000) { responseData.Ping = long.MaxValue; break;} }
protocol.Disconnect();
protocol.Dispose();
if(response)
responseData.Ping = PingServer(ip, port);
return responseData;
}
public ServerInfo GetServerInfo(string ip, ushort port, int protocolVersion)
{
var serverInfo = new ServerInfo();
var response = false;
var protocol = new Protocol(null, true);
protocol.Connect(ip, port);
protocol.RegisterSending(typeof(SendRequest), SendRequest);
protocol.DoSending(typeof(SendRequest), new SendRequestArgs());
protocol.RegisterReceiving(typeof(ResponsePacket), SendResponse);
OnResponsePacket += packet => { serverInfo = ParseResponse(packet); response = true; };
while (!response) { Task.Delay(100).Wait(); }
protocol.Disconnect();
protocol.Dispose();
return serverInfo;
}
public long GetPing(string ip, ushort port)
{
return PingServer(ip, port);
}
private static void Handshake(SendingEventArgs args)
{
var data = (HandshakeArgs) args;
args.SendPacket?.Invoke(new HandshakePacket
{
ServerAddress = data.IP,
ServerPort = data.Port,
ProtocolVersion = data.ProtocolVersion,
NextState = (int) NextState.Status
});
}
private static void SendRequest(SendingEventArgs args)
{
var data = (SendRequestArgs) args;
args.SendPacket?.Invoke(new RequestPacket());
}
private async Task SendResponse(ProtobufPacket packet)
{
OnResponsePacket?.Invoke(packet);
}
private static ServerInfo ParseResponse(ProtobufPacket packet)
{
var response = (ResponsePacket) packet;
return JsonConvert.DeserializeObject<ServerInfo>(response.JSONResponse, new Base64JsonConverter());
}
private static long PingServer(string host, ushort port)
{
var watch = Stopwatch.StartNew();
var client = TCPClientWrapper.CreateTCPClient();
client.Connect(host, port);
client.Disconnect();
watch.Stop();
return watch.ElapsedMilliseconds;
}
}
}