rsiot/components/shared_tasks/cmp_websocket_client_general/tasks/
connection_state.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use tokio::sync::mpsc;

use crate::{
    executor::CmpInOut,
    message::{Message, MsgDataBound},
};

/// Задача контроля соединения
///
/// Должна вызываться отдельно от остальных задач
pub struct ConnectionState<TMsg>
where
    TMsg: MsgDataBound,
{
    /// Канал получения состояния соединения
    pub input: mpsc::Receiver<bool>,
    /// Шина сообщений
    pub output: CmpInOut<TMsg>,
    /// Преобразование состояния в исходящее сообщение
    pub fn_connection_state: fn(bool) -> Option<Message<TMsg>>,
}

impl<TMsg> ConnectionState<TMsg>
where
    TMsg: MsgDataBound,
{
    /// Запуск на выполнение
    pub async fn spawn(mut self) -> super::Result<()> {
        create_msg_and_send(false, self.fn_connection_state, &self.output).await?;
        while let Some(state) = self.input.recv().await {
            create_msg_and_send(state, self.fn_connection_state, &self.output).await?;
        }
        Err(super::Error::TaskConnectionState)
    }
}

async fn create_msg_and_send<TMsg>(
    state: bool,
    fn_connection_state: fn(bool) -> Option<Message<TMsg>>,
    output: &CmpInOut<TMsg>,
) -> super::Result<()>
where
    TMsg: MsgDataBound,
{
    let msg = (fn_connection_state)(state);
    let Some(msg) = msg else { return Ok(()) };
    output
        .send_output(msg)
        .await
        .map_err(|_| super::Error::TokioSyncMpsc)
}