diff options
-rw-r--r-- | src/SMAPI/Framework/Networking/SGalaxyNetClient.cs | 52 | ||||
-rw-r--r-- | src/SMAPI/Framework/Networking/SGalaxyNetServer.cs | 96 | ||||
-rw-r--r-- | src/SMAPI/Framework/SMultiplayer.cs | 13 | ||||
-rw-r--r-- | src/SMAPI/StardewModdingAPI.csproj | 6 |
4 files changed, 165 insertions, 2 deletions
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 +{ + /// <summary>A multiplayer client used to connect to a hosted server. This is an implementation of <see cref="GalaxyNetClient"/> with callbacks for SMAPI functionality.</summary> + internal class SGalaxyNetClient : GalaxyNetClient + { + /********* + ** Properties + *********/ + /// <summary>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.</summary> + private readonly Action<IncomingMessage, Action<OutgoingMessage>, Action> OnProcessingMessage; + + /// <summary>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.</summary> + private readonly Action<OutgoingMessage, Action<OutgoingMessage>, Action> OnSendingMessage; + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="address">The remote address being connected.</param> + /// <param name="onProcessingMessage">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.</param> + /// <param name="onSendingMessage">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.</param> + public SGalaxyNetClient(GalaxyID address, Action<IncomingMessage, Action<OutgoingMessage>, Action> onProcessingMessage, Action<OutgoingMessage, Action<OutgoingMessage>, Action> onSendingMessage) + : base(address) + { + this.OnProcessingMessage = onProcessingMessage; + this.OnSendingMessage = onSendingMessage; + } + + /// <summary>Send a message to the connected peer.</summary> + public override void sendMessage(OutgoingMessage message) + { + this.OnSendingMessage(message, base.sendMessage, () => base.sendMessage(message)); + } + + + /********* + ** Protected methods + *********/ + /// <summary>Process an incoming network message.</summary> + /// <param name="message">The message to process.</param> + 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 +{ + /// <summary>A multiplayer server used to connect to an incoming player. This is an implementation of <see cref="LidgrenServer"/> that adds support for SMAPI's metadata context exchange.</summary> + internal class SGalaxyNetServer : GalaxyNetServer + { + /********* + ** Properties + *********/ + /// <summary>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.</summary> + private readonly Action<IncomingMessage, Action<OutgoingMessage>, Action> OnProcessingMessage; + + /// <summary>The peer connections.</summary> + private readonly Bimap<long, ulong> Peers; + + /// <summary>The underlying net server.</summary> + private readonly IReflectedField<GalaxySocket> Server; + + /// <summary>The underlying method which handles incoming connections.</summary> + private readonly Action<GalaxyID> BaseReceiveConnection; + + /// <summary>The underlying method which handles incoming disconnections.</summary> + private readonly Action<GalaxyID> BaseReceiveDisconnect; + + /// <summary>The underlying method which handles incoming errors.</summary> + private readonly Action<string> BaseReceiveError; + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="gameServer">The underlying game server.</param> + /// <param name="reflection">Simplifies access to private code.</param> + /// <param name="onProcessingMessage">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.</param> + public SGalaxyNetServer(IGameServer gameServer, Reflector reflection, Action<IncomingMessage, Action<OutgoingMessage>, Action> onProcessingMessage) + : base(gameServer) + { + this.OnProcessingMessage = onProcessingMessage; + this.Peers = reflection.GetField<Bimap<long, ulong>>(this, "peers").GetValue(); + this.Server = reflection.GetField<GalaxySocket>(this, "server"); + + this.BaseReceiveConnection = (Action<GalaxyID>)Delegate.CreateDelegate(typeof(Action<GalaxyID>), this, reflection.GetMethod(this, "onReceiveConnection").MethodInfo); + this.BaseReceiveDisconnect = (Action<GalaxyID>)Delegate.CreateDelegate(typeof(Action<GalaxyID>), this, reflection.GetMethod(this, "onReceiveDisconnect").MethodInfo); + this.BaseReceiveError = (Action<string>)Delegate.CreateDelegate(typeof(Action<string>), this, reflection.GetMethod(this, "onReceiveError").MethodInfo); + } + + /// <summary>Receive and process messages from the client.</summary> + 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); + } + } + + /// <summary>Read and process a message from the client.</summary> + /// <param name="peerID">The Galaxy peer ID.</param> + /// <param name="data">The data to process.</param> + 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()); + } + }); + } + } + } +} diff --git a/src/SMAPI/Framework/SMultiplayer.cs b/src/SMAPI/Framework/SMultiplayer.cs index 6ce7596d..5a8aa3e5 100644 --- a/src/SMAPI/Framework/SMultiplayer.cs +++ b/src/SMAPI/Framework/SMultiplayer.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using Galaxy.Api; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using StardewModdingAPI.Events; @@ -11,6 +12,7 @@ using StardewModdingAPI.Framework.Reflection; using StardewModdingAPI.Toolkit.Serialisation; using StardewValley; using StardewValley.Network; +using StardewValley.SDKs; namespace StardewModdingAPI.Framework { @@ -113,6 +115,12 @@ namespace StardewModdingAPI.Framework return new SLidgrenClient(address, this.OnClientProcessingMessage, this.OnClientSendingMessage); } + case GalaxyNetClient _: + { + GalaxyID address = this.Reflection.GetField<GalaxyID>(client, "lobbyId").GetValue(); + return new SGalaxyNetClient(address, this.OnClientProcessingMessage, this.OnClientSendingMessage); + } + default: return client; } @@ -130,6 +138,11 @@ namespace StardewModdingAPI.Framework return new SLidgrenServer(gameServer, this.Reflection, this.readFarmer, this.OnServerProcessingMessage); } + case GalaxyNetServer _: + { + IGameServer gameServer = this.Reflection.GetField<IGameServer>(server, "gameServer").GetValue(); + return new SGalaxyNetServer(gameServer, this.Reflection, this.OnServerProcessingMessage); + } default: return server; diff --git a/src/SMAPI/StardewModdingAPI.csproj b/src/SMAPI/StardewModdingAPI.csproj index 471e98fc..29c9f7fa 100644 --- a/src/SMAPI/StardewModdingAPI.csproj +++ b/src/SMAPI/StardewModdingAPI.csproj @@ -55,7 +55,7 @@ </PropertyGroup> <ItemGroup> <PackageReference Include="LargeAddressAware" Version="1.0.3" /> - <PackageReference Include="Lib.Harmony" Version="1.2.0.1 "/> + <PackageReference Include="Lib.Harmony" Version="1.2.0.1" /> <PackageReference Include="Mono.Cecil" Version="0.10.1" /> <PackageReference Include="Newtonsoft.Json" Version="11.0.2" /> </ItemGroup> @@ -174,13 +174,15 @@ <Compile Include="Framework\Events\ModPlayerEvents.cs" /> <Compile Include="Framework\Events\ModSpecialisedEvents.cs" /> <Compile Include="Framework\Events\ModWorldEvents.cs" /> - <Compile Include="Framework\Networking\MessageType.cs" /> <Compile Include="Framework\ModHelpers\DataHelper.cs" /> + <Compile Include="Framework\Networking\MessageType.cs" /> <Compile Include="Framework\Networking\ModMessageModel.cs" /> <Compile Include="Framework\Networking\MultiplayerPeer.cs" /> <Compile Include="Framework\Networking\MultiplayerPeerMod.cs" /> <Compile Include="Framework\Networking\RemoteContextModel.cs" /> <Compile Include="Framework\Networking\RemoteContextModModel.cs" /> + <Compile Include="Framework\Networking\SGalaxyNetClient.cs" /> + <Compile Include="Framework\Networking\SGalaxyNetServer.cs" /> <Compile Include="Framework\Networking\SLidgrenClient.cs" /> <Compile Include="Framework\Networking\SLidgrenServer.cs" /> <Compile Include="Framework\SCore.cs" /> |