1
|
using System;
|
2
|
using System.Net.Sockets;
|
3
|
using System.Threading;
|
4
|
using System.IO;
|
5
|
using System.Linq;
|
6
|
using System.Text;
|
7
|
using UnityEngine;
|
8
|
|
9
|
namespace DeltaRobotVr
|
10
|
{
|
11
|
|
12
|
/// <summary>
|
13
|
/// Singleton class responsible for communication with the Deltarobot server.
|
14
|
/// </summary>
|
15
|
public sealed class Client
|
16
|
{
|
17
|
// singleton instance
|
18
|
public static readonly Client Instance = new Client();
|
19
|
|
20
|
// default server
|
21
|
private const string DefaultHost = "127.0.0.1";
|
22
|
private const int DefaultPort = 4242;
|
23
|
|
24
|
// protocol constants
|
25
|
private const UInt16 ProtocolMagicId = 0x3001;
|
26
|
private const UInt16 ProtocolVersionId = 0x2002;
|
27
|
private const UInt16 PingId = 0x3004;
|
28
|
private const UInt16 PongId = 0x3005;
|
29
|
private const UInt16 EotId = 0xF006;
|
30
|
private const UInt16 CurrentActuatorPositionId = 0x4003;
|
31
|
private const UInt16 CurrentDirectionVectorId = 0x4007;
|
32
|
private const UInt16 DesiredDirectionVectorId = 0x4008;
|
33
|
private const UInt16 CurveId = 0xF009;
|
34
|
|
35
|
private const UInt16 Size8 = 0x0000;
|
36
|
private const UInt16 Size16 = 0x1000;
|
37
|
private const UInt16 Size32 = 0x2000;
|
38
|
private const UInt16 Size64 = 0x3000;
|
39
|
private const UInt16 Size128 = 0x4000;
|
40
|
private const UInt16 SizeVariable = 0xF000;
|
41
|
|
42
|
private static readonly byte[] ProtocolMagicValue = Encoding.ASCII.GetBytes("DeltaRVr");
|
43
|
private const UInt32 ProtocolVersionValue = 1;
|
44
|
|
45
|
private const UInt32 PointLength = 12;
|
46
|
|
47
|
private const int ReconnectPollMillis = 200;
|
48
|
private const int ReconnectTimeMillis = 3000;
|
49
|
|
50
|
|
51
|
private Thread _thread;
|
52
|
|
53
|
private readonly object _isRunningLock = new object();
|
54
|
private bool _isRunning;
|
55
|
|
56
|
private readonly object _actuatorPositionLock = new object();
|
57
|
private Single3 _actuatorPosition;
|
58
|
|
59
|
private readonly object _currentDirectionVectorLock = new object();
|
60
|
private Single3 _currentDirectionVector;
|
61
|
|
62
|
private readonly object _desiredDirectionVectorLock = new object();
|
63
|
private Single3 _desiredDirectionVector;
|
64
|
|
65
|
private readonly object _curveLock = new object();
|
66
|
private Single3[] _curve;
|
67
|
private long _curveCounter = 0;
|
68
|
|
69
|
private volatile bool _isConnected;
|
70
|
private volatile string _eotReason = string.Empty;
|
71
|
|
72
|
private readonly object _reconnectTimeLock = new object();
|
73
|
private DateTime _reconnectTime = DateTime.Now;
|
74
|
|
75
|
static Client()
|
76
|
{
|
77
|
}
|
78
|
|
79
|
private Client()
|
80
|
{
|
81
|
}
|
82
|
|
83
|
/// <summary>
|
84
|
/// Starts the client thread and connects to the server.
|
85
|
/// </summary>
|
86
|
public void Start()
|
87
|
{
|
88
|
IsRunning = true;
|
89
|
_thread = new Thread(ThreadProcedure);
|
90
|
_thread.Start();
|
91
|
}
|
92
|
|
93
|
/// <summary>
|
94
|
/// Gracefully stops the client thread. Blocks until the thread actually stops.
|
95
|
/// </summary>
|
96
|
public void Stop()
|
97
|
{
|
98
|
IsRunning = false;
|
99
|
if (_thread != null)
|
100
|
{
|
101
|
_thread.Join();
|
102
|
_thread = null;
|
103
|
}
|
104
|
}
|
105
|
|
106
|
/// <summary>
|
107
|
/// Contains a reason received in an end-of-transmission message.
|
108
|
/// </summary>
|
109
|
public string EotReason
|
110
|
{
|
111
|
get => _eotReason;
|
112
|
private set => _eotReason = value;
|
113
|
}
|
114
|
|
115
|
/// <summary>
|
116
|
/// Whether the client is currently maintaining an active connection to the server.
|
117
|
/// </summary>
|
118
|
public bool IsConnected
|
119
|
{
|
120
|
get => _isConnected;
|
121
|
private set => _isConnected = value;
|
122
|
}
|
123
|
|
124
|
/// <summary>
|
125
|
/// Current position of the actuator (in server's world space).
|
126
|
/// </summary>
|
127
|
public Single3 ActuatorPosition
|
128
|
{
|
129
|
get
|
130
|
{
|
131
|
lock (_actuatorPositionLock)
|
132
|
{
|
133
|
return _actuatorPosition;
|
134
|
}
|
135
|
}
|
136
|
private set
|
137
|
{
|
138
|
lock (_actuatorPositionLock)
|
139
|
{
|
140
|
_actuatorPosition = value;
|
141
|
}
|
142
|
}
|
143
|
}
|
144
|
|
145
|
/// <summary>
|
146
|
/// The actual direction the actuator is currently moving in.
|
147
|
/// </summary>
|
148
|
public Single3 ActualDirectionVector
|
149
|
{
|
150
|
get
|
151
|
{
|
152
|
lock (_currentDirectionVectorLock)
|
153
|
{
|
154
|
return _currentDirectionVector;
|
155
|
}
|
156
|
}
|
157
|
private set
|
158
|
{
|
159
|
lock (_currentDirectionVectorLock)
|
160
|
{
|
161
|
_currentDirectionVector = value;
|
162
|
}
|
163
|
}
|
164
|
}
|
165
|
|
166
|
/// <summary>
|
167
|
/// The target direction the actuator should be moving in so that it draws the curve.
|
168
|
/// </summary>
|
169
|
public Single3 TargetDirectionVector
|
170
|
{
|
171
|
get
|
172
|
{
|
173
|
lock (_desiredDirectionVectorLock)
|
174
|
{
|
175
|
return _desiredDirectionVector;
|
176
|
}
|
177
|
}
|
178
|
private set
|
179
|
{
|
180
|
lock (_desiredDirectionVectorLock)
|
181
|
{
|
182
|
_desiredDirectionVector = value;
|
183
|
}
|
184
|
}
|
185
|
}
|
186
|
|
187
|
/// <summary>
|
188
|
/// The current exercise curve.
|
189
|
/// </summary>
|
190
|
public Single3[] Curve
|
191
|
{
|
192
|
get
|
193
|
{
|
194
|
lock (_curveLock)
|
195
|
{
|
196
|
return _curve;
|
197
|
}
|
198
|
}
|
199
|
private set
|
200
|
{
|
201
|
lock (_curveLock)
|
202
|
{
|
203
|
_curve = value;
|
204
|
}
|
205
|
}
|
206
|
}
|
207
|
|
208
|
/// <summary>
|
209
|
/// A counter used to determine whether the curve has changed. The curve remains the same while the counter is
|
210
|
/// the same.
|
211
|
/// </summary>
|
212
|
public long CurveCounter
|
213
|
{
|
214
|
get
|
215
|
{
|
216
|
lock (_curveLock)
|
217
|
{
|
218
|
return _curveCounter;
|
219
|
}
|
220
|
}
|
221
|
private set
|
222
|
{
|
223
|
lock (_curveLock)
|
224
|
{
|
225
|
_curveCounter = value;
|
226
|
}
|
227
|
}
|
228
|
}
|
229
|
|
230
|
/// <summary>
|
231
|
/// Whether the client thread should keep on reading data from the server.
|
232
|
/// </summary>
|
233
|
public bool IsRunning
|
234
|
{
|
235
|
get {
|
236
|
lock (_isRunningLock)
|
237
|
{
|
238
|
return _isRunning;
|
239
|
}
|
240
|
}
|
241
|
private set
|
242
|
{
|
243
|
lock (_isRunningLock)
|
244
|
{
|
245
|
_isRunning = value;
|
246
|
}
|
247
|
}
|
248
|
}
|
249
|
|
250
|
/// <summary>
|
251
|
/// The time at which the client will attempt to reconnect to the server again.
|
252
|
/// </summary>
|
253
|
public DateTime ReconnectTime
|
254
|
{
|
255
|
get
|
256
|
{
|
257
|
lock (_reconnectTimeLock)
|
258
|
{
|
259
|
return _reconnectTime;
|
260
|
}
|
261
|
}
|
262
|
private set
|
263
|
{
|
264
|
lock (_reconnectTimeLock)
|
265
|
{
|
266
|
_reconnectTime = value;
|
267
|
}
|
268
|
}
|
269
|
}
|
270
|
|
271
|
/// <summary>
|
272
|
/// The client thread function.
|
273
|
/// </summary>
|
274
|
private void ThreadProcedure()
|
275
|
{
|
276
|
while (IsRunning)
|
277
|
{
|
278
|
try
|
279
|
{
|
280
|
var host = ConfigurationProvider.Instance.Host;
|
281
|
var port = ConfigurationProvider.Instance.Port;
|
282
|
|
283
|
if (port < 0 || host.Length == 0)
|
284
|
{
|
285
|
// Obviously invalid values
|
286
|
host = DefaultHost;
|
287
|
port = DefaultPort;
|
288
|
}
|
289
|
|
290
|
using var client = new TcpClient(host, port);
|
291
|
using var stream = client.GetStream();
|
292
|
BinaryReader reader = new BinaryReader(stream);
|
293
|
BinaryWriter writer = new BinaryWriter(stream);
|
294
|
IsConnected = true;
|
295
|
|
296
|
SendProtocolPreamble(writer);
|
297
|
|
298
|
// main message reception loop
|
299
|
while (IsRunning && IsConnected)
|
300
|
{
|
301
|
ReadMessages(reader, writer);
|
302
|
}
|
303
|
|
304
|
if (client.Connected)
|
305
|
{
|
306
|
SendEot(writer, "Client stopped by user");
|
307
|
}
|
308
|
}
|
309
|
catch (Exception e)
|
310
|
{
|
311
|
if (e is SocketException || e is IOException)
|
312
|
{
|
313
|
Debug.LogWarning($"Connection error:\n{e}");
|
314
|
}
|
315
|
else
|
316
|
{
|
317
|
Debug.LogError($"Exception in communication thread:\n{e}");
|
318
|
}
|
319
|
}
|
320
|
finally
|
321
|
{
|
322
|
IsConnected = false;
|
323
|
}
|
324
|
|
325
|
// wait before reconnection - short polls to prevent blocking if application is closed
|
326
|
ReconnectTime = DateTime.Now.AddMilliseconds(ReconnectTimeMillis);
|
327
|
while (IsRunning && DateTime.Now < ReconnectTime)
|
328
|
{
|
329
|
Thread.Sleep(ReconnectPollMillis);
|
330
|
}
|
331
|
}
|
332
|
}
|
333
|
|
334
|
|
335
|
private void ReadMessages(BinaryReader reader, BinaryWriter writer)
|
336
|
{
|
337
|
var messageIdentifier = reader.ReadUInt16();
|
338
|
switch (messageIdentifier)
|
339
|
{
|
340
|
case ProtocolMagicId:
|
341
|
ProcessProtocolMagic(reader);
|
342
|
break;
|
343
|
case ProtocolVersionId:
|
344
|
ProcessProtocolVersion(reader);
|
345
|
break;
|
346
|
case PingId:
|
347
|
ProcessPing(reader, writer);
|
348
|
break;
|
349
|
case EotId:
|
350
|
ProcessEot(reader);
|
351
|
break;
|
352
|
case CurrentActuatorPositionId:
|
353
|
ProcessActuatorPosition(reader);
|
354
|
break;
|
355
|
case CurrentDirectionVectorId:
|
356
|
ProcessActualDirectionVector(reader);
|
357
|
break;
|
358
|
case DesiredDirectionVectorId:
|
359
|
ProcessTargetDirectionVector(reader);
|
360
|
break;
|
361
|
case CurveId:
|
362
|
ProcessCurve(reader);
|
363
|
break;
|
364
|
default: // unknown message
|
365
|
var sizeIdentifier = (UInt16) (messageIdentifier & 0xF000);
|
366
|
switch (sizeIdentifier)
|
367
|
{
|
368
|
case Size8:
|
369
|
Skip8(reader);
|
370
|
break;
|
371
|
case Size16:
|
372
|
Skip16(reader);
|
373
|
break;
|
374
|
case Size32:
|
375
|
Skip32(reader);
|
376
|
break;
|
377
|
case Size64:
|
378
|
Skip64(reader);
|
379
|
break;
|
380
|
case Size128:
|
381
|
Skip128(reader);
|
382
|
break;
|
383
|
case SizeVariable:
|
384
|
SkipVariableLength(reader);
|
385
|
break;
|
386
|
}
|
387
|
|
388
|
break;
|
389
|
}
|
390
|
}
|
391
|
|
392
|
private void SendProtocolPreamble(BinaryWriter writer)
|
393
|
{
|
394
|
writer.Write(ProtocolMagicId);
|
395
|
writer.Write(ProtocolMagicValue);
|
396
|
writer.Write(ProtocolVersionId);
|
397
|
writer.Write(ProtocolVersionValue);
|
398
|
}
|
399
|
|
400
|
private void Skip8(BinaryReader reader)
|
401
|
{
|
402
|
reader.ReadByte();
|
403
|
}
|
404
|
|
405
|
private void Skip16(BinaryReader reader)
|
406
|
{
|
407
|
reader.ReadUInt16();
|
408
|
}
|
409
|
|
410
|
private void Skip32(BinaryReader reader)
|
411
|
{
|
412
|
reader.ReadUInt32();
|
413
|
}
|
414
|
|
415
|
private void Skip64(BinaryReader reader)
|
416
|
{
|
417
|
reader.ReadUInt64();
|
418
|
}
|
419
|
|
420
|
private void Skip128(BinaryReader reader)
|
421
|
{
|
422
|
reader.ReadUInt64();
|
423
|
reader.ReadUInt64();
|
424
|
}
|
425
|
|
426
|
private void SkipVariableLength(BinaryReader reader)
|
427
|
{
|
428
|
UInt32 numberOfBytes = reader.ReadUInt32();
|
429
|
reader.ReadBytes((int) numberOfBytes);
|
430
|
}
|
431
|
|
432
|
private void ProcessActuatorPosition(BinaryReader reader)
|
433
|
{
|
434
|
var x = reader.ReadSingle();
|
435
|
var y = reader.ReadSingle();
|
436
|
var z = reader.ReadSingle();
|
437
|
reader.ReadSingle(); // skip unused
|
438
|
|
439
|
ActuatorPosition = new Single3(x, y, z);
|
440
|
}
|
441
|
|
442
|
private void ProcessActualDirectionVector(BinaryReader reader)
|
443
|
{
|
444
|
var x = reader.ReadSingle();
|
445
|
var y = reader.ReadSingle();
|
446
|
var z = reader.ReadSingle();
|
447
|
reader.ReadSingle(); // skip unused
|
448
|
|
449
|
ActualDirectionVector = new Single3(x, y, z);
|
450
|
}
|
451
|
|
452
|
private void ProcessTargetDirectionVector(BinaryReader reader)
|
453
|
{
|
454
|
var x = reader.ReadSingle();
|
455
|
var y = reader.ReadSingle();
|
456
|
var z = reader.ReadSingle();
|
457
|
reader.ReadSingle(); // skip unused
|
458
|
|
459
|
TargetDirectionVector = new Single3(x, y, z);
|
460
|
}
|
461
|
|
462
|
private void ProcessCurve(BinaryReader reader)
|
463
|
{
|
464
|
UInt32 numberOfBytes = reader.ReadUInt32();
|
465
|
UInt32 numberOfPoints = numberOfBytes / PointLength;
|
466
|
Single3[] curve = new Single3[numberOfPoints];
|
467
|
|
468
|
for(int i = 0; i < numberOfPoints; i++)
|
469
|
{
|
470
|
var x = reader.ReadSingle();
|
471
|
var y = reader.ReadSingle();
|
472
|
var z = reader.ReadSingle();
|
473
|
curve[i] = new Single3(x, y, z);
|
474
|
}
|
475
|
|
476
|
lock(_curveLock)
|
477
|
{
|
478
|
CurveCounter++;
|
479
|
Curve = curve;
|
480
|
}
|
481
|
}
|
482
|
|
483
|
private void ProcessEot(BinaryReader reader)
|
484
|
{
|
485
|
UInt32 numberOfBytes = reader.ReadUInt32();
|
486
|
Byte[] buffer = reader.ReadBytes((int) numberOfBytes);
|
487
|
EotReason = Encoding.ASCII.GetString(buffer);
|
488
|
IsConnected = false;
|
489
|
}
|
490
|
|
491
|
private void ProcessProtocolMagic(BinaryReader reader)
|
492
|
{
|
493
|
byte[] value = reader.ReadBytes(8);
|
494
|
if (!value.SequenceEqual(ProtocolMagicValue))
|
495
|
{
|
496
|
IsConnected = false;
|
497
|
}
|
498
|
}
|
499
|
|
500
|
private void ProcessProtocolVersion(BinaryReader reader)
|
501
|
{
|
502
|
UInt32 value = reader.ReadUInt32();
|
503
|
if (value != ProtocolVersionValue)
|
504
|
{
|
505
|
IsConnected = false;
|
506
|
}
|
507
|
}
|
508
|
|
509
|
private void ProcessPing(BinaryReader reader, BinaryWriter writer)
|
510
|
{
|
511
|
UInt64 pingValue = reader.ReadUInt64();
|
512
|
|
513
|
writer.Write(PongId);
|
514
|
writer.Write(pingValue);
|
515
|
}
|
516
|
|
517
|
private void SendEot(BinaryWriter writer, string reason)
|
518
|
{
|
519
|
writer.Write(EotId);
|
520
|
writer.Write((UInt32) reason.Length);
|
521
|
writer.Write(Encoding.ASCII.GetBytes(reason));
|
522
|
}
|
523
|
}
|
524
|
}
|