Example #1
0
        /// <summary>
        /// Retrieves a list of public games, which pass a provided filter.
        /// (You can implement your own filtering by extending modules or "classes"
        /// that implement <see cref="IGamesProvider"/>)
        /// </summary>
        public void FindGames(MstProperties filter, FindGamesCallback callback, IClientSocket connection)
        {
            if (!connection.IsConnected)
            {
                Games = new List <GameInfoPacket>();
                Logs.Error("Not connected");
                callback.Invoke(Games);
                return;
            }

            connection.SendMessage((short)MstMessageCodes.FindGamesRequest, filter.ToBytes(), (status, response) =>
            {
                if (status != ResponseStatus.Success)
                {
                    Games = new List <GameInfoPacket>();
                    Logs.Error(response.AsString("Unknown error while requesting a list of games"));
                    callback.Invoke(Games);
                    return;
                }

                Games = response.DeserializeList(() => new GameInfoPacket()).ToList();
                callback.Invoke(Games);
            });
        }
 public UpdateAccountPropertiesPacket()
 {
     UserId     = string.Empty;
     Properties = new MstProperties();
 }
Example #3
0
 /// <summary>
 /// Sends a request to create a lobby, using a specified factory
 /// </summary>
 public void CreateLobby(string factory, MstProperties options, CreateLobbyCallback calback)
 {
     CreateLobby(factory, options, calback, Connection);
 }
Example #4
0
 /// <summary>
 /// Set's lobby user properties (current player sets his own properties,
 ///  which can be accessed by game server and etc.)
 /// </summary>
 /// <param name="properties"></param>
 /// <param name="callback"></param>
 public void SetMyProperties(MstProperties properties, SuccessCallback callback)
 {
     SetMyProperties(properties, callback, Connection);
 }
Example #5
0
        /// <summary>
        /// Sends a generic login request
        /// </summary>
        public void SignIn(MstProperties data, SignInCallback callback, IClientSocket connection)
        {
            Logs.Debug("Signing in...");

            if (!connection.IsConnected)
            {
                callback.Invoke(null, "Not connected to server");
                return;
            }

            IsNowSigningIn = true;

            // We first need to get an aes key
            // so that we can encrypt our login data
            Mst.Security.GetAesKey(aesKey =>
            {
                if (aesKey == null)
                {
                    IsNowSigningIn = false;
                    callback.Invoke(null, "Failed to log in due to security issues");
                    return;
                }

                var encryptedData = Mst.Security.EncryptAES(data.ToBytes(), aesKey);

                connection.SendMessage((short)MstMessageCodes.SignIn, encryptedData, (status, response) =>
                {
                    IsNowSigningIn = false;

                    if (status != ResponseStatus.Success)
                    {
                        ClearAuthToken();

                        callback.Invoke(null, response.AsString("Unknown error"));
                        return;
                    }

                    // Parse account info
                    var accountInfoPacket = response.Deserialize(new AccountInfoPacket());

                    AccountInfo = new ClientAccountInfo()
                    {
                        Id               = accountInfoPacket.Id,
                        Username         = accountInfoPacket.Username,
                        Email            = accountInfoPacket.Email,
                        PhoneNumber      = accountInfoPacket.PhoneNumber,
                        Facebook         = accountInfoPacket.Facebook,
                        Token            = accountInfoPacket.Token,
                        IsAdmin          = accountInfoPacket.IsAdmin,
                        IsGuest          = accountInfoPacket.IsGuest,
                        IsEmailConfirmed = accountInfoPacket.IsEmailConfirmed,
                        Properties       = accountInfoPacket.Properties,
                    };

                    // If RememberMe is checked on and we are not guset, then save auth token
                    if (RememberMe && !AccountInfo.IsGuest && !string.IsNullOrEmpty(AccountInfo.Token))
                    {
                        SaveAuthToken(AccountInfo.Token);
                    }
                    else
                    {
                        ClearAuthToken();
                    }

                    IsSignedIn = true;

                    callback.Invoke(AccountInfo, null);

                    OnSignedInEvent?.Invoke();
                });
            }, connection);
        }
Example #6
0
 /// <summary>
 /// Sends a registration request to server
 /// </summary>
 /// <param name="data"></param>
 /// <param name="callback"></param>
 public void SignUp(MstProperties data, SuccessCallback callback)
 {
     SignUp(data, callback, Connection);
 }
Example #7
0
 /// <summary>
 /// Tries to get an access to a room with a given room id, password,
 /// and some other <paramref name="customOptions"/>, which will be visible to the room (game server)
 /// </summary>
 /// <param name="roomId"></param>
 /// <param name="callback"></param>
 /// <param name="password"></param>
 /// <param name="customOptions"></param>
 public void GetAccess(int roomId, string password, MstProperties customOptions, RoomAccessCallback callback)
 {
     GetAccess(roomId, password, customOptions, callback, Connection);
 }
