Exemplo n.º 1
0
        protected void S2X_ReqOtherServerInfo(IIncommingMessage reader)
        {
            var msg = reader.Parse <Msg_ReqOtherServerInfo>();

            _netClientYX.SendMessage(EMsgYX.X2Y_ReqOtherServerInfo, msg,
                                     (status, respond) => {
                if (status == EResponseStatus.Failed)
                {
                    reader.Respond(0, EResponseStatus.Failed);
                }
                else
                {
                    var rMsg = respond.Parse <Msg_RepOtherServerInfo>();
                    reader.Respond(rMsg);
                }
            });
        }
Exemplo n.º 2
0
        protected void X2Y_ReqOtherServerInfo(IIncommingMessage reader)
        {
            var msg = reader.Parse <Msg_ReqOtherServerInfo>();

            if (_type2MasterPeer.TryGetValue((EServerType)msg.ServerType, out var peer))
            {
                peer.SendMessage((short)EMsgYM.Y2M_ReqOtherServerInfo, msg,
                                 (status, respond) => {
                    var rMsg = respond.Parse <Msg_RepOtherServerInfo>();
                    reader.Respond(rMsg);
                });
            }
            else
            {
                reader.Respond(0, EResponseStatus.Failed);
            }
        }
Exemplo n.º 3
0
        protected virtual void OnMessageReceived(IIncommingMessage message)
        {
            try
            {
                IPacketHandler handler;
                Handlers.TryGetValue(message.OpCode, out handler);

                if (handler == null)
                {
                    Logger.Warn(string.Format("Handler for OpCode {0} does not exist", message.OpCode));

                    if (message.IsExpectingResponse)
                    {
                        message.Respond(InternalServerErrorMessage, ResponseStatus.NotHandled);
                        return;
                    }
                    return;
                }

                handler.Handle(message);
            }
            catch (Exception e)
            {
                if (Msf.Runtime.IsEditor)
                {
                    throw;
                }

                Logger.Error("Error while handling a message from Client. OpCode: " + message.OpCode);
                Logger.Error(e);

                if (!message.IsExpectingResponse)
                {
                    return;
                }

                try
                {
                    message.Respond(InternalServerErrorMessage, ResponseStatus.Error);
                }
                catch (Exception exception)
                {
                    Logs.Error(exception);
                }
            }
        }
Exemplo n.º 4
0
        private void HandleRegisterSpawner(IIncommingMessage message)
        {
            if (!HasCreationPermissions(message.Peer))
            {
                message.Respond("Insufficient permissions", ResponseStatus.Unauthorized);
                return;
            }

            var options = message.Deserialize <SpawnerOptions>();

            var spawner = CreateSpawner(message.Peer, options);

            _logger.Info($"New Spawner registered. ID: {spawner.SpawnerId}. Region: {options.Region}");

            // Respond with spawner id
            message.Respond(spawner.SpawnerId, ResponseStatus.Success);
        }
Exemplo n.º 5
0
        protected void S2D_SaveGameData(IIncommingMessage reader)
        {
            var msg = reader.Parse <Msg_S2D_SaveGameData>();

            _authDb.UpdateGameData(msg.data, () => {
                reader.Respond(1, EResponseStatus.Success);
            });
        }
Exemplo n.º 6
0
        private void HandleValidateRoomAccess(IIncommingMessage message)
        {
            var data = message.Deserialize(new RoomAccessValidatePacket());

            RegisteredRoom room;

            Rooms.TryGetValue(data.RoomId, out room);

            if (room == null)
            {
                message.Respond("Room does not exist", ResponseStatus.Failed);
                return;
            }

            if (message.Peer != room.Peer)
            {
                // Wrong peer unregistering the room
                message.Respond("You're not the creator of the room", ResponseStatus.Unauthorized);
                return;
            }

            IPeer playerPeer;

            if (!room.ValidateAccess(data.Token, out playerPeer))
            {
                message.Respond("Failed to confirm the access", ResponseStatus.Unauthorized);
                return;
            }

            var packet = new UsernameAndPeerIdPacket()
            {
                PeerId = playerPeer.Id
            };

            // Add username if available
            var userExt = playerPeer.GetExtension <IUserExtension>();

            if (userExt != null)
            {
                packet.Username = userExt.Username ?? "";
            }

            // Respond with success and player's peer id
            message.Respond(packet, ResponseStatus.Success);
        }
