-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.cs
145 lines (126 loc) · 4.67 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace IPOCS
{
public class Client
{
private TcpClient TcpClient { get; }
private Thread ClientReadThread { get; }
private Timer StaleTimer { get; }
public string Name { get; set; } = string.Empty;
public System.Net.EndPoint RemoteEndpoint
{
get
{
return TcpClient.Client.RemoteEndPoint;
}
}
public bool Connected
{
get
{
return this.TcpClient?.Client != null ? this.TcpClient.Connected : false;
}
}
public Client(TcpClient client)
{
this.TcpClient = client;
this.ClientReadThread = new Thread(new ThreadStart(this.ClientReader));
this.StaleTimer = new Timer(new TimerCallback(StaleTimerFunc), null, 1000, Timeout.Infinite);
this.ClientReadThread.Start();
}
public delegate void OnDisconnectDelegate(Client client);
public event OnDisconnectDelegate OnDisconnect;
public delegate bool? OnConnectionRequestDelegate(Client client, Protocol.Packets.ConnectionRequest request);
public OnConnectionRequestDelegate OnConnectionRequest;
public OnDisconnectDelegate OnConnect;
public delegate void OnMessageDelegate(IPOCS.Protocol.Message msg);
public event OnMessageDelegate OnMessage;
//public ObjectTypes.Concentrator unit { get; private set; } = null;
void StaleTimerFunc(object state)
{
this.TcpClient.Close();
}
private void ClientReader()
{
try
{
while (this.Connected)
{
var buffer = new byte[255];
int recievedCount = 0;
try
{
recievedCount = this.TcpClient.GetStream().Read(buffer, 0, 1);
}
catch { break; }
if (0 == recievedCount)
continue;
try
{
recievedCount += this.TcpClient.GetStream().Read(buffer, 1, buffer[0] - 1);
}
catch { break; }
if (0 == recievedCount)
continue;
// Message received. Parse it.
var message = IPOCS.Protocol.Message.create(buffer.Take(recievedCount).ToArray());
// If unit has not yet sent a ConnectionRequest
if (string.IsNullOrWhiteSpace(this.Name))
{
var pkt = message.packets.FirstOrDefault((p) => p is Protocol.Packets.ConnectionRequest) as Protocol.Packets.ConnectionRequest;
if (pkt == null)
{
// First message must be a Connection Request
break;
}
this.Name = message.RXID_OBJECT;
if (OnConnectionRequest != null)
{
if (!(OnConnectionRequest?.Invoke(this, pkt)).Value)
{
break;
}
}
this.StaleTimer.Change(Timeout.Infinite, Timeout.Infinite);
var responseMsg = new Protocol.Message();
responseMsg.RXID_OBJECT = Name;
responseMsg.packets.Add(new IPOCS.Protocol.Packets.ConnectionResponse
{
RM_PROTOCOL_VERSION = pkt.RM_PROTOCOL_VERSION
});
this.Send(responseMsg);
OnConnect?.Invoke(this);
}
else
// And if not, hand it to listeners
OnMessage?.Invoke(message);
}
} catch (Exception e)
{
throw;
}
finally
{
}
OnDisconnect?.Invoke(this);
}
public void Disconnect()
{
this.TcpClient.Close();
}
public void Send(IPOCS.Protocol.Message msg)
{
var buffer = msg.serialize().ToArray();
try
{
this.TcpClient.GetStream().Write(buffer, 0, buffer.Length);
} catch { }
}
}
}