Example #8
0
        /// <summary>
        /// Sends a request to room, to retrieve an access to it for a specified peer, 
        /// with some extra options
        /// </summary>
        /// <param name="peer"></param>
        /// <param name="customOptions"></param>
        /// <param name="callback"></param>
        public void GetAccess(IPeer peer, MstProperties customOptions, GetAccessCallback callback)
        {
            // If request is already pending
            if (requestsInProgress.Contains(peer.Id))
            {
                callback.Invoke(null, "You've already requested an access to this room");
                return;
            }

            // If player is already in the game
            if (connectedPlayers.ContainsKey(peer.Id))
            {
                callback.Invoke(null, "You are already in this room");
                return;
            }

            // If player has already received an access and didn't claim it
            // but is requesting again - send him the old one
            var currentAccess = unconfirmedAccesses.Values.FirstOrDefault(v => v.Peer == peer);
            if (currentAccess != null)
            {
                // Restore the timeout
                currentAccess.Timeout = DateTime.Now.AddSeconds(Options.AccessTimeoutPeriod);

                callback.Invoke(currentAccess.Access, null);
                return;
            }

            // If there's a player limit
            if (Options.MaxConnections != 0)
            {
                var playerSlotsTaken = requestsInProgress.Count + accessesInUse.Count + unconfirmedAccesses.Count;

                if (playerSlotsTaken >= Options.MaxConnections)
                {
                    callback.Invoke(null, "Room is already full");
                    return;
                }
            }

            // Create packet to request checking of access
            var provideRoomAccessCheckPacket = new ProvideRoomAccessCheckPacket()
            {
                PeerId = peer.Id,
                RoomId = RoomId,
                CustomOptions = customOptions
            };

            // Try to find out if requester is logged in add the username if available
            // Simetimes we want to check if user is banned
            var userPeerExtension = peer.GetExtension<IUserPeerExtension>();
            if (userPeerExtension != null && !string.IsNullOrEmpty(userPeerExtension.Username))
            {
                provideRoomAccessCheckPacket.Username = userPeerExtension.Username;
            }

            // Add requester peer id to pending list to prevent new access request to this room
            requestsInProgress.Add(peer.Id);

            // Send request to owner of the room to get access token
            Peer.SendMessage((short)MstMessageCodes.ProvideRoomAccessCheck, provideRoomAccessCheckPacket, (status, response) =>
            {
                // Remove requester peer id from pending list
                requestsInProgress.Remove(peer.Id);

                if (status != ResponseStatus.Success)
                {
                    callback.Invoke(null, response.AsString("Unknown Error"));
                    return;
                }

                // Parse access data from message
                var accessData = response.Deserialize(new RoomAccessPacket());

                // Create new access info
                var access = new RoomAccessData()
                {
                    Access = accessData,
                    Peer = peer,
                    Timeout = DateTime.Now.AddSeconds(Options.AccessTimeoutPeriod)
                };

                // Save the access info to list and wait for confirmation
                unconfirmedAccesses[access.Access.Token] = access;

                callback.Invoke(access.Access, null);
            });
        }
Example #9
0
 /// <summary>
 /// Returns list of properties of public room
 /// </summary>
 /// <param name="player"></param>
 /// <param name="room"></param>
 /// <param name="playerFilters"></param>
 /// <returns></returns>
 public virtual MstProperties GetPublicRoomOptions(IPeer player, RegisteredRoom room, MstProperties playerFilters)
 {
     return(room.Options.CustomOptions);
 }
Example #10
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);
            }
        }
Example #11
0
 public override void FromBinaryReader(EndianBinaryReader reader)
 {
     RoomId        = reader.ReadInt32();
     Password      = reader.ReadString();
     CustomOptions = new MstProperties(reader.ReadDictionary());
 }
Example #12
0
 public override void FromBinaryReader(EndianBinaryReader reader)
 {
     Options       = new MstProperties(reader.ReadDictionary());
     CustomOptions = new MstProperties(reader.ReadDictionary());
 }
Example #13
0
 public ClientsSpawnRequestPacket()
 {
     Options       = new MstProperties();
     CustomOptions = new MstProperties();
 }
Example #14
0
 public override void FromBinaryReader(EndianBinaryReader reader)
 {
     UserId     = reader.ReadString();
     Properties = new MstProperties(reader.ReadDictionary());
 }
Example #15
0
 public UpdateAccountPropertiesPacket(string userId, MstProperties properties)
 {
     UserId     = userId ?? throw new ArgumentNullException(nameof(userId));
     Properties = properties ?? throw new ArgumentNullException(nameof(properties));
 }
Example #16
0
 /// <summary>
 /// Adds new or updates existing options
 /// </summary>
 /// <param name="options"></param>
 /// <returns></returns>
 public bool AddOrUpdate(MstProperties options)
 {
     return(AddOrUpdate(options.ToDictionary()));
 }
