From 8e1d45b3100453a11bcdf663d3567935c592098c Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 14 Nov 2018 00:57:52 -0500 Subject: override Galaxy client/server to support context sync (#480) This commit assumes the changes I requested in the game code to make GalaxyNetClient and GalaxyNetServer public are implemented in 1.3.32. --- src/SMAPI/Framework/Networking/SGalaxyNetClient.cs | 52 ++++++++++++ src/SMAPI/Framework/Networking/SGalaxyNetServer.cs | 96 ++++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 src/SMAPI/Framework/Networking/SGalaxyNetClient.cs create mode 100644 src/SMAPI/Framework/Networking/SGalaxyNetServer.cs (limited to 'src/SMAPI/Framework/Networking') diff --git a/src/SMAPI/Framework/Networking/SGalaxyNetClient.cs b/src/SMAPI/Framework/Networking/SGalaxyNetClient.cs new file mode 100644 index 00000000..fddd423d --- /dev/null +++ b/src/SMAPI/Framework/Networking/SGalaxyNetClient.cs @@ -0,0 +1,52 @@ +using System; +using Galaxy.Api; +using StardewValley.Network; +using StardewValley.SDKs; + +namespace StardewModdingAPI.Framework.Networking +{ + /// A multiplayer client used to connect to a hosted server. This is an implementation of with callbacks for SMAPI functionality. + internal class SGalaxyNetClient : GalaxyNetClient + { + /********* + ** Properties + *********/ + /// A callback to raise when receiving a message. This receives the incoming message, a method to send an arbitrary message, and a callback to run the default logic. + private readonly Action, Action> OnProcessingMessage; + + /// A callback to raise when sending a message. This receives the outgoing message, a method to send an arbitrary message, and a callback to resume the default logic. + private readonly Action, Action> OnSendingMessage; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The remote address being connected. + /// A callback to raise when receiving a message. This receives the incoming message, a method to send an arbitrary message, and a callback to run the default logic. + /// A callback to raise when sending a message. This receives the outgoing message, a method to send an arbitrary message, and a callback to resume the default logic. + public SGalaxyNetClient(GalaxyID address, Action, Action> onProcessingMessage, Action, Action> onSendingMessage) + : base(address) + { + this.OnProcessingMessage = onProcessingMessage; + this.OnSendingMessage = onSendingMessage; + } + + /// Send a message to the connected peer. + public override void sendMessage(OutgoingMessage message) + { + this.OnSendingMessage(message, base.sendMessage, () => base.sendMessage(message)); + } + + + /********* + ** Protected methods + *********/ + /// Process an incoming network message. + /// The message to process. + protected override void processIncomingMessage(IncomingMessage message) + { + this.OnProcessingMessage(message, base.sendMessage, () => base.processIncomingMessage(message)); + } + } +} diff --git a/src/SMAPI/Framework/Networking/SGalaxyNetServer.cs b/src/SMAPI/Framework/Networking/SGalaxyNetServer.cs new file mode 100644 index 00000000..99eae8ad --- /dev/null +++ b/src/SMAPI/Framework/Networking/SGalaxyNetServer.cs @@ -0,0 +1,96 @@ +using System; +using System.IO; +using Galaxy.Api; +using StardewModdingAPI.Framework.Reflection; +using StardewValley; +using StardewValley.Network; +using StardewValley.SDKs; + +namespace StardewModdingAPI.Framework.Networking +{ + /// A multiplayer server used to connect to an incoming player. This is an implementation of that adds support for SMAPI's metadata context exchange. + internal class SGalaxyNetServer : GalaxyNetServer + { + /********* + ** Properties + *********/ + /// 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; + + /// The underlying method which handles incoming connections. + private readonly Action BaseReceiveConnection; + + /// The underlying method which handles incoming disconnections. + private readonly Action BaseReceiveDisconnect; + + /// The underlying method which handles incoming errors. + private readonly Action BaseReceiveError; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The underlying game server. + /// Simplifies access to private code. + /// 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 SGalaxyNetServer(IGameServer gameServer, Reflector reflection, Action, Action> onProcessingMessage) + : base(gameServer) + { + this.OnProcessingMessage = onProcessingMessage; + this.Peers = reflection.GetField>(this, "peers").GetValue(); + this.Server = reflection.GetField(this, "server"); + + this.BaseReceiveConnection = (Action)Delegate.CreateDelegate(typeof(Action), this, reflection.GetMethod(this, "onReceiveConnection").MethodInfo); + this.BaseReceiveDisconnect = (Action)Delegate.CreateDelegate(typeof(Action), this, reflection.GetMethod(this, "onReceiveDisconnect").MethodInfo); + this.BaseReceiveError = (Action)Delegate.CreateDelegate(typeof(Action), this, reflection.GetMethod(this, "onReceiveError").MethodInfo); + } + + /// Receive and process messages from the client. + public override void receiveMessages() + { + GalaxySocket server = this.Server.GetValue(); + if (server == null) + return; + + server.Receive(this.BaseReceiveConnection, this.OnReceiveMessage, this.BaseReceiveDisconnect, this.BaseReceiveError); + server.Heartbeat(server.LobbyMembers()); + foreach (GalaxyID connection in server.Connections) + { + if (server.GetPingWith(connection) > 30000L) + server.Kick(connection); + } + } + + /// Read and process a message from the client. + /// The Galaxy peer ID. + /// The data to process. + private void OnReceiveMessage(GalaxyID peerID, Stream data) + { + using (IncomingMessage message = new IncomingMessage()) + using (BinaryReader reader = new BinaryReader(data)) + { + message.Read(reader); + this.OnProcessingMessage(message, outgoing => this.sendMessage(peerID, outgoing), () => + { + if (this.Peers.ContainsLeft(message.FarmerID) && (long)this.Peers[message.FarmerID] == (long)peerID.ToUint64()) + { + this.gameServer.processIncomingMessage(message); + } + else if (message.MessageType == Multiplayer.playerIntroduction) + { + NetFarmerRoot farmer = Game1.multiplayer.readFarmer(message.Reader); + GalaxyID capturedPeer = new GalaxyID(peerID.ToUint64()); + this.gameServer.checkFarmhandRequest(Convert.ToString(peerID.ToUint64()), farmer, msg => this.sendMessage(capturedPeer, msg), () => this.Peers[farmer.Value.UniqueMultiplayerID] = capturedPeer.ToUint64()); + } + }); + } + } + } +} -- cgit