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
{
/*********
** Fields
*********/
/// 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 SLidgrenClient(string 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));
}
}
}