Revize 04839796
Přidáno uživatelem Oto Šťáva před téměř 4 roky(ů)
deltarobot-vr/Assets/DeltaRobotVr/Client.cs | ||
---|---|---|
8 | 8 |
|
9 | 9 |
namespace DeltaRobotVr |
10 | 10 |
{ |
11 |
public readonly struct Single3 |
|
12 |
{ |
|
13 |
public float X { get; } |
|
14 |
public float Y { get; } |
|
15 |
public float Z { get; } |
|
16 |
|
|
17 |
public Single3(float x, float y, float z) |
|
18 |
{ |
|
19 |
X = x; |
|
20 |
Y = y; |
|
21 |
Z = z; |
|
22 |
} |
|
23 |
|
|
24 |
public override string ToString() => "Single3[ " + X + " " + Y + " " + Z + " ]"; |
|
25 |
} |
|
26 |
|
|
11 |
|
|
12 |
/// <summary> |
|
13 |
/// Singleton class responsible for communication with the Deltarobot server. |
|
14 |
/// </summary> |
|
27 | 15 |
public sealed class Client |
28 | 16 |
{ |
29 | 17 |
// singleton instance |
... | ... | |
79 | 67 |
private long _curveCounter = 0; |
80 | 68 |
|
81 | 69 |
private volatile bool _isConnected; |
82 |
private volatile string _eotMsg = string.Empty;
|
|
70 |
private volatile string _eotReason = string.Empty;
|
|
83 | 71 |
|
84 | 72 |
private readonly object _reconnectTimeLock = new object(); |
85 | 73 |
private DateTime _reconnectTime = DateTime.Now; |
... | ... | |
92 | 80 |
{ |
93 | 81 |
} |
94 | 82 |
|
83 |
/// <summary> |
|
84 |
/// Starts the client thread and connects to the server. |
|
85 |
/// </summary> |
|
95 | 86 |
public void Start() |
96 | 87 |
{ |
97 | 88 |
IsRunning = true; |
... | ... | |
99 | 90 |
_thread.Start(); |
100 | 91 |
} |
101 | 92 |
|
93 |
/// <summary> |
|
94 |
/// Gracefully stops the client thread. Blocks until the thread actually stops. |
|
95 |
/// </summary> |
|
102 | 96 |
public void Stop() |
103 | 97 |
{ |
104 | 98 |
IsRunning = false; |
... | ... | |
109 | 103 |
} |
110 | 104 |
} |
111 | 105 |
|
112 |
public string EotMsg |
|
106 |
/// <summary> |
|
107 |
/// Contains a reason received in an end-of-transmission message. |
|
108 |
/// </summary> |
|
109 |
public string EotReason |
|
113 | 110 |
{ |
114 |
get => _eotMsg;
|
|
115 |
private set => _eotMsg = value;
|
|
111 |
get => _eotReason;
|
|
112 |
private set => _eotReason = value;
|
|
116 | 113 |
} |
117 | 114 |
|
115 |
/// <summary> |
|
116 |
/// Whether the client is currently maintaining an active connection to the server. |
|
117 |
/// </summary> |
|
118 | 118 |
public bool IsConnected |
119 | 119 |
{ |
120 | 120 |
get => _isConnected; |
121 | 121 |
private set => _isConnected = value; |
122 | 122 |
} |
123 | 123 |
|
124 |
/// <summary> |
|
125 |
/// Current position of the actuator (in server's world space). |
|
126 |
/// </summary> |
|
124 | 127 |
public Single3 ActuatorPosition |
125 | 128 |
{ |
126 | 129 |
get |
... | ... | |
139 | 142 |
} |
140 | 143 |
} |
141 | 144 |
|
142 |
public Single3 CurrentDirectionVector |
|
145 |
/// <summary> |
|
146 |
/// The actual direction the actuator is currently moving in. |
|
147 |
/// </summary> |
|
148 |
public Single3 ActualDirectionVector |
|
143 | 149 |
{ |
144 | 150 |
get |
145 | 151 |
{ |
... | ... | |
157 | 163 |
} |
158 | 164 |
} |
159 | 165 |
|
160 |
public Single3 DesiredDirectionVector |
|
166 |
/// <summary> |
|
167 |
/// The target direction the actuator should be moving in so that it draws the curve. |
|
168 |
/// </summary> |
|
169 |
public Single3 TargetDirectionVector |
|
161 | 170 |
{ |
162 | 171 |
get |
163 | 172 |
{ |
... | ... | |
175 | 184 |
} |
176 | 185 |
} |
177 | 186 |
|
187 |
/// <summary> |
|
188 |
/// The current exercise curve. |
|
189 |
/// </summary> |
|
178 | 190 |
public Single3[] Curve |
179 | 191 |
{ |
180 | 192 |
get |
... | ... | |
193 | 205 |
} |
194 | 206 |
} |
195 | 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> |
|
196 | 212 |
public long CurveCounter |
197 | 213 |
{ |
198 | 214 |
get |
... | ... | |
211 | 227 |
} |
212 | 228 |
} |
213 | 229 |
|
230 |
/// <summary> |
|
231 |
/// Whether the client thread should keep on reading data from the server. |
|
232 |
/// </summary> |
|
214 | 233 |
public bool IsRunning |
215 | 234 |
{ |
216 | 235 |
get { |
... | ... | |
228 | 247 |
} |
229 | 248 |
} |
230 | 249 |
|
250 |
/// <summary> |
|
251 |
/// The time at which the client will attempt to reconnect to the server again. |
|
252 |
/// </summary> |
|
231 | 253 |
public DateTime ReconnectTime |
232 | 254 |
{ |
233 | 255 |
get |
... | ... | |
246 | 268 |
} |
247 | 269 |
} |
248 | 270 |
|
271 |
/// <summary> |
|
272 |
/// The client thread function. |
|
273 |
/// </summary> |
|
249 | 274 |
private void ThreadProcedure() |
250 | 275 |
{ |
251 | 276 |
while (IsRunning) |
... | ... | |
315 | 340 |
ProcessEot(reader); |
316 | 341 |
break; |
317 | 342 |
case CurrentActuatorPositionId: |
318 |
ProcessCurrentActuatorPosition(reader);
|
|
343 |
ProcessActuatorPosition(reader); |
|
319 | 344 |
break; |
320 | 345 |
case CurrentDirectionVectorId: |
321 |
ProcessCurrentDirectionVector(reader);
|
|
346 |
ProcessActualDirectionVector(reader);
|
|
322 | 347 |
break; |
323 | 348 |
case DesiredDirectionVectorId: |
324 |
ProcessDesiredDirectionVector(reader);
|
|
349 |
ProcessTargetDirectionVector(reader);
|
|
325 | 350 |
break; |
326 | 351 |
case CurveId: |
327 | 352 |
ProcessCurve(reader); |
... | ... | |
394 | 419 |
reader.ReadBytes((int) numberOfBytes); |
395 | 420 |
} |
396 | 421 |
|
397 |
private void ProcessCurrentActuatorPosition(BinaryReader reader)
|
|
422 |
private void ProcessActuatorPosition(BinaryReader reader) |
|
398 | 423 |
{ |
399 | 424 |
var x = reader.ReadSingle(); |
400 | 425 |
var y = reader.ReadSingle(); |
... | ... | |
404 | 429 |
ActuatorPosition = new Single3(x, y, z); |
405 | 430 |
} |
406 | 431 |
|
407 |
private void ProcessCurrentDirectionVector(BinaryReader reader)
|
|
432 |
private void ProcessActualDirectionVector(BinaryReader reader)
|
|
408 | 433 |
{ |
409 | 434 |
var x = reader.ReadSingle(); |
410 | 435 |
var y = reader.ReadSingle(); |
411 | 436 |
var z = reader.ReadSingle(); |
412 | 437 |
reader.ReadSingle(); // skip unused |
413 | 438 |
|
414 |
CurrentDirectionVector = new Single3(x, y, z);
|
|
439 |
ActualDirectionVector = new Single3(x, y, z);
|
|
415 | 440 |
} |
416 | 441 |
|
417 |
private void ProcessDesiredDirectionVector(BinaryReader reader)
|
|
442 |
private void ProcessTargetDirectionVector(BinaryReader reader)
|
|
418 | 443 |
{ |
419 | 444 |
var x = reader.ReadSingle(); |
420 | 445 |
var y = reader.ReadSingle(); |
421 | 446 |
var z = reader.ReadSingle(); |
422 | 447 |
reader.ReadSingle(); // skip unused |
423 | 448 |
|
424 |
DesiredDirectionVector = new Single3(x, y, z);
|
|
449 |
TargetDirectionVector = new Single3(x, y, z);
|
|
425 | 450 |
} |
426 | 451 |
|
427 | 452 |
private void ProcessCurve(BinaryReader reader) |
... | ... | |
449 | 474 |
{ |
450 | 475 |
UInt32 numberOfBytes = reader.ReadUInt32(); |
451 | 476 |
Byte[] buffer = reader.ReadBytes((int) numberOfBytes); |
452 |
EotMsg = Encoding.ASCII.GetString(buffer);
|
|
477 |
EotReason = Encoding.ASCII.GetString(buffer);
|
|
453 | 478 |
IsConnected = false; |
454 | 479 |
} |
455 | 480 |
|
Také k dispozici: Unified diff
Re #8906 - Server and client refactor