From 024d83619d7783d1084f64c892b1d4ce6262f71d Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 10 Feb 2021 18:26:19 +0100 Subject: [PATCH] Close concurrent call accesses --- src/controllers/calls_controller.rs | 21 +++++++++++++++++++-- src/controllers/user_ws_controller.rs | 4 +++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/controllers/calls_controller.rs b/src/controllers/calls_controller.rs index 99d3dd5..8a7bf4c 100644 --- a/src/controllers/calls_controller.rs +++ b/src/controllers/calls_controller.rs @@ -72,8 +72,25 @@ pub fn join_call(r: &mut UserWsRequestHandler) -> RequestResult { r.forbidden("This conversation can not be used to make calls!".to_string())?; } - // TODO : Remove any previous call linked to current connection + any other active connection - // of current user to current conversation call + // Remove any other active call with current WebSocket + if let Some(call) = &r.get_conn().active_call { + make_user_leave_call(&call.conv_id, r.get_conn())?; + } + + // Remove any other active connection to current call of current user + user_ws_controller::foreach_connection(|conn| { + if &conn.user_id != r.user_id_ref()? || conn.session.eq(&r.get_conn().session) { + return Ok(()); + } + + if let Some(call) = &conn.active_call { + if call.conv_id == conv_id { + make_user_leave_call(&call.conv_id, conn)?; + } + } + + Ok(()) + })?; r.update_conn(|r| r.active_call = Some(ActiveCall { conv_id, diff --git a/src/controllers/user_ws_controller.rs b/src/controllers/user_ws_controller.rs index ed3b06a..bb5c721 100644 --- a/src/controllers/user_ws_controller.rs +++ b/src/controllers/user_ws_controller.rs @@ -573,7 +573,9 @@ pub fn disconnect_user_from_all_sockets(user_id: &UserID) -> Res { /// Do something with all active connections pub fn foreach_connection(mut f: F) -> Res where F: FnMut(&UserWsConnection) -> Res { - for conn in get_ws_connections_list().lock().unwrap().iter() { + let list = get_ws_connections_list().lock().unwrap().clone(); + + for conn in list.iter() { f(conn)?; }