Added lobby chat syncing

This commit is contained in:
juanjp600
2016-09-01 20:45:43 -03:00
parent fc457e0f18
commit 028c3a8bc1
7 changed files with 366 additions and 209 deletions

View File

@@ -39,6 +39,9 @@ namespace Barotrauma.Networking
private set;
}
public static UInt32 LastID = 0;
public UInt32 ID = 0;
private ChatMessage(string senderName, string text, ChatMessageType type, Character sender)
{
Text = text;
@@ -49,6 +52,9 @@ namespace Barotrauma.Networking
SenderName = senderName;
TextWithSender = string.IsNullOrWhiteSpace(senderName) ? text : senderName + ": " + text;
LastID++;
ID = LastID;
}
public static ChatMessage Create(string senderName, string text, ChatMessageType type, Character sender)

View File

@@ -29,6 +29,9 @@ namespace Barotrauma.Networking
public string version;
public bool inGame;
public UInt32 lastSentChatMsgID = 0; //last msg this client said
public UInt32 lastRecvChatMsgID = 0; //last msg this client knows about
public List<string> ChatMessages = new List<string>();
public float ChatSpamSpeed;
public float ChatSpamTimer;

View File

@@ -35,6 +35,9 @@ namespace Barotrauma.Networking
private int nonce;
private string saltedPw;
private UInt32 lastRecvChatMsgID = 0; //last message the server received from this client
private List<ChatMessage> chatMsgQueue = new List<ChatMessage>();
public byte ID
{
get { return myID; }
@@ -475,6 +478,11 @@ namespace Barotrauma.Networking
}
}
if (!gameStarted)
{
SendLobbyUpdate();
}
// Update current time
updateTimer = DateTime.Now + updateInterval;
}
@@ -492,10 +500,71 @@ namespace Barotrauma.Networking
if (startGameCoroutine != null && CoroutineManager.IsCoroutineRunning(startGameCoroutine)) return;
while ((inc = client.ReadMessage()) != null)
{
//TODO: read message data
{
switch (inc.MessageType)
{
case NetIncomingMessageType.Data:
ServerPacketHeader header = (ServerPacketHeader)inc.ReadByte();
switch (header)
{
case ServerPacketHeader.UPDATE_LOBBY:
ReadLobbyUpdate(inc);
break;
}
break;
}
}
}
private void ReadLobbyUpdate(NetIncomingMessage inc)
{
lastRecvChatMsgID = inc.ReadUInt32();
ServerNetObject objHeader;
while ((objHeader = (ServerNetObject)inc.ReadByte()) != ServerNetObject.END_OF_MESSAGE)
{
switch (objHeader)
{
case ServerNetObject.CHAT_MESSAGE:
//TODO: READ CHAT MESSAGES FROM SERVER
break;
}
}
}
private void SendLobbyUpdate()
{
NetOutgoingMessage outmsg = client.CreateMessage();
outmsg.Write((byte)ClientPacketHeader.UPDATE_LOBBY);
ChatMessage removeMsg;
while ((removeMsg=chatMsgQueue.Find(cMsg => cMsg.ID <= lastRecvChatMsgID)) != null)
{
chatMsgQueue.Remove(removeMsg);
}
foreach (ChatMessage cMsg in chatMsgQueue)
{
outmsg.Write((byte)ClientNetObject.CHAT_MESSAGE);
outmsg.Write(cMsg.ID);
outmsg.Write(cMsg.Text);
}
outmsg.Write((byte)ClientNetObject.END_OF_MESSAGE);
client.SendMessage(outmsg, NetDeliveryMethod.Unreliable);
}
public override void SendChatMessage(string message, ChatMessageType? type = null)
{
if (client.ServerConnection == null) return;
type = ChatMessageType.Default;
ChatMessage chatMessage = ChatMessage.Create(
gameStarted && myCharacter != null ? myCharacter.Name : name,
message, (ChatMessageType)type, gameStarted ? myCharacter : null);
chatMsgQueue.Add(chatMessage);
}
public bool HasPermission(ClientPermissions permission)
{

View File

@@ -354,7 +354,21 @@ namespace Barotrauma.Networking
foreach (Client c in connectedClients)
{
//c.ReliableChannel.Update(deltaTime);
if (gameStarted)
{
if (c.inGame)
{
ClientWriteIngame(c);
}
else
{
}
}
else
{
ClientWriteLobby(c);
}
//slowly reset spam timers
c.ChatSpamTimer = Math.Max(0.0f, c.ChatSpamTimer - deltaTime);
@@ -382,34 +396,31 @@ namespace Barotrauma.Networking
ClientAuthRequest(inc.SenderConnection);
break;
case ClientPacketHeader.REQUEST_INIT:
ClientInitialize(inc);
ClientInitRequest(inc);
break;
case ClientPacketHeader.UPDATE_LOBBY:
//TODO
ClientReadLobby(inc);
break;
case ClientPacketHeader.UPDATE_INGAME_ALIVE:
//TODO
break;
case ClientPacketHeader.UPDATE_INGAME_SPECTATING:
case ClientPacketHeader.UPDATE_INGAME:
//TODO
break;
}
}
break;
case NetIncomingMessageType.StatusChanged:
switch (inc.SenderConnection.Status)
{
case NetConnectionStatus.Disconnected:
var connectedClient = connectedClients.Find(c => c.Connection == inc.SenderConnection);
/*if (connectedClient != null && !disconnectedClients.Contains(connectedClient))
{
connectedClient.deleteDisconnectedTimer = NetConfig.DeleteDisconnectedTime;
disconnectedClients.Add(connectedClient);
}
*/
DisconnectClient(inc.SenderConnection,
connectedClient != null ? connectedClient.name + " has disconnected" : "");
break;
switch (inc.SenderConnection.Status)
{
case NetConnectionStatus.Disconnected:
var connectedClient = connectedClients.Find(c => c.Connection == inc.SenderConnection);
/*if (connectedClient != null && !disconnectedClients.Contains(connectedClient))
{
connectedClient.deleteDisconnectedTimer = NetConfig.DeleteDisconnectedTime;
disconnectedClients.Add(connectedClient);
}
*/
DisconnectClient(inc.SenderConnection,
connectedClient != null ? connectedClient.name + " has disconnected" : "");
break;
}
break;
case NetIncomingMessageType.ConnectionApproval:
@@ -423,8 +434,8 @@ namespace Barotrauma.Networking
}
else
{
if ((ClientPacketHeader)inc.SenderConnection.RemoteHailMessage.ReadByte() == ClientPacketHeader.REQUEST_AUTH)
{
if ((ClientPacketHeader)inc.SenderConnection.RemoteHailMessage.ReadByte() == ClientPacketHeader.REQUEST_AUTH)
{
inc.SenderConnection.Approve();
ClientAuthRequest(inc.SenderConnection);
}
@@ -522,8 +533,67 @@ namespace Barotrauma.Networking
userID++;
}
return userID;
}
private void ClientReadLobby(NetIncomingMessage inc)
{
Client c = ConnectedClients.Find(x => x.Connection == inc.SenderConnection);
if (c == null)
{
inc.SenderConnection.Disconnect("You're not a connected client.");
return;
}
ClientNetObject objHeader;
while ((objHeader=(ClientNetObject)inc.ReadByte()) != ClientNetObject.END_OF_MESSAGE)
{
switch (objHeader)
{
case ClientNetObject.CHAT_MESSAGE:
UInt32 ID = inc.ReadUInt32();
string msg = inc.ReadString();
if (c.lastSentChatMsgID<ID)
{
//this chat message is new to the server
AddChatMessage(msg, ChatMessageType.Default, c.name);
c.lastSentChatMsgID = ID;
}
break;
}
}
}
private void ClientReadIngame(NetIncomingMessage inc)
{
Client c = ConnectedClients.Find(x => x.Connection == inc.SenderConnection);
if (c == null)
{
inc.SenderConnection.Disconnect("You're not a connected client.");
return;
}
}
private void ClientWriteIngame(Client c)
{
if (c.Character != null && !c.Character.IsDead)
{
}
else
{
}
}
private void ClientWriteLobby(Client c)
{
NetOutgoingMessage outmsg = server.CreateMessage();
outmsg.Write((byte)ServerPacketHeader.UPDATE_LOBBY);
outmsg.Write(c.lastSentChatMsgID); //send this to client so they know which messages weren't received by the server
server.SendMessage(outmsg,c.Connection,NetDeliveryMethod.Unreliable);
}
public bool StartGameClicked(GUIButton button, object obj)
{
Submarine selectedSub = null;
@@ -629,13 +699,13 @@ namespace Barotrauma.Networking
GameMain.NetLobbyScreen.Select();
yield return CoroutineStatus.Success;
}
}
public override void KickPlayer(string playerName, bool ban)
{
playerName = playerName.ToLowerInvariant();
Client client = connectedClients.Find(c =>
Client client = connectedClients.Find(c =>
c.name.ToLowerInvariant() == playerName ||
(c.Character != null && c.Character.Name.ToLowerInvariant() == playerName));
@@ -644,20 +714,20 @@ namespace Barotrauma.Networking
public void KickClient(NetConnection conn, bool ban = false)
{
Client client = connectedClients.Find(c => c.Connection == conn);
if (client == null)
{
conn.Disconnect(ban ? "You have been banned from the server" : "You have been kicked from the server");
if (ban)
{
if (!banList.IsBanned(conn.RemoteEndPoint.Address.ToString()))
{
banList.BanPlayer("Unnamed", conn.RemoteEndPoint.Address.ToString());
}
}
}
else
{
Client client = connectedClients.Find(c => c.Connection == conn);
if (client == null)
{
conn.Disconnect(ban ? "You have been banned from the server" : "You have been kicked from the server");
if (ban)
{
if (!banList.IsBanned(conn.RemoteEndPoint.Address.ToString()))
{
banList.BanPlayer("Unnamed", conn.RemoteEndPoint.Address.ToString());
}
}
}
else
{
KickClient(client, ban);
}
}
@@ -676,12 +746,12 @@ namespace Barotrauma.Networking
DisconnectClient(client, client.name + " has been kicked from the server", "You have been kicked from the server");
}
}
private void DisconnectClient(NetConnection senderConnection, string msg = "", string targetmsg = "")
{
Client client = connectedClients.Find(x => x.Connection == senderConnection);
if (client == null) return;
if (client == null) return;
DisconnectClient(client, msg, targetmsg);
}
@@ -701,10 +771,10 @@ namespace Barotrauma.Networking
if (string.IsNullOrWhiteSpace(msg)) msg = client.name + " has left the server";
if (string.IsNullOrWhiteSpace(targetmsg)) targetmsg = "You have left the server";
Log(msg, ChatMessage.MessageColor[(int)ChatMessageType.Server]);
client.Connection.Disconnect(targetmsg);
Log(msg, ChatMessage.MessageColor[(int)ChatMessageType.Server]);
client.Connection.Disconnect(targetmsg);
GameMain.NetLobbyScreen.RemovePlayer(client.name);
connectedClients.Remove(client);

View File

@@ -30,103 +30,103 @@ namespace Barotrauma.Networking
partial class GameServer : NetworkMember, IPropertyObject
{
List<UnauthenticatedClient> unauthenticatedClients = new List<UnauthenticatedClient>();
private void ClientAuthRequest(NetConnection conn)
{
//client wants to know if server requires password
if (ConnectedClients.Find(c => c.Connection == conn) != null)
{
//this client has already been authenticated
return;
}
UnauthenticatedClient unauthClient = unauthenticatedClients.Find(uc => uc.Connection == conn);
if (unauthClient == null)
{
//new client, generate nonce and add to unauth queue
if (ConnectedClients.Count >= MaxPlayers)
{
//server is full, can't allow new connection
conn.Disconnect("Server full");
return;
}
int nonce = CryptoRandom.Instance.Next();
unauthClient = new UnauthenticatedClient(conn, nonce);
unauthenticatedClients.Add(unauthClient);
}
unauthClient.AuthTimer = 10.0f;
//if the client is already in the queue, getting another unauth request means that our response was lost; resend
NetOutgoingMessage nonceMsg = server.CreateMessage();
nonceMsg.Write((byte)ServerPacketHeader.AUTH_RESPONSE);
if (string.IsNullOrEmpty(password))
{
nonceMsg.Write(false); //false = no password
}
else
{
nonceMsg.Write(true); //true = password
nonceMsg.Write((Int32)unauthClient.Nonce); //here's nonce, encrypt with this
}
server.SendMessage(nonceMsg, conn, NetDeliveryMethod.Unreliable);
}
private void ClientInitialize(NetIncomingMessage inc)
{
if (ConnectedClients.Find(c => c.Connection == inc.SenderConnection) != null)
{
//this client was already authenticated
//another init request means they didn't get any update packets yet
return;
}
UnauthenticatedClient unauthClient = unauthenticatedClients.Find(uc => uc.Connection == inc.SenderConnection);
if (unauthClient == null)
{
//client did not ask for nonce first, can't authorize
inc.SenderConnection.Disconnect("Client did not properly request authentication.");
return;
}
if (!string.IsNullOrEmpty(password))
{
//decrypt message and compare password
string saltedPw = password;
saltedPw = saltedPw + Convert.ToString(unauthClient.Nonce);
saltedPw = Encoding.UTF8.GetString(NetUtility.ComputeSHAHash(Encoding.UTF8.GetBytes(saltedPw)));
string clPw = inc.ReadString();
if (clPw != saltedPw)
{
unauthClient.failedAttempts++;
if (unauthClient.failedAttempts > 3)
{
//disconnect after too many failed attempts
unauthClient.Connection.Disconnect("Too many failed login attempts.");
unauthenticatedClients.Remove(unauthClient);
unauthClient = null;
return;
}
else
{
//not disconnecting the player here, because they'll still use the same connection and nonce if they try logging in again
NetOutgoingMessage reject = server.CreateMessage();
reject.Write((byte)ServerPacketHeader.AUTH_FAILURE);
reject.Write("Wrong password!");
server.SendMessage(reject, unauthClient.Connection, NetDeliveryMethod.Unreliable);
unauthClient.AuthTimer = 10.0f;
return;
}
}
}
string clVersion = inc.ReadString();
string clPackageName = inc.ReadString();
string clPackageHash = inc.ReadString();
List<UnauthenticatedClient> unauthenticatedClients = new List<UnauthenticatedClient>();
private void ClientAuthRequest(NetConnection conn)
{
//client wants to know if server requires password
if (ConnectedClients.Find(c => c.Connection == conn) != null)
{
//this client has already been authenticated
return;
}
UnauthenticatedClient unauthClient = unauthenticatedClients.Find(uc => uc.Connection == conn);
if (unauthClient == null)
{
//new client, generate nonce and add to unauth queue
if (ConnectedClients.Count >= MaxPlayers)
{
//server is full, can't allow new connection
conn.Disconnect("Server full");
return;
}
int nonce = CryptoRandom.Instance.Next();
unauthClient = new UnauthenticatedClient(conn, nonce);
unauthenticatedClients.Add(unauthClient);
}
unauthClient.AuthTimer = 10.0f;
//if the client is already in the queue, getting another unauth request means that our response was lost; resend
NetOutgoingMessage nonceMsg = server.CreateMessage();
nonceMsg.Write((byte)ServerPacketHeader.AUTH_RESPONSE);
if (string.IsNullOrEmpty(password))
{
nonceMsg.Write(false); //false = no password
}
else
{
nonceMsg.Write(true); //true = password
nonceMsg.Write((Int32)unauthClient.Nonce); //here's nonce, encrypt with this
}
server.SendMessage(nonceMsg, conn, NetDeliveryMethod.Unreliable);
}
private void ClientInitRequest(NetIncomingMessage inc)
{
if (ConnectedClients.Find(c => c.Connection == inc.SenderConnection) != null)
{
//this client was already authenticated
//another init request means they didn't get any update packets yet
return;
}
UnauthenticatedClient unauthClient = unauthenticatedClients.Find(uc => uc.Connection == inc.SenderConnection);
if (unauthClient == null)
{
//client did not ask for nonce first, can't authorize
inc.SenderConnection.Disconnect("Client did not properly request authentication.");
return;
}
if (!string.IsNullOrEmpty(password))
{
//decrypt message and compare password
string saltedPw = password;
saltedPw = saltedPw + Convert.ToString(unauthClient.Nonce);
saltedPw = Encoding.UTF8.GetString(NetUtility.ComputeSHAHash(Encoding.UTF8.GetBytes(saltedPw)));
string clPw = inc.ReadString();
if (clPw != saltedPw)
{
unauthClient.failedAttempts++;
if (unauthClient.failedAttempts > 3)
{
//disconnect after too many failed attempts
unauthClient.Connection.Disconnect("Too many failed login attempts.");
unauthenticatedClients.Remove(unauthClient);
unauthClient = null;
return;
}
else
{
//not disconnecting the player here, because they'll still use the same connection and nonce if they try logging in again
NetOutgoingMessage reject = server.CreateMessage();
reject.Write((byte)ServerPacketHeader.AUTH_FAILURE);
reject.Write("Wrong password!");
server.SendMessage(reject, unauthClient.Connection, NetDeliveryMethod.Unreliable);
unauthClient.AuthTimer = 10.0f;
return;
}
}
}
string clVersion = inc.ReadString();
string clPackageName = inc.ReadString();
string clPackageHash = inc.ReadString();
if (clVersion != GameMain.Version.ToString())
{
inc.SenderConnection.Disconnect("Version " + GameMain.Version + " required to connect to the server (Your version: " + clVersion + ")");
unauthenticatedClients.Remove(unauthClient);
unauthenticatedClients.Remove(unauthClient);
unauthClient = null;
DebugConsole.NewMessage(name + " couldn't join the server (wrong game version)", Color.Red);
return;
@@ -134,7 +134,7 @@ namespace Barotrauma.Networking
if (clPackageName != GameMain.SelectedPackage.Name)
{
inc.SenderConnection.Disconnect("Your content package (" + clPackageName + ") doesn't match the server's version (" + GameMain.SelectedPackage.Name + ")");
unauthenticatedClients.Remove(unauthClient);
unauthenticatedClients.Remove(unauthClient);
unauthClient = null;
DebugConsole.NewMessage(name + " couldn't join the server (wrong content package name)", Color.Red);
return;
@@ -142,62 +142,56 @@ namespace Barotrauma.Networking
if (clPackageHash != GameMain.SelectedPackage.MD5hash.Hash)
{
unauthClient.Connection.Disconnect("Your content package (MD5: " + clPackageHash + ") doesn't match the server's version (MD5: " + GameMain.SelectedPackage.MD5hash.Hash + ")");
unauthenticatedClients.Remove(unauthClient);
unauthenticatedClients.Remove(unauthClient);
unauthClient = null;
DebugConsole.NewMessage(name + " couldn't join the server (wrong content package hash)", Color.Red);
return;
}
string clName = Client.SanitizeName(inc.ReadString());
}
string clName = Client.SanitizeName(inc.ReadString());
if (string.IsNullOrWhiteSpace(clName))
{
unauthClient.Connection.Disconnect("You need a name.");
unauthenticatedClients.Remove(unauthClient);
unauthClient = null;
return;
}
{
unauthClient.Connection.Disconnect("You need a name.");
unauthenticatedClients.Remove(unauthClient);
unauthClient = null;
return;
}
if (!Client.IsValidName(name))
{
unauthClient.Connection.Disconnect("Your name contains illegal symbols.");
unauthenticatedClients.Remove(unauthClient);
unauthClient = null;
return;
}
Client nameTaken = ConnectedClients.Find(c => c.name.ToLower() == clName.ToLower());
if (nameTaken != null)
{
if (nameTaken.Connection.RemoteEndPoint.Address.ToString() == inc.SenderEndPoint.Address.ToString())
{
//both name and IP address match, replace this player's connection
nameTaken.Connection.Disconnect("Your session was taken by a new connection on the same IP address.");
nameTaken.Connection = unauthClient.Connection;
unauthenticatedClients.Remove(unauthClient);
unauthClient = null;
return;
}
else
{
//can't authorize this client
unauthClient.Connection.Disconnect("That name is taken.");
unauthenticatedClients.Remove(unauthClient);
unauthClient = null;
return;
}
}
//new client
Client newClient = new Client(clName, GetNewClientID());
newClient.Connection = unauthClient.Connection;
unauthenticatedClients.Remove(unauthClient);
unauthClient = null;
//TEMPORARY TEST CODE; MUST REMOVE
NetOutgoingMessage testMsg = server.CreateMessage();
testMsg.Write((byte)ServerPacketHeader.UPDATE_LOBBY);
server.SendMessage(testMsg, newClient.Connection, NetDeliveryMethod.Unreliable);
//END TEMPORARY TEST CODE
return;
{
unauthClient.Connection.Disconnect("Your name contains illegal symbols.");
unauthenticatedClients.Remove(unauthClient);
unauthClient = null;
return;
}
Client nameTaken = ConnectedClients.Find(c => c.name.ToLower() == clName.ToLower());
if (nameTaken != null)
{
if (nameTaken.Connection.RemoteEndPoint.Address.ToString() == inc.SenderEndPoint.Address.ToString())
{
//both name and IP address match, replace this player's connection
nameTaken.Connection.Disconnect("Your session was taken by a new connection on the same IP address.");
nameTaken.Connection = unauthClient.Connection;
unauthenticatedClients.Remove(unauthClient);
unauthClient = null;
return;
}
else
{
//can't authorize this client
unauthClient.Connection.Disconnect("That name is taken.");
unauthenticatedClients.Remove(unauthClient);
unauthClient = null;
return;
}
}
//new client
Client newClient = new Client(clName, GetNewClientID());
newClient.Connection = unauthClient.Connection;
unauthenticatedClients.Remove(unauthClient);
unauthClient = null;
ConnectedClients.Add(newClient);
return;
}
}
}

View File

@@ -9,21 +9,36 @@ using Barotrauma.Items.Components;
namespace Barotrauma.Networking
{
enum ClientPacketHeader
{
REQUEST_AUTH, //ask the server if a password is needed, if so we'll get nonce for encryption
REQUEST_INIT, //ask the server to give you initialization
UPDATE_LOBBY, //update state in lobby
UPDATE_INGAME_ALIVE, //update state ingame while alive (allow character input)
UPDATE_INGAME_SPECTATING, //update state ingame while spectating/dead
enum ClientPacketHeader
{
REQUEST_AUTH, //ask the server if a password is needed, if so we'll get nonce for encryption
REQUEST_INIT, //ask the server to give you initialization
UPDATE_LOBBY, //update state in lobby
UPDATE_INGAME, //update state ingame while alive
}
enum ServerPacketHeader
enum ClientNetObject
{
AUTH_RESPONSE, //tell the player if they require a password to log in
AUTH_FAILURE, //the server won't authorize player yet, however connection is still alive
UPDATE_LOBBY, //update state in lobby (votes and chat messages)
UPDATE_INGAME_ALIVE, //update state ingame while alive (character input and chat messages)
UPDATE_INGAME_SPECTATING, //update state ingame while spectating/dead (chat messages)
END_OF_MESSAGE, //self-explanatory
CHAT_MESSAGE, //also self-explanatory
VOTE, //you get the idea
CHARACTER_INPUT,
ITEM_INTERACTION
}
enum ServerPacketHeader
{
AUTH_RESPONSE, //tell the player if they require a password to log in
AUTH_FAILURE, //the server won't authorize player yet, however connection is still alive
UPDATE_LOBBY, //update state in lobby (votes and chat messages)
UPDATE_INGAME, //update state ingame while alive (character input and chat messages)
}
enum ServerNetObject
{
END_OF_MESSAGE,
CHAT_MESSAGE,
VOTE,
CHARACTER_POSITION,
ITEM_STATE
}
enum VoteType

View File

@@ -989,7 +989,7 @@ namespace Barotrauma
((chatBox.CountChildren % 2) == 0) ? Color.Transparent : Color.Black*0.1f, message.Color,
Alignment.Left, GUI.Style, null, true);
msg.Font = GUI.SmallFont;
msg.UserData = message.SenderName;
msg.UserData = message;
msg.CanBeFocused = false;
msg.Padding = new Vector4(20, 0, 0, 0);