Inheritance: ConnectionEventArgs
Exemplo n.º 1
0
        void RaiseSpecificPacketEvents()
        {
            while (!abort)
            {
                while (SpecificPacketQueue.Count > 0)
                {
                    PackageEventArgs pea = SpecificPacketQueue.Dequeue();
                    try
                    {
                        string methodName = pea.ReceivedPacket.PacketType.ToString() + "Received";

                        Type t = this.GetType();
                        if (t.GetEvent(methodName) != null)
                        {
                            var eventDelegate = (MulticastDelegate)t.GetField(methodName, BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this);
                            OnEvent(eventDelegate as EventHandler <PackageEventArgs>, pea);
                            //if (eventDelegate != null)
                            //{
                            //    foreach (var handler in eventDelegate.GetInvocationList())
                            //    {
                            //        try
                            //        {

                            //            //This code is to make the raising of the event threadsafe with the UI thread.
                            //            ISynchronizeInvoke syncInvoke = handler.Target as ISynchronizeInvoke;
                            //            if (syncInvoke != null && syncInvoke.InvokeRequired)
                            //            {
                            //                syncInvoke.BeginInvoke(handler, new object[] { this, pea });
                            //            }
                            //            else
                            //            {
                            //                handler.Method.Invoke(handler.Target, new object[] { this, pea });
                            //            }

                            //        }
                            //        catch (Exception ex)
                            //        {
                            //            RaiseExceptionEncountered(ex, pea.ID);

                            //        }
                            //    }
                            //}
                        }
                        else
                        {
                            OnEvent(UndefinedPacketReceived, pea);
                        }
                    }
                    catch (Exception e)
                    {
                        RaiseExceptionEncountered(e, pea.ID);
                    }
                }
                if (!abort)
                {
                    mreSpecificPacketReceived.Reset();
                    mreSpecificPacketReceived.WaitOne();
                }
            }
        }
Exemplo n.º 2
0
 protected virtual void OnEvent(EventHandler <PackageEventArgs> handler, PackageEventArgs e)
 {
     if (handler != null)
     {
         foreach (EventHandler <PackageEventArgs> singleCast in handler.GetInvocationList())
         {
             ISynchronizeInvoke syncInvoke = singleCast.Target as ISynchronizeInvoke;
             try
             {
                 //This code is to make the raising of the event threadsafe with the UI thread.
                 if (syncInvoke != null && syncInvoke.InvokeRequired)
                 {
                     syncInvoke.BeginInvoke(singleCast, new object[] { this, e });
                 }
                 else
                 {
                     singleCast(this, e);
                 }
             }
             catch (Exception ex)
             {
                 if (e != null)
                 {
                     RaiseExceptionEncountered(ex, e.ID);
                 }
                 else
                 {
                     RaiseExceptionEncountered(ex, Guid.Empty);
                 }
             }
         }
     }
 }
        void connector_GameStartPacketReceived(object sender, PackageEventArgs e)
        {
            GameInProgress = true;
            //View.GameStarted();
            StartReady2Timer();

        }
