From be8bb20e820598c1748c544aed2902f57fd2c020 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Mon, 25 Jan 2010 18:29:56 +1300 Subject: [PATCH] added protocol versioning. AssignPlayer order is dead. --- OpenRA.Server/Server.cs | 6 ++---- OpenRa.FileFormats/OpenRa.FileFormats.csproj | 1 + OpenRa.FileFormats/ProtocolVersion.cs | 13 +++++++++++++ OpenRa.Game/Game.cs | 13 +++---------- OpenRa.Game/Network/Connection.cs | 8 ++++++++ OpenRa.Game/Network/UnitOrders.cs | 6 ------ OpenRa.Game/World.cs | 11 ++++++++--- 7 files changed, 35 insertions(+), 23 deletions(-) create mode 100644 OpenRa.FileFormats/ProtocolVersion.cs diff --git a/OpenRA.Server/Server.cs b/OpenRA.Server/Server.cs index c29924d4bb..3098e651e5 100644 --- a/OpenRA.Server/Server.cs +++ b/OpenRA.Server/Server.cs @@ -77,7 +77,8 @@ namespace OpenRA.Server // assign the player number. newConn.PlayerIndex = ChooseFreePlayerIndex(); - newConn.socket.Send( BitConverter.GetBytes( newConn.PlayerIndex ) ); + newConn.socket.Send(BitConverter.GetBytes(ProtocolVersion.Version)); + newConn.socket.Send(BitConverter.GetBytes(newConn.PlayerIndex)); conns.Add(newConn); lobbyInfo.Clients.Add( @@ -90,9 +91,6 @@ namespace OpenRA.Server State = Session.ClientState.NotReady }); - DispatchOrdersToClient(newConn, 0, 0, - new ServerOrder(newConn.PlayerIndex, "AssignPlayer", "").Serialize()); - Console.WriteLine("Accepted connection from {0}.", newConn.socket.RemoteEndPoint); diff --git a/OpenRa.FileFormats/OpenRa.FileFormats.csproj b/OpenRa.FileFormats/OpenRa.FileFormats.csproj index 8e12639d1b..617103e8ae 100644 --- a/OpenRa.FileFormats/OpenRa.FileFormats.csproj +++ b/OpenRa.FileFormats/OpenRa.FileFormats.csproj @@ -72,6 +72,7 @@ + diff --git a/OpenRa.FileFormats/ProtocolVersion.cs b/OpenRa.FileFormats/ProtocolVersion.cs new file mode 100644 index 0000000000..8d0eef523c --- /dev/null +++ b/OpenRa.FileFormats/ProtocolVersion.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace OpenRa.FileFormats +{ + public static class ProtocolVersion + { + // you *must* increment this whenever you make an incompatible protocol change + public static readonly int Version = 1; + } +} diff --git a/OpenRa.Game/Game.cs b/OpenRa.Game/Game.cs index 3331e5d171..f1a55533ec 100644 --- a/OpenRa.Game/Game.cs +++ b/OpenRa.Game/Game.cs @@ -87,7 +87,6 @@ namespace OpenRa internal static void Initialize(string mapName, Renderer renderer, int2 clientSize, int localPlayer, Controller controller) { - //localPlayerIndex = localPlayer; Game.renderer = renderer; Game.clientSize = clientSize; @@ -102,7 +101,6 @@ namespace OpenRa ChangeMap(mapName); if (Settings.Replay != "") - //orderManager = new OrderManager(new IOrderSource[] { new ReplayOrderSource(Settings.Replay) }); throw new NotImplementedException(); else { @@ -197,6 +195,9 @@ namespace OpenRa LobbyInfo = session; + if (Game.orderManager.Connection.ConnectionState == ConnectionState.Connected) + world.SetLocalPlayer(Game.orderManager.Connection.LocalClientId); + if (Game.orderManager.FramesAhead != LobbyInfo.GlobalSettings.OrderLatency && !Game.orderManager.GameStarted) { @@ -277,14 +278,6 @@ namespace OpenRa new Order( "ToggleReady", Game.world.LocalPlayer.PlayerActor, "" ) { IsImmediate = true } ); } - /* temporary hack: DO NOT LEAVE IN */ - if( e.KeyCode == Keys.F2 ) - Game.world.LocalPlayer = Game.world.players[ ( Game.world.LocalPlayer.Index + 1 ) % 4 ]; - if( e.KeyCode == Keys.F3 ) - Game.controller.orderGenerator = new SellOrderGenerator(); - if( e.KeyCode == Keys.F4 ) - Game.controller.orderGenerator = new RepairOrderGenerator(); - if( !Game.chat.isChatting ) if( e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9 ) Game.controller.DoControlGroup( world, (int)e.KeyCode - (int)Keys.D0, (Modifiers)(int)e.Modifiers ); diff --git a/OpenRa.Game/Network/Connection.cs b/OpenRa.Game/Network/Connection.cs index 5becdcee8a..ee5b26cd2b 100755 --- a/OpenRa.Game/Network/Connection.cs +++ b/OpenRa.Game/Network/Connection.cs @@ -5,6 +5,7 @@ using System.Text; using System.Net.Sockets; using System.Threading; using System.IO; +using OpenRa.FileFormats; namespace OpenRa.Network { @@ -78,6 +79,13 @@ namespace OpenRa.Network { socket = new TcpClient( host, port ); var reader = new BinaryReader( socket.GetStream() ); + var serverProtocol = reader.ReadInt32(); + + if (ProtocolVersion.Version != serverProtocol) + throw new InvalidOperationException( + "Protocol version mismatch. Server={0} Client={1}" + .F(serverProtocol, ProtocolVersion.Version)); + clientId = reader.ReadInt32(); connectionState = ConnectionState.Connected; diff --git a/OpenRa.Game/Network/UnitOrders.cs b/OpenRa.Game/Network/UnitOrders.cs index c43d62356b..b5fade9941 100755 --- a/OpenRa.Game/Network/UnitOrders.cs +++ b/OpenRa.Game/Network/UnitOrders.cs @@ -18,12 +18,6 @@ namespace OpenRa.Network 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."); diff --git a/OpenRa.Game/World.cs b/OpenRa.Game/World.cs index 6edf470c32..43e2d4c670 100644 --- a/OpenRa.Game/World.cs +++ b/OpenRa.Game/World.cs @@ -21,10 +21,15 @@ namespace OpenRa public Player LocalPlayer { get { return players[localPlayerIndex]; } - set + } + + public void SetLocalPlayer(int index) + { + if (index != localPlayerIndex) { - localPlayerIndex = value.Index; - Game.viewport.GoToStartLocation( value ); + localPlayerIndex = index; + Game.viewport.GoToStartLocation(LocalPlayer); + Game.chat.AddLine(LocalPlayer, "is now YOU"); } }