Dwarrowdelf.Server.GameDispatcher.Run C# (CSharp) Method

Run() public method

public Run ( Func workFunc ) : void
workFunc Func
return void
        public void Run(Func<bool> workFunc)
        {
            SynchronizationContext oldSyncCtx = null;

            try
            {
                oldSyncCtx = SynchronizationContext.Current;

                var syncCtx = new GameSyncContext(this);
                SynchronizationContext.SetSynchronizationContext(syncCtx);

                bool again = true;

                while (m_exit == false)
                {
                    if (!again)
                        m_signal.WaitOne();

                    again = false;

                    KeyValuePair<SendOrPostCallback, object> entry;
                    if (m_queue.TryDequeue(out entry))
                    {
                        entry.Key(entry.Value);
                        again |= m_queue.Count > 0;
                    }

                    again |= workFunc();
                }
            }
            finally
            {
                SynchronizationContext.SetSynchronizationContext(oldSyncCtx);
            }
        }

Usage Example

Ejemplo n.º 1
0
        public void Run(EventWaitHandle serverStartWaitHandle)
        {
            this.World.TickStarted  += OnTickStarted;
            this.World.TickEnded    += OnTickEnded;
            this.World.TurnStarting += OnTurnStarting;
            this.World.TurnEnded    += OnTurnEnded;

            PipeConnectionListener.StartListening(_OnNewConnection);
            TcpConnectionListener.StartListening(_OnNewConnection, "SNet");
            DirectConnectionListener.StartListening(_OnNewConnection);

            trace.TraceInformation("The server is ready.");

            if (serverStartWaitHandle != null)
            {
                serverStartWaitHandle.Set();
            }

            CheckForStartTick();

            // Enter the main loop

            m_dispatcher.Run(MainWork);

            trace.TraceInformation("Server exiting");

            DirectConnectionListener.StopListening();
            TcpConnectionListener.StopListening();
            PipeConnectionListener.StopListening();

            this.World.TurnEnded    -= OnTurnEnded;
            this.World.TurnStarting -= OnTurnStarting;
            this.World.TickEnded    -= OnTickEnded;
            this.World.TickStarted  -= OnTickStarted;

            // Need to disconnect the sockets
            foreach (var user in m_users)
            {
                if (user.IsConnected)
                {
                    user.Disconnect();
                }
            }

            trace.TraceInformation("Server exit");
        }