summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Networking
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2018-11-04 21:34:48 -0500
committerJesse Plamondon-Willard <github@jplamondonw.com>2018-11-04 21:34:48 -0500
commitbfb40202793f2f7f2c9c73272f01a477b23edfa2 (patch)
tree485dda7f78e988bb5e0ecd605cbcb708831249d9 /src/SMAPI/Framework/Networking
parent02a46bf13f29ce0dd8ac2f422113083c59dae42d (diff)
downloadSMAPI-bfb40202793f2f7f2c9c73272f01a477b23edfa2.tar.gz
SMAPI-bfb40202793f2f7f2c9c73272f01a477b23edfa2.tar.bz2
SMAPI-bfb40202793f2f7f2c9c73272f01a477b23edfa2.zip
rewrite multiplayer sync to use generic callbacks from client/server for better extensibility (#480)
Diffstat (limited to 'src/SMAPI/Framework/Networking')
-rw-r--r--src/SMAPI/Framework/Networking/MessageType.cs26
-rw-r--r--src/SMAPI/Framework/Networking/MultiplayerPeer.cs6
-rw-r--r--src/SMAPI/Framework/Networking/SLidgrenClient.cs37
-rw-r--r--src/SMAPI/Framework/Networking/SLidgrenServer.cs122
4 files changed, 160 insertions, 31 deletions
diff --git a/src/SMAPI/Framework/Networking/MessageType.cs b/src/SMAPI/Framework/Networking/MessageType.cs
new file mode 100644
index 00000000..bd9acfa9
--- /dev/null
+++ b/src/SMAPI/Framework/Networking/MessageType.cs
@@ -0,0 +1,26 @@
+using StardewValley;
+
+namespace StardewModdingAPI.Framework.Networking
+{
+ /// <summary>Network message types recognised by SMAPI and Stardew Valley.</summary>
+ internal enum MessageType : byte
+ {
+ /*********
+ ** SMAPI
+ *********/
+ /// <summary>A data message intended for mods to consume.</summary>
+ ModMessage = 254,
+
+ /// <summary>Metadata context about a player synced by SMAPI.</summary>
+ ModContext = 255,
+
+ /*********
+ ** Vanilla
+ *********/
+ /// <summary>Metadata about the host server sent to a farmhand.</summary>
+ ServerIntroduction = Multiplayer.serverIntroduction,
+
+ /// <summary>Metadata about a player sent to a farmhand or server.</summary>
+ PlayerIntroduction = Multiplayer.playerIntroduction
+ }
+}
diff --git a/src/SMAPI/Framework/Networking/MultiplayerPeer.cs b/src/SMAPI/Framework/Networking/MultiplayerPeer.cs
index c7f8ffad..e703dbb1 100644
--- a/src/SMAPI/Framework/Networking/MultiplayerPeer.cs
+++ b/src/SMAPI/Framework/Networking/MultiplayerPeer.cs
@@ -2,7 +2,6 @@ using System;
using System.Collections.Generic;
using System.Linq;
using Lidgren.Network;
-using StardewValley;
using StardewValley.Network;
namespace StardewModdingAPI.Framework.Networking
@@ -95,7 +94,8 @@ namespace StardewModdingAPI.Framework.Networking
/// <param name="playerID">The player's unique ID.</param>
/// <param name="model">The metadata to copy.</param>
/// <param name="client">The client through which to send messages.</param>
- public static MultiplayerPeer ForConnectionToHost(long playerID, RemoteContextModel model, SLidgrenClient client)
+ /// <param name="isHost">Whether this connection is for the host player.</param>
+ public static MultiplayerPeer ForConnectionToHost(long playerID, RemoteContextModel model, SLidgrenClient client, bool isHost)
{
return new MultiplayerPeer(
playerID: playerID,
@@ -103,7 +103,7 @@ namespace StardewModdingAPI.Framework.Networking
server: null,
serverConnection: null,
client: client,
- isHost: true
+ isHost: isHost
);
}
diff --git a/src/SMAPI/Framework/Networking/SLidgrenClient.cs b/src/SMAPI/Framework/Networking/SLidgrenClient.cs
index 9dfdba15..c05e6b76 100644
--- a/src/SMAPI/Framework/Networking/SLidgrenClient.cs
+++ b/src/SMAPI/Framework/Networking/SLidgrenClient.cs
@@ -1,44 +1,38 @@
using System;
-using StardewValley;
using StardewValley.Network;
namespace StardewModdingAPI.Framework.Networking
{
- /// <summary>A multiplayer client used to connect to a hosted server. This is an implementation of <see cref="LidgrenClient"/> that adds support for SMAPI's metadata context exchange.</summary>
+ /// <summary>A multiplayer client used to connect to a hosted server. This is an implementation of <see cref="LidgrenClient"/> with callbacks for SMAPI functionality.</summary>
internal class SLidgrenClient : LidgrenClient
{
/*********
** Properties
*********/
- /// <summary>Get the metadata to include in a metadata message sent to other players.</summary>
- private readonly Func<object[]> GetMetadataMessageFields;
-
- /// <summary>The method to call when receiving a custom SMAPI message from the server, which returns whether the message was processed.</summary>
- private readonly Func<SLidgrenClient, IncomingMessage, bool> TryProcessMessage;
+ /// <summary>A callback to raise when receiving a message. This receives the client instance, incoming message, and a callback to run the default logic.</summary>
+ private readonly Action<SLidgrenClient, IncomingMessage, Action> OnProcessingMessage;
+ /// <summary>A callback to raise when sending a message. This receives the client instance, outgoing message, and a callback to run the default logic.</summary>
+ private readonly Action<SLidgrenClient, OutgoingMessage, Action> OnSendingMessage;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="address">The remote address being connected.</param>
- /// <param name="getMetadataMessageFields">Get the metadata to include in a metadata message sent to other players.</param>
- /// <param name="tryProcessMessage">The method to call when receiving a custom SMAPI message from the server, which returns whether the message was processed..</param>
- public SLidgrenClient(string address, Func<object[]> getMetadataMessageFields, Func<SLidgrenClient, IncomingMessage, bool> tryProcessMessage)
+ /// <param name="onProcessingMessage">A callback to raise when receiving a message. This receives the client instance, incoming message, and a callback to run the default logic.</param>
+ /// <param name="onSendingMessage">A callback to raise when sending a message. This receives the client instance, outgoing message, and a callback to run the default logic.</param>
+ public SLidgrenClient(string address, Action<SLidgrenClient, IncomingMessage, Action> onProcessingMessage, Action<SLidgrenClient, OutgoingMessage, Action> onSendingMessage)
: base(address)
{
- this.GetMetadataMessageFields = getMetadataMessageFields;
- this.TryProcessMessage = tryProcessMessage;
+ this.OnProcessingMessage = onProcessingMessage;
+ this.OnSendingMessage = onSendingMessage;
}
- /// <summary>Send the metadata needed to connect with a remote server.</summary>
- public override void sendPlayerIntroduction()
+ /// <summary>Send a message to the connected peer.</summary>
+ public override void sendMessage(OutgoingMessage message)
{
- // send custom intro
- if (this.getUserID() != "")
- Game1.player.userID.Value = this.getUserID();
- this.sendMessage(SMultiplayer.ContextSyncMessageID, this.GetMetadataMessageFields());
- base.sendPlayerIntroduction();
+ this.OnSendingMessage(this, message, () => base.sendMessage(message));
}
@@ -49,10 +43,7 @@ namespace StardewModdingAPI.Framework.Networking
/// <param name="message">The message to process.</param>
protected override void processIncomingMessage(IncomingMessage message)
{
- if (this.TryProcessMessage(this, message))
- return;
-
- base.processIncomingMessage(message);
+ this.OnProcessingMessage(this, message, () => base.processIncomingMessage(message));
}
}
}
diff --git a/src/SMAPI/Framework/Networking/SLidgrenServer.cs b/src/SMAPI/Framework/Networking/SLidgrenServer.cs
index 971eb66d..060b433b 100644
--- a/src/SMAPI/Framework/Networking/SLidgrenServer.cs
+++ b/src/SMAPI/Framework/Networking/SLidgrenServer.cs
@@ -1,5 +1,11 @@
+using System;
+using System.Diagnostics.CodeAnalysis;
+using System.IO;
using System.Reflection;
using Lidgren.Network;
+using StardewModdingAPI.Framework.Reflection;
+using StardewModdingAPI.Patches;
+using StardewValley;
using StardewValley.Network;
namespace StardewModdingAPI.Framework.Networking
@@ -10,8 +16,27 @@ namespace StardewModdingAPI.Framework.Networking
/*********
** Properties
*********/
- /// <summary>A method which sends a message through a specific connection.</summary>
- private readonly MethodInfo SendMessageToConnectionMethod;
+
+ /// <summary>The constructor for the internal <c>NetBufferReadStream</c> type.</summary>
+ private readonly ConstructorInfo NetBufferReadStreamConstructor = SLidgrenServer.GetNetBufferReadStreamConstructor();
+
+ /// <summary>The constructor for the internal <c>NetBufferWriteStream</c> type.</summary>
+ private readonly ConstructorInfo NetBufferWriteStreamConstructor = SLidgrenServer.GetNetBufferWriteStreamConstructor();
+
+ /// <summary>A method which reads farmer data from the given binary reader.</summary>
+ private readonly Func<BinaryReader, NetFarmerRoot> ReadFarmer;
+
+ /// <summary>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.</summary>
+ private readonly Action<SLidgrenServer, NetIncomingMessage, IncomingMessage, Action> OnProcessingMessage;
+
+ /// <summary>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.</summary>
+ private readonly Action<SLidgrenServer, NetConnection, OutgoingMessage, Action> OnSendingMessage;
+
+ /// <summary>The peer connections.</summary>
+ private readonly Bimap<long, NetConnection> Peers;
+
+ /// <summary>The underlying net server.</summary>
+ private readonly IReflectedField<NetServer> Server;
/*********
@@ -19,18 +44,105 @@ namespace StardewModdingAPI.Framework.Networking
*********/
/// <summary>Construct an instance.</summary>
/// <param name="gameServer">The underlying game server.</param>
- public SLidgrenServer(IGameServer gameServer)
+ /// <param name="reflection">Simplifies access to private code.</param>
+ /// <param name="readFarmer">A method which reads farmer data from the given binary reader.</param>
+ /// <param name="onProcessingMessage">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.</param>
+ /// <param name="onSendingMessage">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.</param>
+ public SLidgrenServer(IGameServer gameServer, Reflector reflection, Func<BinaryReader, NetFarmerRoot> readFarmer, Action<SLidgrenServer, NetIncomingMessage, IncomingMessage, Action> onProcessingMessage, Action<SLidgrenServer, NetConnection, OutgoingMessage, Action> onSendingMessage)
: base(gameServer)
{
- this.SendMessageToConnectionMethod = typeof(LidgrenServer).GetMethod(nameof(LidgrenServer.sendMessage), BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(NetConnection), typeof(OutgoingMessage) }, null);
+ this.ReadFarmer = readFarmer;
+ this.OnProcessingMessage = onProcessingMessage;
+ this.OnSendingMessage = onSendingMessage;
+ this.Peers = reflection.GetField<Bimap<long, NetConnection>>(this, "peers").GetValue();
+ this.Server = reflection.GetField<NetServer>(this, "server");
}
/// <summary>Send a message to a remote server.</summary>
/// <param name="connection">The network connection.</param>
/// <param name="message">The message to send.</param>
+ /// <remarks>This is an implementation of <see cref="LidgrenServer.sendMessage(NetConnection, OutgoingMessage)"/> which calls <see cref="OnSendingMessage"/>. This method is invoked via <see cref="LidgrenServerPatch.Prefix_LidgrenServer_SendMessage"/>.</remarks>
public void SendMessage(NetConnection connection, OutgoingMessage message)
{
- this.SendMessageToConnectionMethod.Invoke(this, new object[] { connection, 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);
+ });
+ }
+
+ /// <summary>Parse a data message from a client.</summary>
+ /// <param name="rawMessage">The raw network message to parse.</param>
+ /// <remarks>This is an implementation of <see cref="LidgrenServer.parseDataMessageFromClient"/> which calls <see cref="OnProcessingMessage"/>. This method is invoked via <see cref="LidgrenServerPatch.Prefix_LidgrenServer_ParseDataMessageFromClient"/>.</remarks>
+ [SuppressMessage("ReSharper", "AccessToDisposedClosure", Justification = "The callback is invoked synchronously.")]
+ public bool ParseDataMessageFromClient(NetIncomingMessage rawMessage)
+ {
+ // add hook to call multiplayer core
+ NetConnection peer = rawMessage.SenderConnection;
+ using (IncomingMessage message = new IncomingMessage())
+ using (Stream readStream = (Stream)this.NetBufferReadStreamConstructor.Invoke(new object[] { rawMessage }))
+ using (BinaryReader reader = new BinaryReader(readStream))
+ {
+ while (rawMessage.LengthBits - rawMessage.Position >= 8)
+ {
+ message.Read(reader);
+ this.OnProcessingMessage(this, rawMessage, message, () =>
+ {
+ 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);
+ }
+ });
+ }
+ }
+
+ return false;
+ }
+
+
+ /*********
+ ** Private methods
+ *********/
+ /// <summary>Get the constructor for the internal <c>NetBufferReadStream</c> type.</summary>
+ private static ConstructorInfo GetNetBufferReadStreamConstructor()
+ {
+ // get type
+ string typeName = $"StardewValley.Network.NetBufferReadStream, {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;
+ }
+
+ /// <summary>Get the constructor for the internal <c>NetBufferWriteStream</c> type.</summary>
+ 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;
}
}
}