1 |
b8512c30
|
Miloslav Kovář
|
using System;
|
2 |
|
|
using System.Net.Sockets;
|
3 |
|
|
using System.Threading;
|
4 |
|
|
using System.IO;
|
5 |
|
|
|
6 |
|
|
namespace ConsoleApp1
|
7 |
|
|
{
|
8 |
|
|
class Program
|
9 |
|
|
{
|
10 |
|
|
static void Main(string[] args)
|
11 |
|
|
{
|
12 |
|
|
Console.WriteLine("Hello World!");
|
13 |
|
|
Singleton client = Singleton.Instance;
|
14 |
|
|
client.start();
|
15 |
|
|
while (client.IsRunning)
|
16 |
|
|
{
|
17 |
|
|
Console.WriteLine(client.X + " " + client.Y + " " + client.Z);
|
18 |
|
|
Thread.Sleep(100);
|
19 |
|
|
}
|
20 |
|
|
}
|
21 |
|
|
}
|
22 |
|
|
|
23 |
|
|
public sealed class Singleton
|
24 |
|
|
{
|
25 |
|
|
private static readonly Singleton instance = new Singleton();
|
26 |
|
|
|
27 |
|
|
private const string SERVER = "127.0.0.1";
|
28 |
|
|
private const Int32 PORT = 4242;
|
29 |
|
|
|
30 |
|
|
private const UInt16 PROTOCOL_MAGIC = 0x3001;
|
31 |
|
|
private const UInt16 PROTOCOL_VERSION = 0x2002;
|
32 |
|
|
private const UInt16 PING = 0x3004;
|
33 |
|
|
private const UInt16 EOT = 0xF006;
|
34 |
|
|
private const UInt16 CURACTPOS = 0x4003;
|
35 |
|
|
|
36 |
|
|
private const UInt16 SIZE_8 = 0x0000;
|
37 |
|
|
private const UInt16 SIZE_16 = 0x1000;
|
38 |
|
|
private const UInt16 SIZE_32 = 0x2000;
|
39 |
|
|
private const UInt16 SIZE_64 = 0x3000;
|
40 |
|
|
private const UInt16 SIZE_128 = 0x4000;
|
41 |
|
|
private const UInt16 SIZE_VARIABLE = 0xF000;
|
42 |
|
|
|
43 |
|
|
private const UInt64 PROTOCOL_MAGIC_VALUE = 0x44656C7461525672; // `DeltaRVr` in ASCII
|
44 |
|
|
private const UInt32 PROTOCOL_VERSION_VALUE = 1;
|
45 |
|
|
|
46 |
|
|
private TcpClient client;
|
47 |
|
|
private NetworkStream stream;
|
48 |
|
|
private Boolean isRunning;
|
49 |
|
|
private Thread t;
|
50 |
|
|
|
51 |
|
|
private Single x;
|
52 |
|
|
private Single y;
|
53 |
|
|
private Single z;
|
54 |
|
|
private string eotMsg;
|
55 |
|
|
|
56 |
|
|
static Singleton()
|
57 |
|
|
{
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
private Singleton()
|
61 |
|
|
{
|
62 |
|
|
}
|
63 |
|
|
|
64 |
|
|
public static Singleton Instance
|
65 |
|
|
{
|
66 |
|
|
get
|
67 |
|
|
{
|
68 |
|
|
return instance;
|
69 |
|
|
}
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
public string EotMsg
|
73 |
|
|
{
|
74 |
|
|
get
|
75 |
|
|
{
|
76 |
|
|
lock (this)
|
77 |
|
|
{
|
78 |
|
|
return eotMsg;
|
79 |
|
|
}
|
80 |
|
|
}
|
81 |
|
|
set
|
82 |
|
|
{
|
83 |
|
|
lock (this)
|
84 |
|
|
{
|
85 |
|
|
eotMsg = value;
|
86 |
|
|
}
|
87 |
|
|
}
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
public Single X
|
91 |
|
|
{
|
92 |
|
|
get
|
93 |
|
|
{
|
94 |
|
|
lock (this)
|
95 |
|
|
{
|
96 |
|
|
return x;
|
97 |
|
|
}
|
98 |
|
|
}
|
99 |
|
|
set
|
100 |
|
|
{
|
101 |
|
|
lock (this)
|
102 |
|
|
{
|
103 |
|
|
x = value;
|
104 |
|
|
}
|
105 |
|
|
}
|
106 |
|
|
}
|
107 |
|
|
public Single Y
|
108 |
|
|
{
|
109 |
|
|
get
|
110 |
|
|
{
|
111 |
|
|
lock (this)
|
112 |
|
|
{
|
113 |
|
|
return y;
|
114 |
|
|
}
|
115 |
|
|
}
|
116 |
|
|
set
|
117 |
|
|
{
|
118 |
|
|
lock (this)
|
119 |
|
|
{
|
120 |
|
|
y = value;
|
121 |
|
|
}
|
122 |
|
|
}
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
public Single Z
|
126 |
|
|
{
|
127 |
|
|
get
|
128 |
|
|
{
|
129 |
|
|
lock (this)
|
130 |
|
|
{
|
131 |
|
|
return z;
|
132 |
|
|
}
|
133 |
|
|
}
|
134 |
|
|
set
|
135 |
|
|
{
|
136 |
|
|
lock(this)
|
137 |
|
|
{
|
138 |
|
|
z = value;
|
139 |
|
|
}
|
140 |
|
|
}
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
public Boolean IsRunning
|
144 |
|
|
{
|
145 |
|
|
get
|
146 |
|
|
{
|
147 |
|
|
lock(this)
|
148 |
|
|
{
|
149 |
|
|
return isRunning;
|
150 |
|
|
}
|
151 |
|
|
}
|
152 |
|
|
private set
|
153 |
|
|
{
|
154 |
|
|
lock (this)
|
155 |
|
|
{
|
156 |
|
|
isRunning = value;
|
157 |
|
|
}
|
158 |
|
|
}
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
private void connect()
|
162 |
|
|
{
|
163 |
|
|
try
|
164 |
|
|
{
|
165 |
|
|
client = new TcpClient(SERVER, PORT);
|
166 |
|
|
stream = client.GetStream();
|
167 |
|
|
BinaryReader reader = new BinaryReader(stream);
|
168 |
|
|
UInt16 sizeIdentifier = 0;
|
169 |
|
|
UInt16 messageIdentifier = 0;
|
170 |
|
|
|
171 |
|
|
|
172 |
|
|
try
|
173 |
|
|
{
|
174 |
|
|
while (IsRunning)
|
175 |
|
|
{
|
176 |
|
|
messageIdentifier = reader.ReadUInt16();
|
177 |
|
|
//Console.WriteLine(String.Format("{0:X}", messageIdentifier));
|
178 |
|
|
switch (messageIdentifier)
|
179 |
|
|
{
|
180 |
|
|
case PROTOCOL_MAGIC:
|
181 |
|
|
protocolMagicProcessing(reader);
|
182 |
|
|
break;
|
183 |
|
|
case PROTOCOL_VERSION:
|
184 |
|
|
protocolVersionProcessing(reader);
|
185 |
|
|
break;
|
186 |
|
|
case PING:
|
187 |
|
|
pingProcessing(reader);
|
188 |
|
|
break;
|
189 |
|
|
case EOT:
|
190 |
|
|
eotProcessing(reader);
|
191 |
|
|
break;
|
192 |
|
|
case CURACTPOS:
|
193 |
|
|
currentActuatorPositionProcessing(reader);
|
194 |
|
|
break;
|
195 |
|
|
default: //client tuto zpravu nezna
|
196 |
|
|
sizeIdentifier = (UInt16)(messageIdentifier & 0xF000);
|
197 |
|
|
switch (sizeIdentifier)
|
198 |
|
|
{
|
199 |
|
|
case SIZE_8:
|
200 |
|
|
skip8(reader);
|
201 |
|
|
break;
|
202 |
|
|
case SIZE_16:
|
203 |
|
|
skip16(reader);
|
204 |
|
|
break;
|
205 |
|
|
case SIZE_32:
|
206 |
|
|
skip32(reader);
|
207 |
|
|
break;
|
208 |
|
|
case SIZE_64:
|
209 |
|
|
skip64(reader);
|
210 |
|
|
break;
|
211 |
|
|
case SIZE_128:
|
212 |
|
|
skip128(reader);
|
213 |
|
|
break;
|
214 |
|
|
case SIZE_VARIABLE:
|
215 |
|
|
skipV(reader);
|
216 |
|
|
break;
|
217 |
|
|
}
|
218 |
|
|
break;
|
219 |
|
|
}
|
220 |
|
|
}
|
221 |
|
|
}
|
222 |
|
|
finally
|
223 |
|
|
{
|
224 |
|
|
stream.Close();
|
225 |
|
|
client.Close();
|
226 |
|
|
}
|
227 |
|
|
}
|
228 |
|
|
catch(Exception)
|
229 |
|
|
{
|
230 |
|
|
Console.WriteLine("failed to connect");
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
}
|
234 |
|
|
|
235 |
|
|
|
236 |
|
|
|
237 |
|
|
private void skip8(BinaryReader reader)
|
238 |
|
|
{
|
239 |
|
|
reader.ReadByte();
|
240 |
|
|
}
|
241 |
|
|
|
242 |
|
|
private void skip16(BinaryReader reader)
|
243 |
|
|
{
|
244 |
|
|
reader.ReadUInt16();
|
245 |
|
|
}
|
246 |
|
|
|
247 |
|
|
private void skip32(BinaryReader reader)
|
248 |
|
|
{
|
249 |
|
|
reader.ReadUInt32();
|
250 |
|
|
}
|
251 |
|
|
|
252 |
|
|
private void skip64(BinaryReader reader)
|
253 |
|
|
{
|
254 |
|
|
reader.ReadUInt64();
|
255 |
|
|
}
|
256 |
|
|
|
257 |
|
|
private void skip128(BinaryReader reader)
|
258 |
|
|
{
|
259 |
|
|
reader.ReadUInt64();
|
260 |
|
|
reader.ReadUInt64();
|
261 |
|
|
}
|
262 |
|
|
|
263 |
|
|
private void skipV(BinaryReader reader)
|
264 |
|
|
{
|
265 |
|
|
UInt32 numberOfBytes = reader.ReadUInt32();
|
266 |
|
|
reader.ReadBytes((int)numberOfBytes);
|
267 |
|
|
}
|
268 |
|
|
|
269 |
|
|
private void currentActuatorPositionProcessing(BinaryReader reader)
|
270 |
|
|
{
|
271 |
|
|
//Console.WriteLine("got position");
|
272 |
|
|
lock(this)
|
273 |
|
|
{
|
274 |
|
|
X = reader.ReadSingle();
|
275 |
|
|
Y = reader.ReadSingle();
|
276 |
|
|
Z = reader.ReadSingle();
|
277 |
|
|
}
|
278 |
|
|
reader.ReadSingle();
|
279 |
|
|
}
|
280 |
|
|
|
281 |
|
|
private void eotProcessing(BinaryReader reader)
|
282 |
|
|
{
|
283 |
|
|
UInt32 numberOfBytes = reader.ReadUInt32();
|
284 |
|
|
Byte[] buffer = reader.ReadBytes((int)numberOfBytes);
|
285 |
|
|
EotMsg = System.Text.Encoding.ASCII.GetString(buffer);
|
286 |
|
|
IsRunning = false;
|
287 |
|
|
}
|
288 |
|
|
|
289 |
|
|
private void protocolMagicProcessing(BinaryReader reader)
|
290 |
|
|
{
|
291 |
|
|
UInt64 value = reader.ReadUInt64();
|
292 |
|
|
if(value != PROTOCOL_MAGIC_VALUE)
|
293 |
|
|
{
|
294 |
|
|
IsRunning = false; // mozna vyhodit vyjimku
|
295 |
|
|
}
|
296 |
|
|
}
|
297 |
|
|
|
298 |
|
|
private void protocolVersionProcessing(BinaryReader reader)
|
299 |
|
|
{
|
300 |
|
|
UInt32 value = reader.ReadUInt32();
|
301 |
|
|
if(value != PROTOCOL_VERSION_VALUE)
|
302 |
|
|
{
|
303 |
|
|
IsRunning = false; // mozna vyhodit vyjimku
|
304 |
|
|
}
|
305 |
|
|
}
|
306 |
|
|
|
307 |
|
|
private void pingProcessing(BinaryReader reader)
|
308 |
|
|
{
|
309 |
|
|
UInt64 pingValue = reader.ReadUInt64();
|
310 |
|
|
|
311 |
|
|
// send pong
|
312 |
|
|
Byte[] buffer = BitConverter.GetBytes(pingValue);
|
313 |
|
|
stream.Write(buffer, 0, buffer.Length);
|
314 |
|
|
}
|
315 |
|
|
|
316 |
|
|
public void start()
|
317 |
|
|
{
|
318 |
|
|
t = new Thread(new ThreadStart(connect));
|
319 |
|
|
IsRunning = true;
|
320 |
|
|
t.Start();
|
321 |
|
|
|
322 |
|
|
}
|
323 |
|
|
|
324 |
|
|
public void stop()
|
325 |
|
|
{
|
326 |
|
|
IsRunning = false;
|
327 |
|
|
t.Join();
|
328 |
|
|
}
|
329 |
|
|
|
330 |
|
|
}
|
331 |
|
|
}
|