public void HandleToClient(CommsInfo info) { if (OnHostToClient != null) { OnHostToClient(info); } }
// A helper method that will marshal a CommsInfo from the client to our UI thread. private void ClientToHost(CommsInfo Info) { // Since it originated from a different thread we need to marshal this back to the current UI thread. //if (this.txtFromClient.InvokeRequired) // this.txtFromClient.Invoke(new delegateCommsInfo(ClientToHost), new object[] { Info }); //else { } Console.WriteLine("[Remoting Server]: Received - " + Info.Message); }
// Called from our t.Start(). A loop invoked by a worker-thread which will monitor the static thread-safe // ClientToServer Queue on the ServerTalk class and passes on any CommsInfo objects that are placed here. // If the variable _ServerStopping turns true it will stop the loop and subsequently the life of the worker-thread. private void CheckClientToServerQueue() { while (!_ServerStopping) { Thread.Sleep(50); // Allow rest of the system to continue whilst waiting... if (RemotingComms.ClientToServerQueue.Count > 0) { // Received a new message, Marshal it to appropriate thread passing it through ClientToHost to update server GUI for example CommsInfo message = (CommsInfo)RemotingComms.ClientToServerQueue.Dequeue(); ClientToHost(message); // Marshall it //---------------------------------------------------------------------------------------------------------- // EXAMPLE (Each game is Different, check your Assembly-CSharp.dll) bool loaded = false; var currentScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene(); if (currentScene.name != "MainMenu") { Game.Settings.RailsCost = message.GConfig.RailsCost; Game.Settings.RailsRefund = message.GConfig.RailsRefund; Game.Settings.EngineMaxVagonsBase = message.GConfig.EngineMaxVagonsBase; Game.Settings.EngineMaxVagonsPerGeneration = message.GConfig.EngineMaxVagonsPerGeneration; Game.Settings.EnginePriceBase = message.GConfig.EnginePriceBase; Game.Settings.EnginePricePerGeneration = message.GConfig.EnginePricePerGeneration; Game.Settings.EnginePricePerUpgrade = message.GConfig.EnginePricePerUpgrade; Game.Settings.EngineSpeedFullLoad = message.GConfig.EngineSpeedFullLoad; Game.Settings.EngineAccelerationFullLoad = message.GConfig.EngineAccelerationFullLoad; loaded = true; } StringBuilder sb = new StringBuilder(); sb.AppendLine(" RailsCost=" + message.GConfig.RailsCost.ToString()); sb.AppendLine(" RailsRefund=" + message.GConfig.RailsRefund.ToString()); sb.AppendLine(" NumBaseCars=" + message.GConfig.EngineMaxVagonsBase.ToString()); sb.AppendLine(" NumCarsPerGen=" + message.GConfig.EngineMaxVagonsPerGeneration.ToString()); sb.AppendLine(" EnginePrice=" + message.GConfig.EnginePriceBase.ToString()); sb.AppendLine(" EnginePricePerGen=" + message.GConfig.EnginePricePerGeneration.ToString()); sb.AppendLine(" EnginePriceUpgrade=" + message.GConfig.EnginePricePerUpgrade.ToString()); sb.AppendLine(" EngineFullLoadSpeed=" + message.GConfig.EngineSpeedFullLoad.ToString()); sb.AppendLine(" EngineFullLoadAccel=" + message.GConfig.EngineAccelerationFullLoad.ToString()); //In this case I'm just sending back the Updated GameConfiguration object if (loaded == true) { Console.WriteLine("[Remoting Server]: Received Message\r\n\r\nGameConfig:\r\n" + sb.ToString()); SendToClient("GameConfig", _Clients.FirstOrDefault(), false, Game.Settings); } else { Console.WriteLine("[Remoting Server]: Received Message\r\n\r\nDefault GameConfig:\r\n" + sb.ToString()); SendToClient("NOTLOADED", _Clients.FirstOrDefault(), false, new GameConfiguration()); } //---------------------------------------------------------------------------------------------------------- } } }
/// <summary> /// This instance method allows a client to send a message to the server /// </summary> /// <param name="Message"></param> public void SendMessageToServer(CommsInfo Message) { _ClientToServer.Enqueue(Message); }