server stuff

This commit is contained in:
Chris Forbes
2009-11-30 21:18:50 +13:00
parent fa02e9d45c
commit 2913c3310d
7 changed files with 163 additions and 50 deletions

View 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 };
}

View File

@@ -48,6 +48,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Connection.cs" />
<Compile Include="Server.cs" /> <Compile Include="Server.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>

View File

@@ -6,70 +6,151 @@ using System.Net.Sockets;
using System.Threading; using System.Threading;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Collections;
namespace OpenRA.Server namespace OpenRA.Server
{ {
static class Server static class Server
{ {
static void Main(string[] args) static List<Connection> conns = new List<Connection>();
{ static TcpListener listener = new TcpListener(IPAddress.Any, 1234);
var listener = new TcpListener(IPAddress.Any, 1234);
var clients = new List<TcpClient>();
public static void Main(string[] args)
{
listener.Start(); 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 (; ; ) for (; ; )
{ {
try try
{ {
var conn = listener.AcceptTcpClient(); if (0 < (len = conn.socket.Receive(rx)))
conn.NoDelay = true;
Console.WriteLine("Accepted connection from {0}",
conn.Client.RemoteEndPoint.ToString());
new Thread(() =>
{ {
lock (clients) Console.WriteLine("Read {0} bytes", len);
clients.Add(conn); conn.data.AddRange(rx.Take(len));
}
var ns = conn.GetStream(); else
break;
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();
} }
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 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 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 });
}
} }
} }

View File

@@ -103,6 +103,7 @@ namespace OpenRa.Game.GameRules
public readonly int Storage = 0; public readonly int Storage = 0;
public readonly bool Unsellable = false; public readonly bool Unsellable = false;
public readonly int[] RallyPoint = { 1, 3 }; public readonly int[] RallyPoint = { 1, 3 };
public readonly float[] SpawnOffset = null;
public BuildingInfo(string name) : base(name) { } public BuildingInfo(string name) : base(name) { }
} }

View File

@@ -1,10 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; using System.IO;
using System.Net; using System.Linq;
using IjwFramework.Types;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading; using System.Threading;

View File

@@ -34,13 +34,18 @@ namespace OpenRa.Game.Traits
{ {
var mobile = newUnit.traits.GetOrDefault<Mobile>(); var mobile = newUnit.traits.GetOrDefault<Mobile>();
if( mobile != null ) 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>(); var heli = newUnit.traits.GetOrDefault<Helicopter>();
if( heli != null ) if (heli != null)
heli.targetLocation = rp.rallyPoint; // TODO: make Activity.Move work for helis. 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 ); Game.world.Add( newUnit );
if( self.traits.Contains<RenderWarFactory>() ) if( self.traits.Contains<RenderWarFactory>() )

View File

@@ -307,6 +307,7 @@ Dimensions=2,2
Footprint=xx xx Footprint=xx xx
Produces=Plane Produces=Plane
SelectionPriority=3 SelectionPriority=3
SpawnOffset=0,0 ; todo: push this up a bit, but we've got a z-order issue first.
[DOME] [DOME]
Description=Radar Dome Description=Radar Dome
Traits=Building, RenderBuilding Traits=Building, RenderBuilding