Exemplo n.º 1
0
        public bool ExecuteNextFrame(out RC.Common.RCPackage[] outgoingCmds)
        {
            TraceManager.WriteAllTrace("SIMULATOR_CALL:", TestConsoleTraceFilters.TEST_INFO);
            TraceManager.WriteAllTrace(string.Format("Counter = {0}", this.counter), TestConsoleTraceFilters.TEST_INFO);
            int currTime = (int)this.localTime.ElapsedMilliseconds;

            TraceManager.WriteAllTrace(string.Format("TimeSinceLastFrame = {0}", (currTime - this.timeOfLastFrame)), TestConsoleTraceFilters.TEST_INFO);
            this.avg.NewItem(currTime - this.timeOfLastFrame);
            TraceManager.WriteAllTrace(string.Format("AvgTimeBetweenFrames = {0}", this.avg.Average), TestConsoleTraceFilters.TEST_INFO);
            this.timeOfLastFrame = currTime;
            this.counter++;
            outgoingCmds = null;
            RCThread.Sleep(35);
            return(true);
        }
Exemplo n.º 2
0
 public bool CreateFrameFinish(long timeSinceLastFrameFinish, long timeSinceThisFrameBegin, long timeSinceDrawFinish)
 {
     if (!this.formClosing.WaitOne(0))
     {
         if (timeSinceLastFrameFinish < 30)
         {
             int waitTime = (int)(30 - timeSinceLastFrameFinish);
             RCThread.Sleep(waitTime);
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// The starting function of the connection manager thread.
        /// </summary>
        private void ConnectionManagerProc()
        {
            /// Create the necessary objects.
            Stopwatch cycleStopWatch      = new Stopwatch();
            Stopwatch connAcceptStopWatch = new Stopwatch();

            connAcceptStopWatch.Restart();

            /// This call is only for callback the listener.
            SendLineStateReports();

            do
            {
                cycleStopWatch.Restart();

                /// First we execute the tasks
                ExecuteTasks();
                /// Then we accept the next incoming connection if this is the time to do it.
                if (connAcceptStopWatch.ElapsedMilliseconds > NetworkingSystemConstants.CONNECTION_ACCEPT_FREQUENCY)
                {
                    List <LobbyLineState[]> reportsToSend = new List <LobbyLineState[]>();
                    lock (this.tasks)
                    {
                        /// Execute every tasks just before we accept the next connection. It is needed to
                        /// lock the tasks FIFO, because we want to be sure that the FIFO is empty when
                        /// a new connection is accepted.
                        ExecuteTasks(ref reportsToSend);

                        /// We can accept the next connection after we have processed all outgoing messages and the
                        /// FIFO is empty.
                        AcceptNextConnection(ref reportsToSend);
                    }
                    foreach (LobbyLineState[] report in reportsToSend)
                    {
                        SendLineStateReports(report);
                    }
                    connAcceptStopWatch.Restart();
                }
                /// Then we read and process all incoming messages.
                ProcessIncomingMessages();
                /// And finally we send ping messages if necessary.
                for (int i = 0; i < this.connections.Length; i++)
                {
                    if (this.connections[i].ConnectionState == LobbyConnectionState.Connected)
                    {
                        if (!this.connections[i].SendPingIfNecessary())
                        {
                            /// Error --> immediate shutdown.
                            this.connections[i].Shutdown();
                            this.connections[i].LineState = LobbyLineState.Opened; /// Keep it opened for other clients.
                            SendLineStateReports();
                        }
                    }
                }
            } while (!this.stopConnectionManagerThread.WaitOne(
                         Math.Max(NetworkingSystemConstants.SERVER_CONNECTION_MANAGER_CYCLE_TIME - (int)cycleStopWatch.ElapsedMilliseconds, 0)));

            ExecuteTasks();

            /// LobbyServer shutdown is initiated.
            while (true)
            {
                /// Initiate disconnect to every clients.
                for (int i = 0; i < this.connections.Length; i++)
                {
                    if (this.connections[i].ConnectionState == LobbyConnectionState.Connected)
                    {
                        this.connections[i].BeginDisconnect();
                    }
                    else if (this.connections[i].ConnectionState == LobbyConnectionState.Disconnecting)
                    {
                        this.connections[i].ContinueDisconnect();
                    }
                }
                /// Wait for a while.
                RCThread.Sleep(NetworkingSystemConstants.SERVER_CONNECTION_MANAGER_CYCLE_TIME);

                /// Check if everybody has been disconnected or not.
                bool connectedClientExists = false;
                for (int i = 0; i < this.connections.Length; i++)
                {
                    if (this.connections[i].ConnectionState != LobbyConnectionState.Disconnected)
                    {
                        connectedClientExists = true;
                        break;
                    }
                }

                /// If everybody has been disconnected we can finish the thread.
                if (!connectedClientExists)
                {
                    break;
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// This is the starting function of the connection manager thread.
        /// </summary>
        private void ConnectionManagerProc()
        {
            Stopwatch stopWatch = new Stopwatch();

            if (!this.connection.BeginConnectToTheServer(this.serverEndpoint))
            {
                /// Connection failed at the beginning --> Stop the connection manager thread.
                this.connection.Shutdown();
                if (this.Disposed != null)
                {
                    this.Disposed(this);
                }
                this.listener.LobbyLost();
                return;
            }

            do
            {
                stopWatch.Restart();

                if (this.connection.ConnectionState == LobbyConnectionState.Connecting)
                {
                    /// We must wait for the first line state report.
                    if (!ContinueConnectToTheServer())
                    {
                        /// There was an error, so finish the connection manager thread.
                        return;
                    }
                }
                else if (this.connection.ConnectionState == LobbyConnectionState.Connected)
                {
                    /// Normal message processing.
                    if (!ProcessIncomingMessages())
                    {
                        /// Connection manager thread has to stop.
                        return;
                    }

                    /// Sending outgoing messages
                    if (!SendOutgoingMessages())
                    {
                        /// Connection manager thread has to stop.
                        return;
                    }

                    if (!this.connection.SendPingIfNecessary())
                    {
                        /// Connection manager thread has to stop.
                        this.connection.Shutdown();
                        if (this.Disposed != null)
                        {
                            this.Disposed(this);
                        }
                        this.listener.LobbyLost();
                        return;
                    }
                }
                else
                {
                    /// Unexpected state --> FATAL ERROR
                    throw new NetworkingSystemException("Unexpected connection state!");
                }
            } while (!this.stopConnectionManagerThread.WaitOne(
                         Math.Max(NetworkingSystemConstants.CLIENT_CONNECTION_MANAGER_CYCLE_TIME - (int)stopWatch.ElapsedMilliseconds, 0)));

            /// Send the remaining outgoing messages to the server.
            SendOutgoingMessages();

            /// LobbyClient shutdown is initiated.
            while (true)
            {
                if (this.connection.ConnectionState == LobbyConnectionState.Connected)
                {
                    /// Initiate disconnect from the server
                    this.connection.BeginDisconnect();
                }
                else if (this.connection.ConnectionState == LobbyConnectionState.Disconnecting)
                {
                    if (this.connection.ContinueDisconnect())
                    {
                        /// Disconnect finished --> Stop the connection manager thread.
                        return;
                    }
                }

                /// Wait for a while.
                RCThread.Sleep(NetworkingSystemConstants.CLIENT_CONNECTION_MANAGER_CYCLE_TIME);
            }
        }