diff --git a/Subsurface/Source/Networking/ChatMessage.cs b/Subsurface/Source/Networking/ChatMessage.cs index 3a98cfc6f..161884249 100644 --- a/Subsurface/Source/Networking/ChatMessage.cs +++ b/Subsurface/Source/Networking/ChatMessage.cs @@ -14,7 +14,7 @@ namespace Barotrauma.Networking Default, Error, Dead, Server, Radio } - class ChatMessage : IClientSerializable, IServerSerializable + class ChatMessage { public const float SpeakRange = 2000.0f; @@ -123,10 +123,45 @@ namespace Barotrauma.Networking return sb.ToString(); } - public void ClientWrite(NetOutgoingMessage msg) { } - public void ServerRead(NetIncomingMessage msg, Client c) { } + public void ClientWrite(NetOutgoingMessage msg) + { + msg.Write((byte)ClientNetObject.CHAT_MESSAGE); + msg.Write(NetStateID); + msg.Write(Text); + } - public void ServerWrite(NetOutgoingMessage msg, Client c) { } - public void ClientRead(NetIncomingMessage msg) { } + static public void ServerRead(NetIncomingMessage msg, Client c) + { + UInt32 ID = msg.ReadUInt32(); + string txt = msg.ReadString(); + if (c.lastSentChatMsgID < ID) + { + //this chat message is new to the server + GameMain.Server.AddChatMessage(txt, ChatMessageType.Default, c.name); + c.lastSentChatMsgID = ID; + } + } + + public void ServerWrite(NetOutgoingMessage msg, Client c) + { + msg.Write((byte)ServerNetObject.CHAT_MESSAGE); + msg.Write(NetStateID); + msg.Write((byte)Type); + msg.Write(SenderName); + msg.Write(Text); + } + + static public void ClientRead(NetIncomingMessage msg) + { + UInt32 ID = msg.ReadUInt32(); + ChatMessageType type = (ChatMessageType)msg.ReadByte(); + string senderName = msg.ReadString(); + string txt = msg.ReadString(); + if (ID > LastID) + { + GameMain.Client.AddChatMessage(txt, type, senderName); + LastID = ID; + } + } } } diff --git a/Subsurface/Source/Networking/GameClient.cs b/Subsurface/Source/Networking/GameClient.cs index 6f9f5baaf..22b759d9f 100644 --- a/Subsurface/Source/Networking/GameClient.cs +++ b/Subsurface/Source/Networking/GameClient.cs @@ -35,8 +35,7 @@ namespace Barotrauma.Networking private int nonce; private string saltedPw; - private UInt32 lastRecvChatMsgID = 0; //last message the server received from this client - private UInt32 lastSentChatMsgID = 0; //last message the server has sent to this client + private UInt32 lastSentChatMsgID = 0; //last message this client has successfully sent private List chatMsgQueue = new List(); public byte ID @@ -562,33 +561,25 @@ namespace Barotrauma.Networking GameMain.NetLobbyScreen.TrySelectSub(selectSubName, selectSubHash, GameMain.NetLobbyScreen.SubList); string selectShuttleName = inc.ReadString(); string selectShuttleHash = inc.ReadString(); - GameMain.NetLobbyScreen.TrySelectSub(selectShuttleName, selectShuttleHash, GameMain.NetLobbyScreen.ShuttleList.ListBox); - - GameMain.NetLobbyScreen.SetTraitorsEnabled((YesNoMaybe)inc.ReadRangedInteger(0, 2)); - - GameMain.NetLobbyScreen.SetMissionType(inc.ReadRangedInteger(0, Mission.MissionTypes.Count - 1)); - - GameMain.NetLobbyScreen.SelectMode(inc.ReadByte()); - - GameMain.NetLobbyScreen.LevelSeed = inc.ReadString(); - - bool autoRestartEnabled = inc.ReadBoolean(); - float autoRestartTimer = autoRestartEnabled ? inc.ReadFloat() : 0.0f; - + GameMain.NetLobbyScreen.TrySelectSub(selectShuttleName, selectShuttleHash, GameMain.NetLobbyScreen.ShuttleList.ListBox); + + GameMain.NetLobbyScreen.SetTraitorsEnabled((YesNoMaybe)inc.ReadRangedInteger(0, 2)); + + GameMain.NetLobbyScreen.SetMissionType(inc.ReadRangedInteger(0, Mission.MissionTypes.Count - 1)); + + GameMain.NetLobbyScreen.SelectMode(inc.ReadByte()); + + GameMain.NetLobbyScreen.LevelSeed = inc.ReadString(); + + bool autoRestartEnabled = inc.ReadBoolean(); + float autoRestartTimer = autoRestartEnabled ? inc.ReadFloat() : 0.0f; + GameMain.NetLobbyScreen.SetAutoRestart(autoRestartEnabled, autoRestartTimer); } - lastRecvChatMsgID = inc.ReadUInt32(); + lastSentChatMsgID = inc.ReadUInt32(); break; case ServerNetObject.CHAT_MESSAGE: - UInt32 ID = inc.ReadUInt32(); - ChatMessageType type = (ChatMessageType)inc.ReadByte(); - string senderName = inc.ReadString(); - string msg = inc.ReadString(); - if (ID > lastSentChatMsgID) - { - AddChatMessage(msg, type, senderName); - lastSentChatMsgID = ID; - } + ChatMessage.ClientRead(inc); break; } } @@ -601,18 +592,16 @@ namespace Barotrauma.Networking outmsg.Write((byte)ClientNetObject.SYNC_IDS); outmsg.Write(GameMain.NetLobbyScreen.LastUpdateID); - outmsg.Write(lastSentChatMsgID); + outmsg.Write(ChatMessage.LastID); ChatMessage removeMsg; - while ((removeMsg=chatMsgQueue.Find(cMsg => cMsg.NetStateID <= lastRecvChatMsgID)) != null) + while ((removeMsg=chatMsgQueue.Find(cMsg => cMsg.NetStateID <= lastSentChatMsgID)) != null) { chatMsgQueue.Remove(removeMsg); } foreach (ChatMessage cMsg in chatMsgQueue) { - outmsg.Write((byte)ClientNetObject.CHAT_MESSAGE); - outmsg.Write(cMsg.NetStateID); - outmsg.Write(cMsg.Text); + cMsg.ClientWrite(outmsg); } outmsg.Write((byte)ClientNetObject.END_OF_MESSAGE); client.SendMessage(outmsg, NetDeliveryMethod.Unreliable); diff --git a/Subsurface/Source/Networking/GameServer.cs b/Subsurface/Source/Networking/GameServer.cs index d14805399..1f807e4c4 100644 --- a/Subsurface/Source/Networking/GameServer.cs +++ b/Subsurface/Source/Networking/GameServer.cs @@ -566,14 +566,7 @@ namespace Barotrauma.Networking } break; case ClientNetObject.CHAT_MESSAGE: - UInt32 ID = inc.ReadUInt32(); - string msg = inc.ReadString(); - if (c.lastSentChatMsgID c.lastRecvChatMsgID) { - outmsg.Write((byte)ServerNetObject.CHAT_MESSAGE); - outmsg.Write(cMsg.NetStateID); - outmsg.Write((byte)cMsg.Type); - outmsg.Write(cMsg.SenderName); - outmsg.Write(cMsg.Text); + cMsg.ServerWrite(outmsg,c); } } }