some Network changes.
This commit is contained in:
@@ -1,11 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OpenRa.Orders
|
||||
{
|
||||
interface IOrderSource
|
||||
{
|
||||
void SendLocalOrders(int localFrame, List<Order> localOrders);
|
||||
List<byte[]> OrdersForFrame(int currentFrame);
|
||||
bool IsReadyForFrame(int frameNumber);
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace OpenRa.Orders
|
||||
{
|
||||
class LocalOrderSource : IOrderSource
|
||||
{
|
||||
Dictionary<int, List<byte[]>> orders = new Dictionary<int, List<byte[]>>();
|
||||
|
||||
public List<byte[]> OrdersForFrame(int currentFrame)
|
||||
{
|
||||
if (!orders.ContainsKey(currentFrame))
|
||||
return new List<byte[]>();
|
||||
|
||||
var result = orders[currentFrame];
|
||||
orders.Remove(currentFrame);
|
||||
return result;
|
||||
}
|
||||
|
||||
public void SendLocalOrders(int localFrame, List<Order> localOrders)
|
||||
{
|
||||
if (localFrame == 0) return;
|
||||
orders[localFrame] = localOrders.Select(o=>o.Serialize()).ToList();
|
||||
}
|
||||
|
||||
public bool IsReadyForFrame(int frameNumber)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System;
|
||||
|
||||
namespace OpenRa.Orders
|
||||
{
|
||||
enum ConnectionState
|
||||
{
|
||||
NotConnected,
|
||||
Connecting,
|
||||
Connected,
|
||||
}
|
||||
|
||||
class NetworkOrderSource : IOrderSource
|
||||
{
|
||||
TcpClient socket;
|
||||
|
||||
Dictionary<int, List<byte[]>> orderBuffers = new Dictionary<int, List<byte[]>>();
|
||||
Dictionary<int, bool> gotEverything = new Dictionary<int, bool>();
|
||||
|
||||
public ConnectionState State { get; private set; }
|
||||
|
||||
public NetworkOrderSource(string host, int port)
|
||||
{
|
||||
State = ConnectionState.Connecting;
|
||||
|
||||
socket = new TcpClient();
|
||||
socket.BeginConnect(host, port, OnConnected, null);
|
||||
socket.NoDelay = true;
|
||||
}
|
||||
|
||||
void OnConnected(IAsyncResult r)
|
||||
{
|
||||
try
|
||||
{
|
||||
socket.EndConnect(r);
|
||||
State = ConnectionState.Connected;
|
||||
new Thread(() =>
|
||||
{
|
||||
var reader = new BinaryReader(socket.GetStream());
|
||||
|
||||
try
|
||||
{
|
||||
for (; ; )
|
||||
{
|
||||
var len = reader.ReadInt32();
|
||||
var frame = reader.ReadInt32();
|
||||
var buf = reader.ReadBytes(len - 4);
|
||||
|
||||
lock (orderBuffers)
|
||||
{
|
||||
if (len == 5 && buf[0] == 0xef) /* got everything marker */
|
||||
gotEverything[frame] = true;
|
||||
else
|
||||
{
|
||||
/* accumulate this chunk */
|
||||
if (!orderBuffers.ContainsKey(frame))
|
||||
orderBuffers[frame] = new List<byte[]> { buf };
|
||||
else
|
||||
orderBuffers[frame].Add(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
State = ConnectionState.NotConnected;
|
||||
}
|
||||
|
||||
}) { IsBackground = true }.Start();
|
||||
}
|
||||
catch
|
||||
{
|
||||
State = ConnectionState.NotConnected;
|
||||
}
|
||||
}
|
||||
|
||||
static List<byte[]> NoOrders = new List<byte[]>();
|
||||
List<byte[]> ExtractOrders(int frame)
|
||||
{
|
||||
lock (orderBuffers)
|
||||
{
|
||||
List<byte[]> result;
|
||||
if (!orderBuffers.TryGetValue(frame, out result))
|
||||
result = NoOrders;
|
||||
orderBuffers.Remove(frame);
|
||||
gotEverything.Remove(frame);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public List<byte[]> OrdersForFrame(int currentFrame)
|
||||
{
|
||||
return ExtractOrders(currentFrame).ToList();
|
||||
}
|
||||
|
||||
public void SendLocalOrders(int localFrame, List<Order> localOrders)
|
||||
{
|
||||
if (socket.Connected)
|
||||
socket.GetStream().WriteFrameData(localOrders, localFrame);
|
||||
}
|
||||
|
||||
public bool IsReadyForFrame(int frameNumber)
|
||||
{
|
||||
lock (orderBuffers)
|
||||
return gotEverything.ContainsKey(frameNumber);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,163 +0,0 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using OpenRa.SupportPowers;
|
||||
|
||||
namespace OpenRa
|
||||
{
|
||||
public sealed class Order
|
||||
{
|
||||
public readonly string OrderString;
|
||||
public readonly Actor Subject;
|
||||
public readonly Actor TargetActor;
|
||||
public readonly int2 TargetLocation;
|
||||
public readonly string TargetString;
|
||||
public bool IsImmediate;
|
||||
|
||||
public Player Player { get { return Subject.Owner; } }
|
||||
|
||||
public Order(string orderString, Actor subject,
|
||||
Actor targetActor, int2 targetLocation, string targetString)
|
||||
{
|
||||
this.OrderString = orderString;
|
||||
this.Subject = subject;
|
||||
this.TargetActor = targetActor;
|
||||
this.TargetLocation = targetLocation;
|
||||
this.TargetString = targetString;
|
||||
}
|
||||
|
||||
public byte[] Serialize()
|
||||
{
|
||||
if (IsImmediate) /* chat, whatever */
|
||||
{
|
||||
var ret = new MemoryStream();
|
||||
var w = new BinaryWriter(ret);
|
||||
w.Write((byte)0xfe);
|
||||
w.Write((uint)Player.Index);
|
||||
w.Write(OrderString);
|
||||
w.Write(TargetString);
|
||||
return ret.ToArray();
|
||||
}
|
||||
|
||||
switch (OrderString)
|
||||
{
|
||||
// Format:
|
||||
// u8 : orderID.
|
||||
// 0xFF: Full serialized order.
|
||||
// varies: rest of order.
|
||||
default:
|
||||
// TODO: specific serializers for specific orders.
|
||||
{
|
||||
var ret = new MemoryStream();
|
||||
var w = new BinaryWriter(ret);
|
||||
w.Write( (byte)0xFF );
|
||||
w.Write(OrderString);
|
||||
w.Write(UIntFromActor(Subject));
|
||||
w.Write(UIntFromActor(TargetActor));
|
||||
w.Write(TargetLocation.X);
|
||||
w.Write(TargetLocation.Y);
|
||||
w.Write(TargetString != null);
|
||||
if (TargetString != null)
|
||||
w.Write(TargetString);
|
||||
return ret.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static Player LookupPlayer(World world, uint index)
|
||||
{
|
||||
return world.players
|
||||
.Where(x => x.Value.Index == index)
|
||||
.First().Value;
|
||||
}
|
||||
|
||||
public static Order Deserialize(World world, BinaryReader r)
|
||||
{
|
||||
switch (r.ReadByte())
|
||||
{
|
||||
case 0xFF:
|
||||
{
|
||||
var order = r.ReadString();
|
||||
var subjectId = r.ReadUInt32();
|
||||
var targetActorId = r.ReadUInt32();
|
||||
var targetLocation = new int2(r.ReadInt32(), 0);
|
||||
targetLocation.Y = r.ReadInt32();
|
||||
var targetString = null as string;
|
||||
if (r.ReadBoolean())
|
||||
targetString = r.ReadString();
|
||||
|
||||
Actor subject, targetActor;
|
||||
if( !TryGetActorFromUInt( world, subjectId, out subject ) || !TryGetActorFromUInt( world, targetActorId, out targetActor ) )
|
||||
return null;
|
||||
|
||||
return new Order( order, subject, targetActor, targetLocation, targetString);
|
||||
}
|
||||
|
||||
case 0xfe:
|
||||
{
|
||||
var playerID = r.ReadUInt32();
|
||||
var name = r.ReadString();
|
||||
var data = r.ReadString();
|
||||
|
||||
return new Order( name, LookupPlayer( world, playerID ).PlayerActor, null, int2.Zero, data ) { IsImmediate = true };
|
||||
}
|
||||
|
||||
default:
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
static uint UIntFromActor(Actor a)
|
||||
{
|
||||
if (a == null) return 0xffffffff;
|
||||
return a.ActorID;
|
||||
}
|
||||
|
||||
static bool TryGetActorFromUInt(World world, uint aID, out Actor ret )
|
||||
{
|
||||
if( aID == 0xFFFFFFFF )
|
||||
{
|
||||
ret = null;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach( var a in world.Actors.Where( x => x.ActorID == aID ) )
|
||||
{
|
||||
ret = a;
|
||||
return true;
|
||||
}
|
||||
ret = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Named constructors for Orders.
|
||||
// Now that Orders are resolved by individual Actors, these are weird; you unpack orders manually, but not pack them.
|
||||
public static Order Chat(Player subject, string text)
|
||||
{
|
||||
return new Order("Chat", subject.PlayerActor, null, int2.Zero, text)
|
||||
{ IsImmediate = true };
|
||||
}
|
||||
|
||||
public static Order StartProduction(Player subject, string item)
|
||||
{
|
||||
return new Order("StartProduction", subject.PlayerActor, null, int2.Zero, item );
|
||||
}
|
||||
|
||||
public static Order PauseProduction(Player subject, string item, bool pause)
|
||||
{
|
||||
return new Order("PauseProduction", subject.PlayerActor, null, new int2( pause ? 1 : 0, 0 ), item);
|
||||
}
|
||||
|
||||
public static Order CancelProduction(Player subject, string item)
|
||||
{
|
||||
return new Order("CancelProduction", subject.PlayerActor, null, int2.Zero, item);
|
||||
}
|
||||
|
||||
public static Order PlayAnimation(Actor actor, string animationString)
|
||||
{
|
||||
return new Order("PlayAnimation", actor, null, int2.Zero, animationString);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace OpenRa.Orders
|
||||
{
|
||||
static class OrderIO
|
||||
{
|
||||
static void Write(this Stream s, byte[] buf)
|
||||
{
|
||||
s.Write(buf, 0, buf.Length);
|
||||
}
|
||||
|
||||
public static void WriteFrameData(this Stream s, IEnumerable<Order> orders, int frameNumber)
|
||||
{
|
||||
var ms = new MemoryStream();
|
||||
ms.Write(BitConverter.GetBytes(frameNumber));
|
||||
foreach (var order in orders)
|
||||
ms.Write(order.Serialize());
|
||||
|
||||
s.Write(BitConverter.GetBytes((int)ms.Length));
|
||||
ms.WriteTo(s);
|
||||
}
|
||||
|
||||
public static List<Order> ToOrderList(this byte[] bytes, World world)
|
||||
{
|
||||
var ms = new MemoryStream(bytes);
|
||||
var reader = new BinaryReader(ms);
|
||||
var ret = new List<Order>();
|
||||
while( ms.Position < ms.Length )
|
||||
{
|
||||
var o = Order.Deserialize( world, reader );
|
||||
if( o != null )
|
||||
ret.Add( o );
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace OpenRa.Orders
|
||||
{
|
||||
class OrderManager
|
||||
{
|
||||
Stream savingReplay;
|
||||
List<IOrderSource> sources;
|
||||
int frameNumber = 0;
|
||||
|
||||
public int FramesAhead = 3;
|
||||
|
||||
public bool GameStarted { get { return frameNumber != 0; } }
|
||||
public bool IsNetplay { get { return sources.OfType<NetworkOrderSource>().Any(); } }
|
||||
|
||||
public void StartGame()
|
||||
{
|
||||
if (GameStarted) return;
|
||||
|
||||
frameNumber = 1;
|
||||
foreach (var p in this.sources)
|
||||
for (int i = frameNumber; i <= FramesAhead; i++)
|
||||
p.SendLocalOrders(i, new List<Order>());
|
||||
}
|
||||
|
||||
public IEnumerable<IOrderSource> Sources { get { return sources; } }
|
||||
|
||||
public int FrameNumber { get { return frameNumber; } }
|
||||
|
||||
public OrderManager( IEnumerable<IOrderSource> sources )
|
||||
{
|
||||
this.sources = sources.ToList();
|
||||
if (!IsNetplay)
|
||||
StartGame();
|
||||
}
|
||||
|
||||
public OrderManager( IEnumerable<IOrderSource> sources, string replayFilename )
|
||||
: this( sources )
|
||||
{
|
||||
savingReplay = new FileStream( replayFilename, FileMode.Create );
|
||||
}
|
||||
|
||||
public bool IsReadyForNextFrame
|
||||
{
|
||||
get
|
||||
{
|
||||
foreach( var p in sources )
|
||||
if( !p.IsReadyForFrame( frameNumber ) )
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void ProcessOrders(World world, int frame, bool save)
|
||||
{
|
||||
var orders = sources
|
||||
.SelectMany(s => s.OrdersForFrame(frame))
|
||||
.SelectMany(x => x.ToOrderList(world))
|
||||
.OrderBy(o => o.Player.Index)
|
||||
.ToList();
|
||||
|
||||
foreach (var o in orders)
|
||||
UnitOrders.ProcessOrder(o);
|
||||
|
||||
if (save && savingReplay != null)
|
||||
savingReplay.WriteFrameData(orders, frame);
|
||||
}
|
||||
|
||||
public void TickImmediate( World world )
|
||||
{
|
||||
var localOrders = Game.controller.GetRecentOrders(true);
|
||||
if (localOrders.Count > 0)
|
||||
foreach (var p in sources)
|
||||
p.SendLocalOrders(0, localOrders);
|
||||
|
||||
ProcessOrders(world, 0, false);
|
||||
}
|
||||
|
||||
public void Tick( World world )
|
||||
{
|
||||
var localOrders = Game.controller.GetRecentOrders(false);
|
||||
|
||||
foreach( var p in sources )
|
||||
p.SendLocalOrders( frameNumber + FramesAhead, localOrders );
|
||||
|
||||
ProcessOrders(world, frameNumber, true);
|
||||
|
||||
++frameNumber;
|
||||
|
||||
// sanity check on the framenumber. This is 2^31 frames maximum, or multiple *years* at 40ms/frame.
|
||||
if( ( frameNumber & 0x80000000 ) != 0 )
|
||||
throw new InvalidOperationException( "(OrderManager) Frame number too large" );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace OpenRa.Orders
|
||||
{
|
||||
class ReplayOrderSource : IOrderSource
|
||||
{
|
||||
BinaryReader replayReader;
|
||||
public ReplayOrderSource(string replayFilename)
|
||||
{
|
||||
replayReader = new BinaryReader(File.Open(replayFilename, FileMode.Open));
|
||||
}
|
||||
|
||||
public void SendLocalOrders(int localFrame, List<Order> localOrders) { }
|
||||
|
||||
public List<byte[]> OrdersForFrame(int frameNumber)
|
||||
{
|
||||
if (frameNumber == 0)
|
||||
return new List<byte[]>();
|
||||
|
||||
try
|
||||
{
|
||||
var len = replayReader.ReadInt32() - 4;
|
||||
var frame = replayReader.ReadInt32();
|
||||
var ret = replayReader.ReadBytes(len);
|
||||
|
||||
if (frameNumber != frame)
|
||||
throw new InvalidOperationException("Attempted time-travel in OrdersForFrame (replay)");
|
||||
|
||||
return new List<byte[]> { ret };
|
||||
}
|
||||
catch (EndOfStreamException)
|
||||
{
|
||||
return new List<byte[]>();
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsReadyForFrame(int frameNumber)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
using System.Drawing;
|
||||
using OpenRa.FileFormats;
|
||||
using OpenRa.GameRules;
|
||||
using OpenRa.Graphics;
|
||||
using OpenRa.Traits;
|
||||
|
||||
namespace OpenRa.Orders
|
||||
{
|
||||
static class UnitOrders
|
||||
{
|
||||
public static void ProcessOrder( Order order )
|
||||
{
|
||||
switch( order.OrderString )
|
||||
{
|
||||
case "Chat":
|
||||
{
|
||||
Game.chat.AddLine(order.Player, order.TargetString);
|
||||
break;
|
||||
}
|
||||
case "AssignPlayer":
|
||||
{
|
||||
order.Player.World.LocalPlayer = order.Player;
|
||||
Game.chat.AddLine(order.Player, "is now YOU.");
|
||||
break;
|
||||
}
|
||||
case "StartGame":
|
||||
{
|
||||
Game.chat.AddLine(Color.White, "Server", "The game has started.");
|
||||
Game.StartGame();
|
||||
break;
|
||||
}
|
||||
case "SyncInfo":
|
||||
{
|
||||
Game.SyncLobbyInfo(order.TargetString);
|
||||
break;
|
||||
}
|
||||
case "FileChunk":
|
||||
{
|
||||
PackageDownloader.ReceiveChunk(order.TargetString);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
foreach (var t in order.Subject.traits.WithInterface<IResolveOrder>())
|
||||
t.ResolveOrder(order.Subject, order);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user