1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using Galaxy.Api;
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
{
/*********
** Fields
*********/
/// <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>SMAPI's implementation of the game's core multiplayer logic.</summary>
private readonly SMultiplayer Multiplayer;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="gameServer">The underlying game server.</param>
/// <param name="multiplayer">SMAPI's implementation of the game's core multiplayer logic.</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, SMultiplayer multiplayer, Action<IncomingMessage, Action<OutgoingMessage>, Action> onProcessingMessage)
: base(gameServer)
{
this.Multiplayer = multiplayer;
this.OnProcessingMessage = onProcessingMessage;
}
/*********
** Protected methods
*********/
/// <summary>Read and process a message from the client.</summary>
/// <param name="peer">The Galaxy peer ID.</param>
/// <param name="messageStream">The data to process.</param>
/// <remarks>This reimplements <see cref="GalaxyNetServer.onReceiveMessage"/>, but adds a callback to <see cref="OnProcessingMessage"/>.</remarks>
[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);
ulong peerID = peer.ToUint64(); // note: GalaxyID instances get reused, so need to store the underlying ID instead
this.OnProcessingMessage(message, outgoing => this.SendMessageToPeerID(peerID, outgoing), () =>
{
if (this.peers.ContainsLeft(message.FarmerID) && (long)this.peers[message.FarmerID] == (long)peerID)
this.gameServer.processIncomingMessage(message);
else if (message.MessageType == StardewValley.Multiplayer.playerIntroduction)
{
NetFarmerRoot farmer = this.Multiplayer.readFarmer(message.Reader);
GalaxyID capturedPeer = new GalaxyID(peerID);
this.gameServer.checkFarmhandRequest(Convert.ToString(peerID), farmer, msg => this.sendMessage(capturedPeer, msg), () => this.peers[farmer.Value.UniqueMultiplayerID] = capturedPeer.ToUint64());
}
});
}
}
/// <summary>Send a message to a remote peer.</summary>
/// <param name="peerID">The unique Galaxy ID, derived from <see cref="GalaxyID.ToUint64"/>.</param>
/// <param name="message">The message to send.</param>
private void SendMessageToPeerID(ulong peerID, OutgoingMessage message)
{
this.sendMessage(new GalaxyID(peerID), message);
}
}
}
|