-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListenerServer.cs
263 lines (223 loc) · 7.69 KB
/
ListenerServer.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Xml.Serialization;
namespace Unitronics.ComDriver
{
public class ListenerServer : Channel
{
#region Locals
private int m_LocalPort;
private Socket socket;
private Unitronics.ComDriver.EthernetListener.ConnectionStatus m_ConnectionStatus =
Unitronics.ComDriver.EthernetListener.ConnectionStatus.Disconnected;
private object lockObj = new object();
public delegate void ConnectionAcceptedDelegate(PLC plcFromListener);
public delegate void ConnectionStatusChangedDelegate(
Unitronics.ComDriver.EthernetListener.ConnectionStatus connectionStatus);
public event ConnectionAcceptedDelegate OnConnectionAccepted;
public event ConnectionStatusChangedDelegate OnConnectionStatusChanged;
#endregion
#region Properties
public int LocalPort
{
get { return m_LocalPort; }
set
{
if (m_LocalPort != value)
{
if (PLCFactory.ValidateChannelPropertyChange(this, value))
{
if (Status == Unitronics.ComDriver.EthernetListener.ConnectionStatus.Disconnected)
{
m_LocalPort = value;
}
else
{
throw new ComDriveExceptions("Local Port cannot be modified on an opened connection",
ComDriveExceptions.ComDriveException.CommunicationParamsException);
}
}
else
{
throw new ComDriveExceptions(
"Cannot change Local Port since it will result a duplicated channel with the same Local Port",
ComDriveExceptions.ComDriveException.CommunicationParamsException);
}
}
}
}
public override bool Connected
{
get
{
throw new NotImplementedException(
"This class acts only as a gate which creates ListenerClient object when needed.\r\nThe 'Connected' property is individual to each of the ListenerClient objects");
}
}
[XmlIgnoreAttribute]
public Unitronics.ComDriver.EthernetListener.ConnectionStatus Status
{
get { return m_ConnectionStatus; }
private set
{
m_ConnectionStatus = value;
string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this);
try
{
switch (m_ConnectionStatus)
{
case EthernetListener.ConnectionStatus.Listening:
ComDriverLogger.LogConnectionState(DateTime.Now, channelLog,
Unitronics.ComDriver.ConnectionStatus.Listening.ToString());
break;
default:
ComDriverLogger.LogConnectionState(DateTime.Now, channelLog,
Unitronics.ComDriver.ConnectionStatus.ConnectionClosed.ToString());
break;
}
}
catch
{
}
if (OnConnectionStatusChanged != null)
{
OnConnectionStatusChanged(m_ConnectionStatus);
}
}
}
#endregion
#region Constructor
public ListenerServer()
: base()
{
}
public ListenerServer(Int32 localPort, int retry, int TimeOut)
: base(retry, TimeOut)
{
m_LocalPort = localPort;
}
#endregion
#region Public
internal override string GetLoggerChannelText()
{
return Utils.HelperComDriverLogger.GetLoggerChannel(this);
}
internal override void Connect()
{
}
internal void Listen()
{
lock (lockObj)
{
if (Status == EthernetListener.ConnectionStatus.Disconnected || socket == null)
{
try
{
listen();
Status = EthernetListener.ConnectionStatus.Listening;
}
catch
{
throw new ComDriveExceptions(
"Failed binding local port " + m_LocalPort.ToString() +
". Please check that the port is not in use",
ComDriveExceptions.ComDriveException.PortInUse);
}
}
}
}
/// <summary>
/// Closes the Socket connection and releases all associated resources.
/// </summary>
public override void Disconnect()
{
if (base.QueueCount != 0)
return;
try
{
socket.Close();
Status = EthernetListener.ConnectionStatus.Disconnected;
}
catch
{
}
}
internal override bool IsEquivalentChannel(Channel anotherChanel)
{
ListenerServer listenerServer = anotherChanel as ListenerServer;
if (listenerServer.LocalPort == m_LocalPort)
{
return true;
}
else
{
return false;
}
}
internal override void SendString(string text, byte messageEnumerator, bool isIdMessage = false,
bool suppressEthernetHeader = false)
{
throw new NotImplementedException();
}
internal override void SendBytes(byte[] bytes, byte messageEnumerator)
{
throw new NotImplementedException();
}
internal override string ReceiveString()
{
throw new NotImplementedException();
}
internal override byte[] ReceiveBytes()
{
throw new NotImplementedException();
}
#endregion
#region privates
private void listen()
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(IPAddress.Any, LocalPort);
try
{
socket.Bind(ep);
}
catch
{
throw;
}
socket.Listen(100);
socket.BeginAccept(onConnect, null);
}
private void onConnect(IAsyncResult ar)
{
try
{
socket.BeginAccept(onConnect, null);
}
catch
{
Status = EthernetListener.ConnectionStatus.Disconnected;
}
Socket worker = null;
try
{
worker = socket.EndAccept(ar);
ListenerClient client = new ListenerClient(worker, LocalPort, base.Retry, base.TimeOut);
System.Threading.Thread.Sleep(1000);
PLC plc = PLCFactory.GetPLC(client, 0);
if (OnConnectionAccepted != null)
OnConnectionAccepted(plc);
}
catch
{
if (worker != null)
worker.Close();
}
}
#endregion
}
}