Example #1
0
        internal void AcceptCompleted(IAsyncResult ar)
        {
            ModbusTcpSlave slave = (ModbusTcpSlave)ar.AsyncState;

            try
            {
                // use Socket async API for compact framework compat
                Socket socket = null;
                lock (_serverLock)
                    socket = Server.Server.EndAccept(ar);

                TcpClient client = new TcpClient {
                    Client = socket
                };
                var masterConnection = new ModbusMasterTcpConnection(client, slave);
                masterConnection.ModbusMasterTcpConnectionClosed += (sender, eventArgs) => RemoveMaster(eventArgs.EndPoint);

                lock (_mastersLock)
                    _masters.Add(client.Client.RemoteEndPoint.ToString(), masterConnection);

                _logger.Debug("Accept completed.");

                // Accept another client
                // use Socket async API for compact framework compat
                lock (_serverLock)
                    Server.Server.BeginAccept(AcceptCompleted, slave);
            }
            catch (ObjectDisposedException)
            {
                // this happens when the server stops
            }
        }
Example #2
0
        private static void AcceptCompleted(IAsyncResult ar)
        {
            ModbusTcpSlave slave = (ModbusTcpSlave)ar.AsyncState;

            try
            {
                try
                {
                    // use Socket async API for compact framework compat
                    Socket socket = null;
                    lock (slave._serverLock)
                    {
                        if (slave._server == null) // Checks for disposal to an otherwise unnecessary exception (which is slow and hinders debugging).
                        {
                            return;
                        }
                        try
                        {
                            socket = slave.Server.Server.EndAccept(ar);
                        }
                        catch (Exception) { }
                    }

                    TcpClient client = new TcpClient {
                        Client = socket
                    };
                    var masterConnection = new ModbusMasterTcpConnection(client, slave);
                    masterConnection.ModbusMasterTcpConnectionClosed += slave.OnMasterConnectionClosedHandler;

                    slave._masters.TryAdd(client.Client.RemoteEndPoint.ToString(), masterConnection);

                    Trace.WriteLine(string.Format("Accept completed."));
                }
                catch (IOException ex)
                {
                    // Abandon the connection attempt and continue to accepting the next connection.
                    Trace.WriteLine(string.Format("Accept failed: " + ex.Message));
                }

                // Accept another client
                // use Socket async API for compact framework compat
                lock (slave._serverLock)
                {
                    slave.Server.Server.BeginAccept(state => AcceptCompleted(state), slave);
                }
            }
            catch (ObjectDisposedException)
            {
                // this happens when the server stops
            }
        }
        /// <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 ModbusMasterTcpConnection(client, this);
                masterConnection.ModbusMasterTcpConnectionClosed += OnMasterConnectionClosedHandler;
                _masters.TryAdd(client.Client.RemoteEndPoint.ToString(), masterConnection);
            }
        }
Example #4
0
        internal void AcceptCompleted(IAsyncResult ar)
        {
            ModbusTcpSlave slave = (ModbusTcpSlave)ar.AsyncState;

            try
            {
                // use Socket async API for compact framework compat
                Socket socket = null;
                lock (_serverLock)
                {
                    if (_server == null) // Checks for disposal to an otherwise unnecessary exception (which is slow and hinders debugging).
                    {
                        return;
                    }
                    socket = Server.Server.EndAccept(ar);
                }

                TcpClient client = new TcpClient {
                    Client = socket
                };
                var masterConnection = new ModbusMasterTcpConnection(client, slave);
                masterConnection.ModbusMasterTcpConnectionClosed +=
                    (sender, eventArgs) => RemoveMaster(eventArgs.EndPoint);

                lock (_mastersLock)
                    _masters.Add(client.Client.RemoteEndPoint.ToString(), masterConnection);

                Debug.WriteLine("Accept completed.");

                // Accept another client
                // use Socket async API for compact framework compat
                lock (_serverLock)
                    Server.Server.BeginAccept(AcceptCompleted, slave);
            }
            catch (ObjectDisposedException)
            {
                // this happens when the server stops
            }
        }
