Move SendData to Connection.

This commit is contained in:
Paul Chote
2021-05-23 15:27:51 +01:00
committed by reaperrr
parent 7c02b4d264
commit 6535411744
2 changed files with 25 additions and 25 deletions

View File

@@ -132,6 +132,29 @@ namespace OpenRA.Server
}
}
public void SendData(byte[] data)
{
var start = 0;
var length = data.Length;
// Non-blocking sends are free to send only part of the data
while (start < length)
{
var sent = Socket.Send(data, start, length - start, SocketFlags.None, out var error);
if (error == SocketError.WouldBlock)
{
Log.Write("server", "Non-blocking send of {0} bytes failed. Falling back to blocking send.", length - start);
Socket.Blocking = true;
sent = Socket.Send(data, start, length - start, SocketFlags.None);
Socket.Blocking = false;
}
else if (error != SocketError.Success)
throw new SocketException((int)error);
start += sent;
}
}
public EndPoint EndPoint => Socket.RemoteEndPoint;
}

View File

@@ -101,29 +101,6 @@ namespace OpenRA.Server
c.Color = pr.LockColor ? pr.Color : c.PreferredColor;
}
static void SendData(Socket s, byte[] data)
{
var start = 0;
var length = data.Length;
// Non-blocking sends are free to send only part of the data
while (start < length)
{
var sent = s.Send(data, start, length - start, SocketFlags.None, out var error);
if (error == SocketError.WouldBlock)
{
Log.Write("server", "Non-blocking send of {0} bytes failed. Falling back to blocking send.", length - start);
s.Blocking = true;
sent = s.Send(data, start, length - start, SocketFlags.None);
s.Blocking = false;
}
else if (error != SocketError.Success)
throw new SocketException((int)error);
start += sent;
}
}
public void Shutdown()
{
State = ServerState.ShuttingDown;
@@ -389,7 +366,7 @@ namespace OpenRA.Server
var ms = new MemoryStream(8);
ms.WriteArray(BitConverter.GetBytes(ProtocolVersion.Handshake));
ms.WriteArray(BitConverter.GetBytes(newConn.PlayerIndex));
SendData(newConn.Socket, ms.ToArray());
newConn.SendData(ms.ToArray());
PreConns.Add(newConn);
@@ -687,7 +664,7 @@ namespace OpenRA.Server
{
try
{
SendData(c.Socket, frameData);
c.SendData(frameData);
}
catch (Exception e)
{