Example #17
0
 /// <summary>
 /// Append options to this list
 /// </summary>
 /// <param name="options"></param>
 public MstProperties Append(MstProperties options)
 {
     return(Append(options.ToDictionary()));
 }
Example #18
0
        public SpawnTask(int spawnTaskId, RegisteredSpawner spawner, MstProperties options, MstProperties customOptions)
        {
            Id = spawnTaskId;

            Spawner       = spawner;
            Options       = options;
            CustomOptions = customOptions;

            UniqueCode        = Mst.Helper.CreateRandomAlphanumericString(6);
            whenDoneCallbacks = new List <Action <SpawnTask> >();
        }
Example #19
0
 /// <summary>
 /// Tries to get an access to a room with a given room id
 /// and some other <paramref name="customOptions"/>, which will be visible to the room (game server)
 /// </summary>
 /// <param name="roomId"></param>
 /// <param name="callback"></param>
 /// <param name="customOptions"></param>
 public void GetAccess(int roomId, RoomAccessCallback callback, MstProperties customOptions)
 {
     GetAccess(roomId, "", customOptions, callback, Connection);
 }
Example #20
0
 public LobbyMember(string username, LobbyUserPeerExtension ext)
 {
     Username   = username;
     Extension  = ext;
     Properties = new MstProperties();
 }
Example #21
0
 /// <summary>
 /// Set's current player's properties
 /// </summary>
 public void SetMyProperties(MstProperties properties, SuccessCallback callback)
 {
     Mst.Client.Lobbies.SetMyProperties(properties, callback, _connection);
 }
Example #22
0
 /// <summary>
 /// Sends a request to master server, to spawn a process with given options
 /// </summary>
 /// <param name="options"></param>
 public void RequestSpawn(MstProperties options)
 {
     RequestSpawn(options, new MstProperties(), string.Empty, null, Connection);
 }
Example #23
0
 /// <summary>
 /// Sends a generic login request
 /// </summary>
 /// <param name="data"></param>
 /// <param name="callback"></param>
 public void SignIn(MstProperties data, SignInCallback callback)
 {
     SignIn(data, callback, Connection);
 }
Example #24
0
 /// <summary>
 /// Sends a request to master server, to spawn a process in a given region, and with given options
 /// </summary>
 /// <param name="options"></param>
 public void RequestSpawn(MstProperties options, string region)
 {
     RequestSpawn(options, new MstProperties(), region, null, Connection);
 }
Example #25
0
 /// <summary>
 /// Sets lobby properties of a specified lobby id
 /// </summary>
 /// <param name="lobbyId"></param>
 /// <param name="properties"></param>
 /// <param name="callback"></param>
 public void SetLobbyProperties(int lobbyId, MstProperties properties, SuccessCallback callback)
 {
     SetLobbyProperties(lobbyId, properties, callback, Connection);
 }
Example #26
0
 /// <summary>
 /// Sends a request to master server, to spawn a process in a given region, and with given options
 /// </summary>
 /// <param name="options"></param>
 /// <param name="region"></param>
 /// <param name="callback"></param>
 public void RequestSpawn(MstProperties options, MstProperties customOptions, string region, SpawnRequestResultHandler callback)
 {
     RequestSpawn(options, customOptions, region, callback, Connection);
 }
Example #27
0
 /// <summary>
 /// Set's lobby user properties (current player sets his own properties,
 ///  which can be accessed by game server and etc.)
 /// </summary>
 public void SetMyProperties(MstProperties properties, SuccessCallback callback, IClientSocket connection)
 {
     connection.SendMessage((short)MstMessageCodes.SetMyLobbyProperties, properties.ToBytes(), Mst.Create.SuccessCallback(callback));
 }
Example #28
0
 public virtual MstProperties GetPublicLobbyProperties(IPeer peer, ILobby lobby, MstProperties playerFilters)
 {
     return(lobby.GetPublicProperties(peer));
 }
Example #29
0
 public SpawnerOptions()
 {
     CustomOptions = new MstProperties();
 }
Example #30
0
 /// <summary>
 /// Instance of room server peer extension
 /// </summary>
 /// <param name="masterPeerId"></param>
 /// <param name="username"></param>
 /// <param name="roomPeer"></param>
 /// <param name="customOptions"></param>
 public RoomPlayerPeerExtension(int masterPeerId, string username, IPeer roomPeer, MstProperties customOptions)
 {
     MasterPeerId = masterPeerId;
     Username     = username ?? throw new ArgumentNullException(nameof(username));
     Peer         = roomPeer ?? throw new ArgumentNullException(nameof(roomPeer));
     Properties   = customOptions ?? throw new ArgumentNullException(nameof(customOptions));
 }