protected void OnConnectionReceive(TcpConnection connection, object packet) { _connection.Closed -= OnConnectionClose; _connection.Received -= OnConnectionReceive; try { // Before token is validated var p = packet as Packet; if (p == null) { _eventStream.Publish(new Warning( _self.Path.ToString(), GetType(), $"Receives null packet from {_connection?.RemoteEndPoint}")); return; } if (p.Type != PacketType.System || (p.Message is string) == false) { _eventStream.Publish(new Warning( _self.Path.ToString(), GetType(), $"Receives a bad packet without a message from {_connection?.RemoteEndPoint}")); return; } // Try to open var token = (string)p.Message; var succeeded = _gateway.EstablishChannel(token, _connection); if (succeeded) { // set null to avoid closing connection in PostStop _connection = null; } else { _eventStream.Publish(new Warning( _self.Path.ToString(), GetType(), $"Receives wrong token({token}) from {_connection?.RemoteEndPoint}")); return; } } finally { // This actor will be destroyed anyway after this call. _self.Tell(PoisonPill.Instance); } }