working on readability of Server

This commit is contained in:
Bob
2010-01-25 19:27:17 +13:00
parent 47a60298a8
commit dfbca88f4a
2 changed files with 64 additions and 65 deletions

View File

@@ -18,6 +18,12 @@ namespace OpenRA.Server
/* client data */
public int PlayerIndex;
/* file server state */
public int NextChunk = 0;
public int NumChunks = 0;
public int RemainingBytes = 0;
public Stream Stream = null;
public byte[] PopBytes(int n)
{
var result = data.GetRange(0, n);
@@ -25,12 +31,60 @@ namespace OpenRA.Server
return result.ToArray();
}
/* file server state */
public int NextChunk = 0;
public int NumChunks = 0;
public int RemainingBytes = 0;
public Stream Stream = null;
}
bool ReadDataInner()
{
var rx = new byte[1024];
var len = 0;
for (; ; )
{
try
{
if (0 < (len = socket.Receive(rx)))
data.AddRange(rx.Take(len));
else
break;
}
catch (SocketException e)
{
if (e.SocketErrorCode == SocketError.WouldBlock) break;
Server.DropClient(this, e);
return false;
}
}
return true;
}
public void ReadData()
{
if (ReadDataInner())
while (data.Count >= ExpectLength)
{
var bytes = PopBytes(ExpectLength);
switch (State)
{
case ReceiveState.Header:
{
ExpectLength = BitConverter.ToInt32(bytes, 0) - 4;
Frame = BitConverter.ToInt32(bytes, 4);
State = ReceiveState.Data;
} break;
case ReceiveState.Data:
{
// if (bytes.Length > 0)
// Console.WriteLine("{0} bytes", bytes.Length);
Server.DispatchOrders(this, Frame, bytes);
ExpectLength = 8;
State = ReceiveState.Header;
Server.UpdateInFlightFrames(this);
} break;
}
}
}}
enum ReceiveState { Header, Data };
}