Server DispatchOrdersToClients create frame once for all clients.

Avoid creating frame data per client connection. Avoid
the allocation of a memory stream and setting frame header
and copying frame data.
This commit is contained in:
Vapre
2020-11-02 00:08:28 +01:00
committed by Paul Chote
parent 78253ce284
commit ce013f17d6

View File

@@ -660,16 +660,26 @@ namespace OpenRA.Server
} }
} }
byte[] CreateFrame(int client, int frame, byte[] data)
{
var ms = new MemoryStream(data.Length + 12);
ms.WriteArray(BitConverter.GetBytes(data.Length + 4));
ms.WriteArray(BitConverter.GetBytes(client));
ms.WriteArray(BitConverter.GetBytes(frame));
ms.WriteArray(data);
return ms.ToArray();
}
void DispatchOrdersToClient(Connection c, int client, int frame, byte[] data) void DispatchOrdersToClient(Connection c, int client, int frame, byte[] data)
{
DispatchFrameToClient(c, client, CreateFrame(client, frame, data));
}
void DispatchFrameToClient(Connection c, int client, byte[] frameData)
{ {
try try
{ {
var ms = new MemoryStream(data.Length + 12); SendData(c.Socket, frameData);
ms.WriteArray(BitConverter.GetBytes(data.Length + 4));
ms.WriteArray(BitConverter.GetBytes(client));
ms.WriteArray(BitConverter.GetBytes(frame));
ms.WriteArray(data);
SendData(c.Socket, ms.ToArray());
} }
catch (Exception e) catch (Exception e)
{ {
@@ -777,8 +787,9 @@ namespace OpenRA.Server
public void DispatchOrdersToClients(Connection conn, int frame, byte[] data) public void DispatchOrdersToClients(Connection conn, int frame, byte[] data)
{ {
var from = conn != null ? conn.PlayerIndex : 0; var from = conn != null ? conn.PlayerIndex : 0;
var frameData = CreateFrame(from, frame, data);
foreach (var c in Conns.Except(conn).ToList()) foreach (var c in Conns.Except(conn).ToList())
DispatchOrdersToClient(c, from, frame, data); DispatchFrameToClient(c, from, frameData);
if (recorder != null) if (recorder != null)
{ {