some Network changes.
This commit is contained in:
111
OpenRa.Game/Network/Connection.cs
Executable file
111
OpenRa.Game/Network/Connection.cs
Executable file
@@ -0,0 +1,111 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.IO;
|
||||
|
||||
namespace OpenRa.Network
|
||||
{
|
||||
enum ConnectionState
|
||||
{
|
||||
NotConnected,
|
||||
Connecting,
|
||||
Connected,
|
||||
}
|
||||
|
||||
interface IConnection
|
||||
{
|
||||
int LocalClientId { get; }
|
||||
ConnectionState ConnectionState { get; }
|
||||
void Send( byte[] packet );
|
||||
void Receive( Action<int, byte[]> packetFn );
|
||||
}
|
||||
|
||||
class EchoConnection : IConnection
|
||||
{
|
||||
protected struct ReceivedPacket
|
||||
{
|
||||
public int FromClient;
|
||||
public byte[] Data;
|
||||
}
|
||||
protected List<ReceivedPacket> receivedPackets = new List<ReceivedPacket>();
|
||||
|
||||
public virtual int LocalClientId
|
||||
{
|
||||
get { return 1; }
|
||||
}
|
||||
|
||||
public virtual ConnectionState ConnectionState
|
||||
{
|
||||
get { return ConnectionState.Connected; }
|
||||
}
|
||||
|
||||
public virtual void Send( byte[] packet )
|
||||
{
|
||||
lock( this )
|
||||
receivedPackets.Add( new ReceivedPacket { FromClient = LocalClientId, Data = packet } );
|
||||
}
|
||||
|
||||
public virtual void Receive( Action<int, byte[]> packetFn )
|
||||
{
|
||||
List<ReceivedPacket> packets;
|
||||
lock( this )
|
||||
{
|
||||
packets = receivedPackets;
|
||||
receivedPackets = new List<ReceivedPacket>();
|
||||
}
|
||||
|
||||
foreach( var p in packets )
|
||||
packetFn( p.FromClient, p.Data );
|
||||
}
|
||||
}
|
||||
|
||||
class NetworkConnection : EchoConnection
|
||||
{
|
||||
TcpClient socket;
|
||||
int clientId;
|
||||
ConnectionState connectionState = ConnectionState.Connecting;
|
||||
|
||||
public NetworkConnection( string host, int port )
|
||||
{
|
||||
new Thread( _ =>
|
||||
{
|
||||
try
|
||||
{
|
||||
socket = new TcpClient( host, port );
|
||||
var reader = new BinaryReader( socket.GetStream() );
|
||||
clientId = reader.ReadInt32();
|
||||
connectionState = ConnectionState.Connected;
|
||||
|
||||
for( ; ; )
|
||||
{
|
||||
var len = reader.ReadInt32();
|
||||
var buf = reader.ReadBytes( len );
|
||||
lock( this )
|
||||
receivedPackets.Add( new ReceivedPacket { FromClient = -1, Data = buf } );
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
connectionState = ConnectionState.NotConnected;
|
||||
}
|
||||
}
|
||||
) { IsBackground = true }.Start();
|
||||
}
|
||||
|
||||
public override int LocalClientId { get { return clientId; } }
|
||||
public override ConnectionState ConnectionState { get { return connectionState; } }
|
||||
|
||||
public override void Send( byte[] packet )
|
||||
{
|
||||
base.Send( packet );
|
||||
|
||||
var ms = new MemoryStream();
|
||||
ms.Write( BitConverter.GetBytes( (int)packet.Length ) );
|
||||
ms.Write( packet );
|
||||
ms.WriteTo( socket.GetStream() );
|
||||
}
|
||||
}
|
||||
}
|
||||
163
OpenRa.Game/Network/Order.cs
Executable file
163
OpenRa.Game/Network/Order.cs
Executable file
@@ -0,0 +1,163 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
45
OpenRa.Game/Network/OrderIO.cs
Executable file
45
OpenRa.Game/Network/OrderIO.cs
Executable file
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace OpenRa.Network
|
||||
{
|
||||
static class OrderIO
|
||||
{
|
||||
public 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 bytes = Serialize( orders, frameNumber );
|
||||
s.Write( BitConverter.GetBytes( (int)bytes.Length ) );
|
||||
s.Write( bytes );
|
||||
}
|
||||
|
||||
public static byte[] Serialize( this IEnumerable<Order> orders, int frameNumber )
|
||||
{
|
||||
var ms = new MemoryStream();
|
||||
ms.Write( BitConverter.GetBytes( frameNumber ) );
|
||||
foreach( var o in orders.Select( o => o.Serialize() ) )
|
||||
ms.Write( o );
|
||||
return ms.ToArray();
|
||||
}
|
||||
|
||||
public static List<Order> ToOrderList(this byte[] bytes, World world)
|
||||
{
|
||||
var ms = new MemoryStream(bytes, 4, bytes.Length - 4);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
102
OpenRa.Game/Network/OrderManager.cs
Executable file
102
OpenRa.Game/Network/OrderManager.cs
Executable file
@@ -0,0 +1,102 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace OpenRa.Network
|
||||
{
|
||||
class OrderManager
|
||||
{
|
||||
Stream savingReplay;
|
||||
int frameNumber = 0;
|
||||
|
||||
public int FramesAhead = 0;
|
||||
|
||||
public bool GameStarted { get { return frameNumber != 0; } }
|
||||
public IConnection Connection { get; private set; }
|
||||
|
||||
Dictionary<int, Dictionary<int, byte[]>> frameClientData = new Dictionary<int, Dictionary<int, byte[]>>();
|
||||
List<int> readyForFrames = new List<int>();
|
||||
List<Order> localOrders = new List<Order>();
|
||||
|
||||
public void StartGame()
|
||||
{
|
||||
if (GameStarted) return;
|
||||
|
||||
frameNumber = 1;
|
||||
for( int i = frameNumber ; i <= FramesAhead ; i++ )
|
||||
Connection.Send( new List<Order>().Serialize( i ) );
|
||||
}
|
||||
|
||||
public int FrameNumber { get { return frameNumber; } }
|
||||
|
||||
public OrderManager( IConnection conn )
|
||||
{
|
||||
Connection = conn;
|
||||
}
|
||||
|
||||
public OrderManager( IConnection conn, string replayFilename )
|
||||
: this( conn )
|
||||
{
|
||||
savingReplay = new FileStream( replayFilename, FileMode.Create );
|
||||
}
|
||||
|
||||
public void IssueOrders( Order[] orders )
|
||||
{
|
||||
foreach( var order in orders )
|
||||
IssueOrder( order );
|
||||
}
|
||||
|
||||
public void IssueOrder( Order order )
|
||||
{
|
||||
localOrders.Add( order );
|
||||
}
|
||||
|
||||
public void TickImmediate( World world )
|
||||
{
|
||||
var immediateOrders = localOrders.Where( o => o.IsImmediate ).ToList();
|
||||
if( immediateOrders.Count != 0 )
|
||||
Connection.Send( immediateOrders.Serialize( 0 ) );
|
||||
localOrders.RemoveAll( o => o.IsImmediate );
|
||||
|
||||
var immediatePackets = new List<byte[]>();
|
||||
|
||||
Connection.Receive(
|
||||
( clientId, packet ) =>
|
||||
{
|
||||
var frame = BitConverter.ToInt32( packet, 0 );
|
||||
if( packet.Length == 5 && packet[ 4 ] == 0xEF )
|
||||
readyForFrames.Add( frame );
|
||||
else if( frame == 0 )
|
||||
immediatePackets.Add( packet );
|
||||
else
|
||||
frameClientData.GetOrAdd( frame ).Add( clientId, packet );
|
||||
} );
|
||||
|
||||
foreach( var p in immediatePackets )
|
||||
foreach( var o in p.ToOrderList( world ) )
|
||||
UnitOrders.ProcessOrder( o );
|
||||
}
|
||||
|
||||
public bool IsReadyForNextFrame
|
||||
{
|
||||
get { return readyForFrames.Contains( FrameNumber ); }
|
||||
}
|
||||
|
||||
public void Tick( World world )
|
||||
{
|
||||
if( !IsReadyForNextFrame )
|
||||
throw new InvalidOperationException();
|
||||
readyForFrames.RemoveAll( f => f <= FrameNumber );
|
||||
|
||||
Connection.Send( localOrders.Serialize( FrameNumber + FramesAhead ) );
|
||||
localOrders.Clear();
|
||||
|
||||
var frameData = frameClientData[ FrameNumber ];
|
||||
foreach( var order in frameData.OrderBy( p => p.Key ).SelectMany( o => o.Value.ToOrderList( world ) ) )
|
||||
UnitOrders.ProcessOrder( order );
|
||||
|
||||
++frameNumber;
|
||||
}
|
||||
}
|
||||
}
|
||||
52
OpenRa.Game/Network/UnitOrders.cs
Executable file
52
OpenRa.Game/Network/UnitOrders.cs
Executable file
@@ -0,0 +1,52 @@
|
||||
using System.Drawing;
|
||||
using OpenRa.FileFormats;
|
||||
using OpenRa.GameRules;
|
||||
using OpenRa.Graphics;
|
||||
using OpenRa.Traits;
|
||||
|
||||
namespace OpenRa.Network
|
||||
{
|
||||
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