/// <see cref="ITrace.WriteJoin"/> public void WriteJoin(RCThread runningThread, RCThread waitingThread) { if (runningThread == null) { throw new ArgumentNullException("runningThread"); } if (waitingThread == null) { throw new ArgumentNullException("waitingThread"); } RCPackage packageToWrite = RCPackage.CreateCustomDataPackage(RCL_JOIN_FORMAT); packageToWrite.WriteInt(0, runningThread.WrappedThread.ManagedThreadId); packageToWrite.WriteString(1, runningThread.Name); packageToWrite.WriteInt(2, waitingThread.WrappedThread.ManagedThreadId); packageToWrite.WriteString(3, waitingThread.Name); packageToWrite.WriteLong(4, timer.ElapsedMilliseconds); byte[] buffer = new byte[packageToWrite.PackageLength]; packageToWrite.WritePackageToBuffer(buffer, 0); this.outputWriter.Write(buffer); this.outputWriter.Flush(); this.outputStream.Flush(); }
/// <see cref="ITrace.WriteFork"/> public void WriteFork(RCThread newThread, RCThread parentThread) { if (newThread == null) { throw new ArgumentNullException("newThread"); } if (parentThread == null) { throw new ArgumentNullException("parentThread"); } RCPackage packageToWrite = RCPackage.CreateCustomDataPackage(RCL_FORK_FORMAT); packageToWrite.WriteInt(0, newThread.WrappedThread.ManagedThreadId); packageToWrite.WriteString(1, newThread.Name); packageToWrite.WriteInt(2, parentThread.WrappedThread.ManagedThreadId); packageToWrite.WriteString(3, parentThread.Name); packageToWrite.WriteLong(4, timer.ElapsedMilliseconds); byte[] buffer = new byte[packageToWrite.PackageLength]; packageToWrite.WritePackageToBuffer(buffer, 0); this.outputWriter.Write(buffer); this.outputWriter.Flush(); this.outputStream.Flush(); }
/// <summary> /// Starts a background task with the given parameter that runs on a parallel thread. Shall be called from the UI-thread. /// </summary> /// <param name="task">The starting method of the task.</param> /// <param name="name">The name of the executing thread of the task.</param> /// <param name="parameter">The starting parameter of the task.</param> /// <returns>A reference to the interface of the created task.</returns> public static IUIBackgroundTask StartParallelTask(UIParallelTaskMethod task, string name, object parameter) { if (uiThread != null && RCThread.CurrentThread != uiThread) { throw new InvalidOperationException("UITaskManager.StartParallelTask shall be called from the UI-thread!"); } if (calledFromUi) { throw new InvalidOperationException("Recursive call on UITaskManager!"); } if (task == null) { throw new ArgumentNullException("task"); } if (name == null) { throw new ArgumentNullException("name"); } calledFromUi = true; uiThread = RCThread.CurrentThread; RCThread taskThread = new RCThread(ParallelTaskProc, name); UIParallelTask taskData = new UIParallelTask(task, parameter); lock (runningParallelTasks) { runningParallelTasks.Add(taskThread, taskData); } taskThread.Start(taskData); calledFromUi = false; return(taskData); }
/// <see cref="ITrace.WriteJoin"/> public void WriteJoin(RCThread runningThread, RCThread waitingThread) { if (runningThread == null) { throw new ArgumentNullException("runningThread"); } if (waitingThread == null) { throw new ArgumentNullException("waitingThread"); } Console.WriteLine(string.Format("THREAD_JOIN @@@({0}): {1} <-- {2}", DateTime.Now, waitingThread.Name, runningThread.Name)); }
/// <see cref="ITrace.WriteFork"/> public void WriteFork(RCThread newThread, RCThread parentThread) { if (newThread == null) { throw new ArgumentNullException("newThread"); } if (parentThread == null) { throw new ArgumentNullException("parentThread"); } Console.WriteLine(string.Format("THREAD_FORK @@@({0}): {1} --> {2}", DateTime.Now, parentThread.Name, newThread.Name)); }
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); }
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); } }
/// <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; } } }
/// <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); } }
public MainForm() { uiThread = RCThread.CurrentThread; InitializeComponent(); /// Initialize the network List <int> wellKnownBroadcastPorts = new List <int>(); wellKnownBroadcastPorts.Add(25000); wellKnownBroadcastPorts.Add(25001); wellKnownBroadcastPorts.Add(25002); wellKnownBroadcastPorts.Add(25003); this.network = Network.CreateLocalAreaNetwork(wellKnownBroadcastPorts); this.hostThread = null; this.guestThread = null; this.dssThread = null; /// Open the images for the indicators connectedIndicator = (Bitmap)Bitmap.FromFile("connected.png"); openedIndicator = (Bitmap)Bitmap.FromFile("opened.png"); closedIndicator = (Bitmap)Bitmap.FromFile("closed.png"); unknownIndicator = (Bitmap)Bitmap.FromFile("unknown.png"); this.extComboChMgr = new ExtComboChangeMgr(); /// Create the control status managers this.hostCtrlStatusMgr = new HostControlStatusManager(this.picChannelStateHost, this.comboColorHost, this.extComboChMgr); //this.guestCtrlStatusMgrList = new GuestControlStatusManager[0]; this.guestCtrlStatusMgrList = new GuestControlStatusManager[7]; this.guestCtrlStatusMgrList[0] = new GuestControlStatusManager(this.picChannelState0, this.btnOpen0, this.btnClose0, this.comboColor0, 0, this.extComboChMgr); this.guestCtrlStatusMgrList[1] = new GuestControlStatusManager(this.picChannelState1, this.btnOpen1, this.btnClose1, this.comboColor1, 1, this.extComboChMgr); this.guestCtrlStatusMgrList[2] = new GuestControlStatusManager(this.picChannelState2, this.btnOpen2, this.btnClose2, this.comboColor2, 2, this.extComboChMgr); this.guestCtrlStatusMgrList[3] = new GuestControlStatusManager(this.picChannelState3, this.btnOpen3, this.btnClose3, this.comboColor3, 3, this.extComboChMgr); this.guestCtrlStatusMgrList[4] = new GuestControlStatusManager(this.picChannelState4, this.btnOpen4, this.btnClose4, this.comboColor4, 4, this.extComboChMgr); this.guestCtrlStatusMgrList[5] = new GuestControlStatusManager(this.picChannelState5, this.btnOpen5, this.btnClose5, this.comboColor5, 5, this.extComboChMgr); this.guestCtrlStatusMgrList[6] = new GuestControlStatusManager(this.picChannelState6, this.btnOpen6, this.btnClose6, this.comboColor6, 6, this.extComboChMgr); this.ctrlStatusMgr = new ControlStatusManager(this.hostCtrlStatusMgr, this.guestCtrlStatusMgrList, this.lstDssLobbies, this.btnCreateDss, this.btnJoinDss, this.btnLeaveDss, this.btnStartSim, this.extComboChMgr); /// Create and fill the control maps this.indicatorMap = new Dictionary <PictureBox, GuestControlStatusManager>(); for (int i = 0; i < this.guestCtrlStatusMgrList.Length; i++) { this.indicatorMap.Add(this.guestCtrlStatusMgrList[i].PicChannelState, this.guestCtrlStatusMgrList[i]); } this.openButtonMap = new Dictionary <Button, GuestControlStatusManager>(); for (int i = 0; i < this.guestCtrlStatusMgrList.Length; i++) { this.openButtonMap.Add(this.guestCtrlStatusMgrList[i].BtnOpen, this.guestCtrlStatusMgrList[i]); } this.closeButtonMap = new Dictionary <Button, GuestControlStatusManager>(); for (int i = 0; i < this.guestCtrlStatusMgrList.Length; i++) { this.closeButtonMap.Add(this.guestCtrlStatusMgrList[i].BtnClose, this.guestCtrlStatusMgrList[i]); } this.comboBoxMap = new Dictionary <ComboBox, GuestControlStatusManager>(); for (int i = 0; i < this.guestCtrlStatusMgrList.Length; i++) { this.comboBoxMap.Add(this.guestCtrlStatusMgrList[i].CbColorSelector, this.guestCtrlStatusMgrList[i]); } this.lobbyLocatorMarshal = new LobbyLocatorMarshal(this.ctrlStatusMgr, this); //this.uiCallMarshal = new UiCallMarshal(this.ctrlStatusMgr, this); this.simulator = new TestSimulator(this.picDisplay.Width, this.picDisplay.Height, OP_COUNT); this.displayGc = this.picDisplay.CreateGraphics(); }