/// <summary>
        /// Clean up transmission object and optionally close it first
        /// </summary>
        internal void CleanupTransmission()
        {
            // First close connection if specified
            _transmission.Disconnect();
            _transmission.Received     -= MessageReceived;
            _transmission.Disconnected -= Disconnected;
            _transmission = null;

            Unregister();
        }
        /// <summary>
        /// Assign connection and publish initial message if already available
        /// </summary>
        /// <param name="transmission">Open connection</param>
        internal void ExecuteAssignConnection(TcpTransmission transmission)
        {
            Logger.Log(LogLevel.Info, "Connection established on port {0}", _config.Port);
            _transmission = transmission;
            _transmission.Disconnected += Disconnected;
            _transmission.Received     += MessageReceived;

            // Configure keep alive on tcp
            if (_config.MonitoringIntervalMs > 0)
            {
                _transmission.ConfigureKeepAlive(_config.MonitoringIntervalMs, _config.MonitoringTimeoutMs);
            }
        }
Exemplo n.º 3
0
        internal void Connected()
        {
            _transmission = new TcpTransmission(_tcpClient, _validator.Interpreter, Logger.GetChild(string.Empty, typeof(TcpTransmission)));
            _transmission.Disconnected     += ConnectionClosed;
            _transmission.ExceptionOccured += OnTransmissionException;
            _transmission.Received         += MessageReceived;
            _transmission.StartReading();

            // Configure TCP keep alive
            if (Config.MonitoringIntervalMs > 0)
            {
                _transmission.ConfigureKeepAlive(Config.MonitoringIntervalMs, Config.MonitoringTimeoutMs);
            }
        }
Exemplo n.º 4
0
        internal void Disconnect()
        {
            var transmission = _transmission;

            _transmission = null;

            var tcpClient = _tcpClient;

            // First close the connection and then unregister events
            transmission.Disconnect();
            transmission.ExceptionOccured -= OnTransmissionException;
            transmission.Received         -= MessageReceived;
            transmission.Disconnected     -= ConnectionClosed;

            tcpClient.Close();
        }
Exemplo n.º 5
0
        private void ClientConnected(IAsyncResult ar)
        {
            TcpClient client;

            try
            {
                client = _tcpListener.EndAcceptTcpClient(ar);
            }
            catch (Exception)
            {
                return;
            }

            // Create new transmission and try to assign
            var tcpTransmission = new TcpTransmission(client, _protocolInterpreter, _logger.GetChild(string.Empty, typeof(TcpTransmission)));

            lock (_listeners)
            {
                if (_listeners.Count == 1 && !_listeners[0].ValidateBeforeAssignment)
                {
                    // If we have only one listenere we can assign directly
                    var listener = _listeners[0];
                    RemoveListener(listener);
                    listener.AssignConnection(tcpTransmission);
                }
                else
                {
                    // Wait for the first message to assign the connection
                    tcpTransmission.Disconnected += TransmissionDisconnected;
                    tcpTransmission.Received     += InitialReceive;
                }


                // If we have listners without a connection keep accepting clients
                if (_listeners.Count > 0)
                {
                    _tcpListener.BeginAcceptTcpClient(ClientConnected, null);
                }
            }
            tcpTransmission.StartReading();
        }
 internal void AssignConnection(TcpTransmission transmission, BinaryMessage message = null)
 {
     lock (_stateLock)
         _state.ConnectionAssigned(transmission, message);
 }
Exemplo n.º 7
0
 private void CleanUp(TcpTransmission transmission)
 {
     transmission.Disconnected -= TransmissionDisconnected;
     transmission.Received     -= InitialReceive;
 }
Exemplo n.º 8
0
 public virtual void ConnectionAssigned(TcpTransmission transmission, BinaryMessage message)
 {
 }