forked from ClassicDIY/ModbusTool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTcpServer.cs
More file actions
110 lines (101 loc) · 4.11 KB
/
TcpServer.cs
File metadata and controls
110 lines (101 loc) · 4.11 KB
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
using System.Diagnostics;
using System.Net.Sockets;
using System.Threading;
using ModbusLib.Protocols;
/*
* Copyright 2012 Mario Vernari (http://cetdevelop.codeplex.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace ModbusLib
{
/// <summary>
/// Concrete implementation of a TCP-listener
/// </summary>
internal class TcpServer
: IpServer
{
public TcpServer(
Socket port,
IProtocol protocol)
: base(port, protocol)
{
}
private const int CacheSize = 300;
internal int IdleTimeout = 60;
/// <summary>
/// Running thread handler
/// </summary>
protected override void Worker()
{
Debug.Print("start");
//start the local timer, which gets the session dying
int counter = IdleTimeout;
using (var timer = new Timer(_ => counter--, null, 1000, 1000))
{
//create a writer for the incoming data
ByteArrayWriter writer = null;
var buffer = new byte[CacheSize];
//loop, until the host closes, or the timer expires
while (_closing == false && counter > 0)
{
//look for incoming data
int length = Port.Available;
if (length > 0)
{
if (length > CacheSize)
length = CacheSize;
//read the data from the physical port
Port.Receive(buffer, length, SocketFlags.None);
Protocol.OnIncommingData(buffer);
//append the data to the writer
if (writer == null)
writer = new ByteArrayWriter();
writer.WriteBytes(buffer, 0, length);
//try to decode the incoming data
var data = new ServerCommData(Protocol);
data.IncomingData = writer.ToReader();
var result = Protocol.Codec.ServerDecode(data);
switch (result.Status)
{
case CommResponse.Ack:
{
//the command is recognized, so call the host back
OnServeCommand(data);
//encode the host data
Protocol.Codec.ServerEncode(data);
//return the resulting data to the remote caller
if (data.OutgoingData != null)
{
byte[] outgoing = data.OutgoingData.ToArray();
Port.Send(outgoing);
Protocol.OnOutgoingData(outgoing);
}
//reset the timer
counter = IdleTimeout;
writer = null;
}
break;
case CommResponse.Ignore:
writer = null;
break;
}
}
Thread.Sleep(100);
}
}
Port.Close();
Debug.Print("close");
}
}
}