using System; using StardewValley.Network; 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 SLidgrenClient : LidgrenClient { /********* ** Properties *********/ /// A callback to raise when receiving a message. This receives the client instance, 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 client instance, outgoing message, and a callback to run the default logic. private readonly Action OnSendingMessage; /********* ** Public methods *********/ /// Construct an instance. /// The remote address being connected. /// A callback to raise when receiving a message. This receives the client instance, incoming message, and a callback to run the default logic. /// A callback to raise when sending a message. This receives the client instance, outgoing message, and a callback to run the default logic. public SLidgrenClient(string address, Action onProcessingMessage, 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(this, message, () => base.sendMessage(message)); } /********* ** Protected methods *********/ /// Process an incoming network message. /// The message to process. protected override void processIncomingMessage(IncomingMessage message) { this.OnProcessingMessage(this, message, () => base.processIncomingMessage(message)); } } }