Exemplo n.º 7
0
        protected virtual void HandleSendChatMessage(IIncommingMessage message)
        {
            var chatUser = message.Peer.GetExtension <ChatUserExtension>();

            if (chatUser == null)
            {
                message.Respond("Chat cannot identify you", ResponseStatus.Unauthorized);
                return;
            }

            var packet = message.Deserialize(new ChatMessagePacket());

            if (!OnChatMessageReceived(packet, chatUser, message))
            {
                // If message was not handled
                message.Respond("Invalid message", ResponseStatus.NotHandled);
            }
        }
        protected virtual void RegisterSpawnerRequestHandler(IIncommingMessage message)
        {
            logger.Debug($"Client [{message.Peer.Id}] requested to be registered as spawner");

            if (!HasCreationPermissions(message.Peer))
            {
                message.Respond("Insufficient permissions", ResponseStatus.Unauthorized);
                return;
            }

            var options = message.Deserialize(new SpawnerOptions());
            var spawner = CreateSpawner(message.Peer, options);

            logger.Debug($"Client [{message.Peer.Id}] was successfully registered as spawner [{spawner.SpawnerId}] with options: {options}");

            // Respond with spawner id
            message.Respond(spawner.SpawnerId, ResponseStatus.Success);
        }
 private bool isValidAdmin(IIncommingMessage rawMsg)
 {
     if (SpectatorAuthModule.existsAdmin(rawMsg.Peer) == false)
     {
         rawMsg.Respond("Peer ID is not a registred Admin", ResponseStatus.Error);
         return(false);
     }
     return(true);
 }
 private void handleRequestSpectatorGames(IIncommingMessage msg)
 {
     if (SpectatorAuthModule.existsAdmin(msg.Peer) == false)
     {
         return;
     }
     byte[] bytes = allGamesList.Select(g => g.convertToGameInfoPacket()).Select(l => (ISerializablePacket)l).ToBytes();
     msg.Respond(bytes, ResponseStatus.Success);
 }
 private void handleStopSpectateGame(IIncommingMessage rawMsg)
 {
     if (isValidAdmin(rawMsg) == false)
     {
         return;
     }
     removeSpectator(adminToRooms[rawMsg.Peer.Id], rawMsg.Peer);
     rawMsg.Respond(ResponseStatus.Success);
 }
Exemplo n.º 12
0
        protected void L2G_UserReconnect(IIncommingMessage reader)
        {
            var msg = reader.Parse <Msg_L2G_UserReconnect>();

            Log("L2G_UserReconnect " + msg);
            if (_uid2Players.TryGetValue(msg.PlayerInfo.UserId, out var player))
            {
                var game = player.Game;
                if (game != null)
                {
                    game.OnPlayerReconnect(msg.PlayerInfo);
                    reader.Respond(1, EResponseStatus.Success);
                    return;
                }
            }

            reader.Respond(0, EResponseStatus.Failed);
        }
Exemplo n.º 13
0
    /// <summary>
    /// Handles a request from game server to teleport
    /// user to another game server / zone.
    /// </summary>
    /// <param name="message"></param>
    public virtual void HandleTeleportRequest(IIncommingMessage message)
    {
        var request = message.Deserialize(new TeleportRequestPacket());

        var user = AuthModule.GetLoggedInUser(request.Username);
        var peer = user.Peer;

        // Find the room which represents the zone we need
        var room = RoomsModule.GetAllRooms()
                   .Where(s => s.Options.Properties.ContainsKey(ZoneNameKey))
                   .FirstOrDefault(s => s.Options.Properties[ZoneNameKey] == request.ZoneName);

        if (room == null)
        {
            // If no room with that zone name was found
            message.Respond("Zone was not found", ResponseStatus.Failed);
            return;
        }

        var accessRequestProperties = new Dictionary <string, string>()
        {
            // Add the new position to the request
            // So that new server knows where exactly to position the player
            { ZonePosition, request.Position }
        };

        // Request an access to room
        room.GetAccess(peer, accessRequestProperties, (access, error) =>
        {
            if (access == null)
            {
                // We didn't get the access
                message.Respond("Failed to get access to the zone: " + error, ResponseStatus.Failed);
                return;
            }

            // We have the access to new zone, let's store it
            // so player can request it when he's on the loading screen
            peer.SetProperty(WorldDemoPropCodes.ZoneAccess, access);

            // Notify game server that access was received
            message.Respond(ResponseStatus.Success);
        });
    }
