private void HandleProvideRoomAccessCheck(IIncommingMessage message) { var data = message.Deserialize(new RoomAccessProvideCheckPacket()); var roomController = _roomsPlugin.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 AppTimer.AfterSeconds(_roomsPlugin.AccessProviderTimeout, () => { if (!isProviderDone) { isProviderDone = true; message.Respond("Timed out", ResponseStatus.Timeout); Logger.Error("Access provider took longer than " + _roomsPlugin.AccessProviderTimeout + " seconds to provide access. " + "If it's intended, increase the threshold at Msf.Server.Rooms.AccessProviderTimeout"); } }); }
/// <summary> /// Override, if you want to manually handle creation of access'es /// </summary> /// <param name="callback"></param> public virtual void CreateAccess(UsernameAndPeerIdPacket requester, RoomAccessProviderCallback callback) { callback.Invoke(new RoomAccessPacket { RoomIp = Controller.Options.RoomIp, RoomPort = Controller.Options.RoomPort, Properties = Controller.Options.Properties, RoomId = Controller.RoomId, SceneName = SceneManager.GetActiveScene().name, Token = Guid.NewGuid().ToString() }, null); }
/// <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 = "SceneName" }, null); }
private void HandleValidateRoomAccess(IIncommingMessage message) { var data = message.Deserialize <RoomAccessValidatePacket>(); _rooms.TryGetValue(data.RoomId, out var room); if (room == null) { message.Respond("Room does not exist", ResponseStatus.Failed); return; } if (message.Peer != room.Peer) { // Wrong peer querying the room message.Respond("You're not the creator of the room", ResponseStatus.Unauthorized); return; } if (!room.ValidateAccess(data.Token, out var playerPeer)) { message.Respond("Failed to confirm the access", ResponseStatus.Unauthorized); return; } var packet = new UsernameAndPeerIdPacket { PeerId = playerPeer.ConnectId }; // Add username if available var userExt = playerPeer.GetExtension <UserExtension>(); if (userExt != null) { packet.Username = userExt.Username ?? ""; } // Respond with success and player's peer id message.Respond(packet, ResponseStatus.Success); }