using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using Galaxy.Api;
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;
/// SMAPI's implementation of the game's core multiplayer logic.
private readonly SMultiplayer Multiplayer;
/*********
** Public methods
*********/
/// Construct an instance.
/// The underlying game server.
/// SMAPI's implementation of the game's core multiplayer logic.
/// 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, SMultiplayer multiplayer, Action, Action> onProcessingMessage)
: base(gameServer)
{
this.Multiplayer = multiplayer;
this.OnProcessingMessage = onProcessingMessage;
}
/// Read and process a message from the client.
/// The Galaxy peer ID.
/// The data to process.
[SuppressMessage("ReSharper", "AccessToDisposedClosure", Justification = "The callback is invoked synchronously.")]
protected override void onReceiveMessage(GalaxyID peer, Stream messageStream)
{
using (IncomingMessage message = new IncomingMessage())
using (BinaryReader reader = new BinaryReader(messageStream))
{
message.Read(reader);
this.OnProcessingMessage(message, outgoing => this.sendMessage(peer, outgoing), () =>
{
if (this.peers.ContainsLeft(message.FarmerID) && (long)this.peers[message.FarmerID] == (long)peer.ToUint64())
{
this.gameServer.processIncomingMessage(message);
}
else if (message.MessageType == StardewValley.Multiplayer.playerIntroduction)
{
NetFarmerRoot farmer = this.Multiplayer.readFarmer(message.Reader);
GalaxyID capturedPeer = new GalaxyID(peer.ToUint64());
this.gameServer.checkFarmhandRequest(Convert.ToString(peer.ToUint64()), farmer, msg => this.sendMessage(capturedPeer, msg), () => this.peers[farmer.Value.UniqueMultiplayerID] = capturedPeer.ToUint64());
}
});
}
}
}
}