예제 #1
0
        internal static void WriteDiscoveryResponse(NetworkAppConfig appConfig, NetOutgoingMessage message, GameInfo gameInfo, bool isFull, MemoryStream applicationData)
        {
            // START: Version safe data
            // {
            message.Write(appConfig.AppId);
            message.Write(gameInfo.Name.FilterName());
            message.Write((UInt16)(appConfig.ApplicationVersion));
            message.WriteVariableUInt32((UInt32)appConfig.ApplicationSignature.Length);
            message.Write(appConfig.ApplicationSignature);
            // }
            // END: Version safe data

            message.Write(gameInfo.IsInternetGame);
            message.Write(isFull);
            message.WriteMemoryStreamAsByteArray(applicationData); // <- fill in for WriteByteArray
        }
예제 #2
0
        public P2PNetwork(NetworkAppConfig appConfig, NetworkLogHandler networkLogHandler = null, BadNetworkSimulation badNetworkSimulation = null)
        {
            if (appConfig == null)
            {
                throw new ArgumentNullException("appConfig");
            }

            this.appConfig         = appConfig;
            this.NetworkLogHandler = networkLogHandler;

            Log("Configuration:");
            Log("  AppId = " + appConfig.AppId);
            Log("  Version = " + appConfig.ApplicationVersion);
            Log("  Signature = " + BitConverter.ToString(appConfig.ApplicationSignature));

            SetupSocket(badNetworkSimulation);

            LocalisedDisconnectReason = null;
        }
예제 #3
0
        public static DiscoveredGame ReadFromDiscoveryResponse(NetworkAppConfig appConfig, NetIncomingMessage message)
        {
            try
            {
                // START: Version safe data
                // {

                var theirAppId = message.ReadString();
                if (theirAppId != appConfig.AppId)
                {
                    return(null);                    // Wrong application
                }
                var gameName = message.ReadString().FilterName();

                var theirAppVersion         = message.ReadUInt16();
                var theirAppSignatureLength = (int)message.ReadVariableUInt32();

                byte[] theirAppSignature;
                if (theirAppSignatureLength < 0 ||
                    theirAppSignatureLength > NetworkAppConfig.ApplicationSignatureMaximumLength)
                {
                    theirAppSignature = null;
                }
                else
                {
                    theirAppSignature = message.ReadBytes(theirAppSignatureLength);
                }

                // }
                // END: Version safe data


                // Check for version mismatch
                if (theirAppVersion != appConfig.ApplicationVersion || theirAppSignature == null ||
                    !appConfig.ApplicationSignature.SequenceEqual(theirAppSignature))
                {
                    var gi = new GameInfo(gameName);
                    var dg = new DiscoveredGame(message);
                    dg.GameInfo = gi;

                    if (theirAppVersion < appConfig.ApplicationVersion)
                    {
                        dg.VersionMismatch = -1;
                    }
                    else if (theirAppVersion > appConfig.ApplicationVersion)
                    {
                        dg.VersionMismatch = 1;
                    }
                    else
                    {
                        dg.VersionMismatch = 0;
                    }

                    Debug.Assert(dg.VersionMismatch.HasValue);

                    return(dg);
                }

                // If we get here, all the versioning is correct:
                {
                    var isInternetGame = message.ReadBoolean();
                    var gi             = new GameInfo(gameName, isInternetGame,
                                                      false); // <- NOTE: we are assuming that the side-channel handles discovery

                    var dg = new DiscoveredGame(message);
                    dg.GameInfo        = gi;
                    dg.IsFull          = message.ReadBoolean();
                    dg.applicationData = message.ReadByteArray();
                    return(dg);
                }
            }
            catch
            {
                return(null);
            }
        }