-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractSendCommand.cs
63 lines (54 loc) · 1.86 KB
/
AbstractSendCommand.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
using System;
using System.Text;
using System.Threading;
using NLog;
namespace nats_tools
{
public abstract class AbstractSendCommand<T> : AbstractNatsCommand<T> where T : AbstractSendOptions
{
int NbMessages { get; set; }
protected abstract void DoWork(string msgTxt, byte[] data);
protected AbstractSendCommand(Logger logger) : base(logger)
{
}
public override int Run()
{
NbMessages = 0;
DateTime end = DateTime.Now.AddSeconds(Options.Wait);
while ( (Options.Count > 0 && NbMessages < Options.Count)
|| (Options.Wait > 0 && DateTime.Now < end))
{
var msg = Options.Message;
msg = msg.Replace("{time}", DateTime.Now.ToString("HH:mm:ss.fff"));
msg = msg.Replace("{n}", NbMessages.ToString());
byte[] data;
if (Options.Length > 0)
{
data = new byte[Options.Length];
Encoding.Default.GetBytes(msg, 0, Math.Min(data.Length, msg.Length), data, 0);
}
else
{
data = Encoding.Default.GetBytes(msg);
}
string msgTxt = msg;
if (Options.Length > 0 && msg.Length > Options.Length)
{
msgTxt = msgTxt.Substring(0, Options.Length);
}
if (msgTxt.Length > 80)
{
msgTxt = msgTxt.Substring(0, 80) + "...";
}
DoWork(msgTxt, data);
NbMessages++;
if (Options.Period > 0)
{
Thread.Sleep(Options.Period);
}
}
Options.Connection.Close();
return 0;
}
}
}