Exemplo n.º 14
0
        /// <summary>
        ///     Handles e-mail confirmation request
        /// </summary>
        /// <param name="message"></param>
        private void HandleEmailConfirmation(IIncommingMessage message)
        {
            var code = message.AsString();

            var extension = message.Peer.GetExtension <UserExtension>();

            if (extension?.AccountData == null)
            {
                message.Respond("Invalid session", ResponseStatus.Unauthorized);
                return;
            }

            if (extension.AccountData.IsGuest)
            {
                message.Respond("Guests cannot confirm e-mails", ResponseStatus.Unauthorized);
                return;
            }

            if (extension.AccountData.IsEmailConfirmed)
            {
                // We still need to respond with "success" in case
                // response is handled somehow on the client
                message.Respond("Your email is already confirmed",
                                ResponseStatus.Success);
                return;
            }

            var requiredCode = _database.GetEmailConfirmationCode(extension.AccountData.Email);

            if (requiredCode != code)
            {
                message.Respond("Invalid activation code", ResponseStatus.Error);
                return;
            }

            // Confirm e-mail
            extension.AccountData.IsEmailConfirmed = true;

            // Update account
            _database.UpdateAccount(extension.AccountData);

            // Respond with success
            message.Respond(ResponseStatus.Success);
        }
