Skip to content

Commit

Permalink
Remove public mutable state from Connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
pchote authored and reaperrr committed May 30, 2021
1 parent dacacdf commit 7e79e69
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 33 deletions.
58 changes: 34 additions & 24 deletions OpenRA.Game/Server/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,29 @@ namespace OpenRA.Server
public class Connection
{
public const int MaxOrderLength = 131072;
public Socket Socket;
public List<byte> Data = new List<byte>();
public ReceiveState State = ReceiveState.Header;
public int ExpectLength = 8;
public int Frame = 0;
public int MostRecentFrame = 0;
public bool Validated;

public readonly Socket Socket;
public readonly List<byte> Data = new List<byte>();
public readonly int PlayerIndex;
public readonly string AuthToken;

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;

/* client data */
public int PlayerIndex;
public string AuthToken;
public Connection(Socket socket, int playerIndex, string authToken)
{
Socket = socket;
PlayerIndex = playerIndex;
AuthToken = authToken;
}

public byte[] PopBytes(int n)
{
Expand Down Expand Up @@ -85,21 +93,22 @@ bool ReadDataInner(Server server)
public void ReadData(Server server)
{
if (ReadDataInner(server))
while (Data.Count >= ExpectLength)
{
while (Data.Count >= expectLength)
{
var bytes = PopBytes(ExpectLength);
switch (State)
var bytes = PopBytes(expectLength);
switch (state)
{
case ReceiveState.Header:
{
ExpectLength = BitConverter.ToInt32(bytes, 0) - 4;
Frame = BitConverter.ToInt32(bytes, 4);
State = ReceiveState.Data;
expectLength = BitConverter.ToInt32(bytes, 0) - 4;
frame = BitConverter.ToInt32(bytes, 4);
state = ReceiveState.Data;

if (ExpectLength < 0 || ExpectLength > MaxOrderLength)
if (expectLength < 0 || expectLength > MaxOrderLength)
{
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;
}

Expand All @@ -108,17 +117,18 @@ public void ReadData(Server server)

case ReceiveState.Data:
{
if (MostRecentFrame < Frame)
MostRecentFrame = Frame;
if (MostRecentFrame < frame)
MostRecentFrame = frame;

server.DispatchOrders(this, Frame, bytes);
ExpectLength = 8;
State = ReceiveState.Header;
server.DispatchOrders(this, frame, bytes);
expectLength = 8;
state = ReceiveState.Header;

break;
}
}
}
}
}
}

Expand Down
15 changes: 6 additions & 9 deletions OpenRA.Game/Server/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,20 +374,17 @@ void AcceptConnection(TcpListener listener)
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
{
newConn.Socket.Blocking = false;
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.
var ms = new MemoryStream(8);
ms.WriteArray(BitConverter.GetBytes(ProtocolVersion.Handshake));
Expand Down

0 comments on commit 7e79e69

Please sign in to comment.