server stuff
This commit is contained in:
27
OpenRA.Server/Connection.cs
Normal file
27
OpenRA.Server/Connection.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Net.Sockets;
|
||||
using System.IO;
|
||||
|
||||
namespace OpenRA.Server
|
||||
{
|
||||
class Connection
|
||||
{
|
||||
public Socket socket;
|
||||
public List<byte> data = new List<byte>();
|
||||
public ReceiveState State = ReceiveState.Header;
|
||||
public int ExpectLength = 8;
|
||||
public int Frame = 0;
|
||||
|
||||
public byte[] PopBytes(int n)
|
||||
{
|
||||
var result = data.GetRange(0, n);
|
||||
data.RemoveRange(0, n);
|
||||
return result.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
enum ReceiveState { Header, Data };
|
||||
}
|
||||
@@ -48,6 +48,7 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Connection.cs" />
|
||||
<Compile Include="Server.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -6,70 +6,151 @@ using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Collections;
|
||||
|
||||
namespace OpenRA.Server
|
||||
{
|
||||
static class Server
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var listener = new TcpListener(IPAddress.Any, 1234);
|
||||
var clients = new List<TcpClient>();
|
||||
static List<Connection> conns = new List<Connection>();
|
||||
static TcpListener listener = new TcpListener(IPAddress.Any, 1234);
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
listener.Start();
|
||||
|
||||
Console.WriteLine("Server started.");
|
||||
|
||||
for (; ; )
|
||||
{
|
||||
var checkRead = new ArrayList();
|
||||
var checkWrite = new ArrayList();
|
||||
var checkError = new ArrayList();
|
||||
checkRead.Add(listener.Server);
|
||||
foreach (var c in conns)
|
||||
checkRead.Add(c.socket);
|
||||
|
||||
Socket.Select(checkRead, null, null, 1000000 /* 1s */);
|
||||
|
||||
Console.WriteLine("Select() completed with {0} sockets",
|
||||
checkRead.Count);
|
||||
|
||||
foreach (Socket s in checkRead)
|
||||
if (s == listener.Server) AcceptConnection();
|
||||
else ReadData(conns.Single(c => c.socket == s));
|
||||
}
|
||||
}
|
||||
|
||||
static void AcceptConnection()
|
||||
{
|
||||
var newConn = new Connection { socket = listener.AcceptSocket() };
|
||||
newConn.socket.Blocking = false;
|
||||
conns.Add(newConn);
|
||||
|
||||
/* todo: assign a player number, setup host behavior, etc */
|
||||
|
||||
Console.WriteLine("Accepted connection from {0}.",
|
||||
newConn.socket.RemoteEndPoint);
|
||||
}
|
||||
|
||||
static bool ReadDataInner(Connection conn)
|
||||
{
|
||||
var rx = new byte[1024];
|
||||
var len = 0;
|
||||
|
||||
for (; ; )
|
||||
{
|
||||
try
|
||||
{
|
||||
var conn = listener.AcceptTcpClient();
|
||||
conn.NoDelay = true;
|
||||
Console.WriteLine("Accepted connection from {0}",
|
||||
conn.Client.RemoteEndPoint.ToString());
|
||||
|
||||
new Thread(() =>
|
||||
if (0 < (len = conn.socket.Receive(rx)))
|
||||
{
|
||||
lock (clients)
|
||||
clients.Add(conn);
|
||||
|
||||
var ns = conn.GetStream();
|
||||
|
||||
try
|
||||
{
|
||||
for (; ; )
|
||||
{
|
||||
var frame = BitConverter.ToInt32(ns.Read(4), 0);
|
||||
var length = BitConverter.ToInt32(ns.Read(4), 0);
|
||||
var data = ns.Read(length);
|
||||
|
||||
lock (clients)
|
||||
foreach (var c in clients)
|
||||
if (c != conn)
|
||||
{
|
||||
var otherStream = c.GetStream();
|
||||
otherStream.Write(BitConverter.GetBytes(frame));
|
||||
otherStream.Write(BitConverter.GetBytes(length));
|
||||
otherStream.Write(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("Client dropped: {0}", conn.Client.RemoteEndPoint.ToString());
|
||||
|
||||
lock (clients)
|
||||
clients.Remove(conn);
|
||||
}
|
||||
}) { IsBackground = true }.Start();
|
||||
Console.WriteLine("Read {0} bytes", len);
|
||||
conn.data.AddRange(rx.Take(len));
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
catch (Exception e)
|
||||
catch (SocketException e)
|
||||
{
|
||||
Console.WriteLine(e.ToString());
|
||||
if (e.SocketErrorCode == SocketError.WouldBlock) break;
|
||||
DropClient(conn, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void ReadData(Connection conn)
|
||||
{
|
||||
Console.WriteLine("Start ReadData() for {0}",
|
||||
conn.socket.RemoteEndPoint);
|
||||
|
||||
if (ReadDataInner(conn))
|
||||
while (conn.data.Count >= conn.ExpectLength)
|
||||
{
|
||||
var bytes = conn.PopBytes(conn.ExpectLength);
|
||||
switch (conn.State)
|
||||
{
|
||||
case ReceiveState.Header:
|
||||
{
|
||||
conn.Frame = BitConverter.ToInt32(bytes, 0);
|
||||
conn.ExpectLength = BitConverter.ToInt32(bytes, 4);
|
||||
conn.State = ReceiveState.Data;
|
||||
} break;
|
||||
|
||||
case ReceiveState.Data:
|
||||
{
|
||||
DispatchOrders(conn, conn.Frame, bytes);
|
||||
conn.ExpectLength = 8;
|
||||
conn.State = ReceiveState.Header;
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("End ReadData() for {0}",
|
||||
conn.socket.RemoteEndPoint);
|
||||
}
|
||||
|
||||
static void DispatchOrders(Connection conn, int frame, byte[] data)
|
||||
{
|
||||
foreach (var c in conns.Except(conn).ToArray())
|
||||
{
|
||||
try
|
||||
{
|
||||
c.socket.Blocking = true;
|
||||
c.socket.Send(BitConverter.GetBytes(frame));
|
||||
c.socket.Send(BitConverter.GetBytes(data.Length));
|
||||
c.socket.Send(data);
|
||||
c.socket.Blocking = false;
|
||||
}
|
||||
catch (Exception e) { DropClient(c, e); }
|
||||
}
|
||||
|
||||
if (frame == 0)
|
||||
InterpretServerOrders(data);
|
||||
}
|
||||
|
||||
static void InterpretServerOrders(byte[] data)
|
||||
{
|
||||
/* todo: handle all server orders! */
|
||||
}
|
||||
|
||||
static void DropClient(Connection c, Exception e)
|
||||
{
|
||||
Console.WriteLine("Client dropped: {0}.", c.socket.RemoteEndPoint);
|
||||
Console.WriteLine(e.ToString());
|
||||
|
||||
conns.Remove(c);
|
||||
|
||||
/* todo: tell everyone else that `c` has dropped */
|
||||
}
|
||||
|
||||
public static void Write(this Stream s, byte[] data) { s.Write(data, 0, data.Length); }
|
||||
public static byte[] Read(this Stream s, int len) { var data = new byte[len]; s.Read(data, 0, len); return data; }
|
||||
public static IEnumerable<T> Except<T>(this IEnumerable<T> ts, T t)
|
||||
{
|
||||
return ts.Except(new[] { t });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,6 +103,7 @@ namespace OpenRa.Game.GameRules
|
||||
public readonly int Storage = 0;
|
||||
public readonly bool Unsellable = false;
|
||||
public readonly int[] RallyPoint = { 1, 3 };
|
||||
public readonly float[] SpawnOffset = null;
|
||||
|
||||
public BuildingInfo(string name) : base(name) { }
|
||||
}
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using IjwFramework.Types;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
|
||||
|
||||
@@ -34,13 +34,18 @@ namespace OpenRa.Game.Traits
|
||||
{
|
||||
var mobile = newUnit.traits.GetOrDefault<Mobile>();
|
||||
if( mobile != null )
|
||||
newUnit.QueueActivity( new Traits.Activities.Move( rp.rallyPoint, 1 ) );
|
||||
newUnit.QueueActivity( new Activities.Move( rp.rallyPoint, 1 ) );
|
||||
|
||||
var heli = newUnit.traits.GetOrDefault<Helicopter>();
|
||||
if( heli != null )
|
||||
if (heli != null)
|
||||
heli.targetLocation = rp.rallyPoint; // TODO: make Activity.Move work for helis.
|
||||
}
|
||||
|
||||
var bi = self.unitInfo as BuildingInfo;
|
||||
if (bi != null && bi.SpawnOffset != null)
|
||||
newUnit.CenterLocation = self.CenterLocation
|
||||
+ new float2(bi.SpawnOffset[0], bi.SpawnOffset[1]);
|
||||
|
||||
Game.world.Add( newUnit );
|
||||
|
||||
if( self.traits.Contains<RenderWarFactory>() )
|
||||
|
||||
Reference in New Issue
Block a user