예제 #1
0
        private void HandleProvideRoomAccessCheck(IIncommingMessage message)
        {
            var data = message.Deserialize(new RoomAccessProvideCheckPacket());

            var roomController = Msf.Server.Rooms.GetRoomController(data.RoomId);

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

            var accessProvider = roomController._accessProvider ?? DefaultAccessProvider;
            var isProviderDone = false;

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

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

                isProviderDone = true;

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

                message.Respond(access, ResponseStatus.Success);

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

            // Timeout the access provider
            MsfTimer.WaitForSeconds(Msf.Server.Rooms.AccessProviderTimeout, () =>
            {
                if (!isProviderDone)
                {
                    isProviderDone = true;
                    message.Respond("Timed out", ResponseStatus.Timeout);
                    Logger.Error("Access provider took longer than " + Msf.Server.Rooms.AccessProviderTimeout + " seconds to provide access. " +
                                 "If it's intended, increase the threshold at Msf.Server.Rooms.AccessProviderTimeout");
                }
            });
        }
예제 #2
0
 /// <summary>
 ///     Default access provider, which always confirms access requests
 /// </summary>
 /// <param name="peerId"></param>
 /// <param name="callback"></param>
 public void DefaultAccessProvider(UsernameAndPeerIdPacket requester, RoomAccessProviderCallback callback)
 {
     callback.Invoke(new RoomAccessPacket {
         RoomIp     = Options.RoomIp,
         RoomPort   = Options.RoomPort,
         Properties = Options.Properties,
         RoomId     = RoomId,
         Token      = Guid.NewGuid().ToString(),
         SceneName  = SceneManager.GetActiveScene().name
     }, null);
 }
예제 #3
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);
        }
예제 #4
0
        private void ProvideRoomAccessCheckHandler(IIncommingMessage message)
        {
            var provideRoomAccessCheckPacket = message.Deserialize(new ProvideRoomAccessCheckPacket());
            var roomController = Msf.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
            MsfTimer.WaitForSeconds(Msf.Server.Rooms.AccessProviderTimeout, () =>
            {
                if (!isProviderDone)
                {
                    isProviderDone = true;
                    message.Respond("Timed out", ResponseStatus.Timeout);
                    Logger.Error($"Access provider took longer than {Msf.Server.Rooms.AccessProviderTimeout} seconds to provide access. " +
                                 "If it's intended, increase the threshold at Msf.Server.Rooms.AccessProviderTimeout");
                }
            });
        }