-
Notifications
You must be signed in to change notification settings - Fork 1
/
Client.cs
84 lines (75 loc) · 2.19 KB
/
Client.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Net;
namespace SerialToNetDotnet
{
class Client
{
const int telnetCmdLen = 2;
public enum State
{
signing,
normal,
iac, // Interpret as command
}
public enum SignatureAppendResult
{
finished,
resume,
empty,
}
public State m_state { get; set; }
private State m_prevState = State.normal;
private IPEndPoint m_remoteEp;
private readonly uint m_id;
private byte[] m_telnetCmd;
private int m_iacByteCounter;
private Server m_server;
public string m_signature { get; set; } = "";
public Client(uint clientId, IPEndPoint remoteEp, Server server)
{
this.m_state = State.signing;
this.m_id = clientId;
this.m_remoteEp = remoteEp;
this.m_server = server;
this.m_telnetCmd = new byte[telnetCmdLen];
this.m_iacByteCounter = 0;
}
public void ToIacState()
{
m_prevState = m_state;
m_state = State.iac;
m_iacByteCounter = 0;
}
public void AddIacByte(byte v)
{
m_telnetCmd[m_iacByteCounter] = v;
++m_iacByteCounter;
if (m_iacByteCounter == telnetCmdLen)
{
m_state = m_prevState;
// TODO interpret command, call server
}
}
public SignatureAppendResult AddSignatureChar(char ch, char inputEndChar)
{
// CR - finish signature input if it is not empty
if (ch == inputEndChar)
{
if (m_signature.Length != 0)
{
m_state = State.normal;
return SignatureAppendResult.finished;
}
else
return SignatureAppendResult.empty;
}
else if (!Char.IsControl(ch))
{
m_signature += ch;
return SignatureAppendResult.resume;
}
else
return SignatureAppendResult.resume;
}
}
}