-
Notifications
You must be signed in to change notification settings - Fork 4
/
Sender.cs
48 lines (39 loc) · 964 Bytes
/
Sender.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
using System;
using System.Net.Sockets;
using System.Threading;
using System.Text;
namespace ExtPlaneNet
{
public class Sender
{
protected readonly NetworkStream Stream;
protected readonly ICommandQueue Commands;
protected readonly CancellationToken CancelToken;
public Sender(NetworkStream stream, ICommandQueue commandQueue, CancellationToken cancelToken)
{
if (stream == null)
throw new ArgumentNullException("stream");
if (commandQueue == null)
throw new ArgumentNullException("commandQueue");
Stream = stream;
Commands = commandQueue;
CancelToken = cancelToken;
}
public void Run()
{
while (true)
{
if (CancelToken.IsCancellationRequested)
break;
var command = Commands.TryDequeue();
if (command != null)
{
string data = command.Build();
byte[] buffer = Encoding.UTF8.GetBytes(data);
Stream.Write(buffer, 0, buffer.Length);
}
Thread.Sleep(1);
}
}
}
}