private void updateTimer_Tick(object sender, EventArgs e) { if (m_ddd != null) { if (!m_ddd.IsConnected()) { MessageBox.Show("Lost connection to the DDD Server", "DDD Connection Error"); Environment.Exit(0); } m_ddd.ProcessEvents(); timeTextBox.Text = m_ddd.DDDTimeString; if (m_currentRun != null) { if (m_ddd.DDDTimeInt >= m_currentRunFinishTime) { FinishCurrentRun(); } } } else { timeTextBox.Text = "N/A"; } }
private void DDDLoop() { bool isConnected = true; lock (_connection) {//lock so you can disconnect on the main thread isConnected = _connection.IsConnected(); } while (isConnected) { _connection.ProcessEvents(); Thread.Sleep(500); try { lock (_connection) { isConnected = _connection.IsConnected(); } } catch (Exception ex) { isConnected = false; } } //MessageBox.Show("DDD Loop Terminated"); }
private void playerListTimer_Tick(object sender, EventArgs e) { m_serverConnection.ProcessEvents(); switch (m_serverConnection.State) { case DDDServerConnection.SessionStateType.WAITING_FOR_PLAYERS: m_selectedPlayer = (String)playerListBox.SelectedItem; playerListBox.Items.Clear(); List <String> availablePlayers = m_serverConnection.Players; foreach (String dm in availablePlayers) { playerListBox.Items.Add(dm); } playerListBox.SelectedItem = m_selectedPlayer; break; case DDDServerConnection.SessionStateType.LOGGED_IN: playerListTimer.Enabled = false; DialogResult = DialogResult.OK; break; } }
public void T_Connect(object p) { object[] param = p as object[]; String host = ""; int port = -1; try { host = param[0] as string; port = (int)param[1]; } catch (Exception ex) { if (_connectCompleteCallback != null) { _connectCompleteCallback(false, "Hostname or port invalid"); } } bool result = false; lock (_dddLock) { result = _dddConnection.ConnectToServer(host, port); if (!result) { goto end; } result = (result && _dddConnection.ReadSimModel()); if (!result) { goto end; } String myDM = "AgentDM"; //PlayerLoginDialog dlg = new PlayerLoginDialog(ref _dddConnection); //dlg.ShowDialog(); // if (dlg.DialogResult == System.Windows.Forms.DialogResult.Cancel) // { // result = false; // goto end; //} //myDM = _dddConnection.PlayerID; //event callbacks _dddConnection.AddEventCallback("TimeTick", new DDDServerConnection.ProcessSimulationEvent(TimeTick)); _dddConnection.AddEventCallback("RevealObject", new DDDServerConnection.ProcessSimulationEvent(RevealObject)); _dddConnection.AddEventCallback("AttackObject", new DDDServerConnection.ProcessSimulationEvent(AttackObject)); _dddConnection.AddEventCallback("StateChange", new DDDServerConnection.ProcessSimulationEvent(StateChange)); _dddConnection.AddEventCallback("SelfDefenseAttackStarted", new DDDServerConnection.ProcessSimulationEvent(SelfDefense)); _dddConnection.AddEventCallback("TransferObject", new DDDServerConnection.ProcessSimulationEvent(TransferObject)); _dddConnection.AddEventCallback("ViewProActiveRegionUpdate", new DDDServerConnection.ProcessSimulationEvent(ViewProActiveRegionUpdate)); _dddConnection.AddEventCallback("NewObject", new DDDServerConnection.ProcessSimulationEvent(NewObject)); _dddConnection.AddEventCallback("MoveObject", new DDDServerConnection.ProcessSimulationEvent(MoveObject)); _dddConnection.AddEventCallback("MoveDone", new DDDServerConnection.ProcessSimulationEvent(MoveDone)); _dddConnection.AddEventCallback("AttackSucceeded", new DDDServerConnection.ProcessSimulationEvent(AttackSucceeded)); _dddConnection.RequestPlayers(); while (_dddConnection.Players.Count == 0) { Thread.Sleep(500); _dddConnection.ProcessEvents(); } _dddConnection.LoginPlayer(myDM, "OBSERVER"); _dddConnection.GetDMView(myDM); //initializes DM View } _dddLoopThread = new Thread(new ThreadStart(StartDDDLoop)); _dddLoopThread.Start(); end: String msg = ""; if (result) { msg = "Connected to DDD"; } else { msg = "Connection failed to DDD (host: " + host + ";port: " + port.ToString() + ")"; } if (_connectCompleteCallback != null) { _connectCompleteCallback(result, msg); } }
private void buttonConnect_Click(object sender, EventArgs e) { _dddHostname = textBoxHostname.Text; if (Int32.TryParse(textBoxPort.Text, out _dddPort) == false) { return; } try { if (_connection != null) { lock (_connection) { if (_connection.IsConnected()) { if (!DisconnectCurrentDDDSession(true)) { throw new Exception("Unable to disconnect from DDD Server"); } } } } _connection = new DDDServerConnection(); string remoteSimulationModel = String.Format(@"\\{0}\DDDClient\SimulationModel.xml", _dddHostname); lock (_connection) { bool simModelResult = _connection.ReadSimModel(remoteSimulationModel); if (!simModelResult) { MessageBox.Show(String.Format("Error in DDD Connection: Failed to read the simulation model at '{0}', please try again.", remoteSimulationModel), "DDD Connection Error"); //AD: Here we could also ask the user to point to a local copy of the sim model, then re-ReadSimModel. return; } if (!_connection.ConnectToServer(_dddHostname, _dddPort)) { throw new Exception("Connection to DDD failed"); } //need Login as DM? no? _connection.RequestPlayers(); List <string> decisionMakers = new List <string>(); while (decisionMakers.Count == 0) { decisionMakers = _connection.Players; _connection.ProcessEvents(); } string selectedDM = decisionMakers[0];; _connection.LoginPlayer(selectedDM, "OBSERVER"); _connection.GetDMView(selectedDM); //initialize view... // _connection.AddEventCallback("TimeTick", new DDDServerConnection.ProcessSimulationEvent(TimeTick)); _connection.AddEventCallback("ExternalApp_SimStart", new DDDServerConnection.ProcessSimulationEvent(ExternalApp_SimStart)); _connection.AddEventCallback("ExternalApp_SimStop", new DDDServerConnection.ProcessSimulationEvent(ExternalApp_SimStop)); _connection.AddEventCallback("GameSpeed", new DDDServerConnection.ProcessSimulationEvent(GameSpeed)); } try { //just in case _dddLoopThread.Abort(); _dddLoopThread = null; } catch (Exception ex) { } _dddLoopThread = new Thread(new ThreadStart(DDDLoop)); _dddLoopThread.Start(); } catch (Exception ex) { MessageBox.Show("Error connecting to DDD Server:\r\n" + ex.Message); return; } UpdateDDDConnectionStatus("CONNECTED"); ((Button)sender).Enabled = false; groupBox2.Enabled = true; groupBox3.Enabled = true; }