void Unsubscribe(PacketProcessing Processor)
 {
     Processor.Connected -= ServerProcessor_Connected;
     Processor.ConnectionLost -= ServerProcessor_ConnectionLost;
     Processor.AudioCommandPacketReceived -= ServerProcessor_AudioCommandPacketReceived;
     Processor.CommsIncomingPacketReceived -= ServerProcessor_CommsIncomingPacketReceived;
     Processor.CommsOutgoingPacketReceived -= ServerProcessor_CommsOutgoingPacketReceived;
     Processor.DestroyObjectPacketReceived -= ServerProcessor_DestroyObjectPacketReceived;
     Processor.EngGridUpdatePacketReceived -= ServerProcessor_EngGridUpdatePacketReceived;
     Processor.ExceptionEncountered -= ServerProcessor_ExceptionEncountered;
     Processor.GameMessagePacketReceived -= ServerProcessor_GameMessagePacketReceived;
     Processor.GameStartPacketReceived -= ServerProcessor_GameStartPacketReceived;
     Processor.IncomingAudioPacketReceived -= ServerProcessor_IncomingAudioPacketReceived;
     Processor.IntelPacketReceived -= ServerProcessor_IntelPacketReceived;
     Processor.ObjectStatusUpdatePacketReceived -= ServerProcessor_ObjectStatusUpdatePacketReceived;
     Processor.PackageReceived -= ServerProcessor_PackageReceived;
     Processor.ShipAction2PacketReceived -= ServerProcessor_ShipAction2PacketReceived;
     Processor.ShipAction3PacketReceived -= ServerProcessor_ShipAction3PacketReceived;
     Processor.ShipActionPacketReceived -= ServerProcessor_ShipActionPacketReceived;
     Processor.StationStatusPacketReceived -= ServerProcessor_StationStatusPacketReceived;
     Processor.UndefinedPacketReceived -= ServerProcessor_UndefinedPacketReceived;
     Processor.Unknown2PacketReceived -= ServerProcessor_Unknown2PacketReceived;
     Processor.VersionPacketReceived -= ServerProcessor_VersionPacketReceived;
     Processor.WelcomePacketReceived -= ServerProcessor_WelcomePacketReceived;
     Processor.NewConnectionCreated -= Processor_NewClientConnected;
 }
 void Disconnect()
 {
     if (connector != null)
     {
         ConnectorUnsubscribe();
         connector.Dispose();
         connector = null;
     }
 }
        private void OnConnect(object sender, RoutedEventArgs e)
        {
            string host = Host.Text;
            connector = new PacketProcessing();


            ConnectorSubscribe();
            connector.SetPort(2010);
            connector.SetServerHost(host);
            connector.StartServerConnection();

        }
        public ProxyProcessor(string serverHost, int serverPort, int listeningPort, ProxyType pType, int[] packetFilter)
        {
            PacketProcessing.CrashOnException = false;
            ServerHost = serverHost;
            ServerPort = serverPort;
            if (packetFilter != null)
            {
                filteredPackets = new List<int>(packetFilter);
            }
            else
            {
                filteredPackets = new List<int>();
            }
            proxyType = pType;
            clientProcessor = new PacketProcessing();
            clientProcessor.SetPort(listeningPort);

            Subscribe(clientProcessor);
            
            clientProcessor.StartClientListener();
        }
        private void OnStartServer(object sender, RoutedEventArgs e)
        {
            if (ServerConnection != null)
            {
                ServerConnection.Dispose();
            }
            ServerConnection = new ArtemisComm.PacketProcessing();
            ServerConnection.SetPort(Port);
            ServerConnection.SetServerHost(Host);

            ServerConnection.AudioCommandPacketReceived += conn_AudioCommandPacketReceived;
            ServerConnection.CommsIncomingPacketReceived += conn_CommsIncomingPacketReceived;
            ServerConnection.CommsOutgoingPacketReceived += conn_CommsOutgoingPacketReceived;
            ServerConnection.DestroyObjectPacketReceived += conn_DestroyObjectPacketReceived;
            ServerConnection.EngGridUpdatePacketReceived += conn_EngGridUpdatePacketReceived;
            ServerConnection.GameMessagePacketReceived += conn_GamesMessagePacketReceived;
            ServerConnection.IncomingAudioPacketReceived += conn_IncomingAudioPacketReceived;
            ServerConnection.ObjectStatusUpdatePacketReceived += conn_ObjectStatusUpdatePacketReceived;

            ServerConnection.PackageReceived += conn_PackageReceived;  //This is not necessary (using only for logging) since all other events are subscribed to.

            ServerConnection.ShipActionPacketReceived += conn_ShipActionPacketReceived;
            ServerConnection.ShipAction2PacketReceived += conn_ShipActionPacket2Received;
            ServerConnection.ShipAction3PacketReceived += conn_ShipActionPacket3Received;
            ServerConnection.StationStatusPacketReceived += conn_StationStatusPacketReceived;
            ServerConnection.UndefinedPacketReceived += conn_UndefinedPacketReceived;
            ServerConnection.GameStartPacketReceived += conn_UnknownPacket1Received;
            ServerConnection.Unknown2PacketReceived += conn_UnknownPacket2Received;
            ServerConnection.VersionPacketReceived += conn_VersionPacketReceived;
            ServerConnection.WelcomePacketReceived += conn_WelcomePacketReceived;
            ServerConnection.StartServerConnection();
            btnStartServer.Visibility = Visibility.Collapsed;
            btnStopServer.Visibility = Visibility.Visible;

        }
        void Connect()
        {


            if (!string.IsNullOrEmpty(View.Host) && View.Port > 0)
            {
                Disconnect();
                connector = new PacketProcessing();


                ConnectorSubscribe();
                connector.SetPort(View.Port);
                connector.SetServerHost(View.Host);
                connector.StartServerConnection();

            }
            else
            {
                View.UnableToConnect();
            }
          
        }
        void StartServerConnection()
        {
            if (serverProcessor == null)
            {
                serverProcessor = new PacketProcessing();
                serverProcessor.SetPort(ServerPort);
                serverProcessor.SetServerHost(ServerHost);

                Subscribe(serverProcessor);
                serverProcessor.StartServerConnection();
            }
            else
            {
                if (proxyType == ProxyType.OneServerConnectionToOneClientConnection)
                {
                    serverProcessor.StartServerConnection();

                }
            }

        }