Remove public mutable state from Connection.

This commit is contained in:
Paul Chote
2021-05-05 22:47:36 +01:00
committed by reaperrr
parent dacacdf130
commit 7e79e69eae
2 changed files with 40 additions and 33 deletions

View File

@@ -19,21 +19,29 @@ namespace OpenRA.Server
public class Connection public class Connection
{ {
public const int MaxOrderLength = 131072; public const int MaxOrderLength = 131072;
public Socket Socket;
public List<byte> Data = new List<byte>(); public readonly Socket Socket;
public ReceiveState State = ReceiveState.Header; public readonly List<byte> Data = new List<byte>();
public int ExpectLength = 8; public readonly int PlayerIndex;
public int Frame = 0; public readonly string AuthToken;
public int MostRecentFrame = 0;
public bool Validated;
public long TimeSinceLastResponse => Game.RunTime - lastReceivedTime; public long TimeSinceLastResponse => Game.RunTime - lastReceivedTime;
public bool TimeoutMessageShown = false; public int MostRecentFrame { get; private set; }
public bool TimeoutMessageShown;
public bool Validated;
ReceiveState state = ReceiveState.Header;
int expectLength = 8;
int frame = 0;
long lastReceivedTime = 0; long lastReceivedTime = 0;
/* client data */ public Connection(Socket socket, int playerIndex, string authToken)
public int PlayerIndex; {
public string AuthToken; Socket = socket;
PlayerIndex = playerIndex;
AuthToken = authToken;
}
public byte[] PopBytes(int n) public byte[] PopBytes(int n)
{ {
@@ -85,21 +93,22 @@ namespace OpenRA.Server
public void ReadData(Server server) public void ReadData(Server server)
{ {
if (ReadDataInner(server)) if (ReadDataInner(server))
while (Data.Count >= ExpectLength) {
while (Data.Count >= expectLength)
{ {
var bytes = PopBytes(ExpectLength); var bytes = PopBytes(expectLength);
switch (State) switch (state)
{ {
case ReceiveState.Header: case ReceiveState.Header:
{ {
ExpectLength = BitConverter.ToInt32(bytes, 0) - 4; expectLength = BitConverter.ToInt32(bytes, 0) - 4;
Frame = BitConverter.ToInt32(bytes, 4); frame = BitConverter.ToInt32(bytes, 4);
State = ReceiveState.Data; state = ReceiveState.Data;
if (ExpectLength < 0 || ExpectLength > MaxOrderLength) if (expectLength < 0 || expectLength > MaxOrderLength)
{ {
server.DropClient(this); server.DropClient(this);
Log.Write("server", "Dropping client {0} for excessive order length = {1}", PlayerIndex, ExpectLength); Log.Write("server", "Dropping client {0} for excessive order length = {1}", PlayerIndex, expectLength);
return; return;
} }
@@ -108,17 +117,18 @@ namespace OpenRA.Server
case ReceiveState.Data: case ReceiveState.Data:
{ {
if (MostRecentFrame < Frame) if (MostRecentFrame < frame)
MostRecentFrame = Frame; MostRecentFrame = frame;
server.DispatchOrders(this, Frame, bytes); server.DispatchOrders(this, frame, bytes);
ExpectLength = 8; expectLength = 8;
State = ReceiveState.Header; state = ReceiveState.Header;
break; break;
} }
} }
} }
}
} }
} }

View File

@@ -374,20 +374,17 @@ namespace OpenRA.Server
return; return;
} }
var newConn = new Connection { Socket = newSocket };
// Validate player identity by asking them to sign a random blob of data
// which we can then verify against the player public key database
var token = Convert.ToBase64String(OpenRA.Exts.MakeArray(256, _ => (byte)Random.Next()));
var newConn = new Connection(newSocket, ChooseFreePlayerIndex(), token);
try try
{ {
newConn.Socket.Blocking = false; newConn.Socket.Blocking = false;
newConn.Socket.NoDelay = true; newConn.Socket.NoDelay = true;
// Validate player identity by asking them to sign a random blob of data
// which we can then verify against the player public key database
var token = Convert.ToBase64String(OpenRA.Exts.MakeArray(256, _ => (byte)Random.Next()));
// Assign the player number.
newConn.PlayerIndex = ChooseFreePlayerIndex();
newConn.AuthToken = token;
// Send handshake and client index. // Send handshake and client index.
var ms = new MemoryStream(8); var ms = new MemoryStream(8);
ms.WriteArray(BitConverter.GetBytes(ProtocolVersion.Handshake)); ms.WriteArray(BitConverter.GetBytes(ProtocolVersion.Handshake));