summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Networking/SLidgrenClient.cs
blob: 9dfdba15fdc4b0692e16df0bd4244df4e6ce6737 (plain)
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
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>
    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;


        /*********
        ** 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)
            : base(address)
        {
            this.GetMetadataMessageFields = getMetadataMessageFields;
            this.TryProcessMessage = tryProcessMessage;
        }

        /// <summary>Send the metadata needed to connect with a remote server.</summary>
        public override void sendPlayerIntroduction()
        {
            // send custom intro
            if (this.getUserID() != "")
                Game1.player.userID.Value = this.getUserID();
            this.sendMessage(SMultiplayer.ContextSyncMessageID, this.GetMetadataMessageFields());
            base.sendPlayerIntroduction();
        }


        /*********
        ** Protected methods
        *********/
        /// <summary>Process an incoming network message.</summary>
        /// <param name="message">The message to process.</param>
        protected override void processIncomingMessage(IncomingMessage message)
        {
            if (this.TryProcessMessage(this, message))
                return;

            base.processIncomingMessage(message);
        }
    }
}