Program() { IPAddress ip = Info.GetIp(); TcpListener listener = new TcpListener(ip,Info.Port); storage = new DataStorage(); listener.Start(); int counter = 0; Console.WriteLine("Server started: {0}",DateTime.Now); Console.WriteLine("Server ip: {0}", ip); Console.WriteLine("Server port: {0}",Info.Port); while (true) { TcpClient newClient = listener.AcceptTcpClient(); if (!IsMonitor(newClient)) { counter++; Console.WriteLine("is client"); Client client = new Client(newClient, this, counter, storage); clients.Add(client); } else if (_monitor != null) { if (!_monitor.TcpClient.Connected) { _monitor = new Monitor(newClient,this,clients, storage); } } else { _monitor = new Monitor(newClient,this,clients, storage); } } }
private void Form1_Load(object sender, EventArgs e) { //Parse the IP address IPAddress ipAdd = IPAddress.Parse("localhost"); //Create a new instance of the Monitor class and pass the IP address to the constructor Monitor myMono = new Monitor(ipAdd); //Bind the event to an event handler myMono.StatusChangedEvent += new StatusChangedHandler(myMono_StatusChanged); //Call the metode that starts monitoring for connections myMono.StartMonitoring(); //Let the user know ChatBox.AppendText("Server is ON"); }
public void ResetMonitor() { _monitor.Close(); _monitor = null; }
internal void GameStart() // start communication with client services to send game state { GameState state = null; if (_isReplica) { // Get primary information and state lock (PrimaryServer) { NUM_PLAYERS = PrimaryServer.GetNumberOfPlayers(); MSEC_PER_ROUND = PrimaryServer.GetMsecPerRound(); _replicas = PrimaryServer.GetReplicaDictionary(); _replicas.Remove(Id); // removes himself from the replicas list _clientList = PrimaryServer.GetPlayerList(); foreach (var client in _clientList.Values) { _isDelayed[client.GetPid()] = false; } Inputs = PrimaryServer.GetInputList(); _states = PrimaryServer.GetStateList(); // update replica UI UpdateStatus(Status.ReplicaStandby); foreach (string client in _clientList.Keys) { _form.Invoke(new AddPlayerDelegate(_form.AddPlayer), client); } _imAliveThread.Start(); Monitor.Wait(PrimaryServer); } } else { _imAliveThread.Start(); UpdateStatus(Status.WaitingPlayers); lock (_clientList) { while (_clientList.Count < NUM_PLAYERS) { Monitor.Wait(_clientList); if (!_running) { return; // Someone might terminate the server while waiting for players } } } } UpdateStatus(Status.GameStarted); if (RoundTimestamp >= 0) // initial state has RoundTimestamp = -1, this is in case the primary server crashes and a replica takes place { lock (_states) { state = _states[RoundTimestamp]; } } else { state = CalculateInitialState(); // Initial state state.RoundTimestamp = RoundTimestamp; UpdateRoundTimestamp(); UpdateReplicasState(state); UpdateClients(state); } _acceptingInput = true; while (_running && !state.GameOver()) { Thread.Sleep(MSEC_PER_ROUND); state = CalculateState(state); state.RoundTimestamp = RoundTimestamp; UpdateRoundTimestamp(); UpdateReplicasState(state); UpdateClients(state); lock (_states) { _states.Add(state.Copy()); } } _acceptingInput = false; NotifyGameOver(); UpdateStatus(Status.GameEnd); }