From 90ecd377c88ba5a4c08a3c7e67618435358e5685 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 14 Nov 2018 00:11:09 -0500 Subject: rework multiplayer code to allow for upcoming Galaxy server overrides (#480) --- src/SMAPI/Framework/Networking/MultiplayerPeer.cs | 60 +++-------------------- src/SMAPI/Framework/Networking/SLidgrenServer.cs | 59 +++------------------- 2 files changed, 12 insertions(+), 107 deletions(-) (limited to 'src/SMAPI/Framework/Networking') diff --git a/src/SMAPI/Framework/Networking/MultiplayerPeer.cs b/src/SMAPI/Framework/Networking/MultiplayerPeer.cs index 7f0fa4f7..44a71978 100644 --- a/src/SMAPI/Framework/Networking/MultiplayerPeer.cs +++ b/src/SMAPI/Framework/Networking/MultiplayerPeer.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using Lidgren.Network; using StardewValley.Network; namespace StardewModdingAPI.Framework.Networking @@ -12,14 +11,8 @@ namespace StardewModdingAPI.Framework.Networking /********* ** Properties *********/ - /// The server through which to send messages, if this is an incoming farmhand. - private readonly SLidgrenServer Server; - - /// The client through which to send messages, if this is the host player. - private readonly SLidgrenClient Client; - - /// The network connection to the player. - private readonly NetConnection ServerConnection; + /// A method which sends a message to the peer. + private readonly Action SendMessageImpl; /********* @@ -53,11 +46,9 @@ namespace StardewModdingAPI.Framework.Networking /// Construct an instance. /// The player's unique ID. /// The metadata to copy. - /// The server through which to send messages. - /// The server connection through which to send messages. - /// The client through which to send messages. + /// A method which sends a message to the peer. /// Whether this is a connection to the host player. - public MultiplayerPeer(long playerID, RemoteContextModel model, SLidgrenServer server, NetConnection serverConnection, SLidgrenClient client, bool isHost) + public MultiplayerPeer(long playerID, RemoteContextModel model, Action sendMessage, bool isHost) { this.PlayerID = playerID; this.IsHost = isHost; @@ -68,43 +59,7 @@ namespace StardewModdingAPI.Framework.Networking this.ApiVersion = model.ApiVersion; this.Mods = model.Mods.Select(mod => new MultiplayerPeerMod(mod)).ToArray(); } - this.Server = server; - this.ServerConnection = serverConnection; - this.Client = client; - } - - /// Construct an instance for a connection to an incoming farmhand. - /// The player's unique ID. - /// The metadata to copy, if available. - /// The server through which to send messages. - /// The server connection through which to send messages. - public static MultiplayerPeer ForConnectionToFarmhand(long playerID, RemoteContextModel model, SLidgrenServer server, NetConnection serverConnection) - { - return new MultiplayerPeer( - playerID: playerID, - model: model, - server: server, - serverConnection: serverConnection, - client: null, - isHost: false - ); - } - - /// Construct an instance for a connection to the host player. - /// The player's unique ID. - /// The metadata to copy. - /// The client through which to send messages. - /// Whether this connection is for the host player. - public static MultiplayerPeer ForConnectionToHost(long playerID, RemoteContextModel model, SLidgrenClient client, bool isHost) - { - return new MultiplayerPeer( - playerID: playerID, - model: model, - server: null, - serverConnection: null, - client: client, - isHost: isHost - ); + this.SendMessageImpl = sendMessage; } /// Get metadata for a mod installed by the player. @@ -123,10 +78,7 @@ namespace StardewModdingAPI.Framework.Networking /// The message to send. public void SendMessage(OutgoingMessage message) { - if (this.IsHost) - this.Client.sendMessage(message); - else - this.Server.SendMessage(this.ServerConnection, message); + this.SendMessageImpl(message); } } } diff --git a/src/SMAPI/Framework/Networking/SLidgrenServer.cs b/src/SMAPI/Framework/Networking/SLidgrenServer.cs index 060b433b..36f96bc3 100644 --- a/src/SMAPI/Framework/Networking/SLidgrenServer.cs +++ b/src/SMAPI/Framework/Networking/SLidgrenServer.cs @@ -20,24 +20,15 @@ namespace StardewModdingAPI.Framework.Networking /// The constructor for the internal NetBufferReadStream type. private readonly ConstructorInfo NetBufferReadStreamConstructor = SLidgrenServer.GetNetBufferReadStreamConstructor(); - /// The constructor for the internal NetBufferWriteStream type. - private readonly ConstructorInfo NetBufferWriteStreamConstructor = SLidgrenServer.GetNetBufferWriteStreamConstructor(); - /// A method which reads farmer data from the given binary reader. private readonly Func ReadFarmer; - /// A callback to raise when receiving a message. This receives the server instance, raw/parsed incoming message, and a callback to run the default logic. - private readonly Action OnProcessingMessage; - - /// A callback to raise when sending a message. This receives the server instance, outgoing connection, outgoing message, target player ID, and a callback to run the default logic. - private readonly Action OnSendingMessage; + /// A callback to raise when receiving a message. This receives the incoming message, a method to send a message, and a callback to run the default logic. + private readonly Action, Action> OnProcessingMessage; /// The peer connections. private readonly Bimap Peers; - /// The underlying net server. - private readonly IReflectedField Server; - /********* ** Public methods @@ -46,34 +37,13 @@ namespace StardewModdingAPI.Framework.Networking /// The underlying game server. /// Simplifies access to private code. /// A method which reads farmer data from the given binary reader. - /// A callback to raise when receiving a message. This receives the server instance, raw/parsed incoming message, and a callback to run the default logic. - /// A callback to raise when sending a message. This receives the server instance, outgoing connection, outgoing message, and a callback to run the default logic. - public SLidgrenServer(IGameServer gameServer, Reflector reflection, Func readFarmer, Action onProcessingMessage, Action onSendingMessage) + /// A callback to raise when receiving a message. This receives the incoming message, a method to send a message, and a callback to run the default logic. + public SLidgrenServer(IGameServer gameServer, Reflector reflection, Func readFarmer, Action, Action> onProcessingMessage) : base(gameServer) { this.ReadFarmer = readFarmer; this.OnProcessingMessage = onProcessingMessage; - this.OnSendingMessage = onSendingMessage; this.Peers = reflection.GetField>(this, "peers").GetValue(); - this.Server = reflection.GetField(this, "server"); - } - - /// Send a message to a remote server. - /// The network connection. - /// The message to send. - /// This is an implementation of which calls . This method is invoked via . - public void SendMessage(NetConnection connection, OutgoingMessage message) - { - this.OnSendingMessage(this, connection, message, () => - { - NetServer server = this.Server.GetValue(); - NetOutgoingMessage netMessage = server.CreateMessage(); - using (Stream bufferWriteStream = (Stream)this.NetBufferWriteStreamConstructor.Invoke(new object[] { netMessage })) - using (BinaryWriter writer = new BinaryWriter(bufferWriteStream)) - message.Write(writer); - - server.SendMessage(netMessage, connection, NetDeliveryMethod.ReliableOrdered); - }); } /// Parse a data message from a client. @@ -91,14 +61,14 @@ namespace StardewModdingAPI.Framework.Networking while (rawMessage.LengthBits - rawMessage.Position >= 8) { message.Read(reader); - this.OnProcessingMessage(this, rawMessage, message, () => + this.OnProcessingMessage(message, outgoing => this.sendMessage(rawMessage.SenderConnection, outgoing), () => { if (this.Peers.ContainsLeft(message.FarmerID) && this.Peers[message.FarmerID] == peer) this.gameServer.processIncomingMessage(message); else if (message.MessageType == Multiplayer.playerIntroduction) { NetFarmerRoot farmer = this.ReadFarmer(message.Reader); - this.gameServer.checkFarmhandRequest("", farmer, msg => this.SendMessage(peer, msg), () => this.Peers[farmer.Value.UniqueMultiplayerID] = peer); + this.gameServer.checkFarmhandRequest("", farmer, msg => this.sendMessage(peer, msg), () => this.Peers[farmer.Value.UniqueMultiplayerID] = peer); } }); } @@ -127,22 +97,5 @@ namespace StardewModdingAPI.Framework.Networking return constructor; } - - /// Get the constructor for the internal NetBufferWriteStream type. - private static ConstructorInfo GetNetBufferWriteStreamConstructor() - { - // get type - string typeName = $"StardewValley.Network.NetBufferWriteStream, {Constants.GameAssemblyName}"; - Type type = Type.GetType(typeName); - if (type == null) - throw new InvalidOperationException($"Can't find type: {typeName}"); - - // get constructor - ConstructorInfo constructor = type.GetConstructor(new[] { typeof(NetBuffer) }); - if (constructor == null) - throw new InvalidOperationException($"Can't find constructor for type: {typeName}"); - - return constructor; - } } } -- cgit