Closed robeving closed 3 years ago
Thanks for the PR. I have a few questions to understand your use case.
TcpClient
instance which you got from your own TcpListener
. Is that right?TcpListener
instance to the Modbus Server via it's Start()
method?TcpClient
and do not want to allow more connections?InternalModbusServer : ModbusTcpServer
instead of InternalModbusServer : ModbusServer
? The line request = new ModbusTcpRequestHandler(client, this);
requires this
to derive from ModbusTcpServer
.I ask this because with the new public access modifiers I need to understand the impact since they would now be part of the public API and need to be documented and supported properly. In any case, unprotecting the ProcessRequests
method is good idea.
Thanks :-)
In my case I have a single TcpClient and I don't want any other connections handled by the 'InternalModbusServer'. I want to managed pretty much everything surrounding the transport layer and only want the modbus library to handle datagrams.
By inheriting from ModbusServer I can have a very small implementation which has no understanding of the transport layer BUT using some of the helper code provided by ModbusTcpRequestHandler really helps with the heavy lifting with a TcpClient. Implementing something without the ModbusTcpRequestHandler class would in reality mean a lot of copy and pasted code, hence why I changed it's protection as well.
Sorry I needed several attempts to go through this pull request and think about its implications.
During that I came up with a different approach:
/// <summary>
/// Starts the server. It will use only the provided <see cref="TcpClient"/>.
/// </summary>
/// <param name="tcpClient">The TCP client to communicate with.</param>
public void Start(TcpClient tcpClient)
{
// "base..." is important!
base.Stop();
base.Start();
this.RequestHandlers = new List<ModbusTcpRequestHandler>()
{
new ModbusTcpRequestHandler(tcpClient, this)
};
}
Would that work in your case, too? I.e. just pass your tcp client into a constructor that accepts just that client?
Yes I think that would work
Okay, I have just published a new version. I am sorry that I could not merge your PR but I think it is better to not expose all the internal types. Anyway, making ProcessRequests
available for subclassing is still a good idea so I have included that change.
When there will be other uses cases that require ModbusTcpHandler
and related types to be exposed, I will come back to this PR.
This PR reduces restriction on a number of classes to enable subclassing of the server component. A very simple TCP simple server can now be created with syntax like this:
This is useful for callers who have their own TcpServer implementation