>2p works; non-netplay works again too.

This commit is contained in:
Chris Forbes
2009-12-02 07:50:57 +13:00
parent 805327a557
commit 7ff7574d3d
2 changed files with 44 additions and 5 deletions

View File

@@ -63,6 +63,8 @@ namespace OpenRA.Server
{ {
static List<Connection> conns = new List<Connection>(); static List<Connection> conns = new List<Connection>();
static TcpListener listener = new TcpListener(IPAddress.Any, 1234); static TcpListener listener = new TcpListener(IPAddress.Any, 1234);
static Dictionary<int, List<Connection>> inFlightFrames
= new Dictionary<int, List<Connection>>();
public static void Main(string[] args) public static void Main(string[] args)
{ {
@@ -175,6 +177,8 @@ namespace OpenRA.Server
DispatchOrders(conn, conn.Frame, bytes); DispatchOrders(conn, conn.Frame, bytes);
conn.ExpectLength = 8; conn.ExpectLength = 8;
conn.State = ReceiveState.Header; conn.State = ReceiveState.Header;
UpdateInFlightFrames(conn);
} break; } break;
} }
} }
@@ -183,6 +187,31 @@ namespace OpenRA.Server
// conn.socket.RemoteEndPoint); // conn.socket.RemoteEndPoint);
} }
static void UpdateInFlightFrames(Connection conn)
{
if (conn.Frame != 0)
{
if (!inFlightFrames.ContainsKey(conn.Frame))
{
Console.WriteLine("{0} opens frame {1}",
conn.socket.RemoteEndPoint,
conn.Frame);
inFlightFrames[conn.Frame] = new List<Connection> { conn };
}
else
inFlightFrames[conn.Frame].Add(conn);
if (conns.All(c => inFlightFrames[conn.Frame].Contains(c)))
{
inFlightFrames.Remove(conn.Frame);
Console.WriteLine("frame {0} completed.",
conn.Frame);
DispatchOrders(null, conn.Frame, new byte[] { 0xef });
}
}
}
static void DispatchOrdersToClient(Connection c, int frame, byte[] data) static void DispatchOrdersToClient(Connection c, int frame, byte[] data)
{ {
try try

View File

@@ -16,6 +16,7 @@ namespace OpenRa.Game
const int FramesAhead = 3; const int FramesAhead = 3;
public bool GameStarted { get { return frameNumber != 0; } } public bool GameStarted { get { return frameNumber != 0; } }
public bool IsNetplay { get { return sources.OfType<NetworkOrderSource>().Any(); } }
public void StartGame() public void StartGame()
{ {
@@ -32,6 +33,8 @@ namespace OpenRa.Game
public OrderManager( IEnumerable<OrderSource> sources ) public OrderManager( IEnumerable<OrderSource> sources )
{ {
this.sources = sources.ToList(); this.sources = sources.ToList();
if (!IsNetplay)
StartGame();
} }
public OrderManager( IEnumerable<OrderSource> sources, string replayFilename ) public OrderManager( IEnumerable<OrderSource> sources, string replayFilename )
@@ -157,6 +160,7 @@ namespace OpenRa.Game
TcpClient socket; TcpClient socket;
Dictionary<int, List<byte[]>> orderBuffers = new Dictionary<int, List<byte[]>>(); Dictionary<int, List<byte[]>> orderBuffers = new Dictionary<int, List<byte[]>>();
Dictionary<int, bool> gotEverything = new Dictionary<int, bool>();
public NetworkOrderSource( TcpClient socket ) public NetworkOrderSource( TcpClient socket )
{ {
@@ -174,11 +178,16 @@ namespace OpenRa.Game
lock (orderBuffers) lock (orderBuffers)
{ {
/* accumulate this chunk */ if (len == 5 && buf[0] == 0xef) /* got everything marker */
if (!orderBuffers.ContainsKey(frame)) gotEverything[frame] = true;
orderBuffers[frame] = new List<byte[]> { buf };
else else
orderBuffers[frame].Add(buf); {
/* accumulate this chunk */
if (!orderBuffers.ContainsKey(frame))
orderBuffers[frame] = new List<byte[]> { buf };
else
orderBuffers[frame].Add(buf);
}
} }
} }
} ) { IsBackground = true }.Start(); } ) { IsBackground = true }.Start();
@@ -193,6 +202,7 @@ namespace OpenRa.Game
if (!orderBuffers.TryGetValue(frame, out result)) if (!orderBuffers.TryGetValue(frame, out result))
result = NoOrders; result = NoOrders;
orderBuffers.Remove(frame); orderBuffers.Remove(frame);
gotEverything.Remove(frame);
return result; return result;
} }
} }
@@ -211,7 +221,7 @@ namespace OpenRa.Game
public bool IsReadyForFrame( int frameNumber ) public bool IsReadyForFrame( int frameNumber )
{ {
lock( orderBuffers ) lock( orderBuffers )
return orderBuffers.ContainsKey( frameNumber ); return gotEverything.ContainsKey( frameNumber );
} }
} }