示例#1
0
        /// <summary>
        ///     Start slave listening for requests.
        /// </summary>
        public override async Task ListenAsync()
        {
            Debug.WriteLine("Start Modbus Tcp Server.");
            // TODO: add state {stoped, listening} and check it before starting
            Server.Start();

            while (true)
            {
                TcpClient client = await Server.AcceptTcpClientAsync().ConfigureAwait(false);

                var masterConnection = new ModbusMasterSerialTcpConnection(client, this);
                masterConnection.ModbusMasterTcpConnectionClosed += OnMasterConnectionClosedHandler;
                _masters.TryAdd(client.Client.RemoteEndPoint.ToString(), masterConnection);
            }
        }
示例#2
0
        /// <summary>
        ///     Start slave listening for requests with CancellationToken.
        /// </summary>
        public async Task ListenAsync(CancellationToken stoppingToken)
        {
            //var cancellation = new CancellationTokenSource();
            //await Task.Run(() => listener.AcceptTcpClientAsync(), cancellation.Token);

            //// somewhere in another thread
            //cancellation.Cancel();
            Debug.WriteLine("Start Modbus Tcp Server.");
            // TODO: add state {stoped, listening} and check it before starting
            Server.Start();

            while (true)
            {
                TcpClient client = await Server.AcceptTcpClientAsync().ConfigureAwait(false);

                var masterConnection = new ModbusMasterSerialTcpConnection(client, this);
                masterConnection.ModbusMasterTcpConnectionClosed += OnMasterConnectionClosedHandler;
                _masters.TryAdd(client.Client.RemoteEndPoint.ToString(), masterConnection);
            }
        }