Exemplo n.º 4
0
        void QueueToPacketProcessor()
        {
            System.Threading.ThreadStart start = new ThreadStart(RaisePackageReceived);
            RaisePackageReceivedThread = new Thread(start);

            RaisePackageReceivedThread.Start();

            start = new ThreadStart(RaiseSpecificPacketEvents);
            RaiseSpecificPackageReceivedThread = new Thread(start);

            RaiseSpecificPackageReceivedThread.Start();

            do
            {
                lock (ProcessQueue)
                {
                    while (ProcessQueue.Count > 0)
                    {
                        KeyValuePair <byte[], Guid> que = ProcessQueue.Dequeue();
                        byte[] byteArray = que.Key;

                        try
                        {
                            Packet           p   = new Packet(byteArray);
                            PackageEventArgs pea = new PackageEventArgs(p, que.Value);
                            if (PackageReceived != null)
                            {
                                PackageReceivedQueue.Enqueue(pea);

                                mrePacketReceived.Set();
                            }
                            SpecificPacketQueue.Enqueue(pea);
                            mreSpecificPacketReceived.Set();
                        }
                        catch (Exception ex)
                        {
                            RaiseExceptionEncountered(ex, que.Value);
                        }
                        if (abort)
                        {
                            break;
                        }
                    }
                }
                if (!abort)
                {
                    mreListener.Reset();
                    mreListener.WaitOne();
                }
            } while (!abort);
        }
        void connector_GamesMessagePacketReceived(object sender, PackageEventArgs e)
        {
            //GameStart and GameOver are all that matter.
            if (e != null && e.ReceivedPacket != null)
            {
                GameMessagePacket p = e.ReceivedPacket.Package as GameMessagePacket;
                if (p != null)
                {
                    if (p.SubPacketType == GameMessageSubPacketType.GameOverSubPacket)
                    {

                        GameInProgress = !GameInProgress;
                        if (GameInProgress)
                        {
                            //View.GameStarted();
                            StartReady2Timer();
                        }
                        else
                        {
                            //View.GameEnded();
                            StopReady2Timer();

                        }

                    }
                    if (p.SubPacketType == GameMessageSubPacketType.SimulationEndSubPacket)
                    {

                        GameInProgress = false;
                        //View.SimulationEnded();
                    }
                    if (p.SubPacketType == GameMessageSubPacketType.AllShipSettingsSubPacket)
                    {
                        AllShipSettingsSubPacket allships = p.SubPacket as AllShipSettingsSubPacket;
                        if (allships != null && allships.Ships != null && !shipSelected)
                        {
                            //Currently only selects first Ship (Artemis)
                            SelectStationAndReady();
                            //View.GetShipSelection(allships.Ships.ToArray<PlayerShip>());

                        }
                    }
                }
            }

        }
 void conn_ShipActionPacket2Received(object sender, PackageEventArgs e)
 {
     AddPacket(e.ReceivedPacket);
 }
 void conn_UndefinedPacketReceived(object sender, PackageEventArgs e)
 {
     AddPacket(e.ReceivedPacket);
 }
 void conn_StationStatusPacketReceived(object sender, PackageEventArgs e)
 {
     AddPacket(e.ReceivedPacket);
 }
 void conn_CommsIncomingPacketReceived(object sender, PackageEventArgs e)
 {
     AddPacket(e.ReceivedPacket);
 }
 void ServerProcessor_AudioCommandPacketReceived(object sender, PackageEventArgs e)
 {
     if (AudioCommandPacketReceived != null)
     {
         AudioCommandPacketReceived(this, GetProxyPacketEventArgs(e).Value);
     }
 }
 void conn_IncomingAudioPacketReceived(object sender, PackageEventArgs e)
 {
     AddPacket(e.ReceivedPacket);
 }
 void conn_EngGridUpdatePacketReceived(object sender, PackageEventArgs e)
 {
     AddPacket(e.ReceivedPacket);
 }
 void ServerProcessor_DestroyObjectPacketReceived(object sender, PackageEventArgs e)
 {
     if (DestroyObjectPacketReceived != null)
     {
         DestroyObjectPacketReceived(this, GetProxyPacketEventArgs(e).Value);
     }
 }
 void ServerProcessor_EngGridUpdatePacketReceived(object sender, PackageEventArgs e)
 {
     if (EngGridUpdatePacketReceived != null)
     {
         EngGridUpdatePacketReceived(this, GetProxyPacketEventArgs(e).Value);
     }
 }
        static void EnqueuePacket(Queue<PackageEventArgs> que, PackageEventArgs pea, ManualResetEvent mre)
        {
            _log.InfoFormat("Queuing Packet {0} for processing.", pea.ReceivedPacket.PacketType.ToString());
            que.Enqueue(pea);

            mre.Set();


        }
        void QueueToPacketProcessor()
        {
           
            do
            {
                lock (ProcessQueue)
                {
                    mreListener.WaitOne();
                    while (ProcessQueue.Count > 0)
                    {
                        KeyValuePair<Stream, Guid> que = ProcessQueue.Dequeue();

                        if (que.Key != null)
                        {
                            try
                            {

                                Packet p = new Packet(que.Key);
                                if (p != null)
                                {
                                    _log.InfoFormat("Processing packet: {0}, for connection {1}", p.PacketType.ToString(), que.Value.ToString());
                                    PackageEventArgs pea = new PackageEventArgs(p, que.Value);
                                    EnqueueReceivedPacket(pea);
                                    if (p.ConversionException != null)
                                    {
                                        if (CrashOnException)
                                        {
                                            throw new InvalidPacketException(p.ConversionException);
                                        }
                                        else
                                        {
                                            RaiseExceptionEncountered(p.ConversionException, que.Value);
                                        }
                                    }
                                    if (p.Package != null)
                                    {
                                        ReadOnlyCollection<Exception> packetErrors = p.Package.GetErrors();
                                        if (packetErrors.Count > 0)
                                        {
                                            foreach (Exception e in packetErrors)
                                            {
                                                RaiseExceptionEncountered(e, que.Value);
                                            }
                                        }
                                    }
                                    

                                    EnqueueSpecificPacket(pea);

                                }
                                else
                                {

                                }


                            }
                            
                            catch (Exception ex)
                            {
                                if (!abort)
                                {
                                    if (CrashOnException)
                                    {
                                        throw new InvalidPacketException(ex);
                                    }
                                    else
                                    {
                                        RaiseExceptionEncountered(ex, que.Value);
                                    }
                                }
                            }
                        }
                        if (abort)
                        {
                            break;
                        }
                    }


                }
                if (!abort)
                {
                    mreListener.Reset();
                    
                }
            } while (!abort);
        }
        void EnqueueReceivedPacket(PackageEventArgs pea)
        {
           
            if (PackageReceived != null)
            {

                EnqueuePacket(PackageReceivedQueue, pea, mrePacketReceived);
            }
        }
        //void ProcessSpecificPacket(object state)
        //{
        //    PackageEventArgs pea = state as PackageEventArgs;
        //    if (pea != null)
        //    {

        //        string methodName = pea.ReceivedPacket.PacketType.ToString() + "Received";

        //        Type t = this.GetType();

        //        var eventDelegate = (MulticastDelegate)t.GetField(methodName, BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this);
        //        OnEvent(eventDelegate as EventHandler<PackageEventArgs>, pea);

        //    }
        //}
        #endregion
        void EnqueueSpecificPacket(PackageEventArgs pea)
        {

            EnqueuePacket(SpecificPacketQueue, pea, mreSpecificPacketReceived);
        }
        protected virtual void OnEvent(EventHandler<PackageEventArgs> handler, PackageEventArgs e)
        {


            if (handler != null)
            {
                foreach (EventHandler<PackageEventArgs> singleCast in handler.GetInvocationList())
                {
                    ISynchronizeInvoke syncInvoke = singleCast.Target as ISynchronizeInvoke;
                    try
                    {
                        //This code is to make the raising of the event threadsafe with the UI thread.
                        if (syncInvoke != null && syncInvoke.InvokeRequired)
                        {
                            syncInvoke.BeginInvoke(singleCast, new object[] { this, e });
                        }
                        else
                        {
                            _log.InfoFormat("Invoking event {0}", singleCast.Method.Name);
                            singleCast(this, e);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (CrashOnException)
                        {
                            throw new PacketProcessingException(ex);

                        }
                        else
                        {
                            if (e != null)
                            {
                                RaiseExceptionEncountered(ex, e.ID);
                            }
                            else
                            {
                                RaiseExceptionEncountered(ex, Guid.Empty);
                            }
                        }
                    }
                }

            }

        }
 void conn_PackageReceived(object sender, PackageEventArgs e)
 {
     //AddPacket(e.ReceivedPacket);
 }
        void connector_ObjectStatusUpdatePacketReceived(object sender, PackageEventArgs e)
        {
            if (GameInProgress)
            {
                if (e != null)
                {

                    if (e.ReceivedPacket != null)
                    {
                        ObjectStatusUpdatePacket objectStat = e.ReceivedPacket.Package as ObjectStatusUpdatePacket;
                        if (objectStat != null)
                        {
                            if (objectStat.SubPacketType == ObjectStatusUpdateSubPacketType.MainPlayerUpdateSubPacket)
                            {
                                MainPlayerUpdateSubPacket mainPlayer = objectStat.SubPacket as MainPlayerUpdateSubPacket;
                                if (mainPlayer != null)
                                {
                                    // || mainPlayer.ShipNumber == null
                                    if (mainPlayer.RedAlert != null && ((mainPlayer.ShipNumber != null && mainPlayer.ShipNumber == (SelectedShip + 1))))
                                    {

                                        redAlertEnabled = Convert.ToBoolean(mainPlayer.RedAlert.Value);
                                        //View.RedAlertEnabled = redAlertEnabled;
                                    }
                                    // || mainPlayer.ShipNumber == null
                                    if (mainPlayer.ShieldState != null && ((mainPlayer.ShipNumber != null && mainPlayer.ShipNumber == (SelectedShip + 1))))
                                    {
                                        shieldsRaised = Convert.ToBoolean(mainPlayer.ShieldState.Value);
                                        //View.ShieldsRaised = shieldsRaised;
                                        if (shieldsRaised && ShieldsMustBeDown)
                                        {
                                            SendDropShields();
                                        }
                                    }
                                }
                            }
                        }

                    }
                }
            }
        }
 void conn_ObjectStatusUpdatePacketReceived(object sender, PackageEventArgs e)
 {
     AddPacket(e.ReceivedPacket);
 }
 void ServerProcessor_GameMessagePacketReceived(object sender, PackageEventArgs e)
 {
     if (GameMessagePacketReceived != null)
     {
         GameMessagePacketReceived(this, GetProxyPacketEventArgs(e).Value);
     }
 }
 void conn_GamesMessagePacketReceived(object sender, PackageEventArgs e)
 {
     AddPacket(e.ReceivedPacket);
 }
 void ServerProcessor_IntelPacketReceived(object sender, PackageEventArgs e)
 {
     if (IntelPacketReceived != null)
     {
         IntelPacketReceived(this, GetProxyPacketEventArgs(e).Value);
     }
 }
 void conn_DestroyObjectPacketReceived(object sender, PackageEventArgs e)
 {
     AddPacket(e.ReceivedPacket);
 }
 void conn_UnknownPacket1Received(object sender, PackageEventArgs e)
 {
     AddPacket(e.ReceivedPacket);
 }
 void conn_AudioCommandPacketReceived(object sender, PackageEventArgs e)
 {
     AddPacket(e.ReceivedPacket);
 }
 void ServerProcessor_CommsIncomingPacketReceived(object sender, PackageEventArgs e)
 {
     if (CommsIncomingPacketReceived != null)
     {
         CommsIncomingPacketReceived(this, GetProxyPacketEventArgs(e).Value);
     }
 }
        void connector_StationStatusPacketReceived(object sender, PackageEventArgs e)
        {

        }
 //Yeah--all events could have subscribed to the same routine. 
 // But, I might want to do something in my tests specific to certain packets later.
 void conn_WelcomePacketReceived(object sender, PackageEventArgs e)
 {
     AddPacket(e.ReceivedPacket);
 }
 void conn_VersionPacketReceived(object sender, PackageEventArgs e)
 {
     AddPacket(e.ReceivedPacket);
 }
 void ServerProcessor_ObjectStatusUpdatePacketReceived(object sender, PackageEventArgs e)
 {
     if (ObjectStatusUpdatePacketReceived != null)
     {
         ObjectStatusUpdatePacketReceived(this, GetProxyPacketEventArgs(e).Value);
     }
 }