Server limits chatmessage length and the number of chatmessages written in one packet

This commit is contained in:
Regalis
2017-05-08 19:06:05 +03:00
parent 152a92e600
commit 7dad837733
6 changed files with 47 additions and 16 deletions
+18
View File
@@ -735,6 +735,24 @@ namespace Barotrauma
GameMain.Server.CreateEntityEvent(wall); GameMain.Server.CreateEntityEvent(wall);
} }
break; break;
case "spamchatmessages":
int msgCount = 1000;
if (commands.Length > 1) int.TryParse(commands[1], out msgCount);
int msgLength = 50;
if (commands.Length > 2) int.TryParse(commands[2], out msgLength);
for (int i = 0; i < msgCount; i++)
{
if (GameMain.Server != null)
{
GameMain.Server.SendChatMessage(ToolBox.RandomSeed(msgLength), ChatMessageType.Default);
}
else
{
GameMain.Client.SendChatMessage(ToolBox.RandomSeed(msgLength));
}
}
break;
#endif #endif
case "cleanbuild": case "cleanbuild":
GameMain.Config.MusicVolume = 0.5f; GameMain.Config.MusicVolume = 0.5f;
+13 -9
View File
@@ -28,7 +28,7 @@ namespace Barotrauma
public bool CaretEnabled; public bool CaretEnabled;
private int? maxTextWidth; private int? maxTextLength;
public GUITextBlock.TextGetterHandler TextGetter public GUITextBlock.TextGetterHandler TextGetter
{ {
@@ -42,14 +42,13 @@ namespace Barotrauma
set { textBlock.Wrap = value; } set { textBlock.Wrap = value; }
} }
public int? MaxTextWidth public int? MaxTextLength
{ {
get { return maxTextWidth; } get { return maxTextLength; }
set set
{ {
textBlock.OverflowClip = value != null && (int)value > textBlock.Rect.Width - textBlock.Padding.X - textBlock.Padding.Z; textBlock.OverflowClip = true;
maxTextLength = value;
maxTextWidth = value;
} }
} }
@@ -142,9 +141,14 @@ namespace Barotrauma
{ {
if (!Wrap) if (!Wrap)
{ {
int maxWidth = MaxTextWidth == null ? (int)(textBlock.Rect.Width - textBlock.Padding.X - textBlock.Padding.Z) : (int)MaxTextWidth; if (maxTextLength != null)
{
if (Font.MeasureString(textBlock.Text).X > maxWidth) if (Text.Length > maxTextLength)
{
Text = textBlock.Text.Substring(0, (int)maxTextLength);
}
}
else if (Font.MeasureString(textBlock.Text).X > (int)(textBlock.Rect.Width - textBlock.Padding.X - textBlock.Padding.Z))
{ {
Text = textBlock.Text.Substring(0, textBlock.Text.Length - 1); Text = textBlock.Text.Substring(0, textBlock.Text.Length - 1);
} }
+8 -1
View File
@@ -6,7 +6,6 @@ using System.Text;
namespace Barotrauma.Networking namespace Barotrauma.Networking
{ {
enum ChatMessageType enum ChatMessageType
{ {
Default, Error, Dead, Server, Radio, Private Default, Error, Dead, Server, Radio, Private
@@ -14,6 +13,8 @@ namespace Barotrauma.Networking
class ChatMessage class ChatMessage
{ {
public const int MaxLength = 150;
public const float SpeakRange = 2000.0f; public const float SpeakRange = 2000.0f;
public static Color[] MessageColor = public static Color[] MessageColor =
@@ -138,6 +139,12 @@ namespace Barotrauma.Networking
{ {
UInt16 ID = msg.ReadUInt16(); UInt16 ID = msg.ReadUInt16();
string txt = msg.ReadString(); string txt = msg.ReadString();
if (txt.Length > MaxLength)
{
txt = txt.Substring(0, MaxLength);
}
if (NetIdUtils.IdMoreRecent(ID, c.lastSentChatMsgID)) if (NetIdUtils.IdMoreRecent(ID, c.lastSentChatMsgID))
{ {
//this chat message is new to the server //this chat message is new to the server
+6 -4
View File
@@ -841,11 +841,13 @@ namespace Barotrauma.Networking
outmsg.Write(c.lastSentEntityEventID); outmsg.Write(c.lastSentEntityEventID);
c.chatMsgQueue.RemoveAll(cMsg => !NetIdUtils.IdMoreRecent(cMsg.NetStateID, c.lastRecvChatMsgID)); c.chatMsgQueue.RemoveAll(cMsg => !NetIdUtils.IdMoreRecent(cMsg.NetStateID, c.lastRecvChatMsgID));
foreach (ChatMessage cMsg in c.chatMsgQueue)
int maxChatMsgsPerPacket = 50;
for (int i = 0; i < c.chatMsgQueue.Count && i < maxChatMsgsPerPacket; i++)
{ {
cMsg.ServerWrite(outmsg, c); c.chatMsgQueue[i].ServerWrite(outmsg, c);
} }
//don't send position updates to characters who are still midround syncing //don't send position updates to characters who are still midround syncing
//characters or items spawned mid-round don't necessarily exist at the client's end yet //characters or items spawned mid-round don't necessarily exist at the client's end yet
if (!c.NeedsMidRoundSync) if (!c.NeedsMidRoundSync)
@@ -158,7 +158,7 @@ namespace Barotrauma.Networking
new Rectangle(chatBox.Rect.X, chatBox.Rect.Y + chatBox.Rect.Height + 20, chatBox.Rect.Width, 25), new Rectangle(chatBox.Rect.X, chatBox.Rect.Y + chatBox.Rect.Height + 20, chatBox.Rect.Width, 25),
Color.White * 0.5f, Color.Black, Alignment.TopLeft, Alignment.Left, "", inGameHUD); Color.White * 0.5f, Color.Black, Alignment.TopLeft, Alignment.Left, "", inGameHUD);
chatMsgBox.Font = GUI.SmallFont; chatMsgBox.Font = GUI.SmallFont;
chatMsgBox.MaxTextWidth = chatBox.Rect.Width * 2; chatMsgBox.MaxTextLength = ChatMessage.MaxLength;
chatMsgBox.Padding = Vector4.Zero; chatMsgBox.Padding = Vector4.Zero;
chatMsgBox.OnEnterPressed = EnterChatMessage; chatMsgBox.OnEnterPressed = EnterChatMessage;
chatMsgBox.OnTextChanged = TypingChatMessage; chatMsgBox.OnTextChanged = TypingChatMessage;
+1 -1
View File
@@ -195,7 +195,7 @@ namespace Barotrauma
chatBox = new GUIListBox(new Rectangle(0,0,0,chatFrame.Rect.Height-80), Color.White, "", chatFrame); chatBox = new GUIListBox(new Rectangle(0,0,0,chatFrame.Rect.Height-80), Color.White, "", chatFrame);
textBox = new GUITextBox(new Rectangle(0, 25, 0, 25), Alignment.Bottom, "", chatFrame); textBox = new GUITextBox(new Rectangle(0, 25, 0, 25), Alignment.Bottom, "", chatFrame);
textBox.MaxTextWidth = textBox.Rect.Width * 2; textBox.MaxTextLength = ChatMessage.MaxLength;
textBox.Font = GUI.SmallFont; textBox.Font = GUI.SmallFont;
//player info panel ------------------------------------------------------------ //player info panel ------------------------------------------------------------