Exemplo n.º 1
0
        /// <summary>
        /// Sends request to get confirmation code
        /// </summary>
        public void RequestConfirmationCode()
        {
            Mst.Events.Invoke(MstEventKeys.showLoadingInfo, "Sending confirmation code... Please wait!");

            logger.Debug("Sending confirmation code... Please wait!");

            MstTimer.WaitForSeconds(1f, () =>
            {
                Mst.Client.Auth.RequestEmailConfirmationCode((isSuccessful, error) =>
                {
                    Mst.Events.Invoke(MstEventKeys.hideLoadingInfo);

                    if (isSuccessful)
                    {
                        Mst.Events.Invoke(MstEventKeys.showOkDialogBox, new OkDialogBoxEventMessage($"We have sent an email with confirmation code to your address '{Mst.Client.Auth.AccountInfo.Email}'", null));
                    }
                    else
                    {
                        string outputMessage = $"An error occurred while requesting confirmation code: {error}";
                        Mst.Events.Invoke(MstEventKeys.showOkDialogBox, new OkDialogBoxEventMessage(outputMessage, null));
                        logger.Error(outputMessage);
                    }
                });
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sends request to master to generate rest password code and send it to user email
        /// </summary>
        /// <param name="userEmail"></param>
        public void RequestResetPasswordCode(string userEmail)
        {
            Mst.Events.Invoke(MstEventKeys.showLoadingInfo, "Sending reset password code... Please wait!");

            logger.Debug("Sending reset password code... Please wait!");

            MstTimer.WaitForSeconds(1f, () =>
            {
                Mst.Client.Auth.RequestPasswordReset(userEmail, (isSuccessful, error) =>
                {
                    Mst.Events.Invoke(MstEventKeys.hideLoadingInfo);

                    if (isSuccessful)
                    {
                        Mst.Options.Set(MstDictKeys.resetPasswordEmail, userEmail);

                        Mst.Events.Invoke(MstEventKeys.hidePasswordResetCodeView);
                        Mst.Events.Invoke(MstEventKeys.showPasswordResetView);
                        Mst.Events.Invoke(MstEventKeys.showOkDialogBox, new OkDialogBoxEventMessage($"We have sent an email with reset code to your address '{userEmail}'", null));
                    }
                    else
                    {
                        string outputMessage = $"An error occurred while password reset code: {error}";
                        Mst.Events.Invoke(MstEventKeys.showOkDialogBox, new OkDialogBoxEventMessage(outputMessage, null));
                        logger.Error(outputMessage);
                    }
                });
            });
        }
Exemplo n.º 3
0
        /// <summary>
        /// Send request to master server to change password
        /// </summary>
        /// <param name="userEmail"></param>
        /// <param name="resetCode"></param>
        /// <param name="newPassword"></param>
        public void ResetPassword(string userEmail, string resetCode, string newPassword)
        {
            Mst.Events.Invoke(MstEventKeys.showLoadingInfo, "Changing password... Please wait!");

            logger.Debug("Changing password... Please wait!");

            MstTimer.WaitForSeconds(1f, () =>
            {
                Mst.Client.Auth.ChangePassword(userEmail, resetCode, newPassword, (isSuccessful, error) =>
                {
                    Mst.Events.Invoke(MstEventKeys.hideLoadingInfo);

                    if (isSuccessful)
                    {
                        Mst.Events.Invoke(MstEventKeys.hidePasswordResetView);
                        Mst.Events.Invoke(MstEventKeys.showSignInView);
                        Mst.Events.Invoke(MstEventKeys.showOkDialogBox, new OkDialogBoxEventMessage("You have successfuly changed your password. Now you can sign in.", null));
                    }
                    else
                    {
                        string outputMessage = $"An error occurred while changing password: {error}";
                        Mst.Events.Invoke(MstEventKeys.showOkDialogBox, new OkDialogBoxEventMessage(outputMessage, null));
                        logger.Error(outputMessage);
                    }
                });
            });
        }
Exemplo n.º 4
0
        /// <summary>
        /// Sends sign up request to master server
        /// </summary>
        public void SignUp(string username, string useremail, string userpassword)
        {
            Mst.Events.Invoke(MstEventKeys.showLoadingInfo, "Signing up... Please wait!");

            logger.Debug("Signing up... Please wait!");

            MstTimer.WaitForSeconds(1f, () =>
            {
                var credentials = new MstProperties();
                credentials.Set(MstDictKeys.userName, username);
                credentials.Set(MstDictKeys.userEmail, useremail);
                credentials.Set(MstDictKeys.userPassword, userpassword);

                Mst.Client.Auth.SignUp(credentials, (isSuccessful, error) =>
                {
                    Mst.Events.Invoke(MstEventKeys.hideLoadingInfo);

                    if (isSuccessful)
                    {
                        Mst.Events.Invoke(MstEventKeys.hideSignUpView);
                        Mst.Events.Invoke(MstEventKeys.showSignInView);
                        Mst.Events.Invoke(MstEventKeys.setSignInDefaultCredentials, credentials);

                        logger.Debug($"You have successfuly signed up. Now you may sign in");
                    }
                    else
                    {
                        string outputMessage = $"An error occurred while signing up: {error}";
                        Mst.Events.Invoke(MstEventKeys.showOkDialogBox, new OkDialogBoxEventMessage(outputMessage, null));
                        logger.Error(outputMessage);
                    }
                });
            });
        }
Exemplo n.º 5
0
        /// <summary>
        /// Sends request to confirm account with confirmation code
        /// </summary>
        public void ConfirmAccount(string confirmationCode)
        {
            Mst.Events.Invoke(MstEventKeys.showLoadingInfo, "Confirming your account... Please wait!");

            logger.Debug("Sending confirmation code... Please wait!");

            MstTimer.WaitForSeconds(1f, () =>
            {
                Mst.Client.Auth.ConfirmEmail(confirmationCode, (isSuccessful, error) =>
                {
                    Mst.Events.Invoke(MstEventKeys.hideLoadingInfo);

                    if (isSuccessful)
                    {
                        Mst.Events.Invoke(MstEventKeys.hideEmailConfirmationView);
                    }
                    else
                    {
                        string outputMessage = $"An error occurred while confirming yor account: {error}";
                        Mst.Events.Invoke(MstEventKeys.showOkDialogBox, new OkDialogBoxEventMessage(outputMessage, null));
                        logger.Error(outputMessage);
                    }
                });
            });
        }
Exemplo n.º 6
0
        protected override void OnInitialize()
        {
            // If we hav offline scene in global options
            if (Mst.Options.Has(MstDictKeys.ROOM_OFFLINE_SCENE_NAME))
            {
                logger.Debug("Assigning offline scene to mirror network manager");
                RoomNetworkManager.offlineScene = Mst.Options.AsString(MstDictKeys.ROOM_OFFLINE_SCENE_NAME);
            }

            // Start listening to OnServerStartedEvent of our MirrorNetworkManager
            if (NetworkManager.singleton is RoomNetworkManager manager)
            {
                manager.OnClientStartedEvent += OnMirrorClientStartedEventHandler;
                manager.OnClientStoppedEvent += OnMirrorClientStoppedEventHandler;
            }
            else
            {
                logger.Error("Before using MirrorNetworkManager add it to scene");
            }

            // Add master server connection and disconnection listeners
            Connection.AddDisconnectionListener(OnDisconnectedFromMasterServerEventHandler, false);

            MstTimer.WaitForSeconds(0.5f, () =>
            {
                // If connection to master server is not established
                if (!Connection.IsConnected && !Connection.IsConnecting)
                {
                    Connection.UseSsl = MstApplicationConfig.Instance.UseSecure;
                    Connection.Connect(masterIp, masterPort);
                }
            });
        }
Exemplo n.º 7
0
        /// <summary>
        /// Sends sign in as guest request to master server
        /// </summary>
        public void SignInAsGuest()
        {
            Mst.Events.Invoke(MstEventKeys.showLoadingInfo, "Signing in as guest... Please wait!");

            logger.Debug("Signing in as guest... Please wait!");

            MstTimer.WaitForSeconds(1f, () =>
            {
                Mst.Client.Auth.SignInAsGuest((accountInfo, error) =>
                {
                    Mst.Events.Invoke(MstEventKeys.hideLoadingInfo);

                    if (accountInfo != null)
                    {
                        Mst.Events.Invoke(MstEventKeys.hideSignInView);

                        logger.Debug($"You are successfully logged in as {Mst.Client.Auth.AccountInfo}");
                    }
                    else
                    {
                        string outputMessage = $"An error occurred while signing in: {error}";
                        Mst.Events.Invoke(MstEventKeys.showOkDialogBox, new OkDialogBoxEventMessage(outputMessage, null));
                        logger.Error(outputMessage);
                    }
                });
            });
        }
Exemplo n.º 8
0
        /// <summary>
        /// Sends request to master server to find games list
        /// </summary>
        public void FindGames()
        {
            ClearGamesList();

            canvasGroup.interactable = false;

            if (statusInfoText)
            {
                statusInfoText.text = "Finding rooms... Please wait!";
                statusInfoText.gameObject.SetActive(true);
            }

            MstTimer.WaitForSeconds(0.2f, () =>
            {
                Mst.Client.Matchmaker.FindGames((games) =>
                {
                    canvasGroup.interactable = true;

                    if (games.Count == 0)
                    {
                        statusInfoText.text = "No games found! Try to create your own.";
                        return;
                    }

                    statusInfoText.gameObject.SetActive(false);
                    DrawGamesList(games);
                });
            });
        }
Exemplo n.º 9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="peer"></param>
        protected override void OnPeerDisconnected(IPeer peer)
        {
            MstTimer.WaitForSeconds(0.2f, () =>
            {
                // Try to find player in filtered list
                if (roomPlayersByRoomPeerId.TryGetValue(peer.Id, out IRoomPlayerPeerExtension player))
                {
                    logger.Debug($"Room server player {player.Username} with room client Id {peer.Id} left the room");

                    // Remove this player from filtered list
                    roomPlayersByRoomPeerId.Remove(player.Peer.Id);
                    roomPlayersByMsfPeerId.Remove(player.MasterPeerId);
                    roomPlayersByUsername.Remove(player.Username);

                    // Notify master server about disconnected player
                    if (RoomController.IsActive)
                    {
                        RoomController.NotifyPlayerLeft(player.MasterPeerId);
                    }

                    // Inform subscribers about this bad guy
                    OnPlayerLeftRoomEvent?.Invoke(player);

                    // Calling termination conditions check
                    OnCheckTerminationConditionEvent?.Invoke();
                }
                else
                {
                    logger.Debug($"Room server client {peer.Id} left the room");
                }
            });
        }
Exemplo n.º 10
0
        protected virtual void JoinTheRoom()
        {
            Mst.Events.Invoke(MstEventKeys.showLoadingInfo, $"Joinig room {roomAccess.RoomId}... Please wait!");

            // Wait for connection to mirror server
            MstTimer.WaitWhile(() => !NetworkClient.isConnected, isSuccessful =>
            {
                Mst.Events.Invoke(MstEventKeys.hideLoadingInfo);

                if (!isSuccessful)
                {
                    logger.Error("We could not join the room. Please try again later or contact to administrator");
                    RoomNetworkManager.StopClient();
                }
                else
                {
                    OnConnectedToMirrorServerEventHandler(NetworkClient.connection);
                }
            }, roomConnectionTimeout);

            // If we are not connected to mirror server
            if (!NetworkClient.isConnected)
            {
                // Let's set the IP before we start connection
                SetAddress(roomAccess.RoomIp);

                // Let's set the port before we start connection
                SetPort(roomAccess.RoomPort);

                logger.Debug("Connecting to mirror server...");

                // Start mirror client
                RoomNetworkManager.StartClient();
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Sends request to master server to signed in with token
        /// </summary>
        public void SignInWithToken()
        {
            Mst.Events.Invoke(MstEventKeys.showLoadingInfo, "Signing in... Please wait!");

            logger.Debug("Signing in... Please wait!");

            MstTimer.WaitForSeconds(1f, () =>
            {
                Mst.Client.Auth.SignInWithToken((accountInfo, error) =>
                {
                    Mst.Events.Invoke(MstEventKeys.hideLoadingInfo);

                    if (accountInfo != null)
                    {
                        if (accountInfo.IsEmailConfirmed)
                        {
                            logger.Debug($"You are successfully logged in. {Mst.Client.Auth.AccountInfo}");
                        }
                        else
                        {
                            Mst.Events.Invoke(MstEventKeys.showEmailConfirmationView, Mst.Client.Auth.AccountInfo.Email);
                        }
                    }
                    else
                    {
                        outputMessage = $"An error occurred while signing in: {error}";
                        Mst.Events.Invoke(MstEventKeys.showOkDialogBox, new OkDialogBoxEventMessage(outputMessage, null));
                        logger.Error(outputMessage);
                    }
                });
            });
        }
Exemplo n.º 12
0
        protected override void OnInitialize()
        {
            Mst.Events.Invoke(MstEventKeys.showLoadingInfo, "Connecting to master... Please wait!");

            // If we want to use default credentials for signin or signup views
            if (useDefaultCredentials && Mst.Runtime.IsEditor)
            {
                var credentials = new MstProperties();
                credentials.Set(MstDictKeys.USER_NAME, defaultUsername);
                credentials.Set(MstDictKeys.USER_PASSWORD, defaultPassword);
                credentials.Set(MstDictKeys.USER_EMAIL, defaultEmail);

                Mst.Events.Invoke(MstEventKeys.setSignInDefaultCredentials, credentials);
                Mst.Events.Invoke(MstEventKeys.setSignUpDefaultCredentials, credentials);
            }

            Mst.Client.Auth.RememberMe = rememberUser;

            MstTimer.WaitForSeconds(0.5f, () =>
            {
                // Listen to connection events
                Connection.AddConnectionListener(OnClientConnectedToServer);
                Connection.AddDisconnectionListener(OnClientDisconnectedFromServer, false);

                if (!Connection.IsConnected && !Connection.IsConnecting)
                {
                    Connector.StartConnection();
                }
            });
        }
Exemplo n.º 13
0
        /// <summary>
        /// This is called on the Server when a Mirror Client disconnects from the Server
        /// </summary>
        /// <param name="obj"></param>
        private void OnMirrorClientDisconnectedEvent(NetworkConnection connection)
        {
            MstTimer.WaitForSeconds(0.2f, () =>
            {
                // Try to find player in filtered list
                if (roomPlayersByMirrorPeerId.TryGetValue(connection.connectionId, out RoomPlayer player))
                {
                    logger.Debug($"Room server player {player.Username} with room client Id {connection.connectionId} left the room");

                    // Remove thisplayer from filtered list
                    roomPlayersByMirrorPeerId.Remove(player.MirrorPeer.connectionId);
                    roomPlayersByMsfPeerId.Remove(player.MasterPeerId);
                    roomPlayersByUsername.Remove(player.Username);

                    // Notify master server about disconnected player
                    if (RoomController.IsActive)
                    {
                        RoomController.NotifyPlayerLeft(player.MasterPeerId);
                    }

                    // Dispose profile
                    player.Profile?.Dispose();

                    // Inform subscribers about this bad guy
                    OnPlayerLeftRoomEvent?.Invoke(player);

                    // Calling termination conditions check
                    OnCheckTerminationConditionEvent?.Invoke();
                }
                else
                {
                    logger.Debug($"Room server client {connection.connectionId} left the room");
                }
            });
        }
Exemplo n.º 14
0
 protected virtual void OnConnectedToMasterServerEventHandler()
 {
     MstTimer.WaitForEndOfFrame(() =>
     {
         MirrorRoomClient.Instance.StartClient();
     });
 }
Exemplo n.º 15
0
        /// <summary>
        /// Loads player profile
        /// </summary>
        /// <param name="successCallback"></param>
        public void LoadPlayerProfile(string username, SuccessCallback successCallback)
        {
            if (roomPlayersByUsername.ContainsKey(username))
            {
                RoomPlayer player = roomPlayersByUsername[username];

                Mst.Server.Profiles.FillProfileValues(player.Profile, (isSuccess, error) =>
                {
                    if (!isSuccess)
                    {
                        logger.Error("Room server cannot retrieve player profile from master server");
                        successCallback?.Invoke(false, "Room server cannot retrieve player profile from master server");

                        if (disconnectIfProfileFailed)
                        {
                            MstTimer.WaitForSeconds(1f, () => player.MirrorPeer.Disconnect());
                        }

                        return;
                    }

                    logger.Debug($"Profile of player {username} is successfully loaded. Player info: {player}");
                    successCallback?.Invoke(true, string.Empty);
                });
            }
        }
Exemplo n.º 16
0
 protected virtual void Start()
 {
     if (IsAllowedToBeStartedInEditor())
     {
         // Start the server on next frame
         MstTimer.WaitForEndOfFrame(() =>
         {
             StartServer();
         });
     }
 }
Exemplo n.º 17
0
 /// <summary>
 /// This will start server in test mode
 /// </summary>
 protected virtual void StartServerInEditor()
 {
     if (startServerAsHost)
     {
         RoomNetworkManager.StopHost();
         MstTimer.WaitForSeconds(0.2f, () => RoomNetworkManager.StartHost());
     }
     else
     {
         RoomNetworkManager.StopServer();
         MstTimer.WaitForSeconds(0.2f, () => RoomNetworkManager.StartServer());
     }
 }
Exemplo n.º 18
0
        protected override void Start()
        {
            base.Start();

            // Start master server at start
            if (Mst.Args.StartMaster && !Mst.Runtime.IsEditor)
            {
                // Start the server on next frame
                MstTimer.WaitForEndOfFrame(() =>
                {
                    StartServer();
                });
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// Joins channel
        /// </summary>
        private void JoinPredefinedChannels()
        {
            canvasGroup.interactable = false;

            if (statusInfoText)
            {
                statusInfoText.text = "Finding rooms... Please wait!";
                statusInfoText.gameObject.SetActive(true);
            }

            MstTimer.WaitForSeconds(0.5f, () =>
            {
                // Join default chat
                Mst.Client.Chat.JoinChannel(defaultChannelName, (isSuccess, error) =>
                {
                    canvasGroup.interactable = true;
                    statusInfoText.gameObject.SetActive(false);

                    if (!isSuccess)
                    {
                        Mst.Events.Invoke(MstEventKeys.showOkDialogBox, new OkDialogBoxEventMessage(error, () =>
                        {
                            Hide();
                            ViewsManager.Show("UsernamePickView");
                        }));

                        return;
                    }

                    // Get my channels
                    Mst.Client.Chat.GetMyChannels((channels, error) =>
                    {
                        statusInfoText.gameObject.SetActive(false);

                        if (!string.IsNullOrEmpty(error))
                        {
                            Mst.Events.Invoke(MstEventKeys.showOkDialogBox, new OkDialogBoxEventMessage(error, () =>
                            {
                                Hide();
                                ViewsManager.Show("UsernamePickView");
                            }));

                            return;
                        }

                        DrawChannelsList(channels);
                    });
                });
            });
        }
Exemplo n.º 20
0
        /// <summary>
        /// Sends request to master server to start new room process
        /// </summary>
        /// <param name="spawnOptions"></param>
        public virtual void CreateNewRoom(string regionName, MstProperties spawnOptions)
        {
            Mst.Events.Invoke(MstEventKeys.showLoadingInfo, "Starting room... Please wait!");

            logger.Debug("Starting room... Please wait!");

            // Custom options that will be given to room directly
            var customSpawnOptions = new MstProperties();

            customSpawnOptions.Add(Mst.Args.Names.StartClientConnection);

            Mst.Client.Spawners.RequestSpawn(spawnOptions, customSpawnOptions, regionName, (controller, error) =>
            {
                if (controller == null)
                {
                    Mst.Events.Invoke(MstEventKeys.hideLoadingInfo);
                    Mst.Events.Invoke(MstEventKeys.showOkDialogBox, new OkDialogBoxEventMessage(error, null));
                    return;
                }

                Mst.Events.Invoke(MstEventKeys.showLoadingInfo, "Room started. Finalizing... Please wait!");

                // Wait for spawning status until it is finished
                // This status must be send by room
                MstTimer.WaitWhile(() =>
                {
                    return(controller.Status != SpawnStatus.Finalized);
                }, (isSuccess) =>
                {
                    Mst.Events.Invoke(MstEventKeys.hideLoadingInfo);

                    if (!isSuccess)
                    {
                        Mst.Client.Spawners.AbortSpawn(controller.SpawnTaskId);

                        logger.Error("Failed spawn new room. Time is up!");
                        Mst.Events.Invoke(MstEventKeys.showOkDialogBox, new OkDialogBoxEventMessage("Failed spawn new room. Time is up!", null));

                        OnRoomStartAbortedEvent?.Invoke();

                        return;
                    }

                    OnRoomStartedEvent?.Invoke();

                    logger.Info("You have successfully spawned new room");
                }, matchCreationTimeout);
            });
        }
Exemplo n.º 21
0
        public void UpdateProfile()
        {
            Mst.Events.Invoke(MstEventKeys.showLoadingInfo, "Saving profile data... Please wait!");

            MstTimer.WaitForSeconds(1f, () =>
            {
                var data = new Dictionary<string, string>
                {
                    { "displayName", profileSettingsView.DisplayName },
                    { "avatarUrl", profileSettingsView.AvatarUrl }
                };

                Connection.SendMessage((short)MstMessageCodes.UpdateDisplayNameRequest, data.ToBytes(), OnSaveProfileResponseCallback);
            });
        }
Exemplo n.º 22
0
        private void Rpc_NotifyDied()
        {
            if (isLocalPlayer)
            {
                characterController.enabled = false;

                IsAlive = false;
                OnDieEvent?.Invoke();
            }

            if (dieEffectPrefab)
            {
                MstTimer.WaitForSeconds(1f, () => {
                    Instantiate(dieEffectPrefab, transform.position, Quaternion.identity);
                });
            }
        }
Exemplo n.º 23
0
        protected override void OnInitialize()
        {
            // Listen to disconnection from master
            masterConnection.AddDisconnectionListener(OnDisconnectedFromMasterEvent, false);

            MstTimer.WaitForSeconds(1f, () =>
            {
                if (IsInTestMode())
                {
                    StartRoomClient(true);
                }

                if (Mst.Options.Has(MstDictKeys.AUTOSTART_ROOM_CLIENT) || Mst.Args.StartClientConnection)
                {
                    StartRoomClient();
                }
            });
        }
Exemplo n.º 24
0
        /// <summary>
        /// Get profile data from master
        /// </summary>
        public virtual void LoadProfile()
        {
            Mst.Events.Invoke(MstEventKeys.showLoadingInfo, "Loading profile... Please wait!");

            MstTimer.WaitForSeconds(0.2f, () =>
            {
                Mst.Client.Profiles.GetProfileValues(Profile, (isSuccessful, error) =>
                {
                    Mst.Events.Invoke(MstEventKeys.hideLoadingInfo);

                    if (isSuccessful)
                    {
                        OnProfileLoadedEvent?.Invoke();
                    }
                    else
                    {
                        logger.Error("Could not load user profile");
                        OnProfileLoadFailedEvent?.Invoke();
                    }
                });
            });
        }
Exemplo n.º 25
0
        private static void SendRequestSpawn(CommandArg[] args)
        {
            var options = new MstProperties();

            options.Add(MstDictKeys.roomName, args[0].String.Replace('+', ' '));

            if (args.Length > 1)
            {
                options.Add(MstDictKeys.roomMaxPlayers, args[1].String);
            }

            var customOptions = new MstProperties();

            customOptions.Add("-myName", "\"John Adams\"");
            customOptions.Add("-myAge", 45);
            customOptions.Add("-msfStartClientConnection", string.Empty);

            Mst.Client.Spawners.RequestSpawn(options, customOptions, string.Empty, (controller, error) =>
            {
                if (controller == null)
                {
                    return;
                }

                MstTimer.WaitWhile(() =>
                {
                    return(controller.Status != SpawnStatus.Finalized);
                }, (isSuccess) =>
                {
                    if (!isSuccess)
                    {
                        Mst.Client.Spawners.AbortSpawn(controller.SpawnTaskId);
                        Logs.Error("You have failed to spawn new room");
                    }

                    Logs.Info("You have successfully spawned new room");
                }, 60f);
            });
        }
Exemplo n.º 26
0
        /// <summary>
        ///
        /// </summary>
        protected virtual void OnClientConnectedToServer()
        {
            Mst.Events.Invoke(MstEventKeys.hideLoadingInfo);

            if (Mst.Client.Auth.IsSignedIn)
            {
                OnSignedInEvent?.Invoke();
            }
            else
            {
                if (Mst.Client.Auth.HasAuthToken())
                {
                    MstTimer.WaitForSeconds(0.2f, () =>
                    {
                        SignInWithToken();
                    });
                }
                else
                {
                    Mst.Events.Invoke(MstEventKeys.showSignInView);
                }
            }
        }
Exemplo n.º 27
0
        /// <summary>
        /// Default spawn spawned process request handler that will be used by controller if <see cref="spawnRequestHandler"/> is not overriden
        /// </summary>
        /// <param name="data"></param>
        /// <param name="message"></param>
        public virtual void SpawnRequestHandler(SpawnRequestPacket data, IIncomingMessage message)
        {
            Logger.Debug($"Default spawn handler started handling a request to spawn process for spawn controller [{SpawnerId}]");

            /************************************************************************/
            // Create process args string
            var processArguments = new MstProperties();

            /************************************************************************/
            // Check if we're overriding an IP to master server
            var masterIpArgument = string.IsNullOrEmpty(SpawnSettings.MasterIp) ?
                                   Connection.ConnectionIp : SpawnSettings.MasterIp;

            // Create master IP arg
            processArguments.Set(Mst.Args.Names.MasterIp, masterIpArgument);

            /************************************************************************/
            /// Check if we're overriding a port to master server
            var masterPortArgument = SpawnSettings.MasterPort < 0 ? Connection.ConnectionPort : SpawnSettings.MasterPort;

            // Create master port arg
            processArguments.Set(Mst.Args.Names.MasterPort, masterPortArgument);

            /************************************************************************/
            // Machine Ip
            processArguments.Set(Mst.Args.Names.RoomIp, SpawnSettings.MachineIp);

            /************************************************************************/
            // Create port for room arg
            int machinePortArgument = Mst.Server.Spawners.GetAvailablePort();

            processArguments.Set(Mst.Args.Names.RoomPort, machinePortArgument);

            /************************************************************************/
            // Room Name
            //processArguments.Set(Mst.Args.Names.RoomName, $"\"{data.Options.AsString(MstDictKeys.roomName, "Room_" + Mst.Helper.CreateRandomString(6))}\"");

            /************************************************************************/
            // Room Region
            //processArguments.Set(Mst.Args.Names.RoomRegion, $"\"{SpawnSettings.Region}\"");

            /************************************************************************/
            // Room Max Connections
            //if (data.Options.Has(MstDictKeys.maxPlayers))
            //{
            //    processArguments.Set(Mst.Args.Names.RoomMaxConnections, data.Options.AsString(MstDictKeys.maxPlayers));
            //}

            /************************************************************************/
            // Get the scene name
            //if (data.Options.Has(MstDictKeys.sceneName))
            //{
            //    processArguments.Set(Mst.Args.Names.LoadScene, data.Options.AsString(MstDictKeys.sceneName));
            //}

            /************************************************************************/
            // Create use websockets arg
            //if (SpawnSettings.UseWebSockets)
            //{
            //    processArguments.Set(Mst.Args.Names.UseWebSockets, string.Empty);
            //}

            /************************************************************************/
            // Create spawn id arg
            processArguments.Set(Mst.Args.Names.SpawnTaskId, data.SpawnTaskId);

            /************************************************************************/
            // Create spawn code arg
            processArguments.Set(Mst.Args.Names.SpawnTaskUniqueCode, data.SpawnTaskUniqueCode);

            /************************************************************************/
            // Create custom args
            processArguments.Append(data.CustomOptions);

            /************************************************************************/
            // Path to executable
            var executablePath = SpawnSettings.ExecutablePath;

            if (string.IsNullOrEmpty(executablePath))
            {
                executablePath = File.Exists(Environment.GetCommandLineArgs()[0])
                    ? Environment.GetCommandLineArgs()[0]
                    : Process.GetCurrentProcess().MainModule.FileName;
            }

            // In case a path is provided with the request
            if (data.Options.Has(MstDictKeys.ROOM_EXE_PATH))
            {
                executablePath = data.Options.AsString(MstDictKeys.ROOM_EXE_PATH);
            }

            if (!string.IsNullOrEmpty(data.OverrideExePath))
            {
                executablePath = data.OverrideExePath;
            }

            /// Create info about starting process
            var startProcessInfo = new ProcessStartInfo(executablePath)
            {
                CreateNoWindow  = false,
                UseShellExecute = true,
                Arguments       = processArguments.ToReadableString(" ", " ")
            };

            Logger.Debug("Starting process with args: " + startProcessInfo.Arguments);

            var processStarted = false;

            try
            {
                new Thread(() =>
                {
                    try
                    {
                        using (var process = Process.Start(startProcessInfo))
                        {
                            Logger.Debug("Process started. Spawn Id: " + data.SpawnTaskId + ", pid: " + process.Id);
                            processStarted = true;

                            lock (processLock)
                            {
                                // Save the process
                                processes[data.SpawnTaskId] = process;
                            }

                            var processId = process.Id;

                            // Notify server that we've successfully handled the request
                            MstTimer.RunInMainThread(() =>
                            {
                                message.Respond(ResponseStatus.Success);
                                NotifyProcessStarted(data.SpawnTaskId, processId, startProcessInfo.Arguments);
                            });

                            process.WaitForExit();
                        }
                    }
                    catch (Exception e)
                    {
                        if (!processStarted)
                        {
                            MstTimer.RunInMainThread(() => { message.Respond(ResponseStatus.Failed); });
                        }

                        Logger.Error("An exception was thrown while starting a process. Make sure that you have set a correct build path. " +
                                     $"We've tried to start a process at [{executablePath}]. You can change it at 'SpawnerBehaviour' component");
                        Logger.Error(e);
                    }
                    finally
                    {
                        lock (processLock)
                        {
                            // Remove the process
                            processes.Remove(data.SpawnTaskId);
                        }

                        MstTimer.RunInMainThread(() =>
                        {
                            // Release the port number
                            Mst.Server.Spawners.ReleasePort(machinePortArgument);
                            Logger.Debug($"Notifying about killed process with spawn id [{data.SpawnTaskId}]");
                            NotifyProcessKilled(data.SpawnTaskId);
                        });
                    }
                }).Start();
            }
            catch (Exception e)
            {
                message.Respond(e.Message, ResponseStatus.Error);
                Logs.Error(e);
            }
        }
Exemplo n.º 28
0
        private void ProvideRoomAccessCheckHandler(IIncomingMessage message)
        {
            var provideRoomAccessCheckPacket = message.Deserialize(new ProvideRoomAccessCheckPacket());
            var roomController = Mst.Server.Rooms.GetRoomController(provideRoomAccessCheckPacket.RoomId);

            if (roomController == null)
            {
                message.Respond($"There's no room controller with room id {provideRoomAccessCheckPacket.RoomId}", ResponseStatus.NotHandled);
                return;
            }

            var isProviderDone = false;

            var requester = new UsernameAndPeerIdPacket()
            {
                PeerId   = provideRoomAccessCheckPacket.PeerId,
                Username = provideRoomAccessCheckPacket.Username
            };

            // Create access provider check options
            var roomAccessProviderCheck = new RoomAccessProviderCheck()
            {
                PeerId        = provideRoomAccessCheckPacket.PeerId,
                Username      = provideRoomAccessCheckPacket.Username,
                CustomOptions = provideRoomAccessCheckPacket.CustomOptions
            };

            // Invoke the access provider
            roomController.AccessProvider.Invoke(roomAccessProviderCheck, (access, error) =>
            {
                // In case provider timed out
                if (isProviderDone)
                {
                    return;
                }

                isProviderDone = true;

                if (access == null)
                {
                    // If access is not provided
                    message.Respond(string.IsNullOrEmpty(error) ? "" : error, ResponseStatus.Failed);
                    return;
                }

                message.Respond(access, ResponseStatus.Success);

                if (Logger.IsLogging(LogLevel.Trace))
                {
                    Logger.Trace("Room controller gave address to peer " + provideRoomAccessCheckPacket.PeerId + ":" + access);
                }
            });

            // Timeout the access provider
            MstTimer.WaitForSeconds(Mst.Server.Rooms.AccessProviderTimeout, () =>
            {
                if (!isProviderDone)
                {
                    isProviderDone = true;
                    message.Respond("Timed out", ResponseStatus.Timeout);
                    Logger.Error($"Access provider took longer than {Mst.Server.Rooms.AccessProviderTimeout} seconds to provide access. " +
                                 "If it's intended, increase the threshold at Msf.Server.Rooms.AccessProviderTimeout");
                }
            });
        }
Exemplo n.º 29
0
        /// <summary>
        /// Fires when client that wants to connect to this room made request to validate the access token
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="msg"></param>
        protected virtual void ValidateRoomAccessRequestHandler(NetworkConnection conn, ValidateRoomAccessRequestMessage msg)
        {
            logger.Debug($"Room client {conn.connectionId} asked to validate access token [{msg.Token}]");

            // Triying to validate given token
            Mst.Server.Rooms.ValidateAccess(RoomController.RoomId, msg.Token, (usernameAndPeerId, error) =>
            {
                // If token is not valid
                if (usernameAndPeerId == null)
                {
                    logger.Error(error);

                    conn.Send(new ValidateRoomAccessResultMessage()
                    {
                        Error  = error,
                        Status = ResponseStatus.Failed
                    });

                    MstTimer.WaitForSeconds(1f, () => conn.Disconnect());

                    return;
                }

                logger.Debug($"Client {conn.connectionId} is successfully validated");
                logger.Debug("Getting his account info...");

                Mst.Server.Auth.GetPeerAccountInfo(usernameAndPeerId.PeerId, (accountInfo, accountError) =>
                {
                    if (accountInfo == null)
                    {
                        logger.Error(accountError);

                        conn.Send(new ValidateRoomAccessResultMessage()
                        {
                            Error  = accountError,
                            Status = ResponseStatus.Error
                        });

                        MstTimer.WaitForSeconds(1f, () => conn.Disconnect());

                        return;
                    }

                    // If we do not want guest users to play in our room
                    if (!allowGuestUsers && accountInfo.Properties.Has(MstDictKeys.USER_IS_GUEST) && accountInfo.Properties.AsBool(MstDictKeys.USER_IS_GUEST))
                    {
                        logger.Error("Guest users cannot play this room. Hands off...");

                        conn.Send(new ValidateRoomAccessResultMessage()
                        {
                            Error  = "Guest users cannot play this room. Hands off...",
                            Status = ResponseStatus.Error
                        });

                        MstTimer.WaitForSeconds(1f, () => conn.Disconnect());

                        return;
                    }

                    // Create new room player
                    var player = new RoomPlayer(usernameAndPeerId.PeerId, conn, accountInfo.UserId, accountInfo.Username, accountInfo.Properties)
                    {
                        Profile = ProfileFactory(accountInfo.UserId)
                    };

                    // Add this player to filtered lists
                    roomPlayersByMsfPeerId.Add(usernameAndPeerId.PeerId, player);
                    roomPlayersByMirrorPeerId.Add(conn.connectionId, player);
                    roomPlayersByUsername.Add(accountInfo.Username, player);

                    // If server is required user profile
                    if (autoLoadUserProfile)
                    {
                        LoadPlayerProfile(accountInfo.Username, (isLoadProfileSuccess, loadProfileError) =>
                        {
                            if (isLoadProfileSuccess)
                            {
                                FinalizePlayerJoining(conn);
                            }
                        });
                    }
                    else
                    {
                        FinalizePlayerJoining(conn);
                    }
                });
            });
        }
Exemplo n.º 30
0
 public void RunInMainThread(Action action)
 {
     MstTimer.RunInMainThread(action);
 }