-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetServer.cs
326 lines (280 loc) · 8.05 KB
/
NetServer.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using static SylverInk.Common;
using static SylverInk.Network;
namespace SylverInk
{
public partial class NetServer
{
public bool Active { get; set; } = false;
public IPAddress? Address { get; set; }
public string? AddressCode { get; set; }
public List<TcpClient> Clients { get; } = [];
private TcpListener DBServer { get; set; } = new(IPAddress.Any, TcpPort);
public static string[] DNSAddresses { get; } = [
"http://checkip.dyndns.org",
"https://ifconfig.me/ip",
"https://icanhazip.com"
];
public byte? Flags;
public System.Windows.Shapes.Ellipse? Indicator { get; set; }
public BackgroundWorker ServerTask { get; set; } = new() { WorkerSupportsCancellation = true };
public bool Serving { get; private set; } = false;
public BackgroundWorker WatchTask { get; set; } = new() { WorkerSupportsCancellation = true };
public NetServer(Database DB)
{
Indicator = new() { StrokeThickness = 1.0 };
ServerTask.DoWork += (sender, _) =>
{
var task = (BackgroundWorker?)sender;
while (!task?.CancellationPending is true)
{
Concurrent(() =>
{
for (int i = Clients.Count - 1; i > -1; i--)
{
try
{
var client = Clients[i];
if (client.Available > 0)
ReadFromStream(client, DB);
if (!client.Connected || !client.GetStream().Socket.Connected)
Clients.RemoveAt(i);
}
catch
{
//TODO: A fault in one connection really shouldn't be grounds for panicking the entire server. This is safe, but it's overkill.
Close();
}
}
});
}
};
ServerTask.RunWorkerCompleted += (_, _) =>
{
foreach (var client in Clients)
client.Close();
};
WatchTask.DoWork += (sender, _) =>
{
var task = (BackgroundWorker?)sender;
while (!task?.CancellationPending is true)
{
if (!DBServer.Pending())
continue;
var client = DBServer.AcceptTcpClient();
SpinWait.SpinUntil(new(() => client.Available != 0));
var stream = client.GetStream();
Flags = (byte)stream.ReadByte();
var data = DB.SerializeRecords(true);
int dataLength = data?.Count ?? 0;
data?.Insert(0, (byte)MessageType.DatabaseInit);
data?.InsertRange(1, [
0, 0, 0, 0,
.. IntToBytes(dataLength)
]);
if (dataLength > 0)
stream.WriteAsync(data?.ToArray()).AsTask().Wait();
Clients.Add(client);
}
};
}
public async void Broadcast(MessageType type, byte[] data)
{
foreach (var client in Clients)
{
if (!client.Connected)
continue;
byte[] id = [(byte)type];
byte[] streamData = [.. id, .. data];
await client.GetStream().WriteAsync(streamData);
}
}
public void Close()
{
WatchTask.CancelAsync();
ServerTask.CancelAsync();
DBServer?.Stop();
DBServer?.Dispose();
Serving = false;
Active = false;
UpdateIndicator();
UpdateContextMenu();
}
private void ReadFromStream(TcpClient client, Database DB)
{
int oldData = client.Available;
bool dataFinished = false;
do
{
dataFinished = !SpinWait.SpinUntil(new(() => client.Available != oldData), 200);
oldData = client.Available;
} while (!dataFinished);
var stream = client.GetStream();
var outBuffer = new List<byte>();
var type = (MessageType)stream.ReadByte();
outBuffer.Add((byte)type);
var intBuffer = new byte[4];
var recordIndex = 0;
byte[] textBuffer;
var textCount = 0;
stream.Read(intBuffer, 0, 4);
recordIndex = IntFromBytes(intBuffer);
outBuffer.AddRange(intBuffer);
switch (type)
{
case MessageType.RecordAdd:
stream.Read(intBuffer, 0, 4);
textCount = IntFromBytes(intBuffer);
outBuffer.AddRange(intBuffer);
if (textCount > 0)
{
textBuffer = new byte[textCount];
stream.Read(textBuffer, 0, textCount);
outBuffer.AddRange(textBuffer);
DB?.CreateRecord(Encoding.UTF8.GetString(textBuffer), false);
Concurrent(() => DeferUpdateRecentNotes());
break;
}
DB?.CreateRecord(string.Empty, false);
Concurrent(() => DeferUpdateRecentNotes());
break;
case MessageType.RecordLock:
DB?.Lock(recordIndex);
break;
case MessageType.RecordRemove:
DB?.DeleteRecord(recordIndex, false);
Concurrent(() => DeferUpdateRecentNotes());
break;
case MessageType.RecordReplace:
stream.Read(intBuffer, 0, 4);
textCount = IntFromBytes(intBuffer);
if (textCount > 0)
{
textBuffer = new byte[textCount];
stream.Read(textBuffer, 0, textCount);
var oldText = Encoding.UTF8.GetString(textBuffer);
stream.Read(intBuffer, 0, 4);
textCount = IntFromBytes(intBuffer);
if (textCount > 0)
{
textBuffer = new byte[textCount];
stream.Read(textBuffer, 0, textCount);
var newText = Encoding.UTF8.GetString(textBuffer);
DB?.Replace(oldText, newText, false);
Concurrent(() => DeferUpdateRecentNotes());
}
}
break;
case MessageType.RecordUnlock:
DB?.Unlock(recordIndex);
break;
case MessageType.TextInsert:
stream.Read(intBuffer, 0, 4);
textCount = IntFromBytes(intBuffer);
outBuffer.AddRange(intBuffer);
if (textCount > 0)
{
textBuffer = new byte[textCount];
stream.Read(textBuffer, 0, textCount);
outBuffer.AddRange(textBuffer);
DB?.CreateRevision(recordIndex, Encoding.UTF8.GetString(textBuffer), false);
Concurrent(() => DeferUpdateRecentNotes());
break;
}
DB?.CreateRevision(recordIndex, string.Empty, false);
Concurrent(() => DeferUpdateRecentNotes());
break;
}
foreach (var otherClient in Clients)
{
if (otherClient.Equals(client))
continue;
try
{
otherClient.GetStream().Write(outBuffer.ToArray());
}
catch
{
otherClient.Close();
}
}
}
public async void Serve(byte Flags)
{
Active = true;
Address = IPAddress.Loopback;
Serving = false;
UpdateIndicator();
this.Flags = Math.Min((byte)15, Flags);
for (int i = 0; i < DNSAddresses.Length; i++)
{
try
{
var DNSAddress = DNSAddresses[i];
if (IPAddress.TryParse(await new HttpClient().GetStringAsync(DNSAddress), out var address))
{
AddressCode = CodeFromAddress(address, this.Flags);
Address = CodeToAddress(AddressCode, out this.Flags);
break;
}
}
catch { }
}
if (Address.Equals(IPAddress.Loopback))
{
MessageBox.Show("Failed to connect to the DNS server. Please try again.", "Sylver Ink: Error", MessageBoxButton.OK, MessageBoxImage.Error);
Active = false;
Serving = false;
return;
}
DBServer = new(IPAddress.Any, TcpPort);
DBServer.Server.ReceiveBufferSize = int.MaxValue;
DBServer.Server.SendBufferSize = int.MaxValue;
try
{
DBServer.Start(256);
}
catch
{
MessageBox.Show($"Failed to open the database server on port {TcpPort}.", "Sylver Ink: Error", MessageBoxButton.OK, MessageBoxImage.Error);
Active = false;
Serving = false;
return;
}
Serving = true;
WatchTask.RunWorkerAsync();
ServerTask.RunWorkerAsync();
UpdateIndicator();
UpdateContextMenu();
var codePopup = (Popup?)Application.Current.MainWindow.FindName("CodePopup");
if (codePopup is null)
return;
var codeBox = (TextBox)codePopup.FindName("CodeBox");
if (codeBox is null)
return;
codePopup.IsOpen = true;
codeBox.Text = AddressCode ?? "Vm000G";
}
public void UpdateIndicator() => Indicator?.Dispatcher.Invoke(() =>
{
Indicator.Fill = Serving ? Brushes.MediumPurple : Brushes.Orange;
Indicator.Height = 12;
Indicator.Margin = new(2, 4, 3, 4);
Indicator.Stroke = Common.Settings.MenuForeground;
Indicator.Width = 12;
Indicator.InvalidateVisual();
UpdateContextMenu();
});
}
}