public IActionResult JoinSession(Guid id, [FromBody] JoinSessionRequest value) { if (!DataContainer.Users.TryGetValue(value.User, out _)) { return(NotFound()); } if (!DataContainer.Sessions.TryGetValue(id, out var session)) { return(NotFound()); } List <string[]> targets = new List <string[]>(); lock (session.Users) { foreach (var user in session.Users) { if (DataContainer.Users.TryGetValue(user, out var userInfo)) { lock (userInfo.Ips) { targets.Add(userInfo.Ips.ToArray()); } } } session.Users.Add(value.User); } return(Ok(new JoinSessionResponse() { RemoteHosts = targets.ToArray() })); }
public async Task <IActionResult> JoinSessionAsync([FromBody] JoinSessionRequest joinSessionRequest, CancellationToken cancellationToken) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var session = await _sessionProvider.GetSessionBySessionCodeAsync(joinSessionRequest.SessionCode, cancellationToken); if (session == null) { return(NotFound()); } var userJoinCheck = await CheckUserCanJoinSessionAsync(session, joinSessionRequest.Nickname, joinSessionRequest.UserId, joinSessionRequest.RejoinCode, cancellationToken); if (userJoinCheck.GetType() != typeof(OkResult)) { return(userJoinCheck); } var response = await _sessionProvider.JoinSessionAsync(joinSessionRequest, session.SessionId, cancellationToken); return(Ok(response)); }
public async Task <JoinSessionResponse> JoinSessionAsync(JoinSessionRequest joinSessionRequest, string sessionId, CancellationToken cancellationToken) { var rejoinCode = GetRandomCode(SessionCodeLength); var authCode = GetRandomCode(AuthCodeLength); var userId = joinSessionRequest.UserId ?? ObjectId.GenerateNewId().ToString(); var session = await _sessionRepo.GetSessionByIdAsync(sessionId, cancellationToken); if (joinSessionRequest.UserId == null) { var user = new UserModel { SessionId = session.SessionId, UserId = userId, Nickname = joinSessionRequest.Nickname, RejoinCode = rejoinCode, AuthCode = authCode }; await _userRepo.InsertUserAsync(user, cancellationToken); } else { await _userRepo.UpdateRejoinCodeAsync(userId, rejoinCode, cancellationToken); } var stories = await _storyRepo.GetStoriesBySessionIdAsync(session.SessionId, cancellationToken); var users = await _userRepo.GetUsersBySessionIdAsync(session.SessionId, cancellationToken); return(new JoinSessionResponse { SessionId = session.SessionId, UserId = userId, RejoinCode = rejoinCode, AuthCode = authCode, Stories = stories, Users = users, PointChoices = session.PointChoices, HasStarted = session.HasStarted, HasFinished = session.HasFinished }); }
/// <summary> /// Handles clients "join session" request. /// </summary> /// <param name="transporter">The transporter.</param> /// <param name="request">The request.</param> /// <returns></returns> /// <exception cref="AuthenticationException">Please login first.</exception> /// <exception cref="System.Exception"> /// Cannot create a loop-back session! /// or /// Client {request.ClientID} was not found. /// or /// Client {request.ClientID} is already in session with another client. /// </exception> private ResonanceActionResult <JoinSessionResponse> OnClientJoinSessionRequest(IResonanceTransporter transporter, JoinSessionRequest request) { ResonanceTcpClient client = transporter as ResonanceTcpClient; if (client.ClientID == null) { throw new AuthenticationException("Please login first."); } var existingClient = _clients.FirstOrDefault(x => x.ClientID == request.ClientID); if (client == existingClient) { throw new Exception($"Cannot create a loop-back session!"); } if (existingClient == null) { throw new Exception($"Client {request.ClientID} was not found."); } if (existingClient.InSession) { throw new Exception($"Client {request.ClientID} is already in session with another client."); } existingClient.SendRequestAsync <JoinSessionRequest, JoinSessionResponse>(new JoinSessionRequest() { ClientID = client.ClientID }, new ResonanceRequestConfig() { Timeout = TimeSpan.FromSeconds(10) }).GetAwaiter().GetResult(); Task.Delay(100).ContinueWith((x) => //Delay the IsSession true to avoid routing of the JoinSessionResponse.. { client.RemoteClient = existingClient; existingClient.RemoteClient = client; client.InSession = true; existingClient.InSession = true; Logger.LogInformation($"Clients {client.ClientID} and {existingClient.ClientID} are now in session."); }); return(new JoinSessionResponse()); }