Exemple #1
0
        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);
            }
        }
Exemple #2
0
        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;
        }