-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitConnection.cs
156 lines (130 loc) · 4.78 KB
/
UnitConnection.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
using System.Diagnostics;
using System.Net.Sockets;
using System.Text;
namespace ScreenLogicConnect;
public class UnitConnection : IDisposable
{
TcpClient? client;
public async Task<bool> ConnectTo(EasyTouchUnit unit, string? password = null)
{
if (unit?.IPAddress == null)
{
return false;
}
if (client != null)
{
client.Dispose();
}
client = new TcpClient();
await client.ConnectAsync(unit.IPAddress, unit.Port);
var stream = client.GetStream();
stream.Write(ConnectionMessageBytes);
Debug.WriteLine("sending challenge string");
stream.SendHLMessage(new Messages.ChallengeString());
var recvBuf = new byte[8];
var readBytes = await stream.ReadAsync(recvBuf);
Debug.WriteLine("read {0} bytes (header)", readBytes);
var recvBody = new byte[Messages.HLMessage.ExtractDataSize(recvBuf)];
if (recvBody.Length == 0)
{
return false;
}
readBytes = await stream.ReadAsync(recvBody);
Debug.WriteLine("read {0} bytes (body)", readBytes);
string challengeStr = Messages.HLMessageTypeHelper.ExtractString(recvBody);
Debug.WriteLine("sending login message");
stream.SendHLMessage(CreateLoginMessage(new HLEncoder(password).GetEncryptedPassword(challengeStr)));
readBytes = await stream.ReadAsync(recvBuf);
Debug.WriteLine("read {0}", readBytes);
recvBody = new byte[Messages.HLMessage.ExtractDataSize(recvBuf)];
if (recvBody.Length > 0)
{
await stream.ReadAsync(recvBody);
}
return recvBuf[2] == Messages.ClientLogin.LoginAnswerId;
}
public async Task<Messages.GetPoolStatus?> GetPoolStatus(short senderId = 0) => await GetResponse<Messages.GetPoolStatus>(senderId);
public async Task<Messages.GetControllerConfig?> GetControllerConfig(short senderId = 0) => await GetResponse<Messages.GetControllerConfig>(senderId);
public async Task<Messages.GetMode?> GetMode(short senderId = 0) => await GetResponse<Messages.GetMode>(senderId);
private async Task<T?> GetResponse<T>(short senderId) where T : Messages.HLMessage, new()
{
if (client == null)
{
return default;
}
Debug.WriteLine("sending status message");
T msg = new();
msg.Encode(senderId);
client.GetStream().SendHLMessage(msg);
return await BuildMessageFromStream<T>(client.GetStream());
}
internal static async Task<T?> BuildMessageFromStream<T>(NetworkStream ns) where T : Messages.HLMessage, new()
{
int bytesRead = 0;
byte[] headerBuffer = new byte[8];
while (bytesRead < 8)
{
try
{
bytesRead += await ns.ReadAsync(headerBuffer.AsMemory(bytesRead, 8 - bytesRead));
if (bytesRead < 0)
{
return null;
}
}
catch { }
}
int msgDataSize = Messages.HLMessage.ExtractDataSize(headerBuffer);
if (msgDataSize <= 0 || msgDataSize >= 100_000)
{
return null;
}
byte[] dataBuffer = new byte[msgDataSize];
bytesRead = 0;
while (bytesRead < msgDataSize)
{
bytesRead += await ns.ReadAsync(dataBuffer.AsMemory(bytesRead, msgDataSize - bytesRead));
if (bytesRead < 0)
{
return null;
}
}
Debug.WriteLine($"read {headerBuffer.Length + dataBuffer.Length} bytes of message data ({headerBuffer.Length} header, {dataBuffer.Length} data)");
T msg = new();
msg.ParseData(headerBuffer, dataBuffer);
return msg;
}
private const string connectionMessage = "CONNECTSERVERHOST\r\n\r\n";
private static ReadOnlySpan<byte> ConnectionMessageBytes => Encoding.ASCII.GetBytes(connectionMessage).AsSpan();
private static Messages.HLMessage CreateLoginMessage(ReadOnlySpan<byte> encodedPwd)
{
Messages.ClientLogin login = new(0);
login.Schema = 348;
login.ConnectionType = 0;
login.Version = "ScreenLogicConnect library";
login.ProcID = 2;
if (encodedPwd != null)
{
if (encodedPwd.Length > 16)
{
login.Password = new byte[16];
encodedPwd[..16].CopyTo(login.Password);
}
else
{
login.Password = new byte[encodedPwd.Length];
encodedPwd.CopyTo(login.Password);
}
}
else
{
login.Password = new byte[16];
}
return login;
}
public void Dispose()
{
GC.SuppressFinalize(this);
client?.Dispose();
}
}