Client/server handshake. Only checks that the correct mods are active.
This commit is contained in:
47
OpenRA.Game/Network/Handshake.cs
Normal file
47
OpenRA.Game/Network/Handshake.cs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#region Copyright & License Information
|
||||||
|
/*
|
||||||
|
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
|
||||||
|
* This file is part of OpenRA, which is free software. It is made
|
||||||
|
* available to you under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation. For more information,
|
||||||
|
* see LICENSE.
|
||||||
|
*/
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Drawing;
|
||||||
|
using System.Linq;
|
||||||
|
using OpenRA.FileFormats;
|
||||||
|
|
||||||
|
namespace OpenRA.Network
|
||||||
|
{
|
||||||
|
public class HandshakeResponse
|
||||||
|
{
|
||||||
|
public string Name;
|
||||||
|
public Color Color1;
|
||||||
|
public Color Color2;
|
||||||
|
public string[] Mods = { "ra" }; // mod names
|
||||||
|
public string Password;
|
||||||
|
|
||||||
|
public string Serialize()
|
||||||
|
{
|
||||||
|
var data = new List<MiniYamlNode>();
|
||||||
|
data.Add(new MiniYamlNode("Handshake", FieldSaver.Save(this)));
|
||||||
|
System.Console.WriteLine("Serializing handshake response:");
|
||||||
|
System.Console.WriteLine(data.WriteToString());
|
||||||
|
|
||||||
|
return data.WriteToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static HandshakeResponse Deserialize(string data)
|
||||||
|
{
|
||||||
|
System.Console.WriteLine("Deserializing handshake response:");
|
||||||
|
System.Console.WriteLine(data);
|
||||||
|
|
||||||
|
var handshake = new HandshakeResponse();
|
||||||
|
var ys = MiniYaml.FromString(data);
|
||||||
|
FieldLoader.Load(handshake, ys.First().Value);
|
||||||
|
return handshake;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -195,6 +195,11 @@ namespace OpenRA
|
|||||||
return new Order("TeamChat", null, false) { IsImmediate = true, TargetString = text };
|
return new Order("TeamChat", null, false) { IsImmediate = true, TargetString = text };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Order HandshakeResponse(string text)
|
||||||
|
{
|
||||||
|
return new Order("HandshakeResponse", null, false) { IsImmediate = true, TargetString = text };
|
||||||
|
}
|
||||||
|
|
||||||
public static Order Command(string text)
|
public static Order Command(string text)
|
||||||
{
|
{
|
||||||
return new Order("Command", null, false) { IsImmediate = true, TargetString = text };
|
return new Order("Command", null, false) { IsImmediate = true, TargetString = text };
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using OpenRA.Traits;
|
using OpenRA.Traits;
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace OpenRA.Network
|
namespace OpenRA.Network
|
||||||
{
|
{
|
||||||
@@ -96,8 +97,37 @@ namespace OpenRA.Network
|
|||||||
Game.StartGame(orderManager.LobbyInfo.GlobalSettings.Map);
|
Game.StartGame(orderManager.LobbyInfo.GlobalSettings.Map);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case "HandshakeRequest":
|
||||||
|
{
|
||||||
|
Console.WriteLine("Client: Recieved HandshakeRequest");
|
||||||
|
// Check valid mods/versions
|
||||||
|
var serverInfo = Session.Deserialize(order.TargetString);
|
||||||
|
var serverMods = serverInfo.GlobalSettings.Mods;
|
||||||
|
var localMods = orderManager.LobbyInfo.GlobalSettings.Mods;
|
||||||
|
|
||||||
|
// TODO: Check that the map exists on the client
|
||||||
|
|
||||||
|
// Todo: Display a friendly dialog
|
||||||
|
if (serverMods.SymmetricDifference(localMods).Count() > 0)
|
||||||
|
throw new InvalidOperationException("Version mismatch. Client: `{0}`, Server: `{1}`"
|
||||||
|
.F(string.Join(",",localMods), string.Join(",",serverMods)));
|
||||||
|
|
||||||
|
var response = new HandshakeResponse()
|
||||||
|
{
|
||||||
|
Name = "Test Player",
|
||||||
|
Color1 = Color.PaleGreen,
|
||||||
|
Color2 = Color.PeachPuff,
|
||||||
|
Mods = localMods,
|
||||||
|
Password = "Foo"
|
||||||
|
};
|
||||||
|
orderManager.IssueOrder(Order.HandshakeResponse(response.Serialize()));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case "SyncInfo":
|
case "SyncInfo":
|
||||||
{
|
{
|
||||||
|
Console.WriteLine("Client: Recieved SyncInfo");
|
||||||
orderManager.LobbyInfo = Session.Deserialize(order.TargetString);
|
orderManager.LobbyInfo = Session.Deserialize(order.TargetString);
|
||||||
|
|
||||||
if (orderManager.FramesAhead != orderManager.LobbyInfo.GlobalSettings.OrderLatency
|
if (orderManager.FramesAhead != orderManager.LobbyInfo.GlobalSettings.OrderLatency
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
|
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
@@ -180,6 +180,7 @@
|
|||||||
<Compile Include="Server\TraitInterfaces.cs" />
|
<Compile Include="Server\TraitInterfaces.cs" />
|
||||||
<Compile Include="Widgets\ScrollPanelWidget.cs" />
|
<Compile Include="Widgets\ScrollPanelWidget.cs" />
|
||||||
<Compile Include="Graphics\ShroudRenderer.cs" />
|
<Compile Include="Graphics\ShroudRenderer.cs" />
|
||||||
|
<Compile Include="Network\Handshake.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">
|
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">
|
||||||
|
|||||||
@@ -24,7 +24,12 @@ namespace OpenRA.Server
|
|||||||
{
|
{
|
||||||
public class Server
|
public class Server
|
||||||
{
|
{
|
||||||
|
// Valid player connections
|
||||||
public List<Connection> conns = new List<Connection>();
|
public List<Connection> conns = new List<Connection>();
|
||||||
|
|
||||||
|
// Pre-verified player connections
|
||||||
|
public List<Connection> preConns = new List<Connection>();
|
||||||
|
|
||||||
TcpListener listener = null;
|
TcpListener listener = null;
|
||||||
Dictionary<int, List<Connection>> inFlightFrames
|
Dictionary<int, List<Connection>> inFlightFrames
|
||||||
= new Dictionary<int, List<Connection>>();
|
= new Dictionary<int, List<Connection>>();
|
||||||
@@ -88,24 +93,31 @@ namespace OpenRA.Server
|
|||||||
var checkRead = new ArrayList();
|
var checkRead = new ArrayList();
|
||||||
checkRead.Add( listener.Server );
|
checkRead.Add( listener.Server );
|
||||||
foreach( var c in conns ) checkRead.Add( c.socket );
|
foreach( var c in conns ) checkRead.Add( c.socket );
|
||||||
|
foreach( var c in preConns ) checkRead.Add( c.socket );
|
||||||
|
|
||||||
Socket.Select( checkRead, null, null, timeout );
|
Socket.Select( checkRead, null, null, timeout );
|
||||||
|
|
||||||
foreach( Socket s in checkRead )
|
foreach( Socket s in checkRead )
|
||||||
if( s == listener.Server ) AcceptConnection();
|
if( s == listener.Server ) AcceptConnection();
|
||||||
|
else if (preConns.Count > 0)
|
||||||
|
{
|
||||||
|
var p = preConns.SingleOrDefault( c => c.socket == s );
|
||||||
|
if (p != null) p.ReadData( this );
|
||||||
|
}
|
||||||
else if (conns.Count > 0) conns.Single( c => c.socket == s ).ReadData( this );
|
else if (conns.Count > 0) conns.Single( c => c.socket == s ).ReadData( this );
|
||||||
|
|
||||||
foreach (var t in ServerTraits.WithInterface<ITick>())
|
foreach (var t in ServerTraits.WithInterface<ITick>())
|
||||||
t.Tick(this);
|
t.Tick(this);
|
||||||
|
|
||||||
if (conns.Count() == 0 || shutdown)
|
if (shutdown)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
GameStarted = false;
|
GameStarted = false;
|
||||||
foreach (var t in ServerTraits.WithInterface<INotifyServerShutdown>())
|
foreach (var t in ServerTraits.WithInterface<INotifyServerShutdown>())
|
||||||
t.ServerShutdown(this);
|
t.ServerShutdown(this);
|
||||||
|
|
||||||
|
preConns.Clear();
|
||||||
conns.Clear();
|
conns.Clear();
|
||||||
try { listener.Stop(); }
|
try { listener.Stop(); }
|
||||||
catch { }
|
catch { }
|
||||||
@@ -120,7 +132,7 @@ namespace OpenRA.Server
|
|||||||
int ChooseFreePlayerIndex()
|
int ChooseFreePlayerIndex()
|
||||||
{
|
{
|
||||||
for (var i = 0; i < 256; i++)
|
for (var i = 0; i < 256; i++)
|
||||||
if (conns.All(c => c.PlayerIndex != i))
|
if (conns.All(c => c.PlayerIndex != i) && preConns.All(c => c.PlayerIndex != i))
|
||||||
return i;
|
return i;
|
||||||
|
|
||||||
throw new InvalidOperationException("Already got 256 players");
|
throw new InvalidOperationException("Already got 256 players");
|
||||||
@@ -134,7 +146,8 @@ namespace OpenRA.Server
|
|||||||
{
|
{
|
||||||
if (!listener.Server.IsBound) return;
|
if (!listener.Server.IsBound) return;
|
||||||
newSocket = listener.AcceptSocket();
|
newSocket = listener.AcceptSocket();
|
||||||
}catch
|
}
|
||||||
|
catch
|
||||||
{
|
{
|
||||||
/* could have an exception here when listener 'goes away' when calling AcceptConnection! */
|
/* could have an exception here when listener 'goes away' when calling AcceptConnection! */
|
||||||
/* alternative would be to use locking but the listener doesnt go away without a reason */
|
/* alternative would be to use locking but the listener doesnt go away without a reason */
|
||||||
@@ -142,6 +155,26 @@ namespace OpenRA.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
var newConn = new Connection { socket = newSocket };
|
var newConn = new Connection { socket = newSocket };
|
||||||
|
try
|
||||||
|
{
|
||||||
|
newConn.socket.Blocking = false;
|
||||||
|
newConn.socket.NoDelay = true;
|
||||||
|
|
||||||
|
// assign the player number.
|
||||||
|
newConn.PlayerIndex = ChooseFreePlayerIndex();
|
||||||
|
newConn.socket.Send(BitConverter.GetBytes(ProtocolVersion.Version));
|
||||||
|
newConn.socket.Send(BitConverter.GetBytes(newConn.PlayerIndex));
|
||||||
|
preConns.Add(newConn);
|
||||||
|
|
||||||
|
// Dispatch a handshake order
|
||||||
|
DispatchOrdersToClient(newConn, 0, 0, new ServerOrder("HandshakeRequest", lobbyInfo.Serialize()).Serialize());
|
||||||
|
}
|
||||||
|
catch (Exception) { DropClient(newConn); }
|
||||||
|
}
|
||||||
|
|
||||||
|
void AcceptPlayer(Connection newConn)
|
||||||
|
{
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (GameStarted)
|
if (GameStarted)
|
||||||
@@ -152,17 +185,13 @@ namespace OpenRA.Server
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
newConn.socket.Blocking = false;
|
preConns.Remove(newConn);
|
||||||
newConn.socket.NoDelay = true;
|
|
||||||
|
|
||||||
// assign the player number.
|
|
||||||
newConn.PlayerIndex = ChooseFreePlayerIndex();
|
|
||||||
newConn.socket.Send(BitConverter.GetBytes(ProtocolVersion.Version));
|
|
||||||
newConn.socket.Send(BitConverter.GetBytes(newConn.PlayerIndex));
|
|
||||||
conns.Add(newConn);
|
conns.Add(newConn);
|
||||||
|
|
||||||
foreach (var t in ServerTraits.WithInterface<IClientJoined>())
|
foreach (var t in ServerTraits.WithInterface<IClientJoined>())
|
||||||
t.ClientJoined(this, newConn);
|
t.ClientJoined(this, newConn);
|
||||||
|
|
||||||
|
Console.WriteLine("Server: Accepted connection as player");
|
||||||
}
|
}
|
||||||
catch (Exception) { DropClient(newConn); }
|
catch (Exception) { DropClient(newConn); }
|
||||||
}
|
}
|
||||||
@@ -246,20 +275,29 @@ namespace OpenRA.Server
|
|||||||
switch (so.Name)
|
switch (so.Name)
|
||||||
{
|
{
|
||||||
case "Command":
|
case "Command":
|
||||||
|
bool handled = false;
|
||||||
|
foreach (var t in ServerTraits.WithInterface<IInterpretCommand>())
|
||||||
|
if ((handled = t.InterpretCommand(this, conn, GetClient(conn), so.Data)))
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (!handled)
|
||||||
{
|
{
|
||||||
bool handled = false;
|
Log.Write("server", "Unknown server command: {0}", so.Data);
|
||||||
foreach (var t in ServerTraits.WithInterface<IInterpretCommand>())
|
SendChatTo(conn, "Unknown server command: {0}".F(so.Data));
|
||||||
if ((handled = t.InterpretCommand(this, conn, GetClient(conn), so.Data)))
|
|
||||||
break;
|
|
||||||
|
|
||||||
if (!handled)
|
|
||||||
{
|
|
||||||
Log.Write("server", "Unknown server command: {0}", so.Data);
|
|
||||||
SendChatTo(conn, "Unknown server command: {0}".F(so.Data));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case "HandshakeResponse":
|
||||||
|
Console.WriteLine("Server Recieved Handshake response");
|
||||||
|
var response = HandshakeResponse.Deserialize(so.Data);
|
||||||
|
|
||||||
|
// Validate versions again
|
||||||
|
|
||||||
|
// Validate password
|
||||||
|
|
||||||
|
// Accept connection; set name, color, etc.
|
||||||
|
AcceptPlayer(conn);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "Chat":
|
case "Chat":
|
||||||
case "TeamChat":
|
case "TeamChat":
|
||||||
var fromClient = GetClient(conn);
|
var fromClient = GetClient(conn);
|
||||||
|
|||||||
Reference in New Issue
Block a user