Example #5
0
        internal void AcceptCompleted(IAsyncResult ar)
        {
            ModbusTcpSlave slave = (ModbusTcpSlave) ar.AsyncState;

            try
            {
                TcpClient client = _server.EndAcceptTcpClient(ar);
                var masterConnection = new ModbusMasterTcpConnection(client, slave);
                masterConnection.ModbusMasterTcpConnectionClosed += (sender, eventArgs) => RemoveMaster(eventArgs.EndPoint);

                lock (_mastersLock)
                    _masters.Add(client.Client.RemoteEndPoint.ToString(), masterConnection);

                _logger.Debug("Accept completed.");

                // Accept another client
                _server.BeginAcceptTcpClient(AcceptCompleted, slave);
            }
            catch (ObjectDisposedException)
            {
                // this happens when the server stops
            }
        }
Example #6
0
        internal void AcceptCompleted(IAsyncResult ar)
        {
            ModbusTcpSlave slave = (ModbusTcpSlave)ar.AsyncState;

            try
            {
                TcpClient client           = _server.EndAcceptTcpClient(ar);
                var       masterConnection = new ModbusMasterTcpConnection(client, slave);
                masterConnection.ModbusMasterTcpConnectionClosed += (sender, eventArgs) => RemoveMaster(eventArgs.EndPoint);

                lock (_mastersLock)
                    _masters.Add(client.Client.RemoteEndPoint.ToString(), masterConnection);

                _logger.Debug("Accept completed.");

                // Accept another client
                _server.BeginAcceptTcpClient(AcceptCompleted, slave);
            }
            catch (ObjectDisposedException)
            {
                // this happens when the server stops
            }
        }
Example #7
0
        internal void AcceptCompleted(IAsyncResult ar)
        {
            ModbusTcpSlave slave = (ModbusTcpSlave) ar.AsyncState;

            try
            {
                // use Socket async API for compact framework compat
                Socket socket = null;
                lock (_serverLock)
                    socket = Server.Server.EndAccept(ar);

                TcpClient client = new TcpClient { Client = socket };
                var masterConnection = new ModbusMasterTcpConnection(client, slave);
                masterConnection.ModbusMasterTcpConnectionClosed += (sender, eventArgs) => RemoveMaster(eventArgs.EndPoint);

                lock (_mastersLock)
                    _masters.Add(client.Client.RemoteEndPoint.ToString(), masterConnection);

                _logger.Debug("Accept completed.");

                // Accept another client
                // use Socket async API for compact framework compat
                lock (_serverLock)
                    Server.Server.BeginAccept(AcceptCompleted, slave);
            }
            catch (ObjectDisposedException)
            {
                // this happens when the server stops
            }
        }
Example #8
0
        internal void AcceptCompleted(IAsyncResult ar)
        {
            ModbusTcpSlave slave = (ModbusTcpSlave) ar.AsyncState;

            try
            {
                try
                {
                    // use Socket async API for compact framework compat
                    Socket socket = null;
                    lock (_serverLock)
                    {
                        if (_server == null) // Checks for disposal to an otherwise unnecessary exception (which is slow and hinders debugging).
                            return;
                        socket = Server.Server.EndAccept(ar);
                    }

                    TcpClient client = new TcpClient {Client = socket};
                    var masterConnection = new ModbusMasterTcpConnection(client, slave);
                    masterConnection.ModbusMasterTcpConnectionClosed += OnMasterConnectionClosedHandler;

                    _masters.TryAdd(client.Client.RemoteEndPoint.ToString(), masterConnection);

                    Debug.WriteLine("Accept completed.");
                }
                catch (IOException ex)
                {
                    // Abandon the connection attempt and continue to accepting the next connection.
                    Debug.WriteLine("Accept failed: " + ex.Message);
                }

                // Accept another client
                // use Socket async API for compact framework compat
                lock (_serverLock)
                    Server.Server.BeginAccept(AcceptCompleted, slave);
            }
            catch (ObjectDisposedException)
            {
                // this happens when the server stops
            }
        }