public void BroadCast(string msg) { if (ServerComm == null) { Connection.Send(msg); } else { try { foreach (OnlinePlayer p in Players) { p.Send(msg); } } catch (Exception) { } } }
public void BroadCast(string msg) { #if DEBUG_MULTIPLAYER Trace.TraceInformation("MPServerBroadcast: {0}", msg); #endif if (ServerComm == null) { Connection.Send(msg); } else { try { foreach (OnlinePlayer p in Players) { p.Send(msg); } } catch (Exception) { } } }
/// <summary> /// Update. Determines what messages to send every some seconds /// 1. every one second will send train location /// 2. by defaulr, every 10 seconds will send switch/signal status, this can be changed by in the menu of setting MPUpdateInterval /// 3. housekeeping (remove/add trains, remove players) /// 4. it will also capture key stroke of horn, panto, wiper, bell, headlight etc. /// </summary> public void Update(double newtime) { if (begineZeroTime == 0) { begineZeroTime = newtime - 10; } CheckPlayerTrainSpad();//over speed or pass a red light //server update train location of all if (Server != null && newtime - lastMoveTime >= 1f) { MSGMove move = new MSGMove(); if (Simulator.PlayerLocomotive.Train.TrainType != Train.TRAINTYPE.REMOTE) { move.AddNewItem(GetUserName(), Simulator.PlayerLocomotive.Train); } Server.BroadCast(OnlineTrains.MoveTrains(move)); MSGExhaust exhaust = new MSGExhaust(); // Also updating loco exhaust Train t = Simulator.PlayerLocomotive.Train; for (int iCar = 0; iCar < t.Cars.Count; iCar++) { if (t.Cars[iCar] is MSTSDieselLocomotive) { exhaust.AddNewItem(GetUserName(), t, iCar); } } // Broadcast also exhaust var exhaustMessage = OnlineTrains.ExhaustingLocos(exhaust); if (exhaustMessage != "") { Server.BroadCast(exhaustMessage); } lastMoveTime = lastSendTime = newtime; #if INDIVIDUAL_CONTROL if (Simulator.PlayerLocomotive.Train.TrainType == Train.TRAINTYPE.REMOTE) { Server.BroadCast((new MSGLocoInfo(Simulator.PlayerLocomotive, GetUserName())).ToString()); } #endif } //server updates switch if (Server != null && newtime - lastSwitchTime >= MPUpdateInterval) { lastSwitchTime = lastSendTime = newtime; var switchStatus = new MSGSwitchStatus(); if (switchStatus.OKtoSend) { BroadCast(switchStatus.ToString()); } var signalStatus = new MSGSignalStatus(); if (signalStatus.OKtoSend) { BroadCast(signalStatus.ToString()); } } //client updates itself if (Client != null && Server == null && newtime - lastMoveTime >= 1f) { Train t = Simulator.PlayerLocomotive.Train; MSGMove move = new MSGMove(); MSGExhaust exhaust = new MSGExhaust(); // Also updating loco exhaust //if I am still controlling the train if (t.TrainType != Train.TRAINTYPE.REMOTE) { if (Math.Abs(t.SpeedMpS) > 0.001 || newtime - begineZeroTime < 5f || Math.Abs(t.LastReportedSpeed) > 0) { move.AddNewItem(GetUserName(), t); for (int iCar = 0; iCar < t.Cars.Count; iCar++) { if (t.Cars[iCar] is MSTSDieselLocomotive) { exhaust.AddNewItem(GetUserName(), t, iCar); } } } else { if (Math.Abs(previousSpeed) > 0.001) { begineZeroTime = newtime; } } } //MoveUncoupledTrains(move); //if there are uncoupled trains //if there are messages to send if (move.OKtoSend()) { Client.Send(move.ToString()); if (exhaust.OKtoSend()) { Client.Send(exhaust.ToString()); } lastMoveTime = lastSendTime = newtime; } previousSpeed = t.SpeedMpS; #if INDIVIDUAL_CONTROL if (Simulator.PlayerLocomotive.Train.TrainType == Train.TRAINTYPE.REMOTE) { Client.Send((new MSGLocoInfo(Simulator.PlayerLocomotive, GetUserName())).ToString()); } #endif } //need to send a keep-alive message if have not sent one to the server for the last 30 seconds if (Client != null && Server == null && newtime - lastSendTime >= 30f) { Notify((new MSGAlive(GetUserName())).ToString()); lastSendTime = newtime; } //some players are removed //need to send a keep-alive message if have not sent one to the server for the last 30 seconds if (IsServer() && newtime - lastSyncTime >= 60f) { Notify((new MSGMessage("All", "TimeCheck", Simulator.ClockTime.ToString(System.Globalization.CultureInfo.InvariantCulture))).ToString()); lastSyncTime = newtime; } RemovePlayer(); //some players are disconnected more than 1 minute ago, will not care if they come back later CleanLostPlayers(); //some trains are added/removed HandleTrainList(); //some locos are added/removed if (IsServer()) { HandleLocoList(); } AddPlayer(); //a new player joined? handle it /* will have this in the future so that helpers can also control * //I am a helper, will see if I need to update throttle and dynamic brake * if (Simulator.PlayerLocomotive.Train != null && Simulator.PlayerLocomotive.Train.TrainType == Train.TRAINTYPE.REMOTE) * { * * } * */ }