Exemplo n.º 15
0
        private void HandleMessage(IIncommingMessage message)
        {
            try
            {
                IPacketHandler handler;
                _handlers.TryGetValue(message.OpCode, out handler);

                if (handler != null)
                {
                    handler.Handle(message);
                }
                else if (message.IsExpectingResponse)
                {
                    Debug.LogError("Connection is missing a handler. OpCode: " + message.OpCode);
                    message.Respond(EResponseStatus.Error);
                }
            }
            catch (Exception e)
            {
#if UNITY_EDITOR
                if (RethrowExceptionsInEditor)
                {
                    throw;
                }
#endif

                Debug.LogError("Failed to handle a message. OpCode: " + message.OpCode);
                Debug.LogError(e);

                if (!message.IsExpectingResponse)
                {
                    return;
                }

                try
                {
                    message.Respond(EResponseStatus.Error);
                }
                catch (Exception exception)
                {
                    Debug.LogError(exception);
                }
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// Handles kill request for all controllers filtered by ID
        /// </summary>
        /// <param name="message"></param>
        private static void KillProcessRequestHandler(IIncommingMessage message)
        {
            var data       = message.Deserialize(new KillSpawnedProcessRequestPacket());
            var controller = Mst.Server.Spawners.GetController(data.SpawnerId) as SpawnerController;

            if (controller == null)
            {
                if (message.IsExpectingResponse)
                {
                    message.Respond("Couldn't find a spawn controller", ResponseStatus.NotHandled);
                }

                return;
            }

            controller.Logger.Debug($"Kill process requested for spawn controller [{controller.SpawnerId}]");
            controller.KillRequestHandler(data.SpawnId);
            message.Respond(ResponseStatus.Success);
        }
Exemplo n.º 17
0
        private void RegisterRoomRequestHandler(IIncommingMessage message)
        {
            logger.Debug($"Client {message.Peer.Id} requested to register new room server");

            if (!HasRoomRegistrationPermissions(message.Peer))
            {
                logger.Debug($"But it has no permission");
                message.Respond("Insufficient permissions", ResponseStatus.Unauthorized);
                return;
            }

            var options = message.Deserialize(new RoomOptions());
            var room    = RegisterRoom(message.Peer, options);

            logger.Debug($"Room {room.RoomId} has been successfully registered with options: {options}");

            // Respond with a room id
            message.Respond(room.RoomId, ResponseStatus.Success);
        }
Exemplo n.º 18
0
        private void OnMessageReceived(IIncommingMessage message)
        {
            try
            {
                _handlers.TryGetValue(message.OpCode, out var handler);

                if (handler == null)
                {
                    _logger.Warn($"Handler for OpCode {message.OpCode} = {(OpCodes) message.OpCode} does not exist");

                    if (message.IsExpectingResponse)
                    {
                        message.Respond(InternalServerErrorMessage, ResponseStatus.NotHandled);
                        return;
                    }

                    return;
                }

                handler.Handle(message);
            }
            catch (Exception e)
            {
                _logger.Error(
                    $"Error while handling a message from Client. OpCode: {message.OpCode} = {(OpCodes) message.OpCode}");
                _logger.Error(e);

                if (!message.IsExpectingResponse)
                {
                    return;
                }

                try
                {
                    message.Respond(InternalServerErrorMessage, ResponseStatus.Error);
                }
                catch (Exception exception)
                {
                    Logs.Error(exception);
                }
            }
        }
Exemplo n.º 19
0
        /// <summary>
        ///     Handles password reset request
        /// </summary>
        /// <param name="message"></param>
        private void HandlePasswordResetRequest(IIncommingMessage message)
        {
            var email = message.AsString();

            var account = _database.GetAccountByEmail(email);

            if (account == null)
            {
                message.Respond("No such e-mail in the system", ResponseStatus.Unauthorized);
                return;
            }

            var code = Util.CreateRandomString(4);

            _database.SavePasswordResetCode(account, code);

            _mailer.SendMail(account.Email, "Password Reset Code", string.Format(_config.PasswordResetEmailBody, code));

            message.Respond(ResponseStatus.Success);
        }
Exemplo n.º 20
0
        protected virtual void SaveRoomOptionsRequestHandler(IIncommingMessage message)
        {
            var data = message.Deserialize(new SaveRoomOptionsPacket());

            if (!roomsList.TryGetValue(data.RoomId, out RegisteredRoom room))
            {
                message.Respond("Room does not exist", ResponseStatus.Failed);
                return;
            }

            if (message.Peer != room.Peer)
            {
                // Wrong peer unregistering the room
                message.Respond("You're not the creator of the room", ResponseStatus.Unauthorized);
                return;
            }

            ChangeRoomOptions(room, data.Options);
            message.Respond(ResponseStatus.Success);
        }
Exemplo n.º 21
0
        protected virtual void OnGetCurrentChannelsRequestHandler(IIncommingMessage message)
        {
            string responseMsg = string.Empty;

            var chatUser = message.Peer.GetExtension <ChatUserPeerExtension>();

            if (chatUser == null)
            {
                responseMsg = "Chat cannot identify you";

                logger.Error(responseMsg);

                message.Respond(responseMsg, ResponseStatus.Unauthorized);
                return;
            }

            var channels = chatUser.CurrentChannels.Select(c => c.Name);

            message.Respond(channels.ToBytes(), ResponseStatus.Success);
        }
        public void handleCreateTournament(IIncommingMessage rawMsg)
        {
            IPeer peer = rawMsg.Peer;

            if (SpectatorAuthModule.existsAdmin(peer) == false)
            {
                rawMsg.Respond("You don't have admin permission", ResponseStatus.Failed);
                return;
            }

            TournamentInfoMsg msg           = rawMsg.Deserialize <TournamentInfoMsg>();
            string            key           = generateGameKey();
            PreTournamentGame newTournament = new PreTournamentGame(msg, peer, key);

            currentPreTournaments.Add(key, newTournament);
            addPlayerToPeerToTournaments(rawMsg.Peer, newTournament);
            rawMsg.Respond(key, ResponseStatus.Success);
            newTournament.updateAdmin();
            Debug.LogError("Created Tournament: " + key + " With admin: " + rawMsg.Peer.Id);
        }
Exemplo n.º 23
0
        //TODO CacheInfo
        protected void S2D_ReqGameData(IIncommingMessage reader)
        {
            var msg = reader.Parse <Msg_S2D_ReqGameData>();

            _authDb.GetGameData(msg.account, (dbData) => {
                reader.Respond(EMsgDS.D2S_RepGameData,
                               new Msg_D2S_RepGameData()
                {
                    data = dbData as GameData
                });
            });
        }
Exemplo n.º 24
0
        protected void S2D_ReqUserInfo(IIncommingMessage reader)
        {
            var msg = reader.Parse <Msg_ReqAccountData>();

            _authDb.GetAccount(msg.account, (dbData) => {
                reader.Respond(EMsgDS.D2S_RepUserInfo,
                               new Msg_RepAccountData()
                {
                    accountData = dbData as AccountData
                });
            });
        }
Exemplo n.º 25
0
        /// <summary>
        ///     Handles a request from user to leave a lobby
        /// </summary>
        /// <param name="message"></param>
        private void HandleLeaveLobby(IIncommingMessage message)
        {
            var lobbyId = message.AsInt();

            _lobbiesById.TryGetValue(lobbyId, out var lobby);

            var lobbiesExt = GetOrCreateLobbiesExtension(message.Peer);

            lobby?.RemovePlayer(lobbiesExt);

            message.Respond(ResponseStatus.Success);
        }
Exemplo n.º 26
0
        private void HandleSpawnRequest(IIncommingMessage message)
        {
            var packet = message.Deserialize <SpawnRequestPacket>();

            if (packet == null)
            {
                message.Respond(ResponseStatus.Error);
                return;
            }
            // Pass the request to handler
            _spawnerRequestsDelegate.HandleSpawnRequest(message, packet);
        }
 private bool findGame(string roomID, out RunningTournamentGame game, IIncommingMessage rawMsg = null, string error = "Could not find game: ")
 {
     if (runningTournaments.TryGetValue(roomID, out game))
     {
         return(true);
     }
     if (rawMsg != null)
     {
         rawMsg.Respond(error + roomID, ResponseStatus.Error);
     }
     return(false);
 }
Exemplo n.º 28
0
        private void PlayerLeftRoomRequestHandler(IIncommingMessage message)
        {
            var data = message.Deserialize(new PlayerLeftRoomPacket());

            if (!roomsList.TryGetValue(data.RoomId, out RegisteredRoom room))
            {
                message.Respond("Room does not exist", ResponseStatus.Failed);
                return;
            }

            if (message.Peer != room.Peer)
            {
                // Wrong peer unregistering the room
                message.Respond("You're not the creator of the room", ResponseStatus.Unauthorized);
                return;
            }

            room.OnPlayerLeft(data.PeerId);

            message.Respond(ResponseStatus.Success);
        }
Exemplo n.º 29
0
        protected virtual void DestroyRoomRequestHandler(IIncommingMessage message)
        {
            var roomId = message.AsInt();

            if (!roomsList.TryGetValue(roomId, out RegisteredRoom room))
            {
                message.Respond("Room does not exist", ResponseStatus.Failed);
                return;
            }

            if (message.Peer != room.Peer)
            {
                // Wrong peer unregistering the room
                message.Respond("You're not the creator of the room", ResponseStatus.Unauthorized);
                return;
            }

            DestroyRoom(room);

            message.Respond(ResponseStatus.Success);
        }
Exemplo n.º 30
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="message"></param>
        private void HandleMessage(IIncommingMessage message)
        {
            try
            {
                if (handlers.TryGetValue(message.OpCode, out IPacketHandler handler))
                {
                    if (handler != null)
                    {
                        handler.Handle(message);
                    }
                    else
                    {
                        Logs.Error($"Connection is missing a handler. OpCode: {message.OpCode}");
                    }
                }
                else if (message.IsExpectingResponse)
                {
                    Logs.Error($"Connection is missing a handler. OpCode: {message.OpCode}");
                    message.Respond(ResponseStatus.Error);
                }
            }
            catch (Exception e)
            {
                Logs.Error($"Failed to handle a message. OpCode: {message.OpCode}, Error: {e}");

                if (!message.IsExpectingResponse)
                {
                    return;
                }

                try
                {
                    message.Respond(ResponseStatus.Error);
                }
                catch (Exception exception)
                {
                    Logs.Error(exception);
                }
            }
        }