/// <summary> /// Sends the network message. It will skip client messages to send when we are not connected /// </summary> /// <param name="message"></param> private static void SendNetworkMessage(IMessageBase message) { if (NetworkMain.ClientConnection.Status == NetPeerStatus.NotRunning) { NetworkMain.ClientConnection.Start(); } var clientMessage = message as IClientMessageBase; var masterSrvMessage = message as IMasterServerMessageBase; if (clientMessage?.MessageType == ClientMessageType.SyncTime) { SystemsContainer.Get <TimeSyncerSystem>().RewriteMessage(message.Data); } message.Data.SentTime = DateTime.UtcNow.Ticks; var bytes = message.Serialize(SettingsSystem.CurrentSettings.CompressionEnabled); if (bytes != null) { try { NetworkStatistics.LastSendTime = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; if (masterSrvMessage != null && clientMessage == null) { foreach (var masterServer in NetworkServerList.MasterServers) { //Create a new message for every main server otherwise lidgren complains when you reuse the msg var lidgrenMsg = NetworkMain.ClientConnection.CreateMessage(bytes.Length); lidgrenMsg.Write(message.Serialize(SettingsSystem.CurrentSettings.CompressionEnabled)); NetworkMain.ClientConnection.SendUnconnectedMessage(lidgrenMsg, masterServer); NetworkMain.ClientConnection.FlushSendQueue(); } } else { if (SystemsContainer.Get <MainSystem>().NetworkState >= ClientState.Connected) { var lidgrenMsg = NetworkMain.ClientConnection.CreateMessage(bytes.Length); lidgrenMsg.Write(message.Serialize(SettingsSystem.CurrentSettings.CompressionEnabled)); NetworkMain.ClientConnection.SendMessage(lidgrenMsg, message.NetDeliveryMethod, message.Channel); } } NetworkMain.ClientConnection.FlushSendQueue(); } catch (Exception e) { NetworkMain.HandleDisconnectException(e); } } }
/// <summary> /// Sends the network message. It will skip client messages to send when we are not connected /// </summary> /// <param name="message"></param> private static void SendNetworkMessage(IMessageBase message) { if (NetworkMain.ClientConnection.Status == NetPeerStatus.NotRunning) { NetworkMain.ClientConnection.Start(); } message.Data.SentTime = LunaNetworkTime.UtcNow.Ticks; try { NetworkStatistics.LastSendTime = LunaNetworkTime.UtcNow; if (message is IMasterServerMessageBase) { foreach (var masterServer in NetworkServerList.MasterServers) { //Don't reuse lidgren messages, he does that on it's own var lidgrenMsg = NetworkMain.ClientConnection.CreateMessage(message.GetMessageSize()); message.Serialize(lidgrenMsg); NetworkMain.ClientConnection.SendUnconnectedMessage(lidgrenMsg, masterServer); //Force send of packets NetworkMain.ClientConnection.FlushSendQueue(); } } else { if (MainSystem.NetworkState >= ClientState.Connected) { var lidgrenMsg = NetworkMain.ClientConnection.CreateMessage(message.GetMessageSize()); message.Serialize(lidgrenMsg); NetworkMain.ClientConnection.SendMessage(lidgrenMsg, message.NetDeliveryMethod, message.Channel); } } //Force send of packets NetworkMain.ClientConnection.FlushSendQueue(); message.Recycle(); } catch (Exception e) { NetworkMain.HandleDisconnectException(e); } }
/// <summary> /// Sends the network message. It will skip client messages to send when we are not connected, /// except if it's directed at master servers, then it will start the NetClient and socket. /// </summary> private static void SendNetworkMessage(IMessageBase message) { message.Data.SentTime = LunaNetworkTime.UtcNow.Ticks; try { if (message is IMasterServerMessageBase) { if (NetworkMain.ClientConnection.Status == NetPeerStatus.NotRunning) { LunaLog.Log("Starting client to send unconnected message"); NetworkMain.ClientConnection.Start(); } while (NetworkMain.ClientConnection.Status != NetPeerStatus.Running) { LunaLog.Log("Waiting for client to start up to send unconnected message"); // Still trying to start up Thread.Sleep(50); } IPEndPoint[] masterServers; if (string.IsNullOrEmpty(SettingsSystem.CurrentSettings.CustomMasterServer)) { masterServers = MasterServerRetriever.MasterServers.GetValues; } else { masterServers = new[] { LunaNetUtils.CreateEndpointFromString(SettingsSystem.CurrentSettings.CustomMasterServer) }; } foreach (var masterServer in masterServers) { // Don't reuse lidgren messages, it does that on it's own var lidgrenMsg = NetworkMain.ClientConnection.CreateMessage(message.GetMessageSize()); message.Serialize(lidgrenMsg); NetworkMain.ClientConnection.SendUnconnectedMessage(lidgrenMsg, masterServer); } // Force send of packets NetworkMain.ClientConnection.FlushSendQueue(); } else { if (NetworkMain.ClientConnection == null || NetworkMain.ClientConnection.Status == NetPeerStatus.NotRunning || MainSystem.NetworkState < ClientState.Connected) { return; } var lidgrenMsg = NetworkMain.ClientConnection.CreateMessage(message.GetMessageSize()); message.Serialize(lidgrenMsg); NetworkMain.ClientConnection.SendMessage(lidgrenMsg, message.NetDeliveryMethod, message.Channel); // Force send of packets NetworkMain.ClientConnection.FlushSendQueue(); } message.Recycle(); } catch (Exception e) { NetworkMain.HandleDisconnectException(e); } }