mahjong_service/socket/
session.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use super::{
    MahjongWebsocketServer, SocketMessageConnect, SocketMessageDisconnect, SocketMessageListRooms,
    SocketMessageStr,
};
use actix::prelude::*;
use actix_web_actors::ws;
use mahjong_core::{GameId, PlayerId};
use service_contracts::SocketMessage;
use std::time::{Duration, Instant};
use tracing::debug;

const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
const CLIENT_TIMEOUT: Duration = Duration::from_secs(10);

pub type RoomId = String;
pub type SessionId = usize;

#[derive(Debug)]
pub struct MahjongWebsocketSession {
    pub addr: Addr<MahjongWebsocketServer>,
    pub hb: Instant,
    pub id: SessionId,
    pub room: RoomId,
}

impl MahjongWebsocketSession {
    pub fn get_room_id(game_id: &GameId, player_id: Option<&PlayerId>) -> RoomId {
        if player_id.is_none() {
            return game_id.to_string();
        }

        format!("{}__{}", game_id, player_id.unwrap())
    }
    fn hb(&self, ctx: &mut ws::WebsocketContext<Self>) {
        ctx.run_interval(HEARTBEAT_INTERVAL, |act, new_ctx| {
            if Instant::now().duration_since(act.hb) > CLIENT_TIMEOUT {
                act.addr.do_send(SocketMessageDisconnect { id: act.id });
                new_ctx.stop();
                return;
            }

            new_ctx.ping(b"");
        });
    }
}

impl Actor for MahjongWebsocketSession {
    type Context = ws::WebsocketContext<Self>;

    fn started(&mut self, ctx: &mut Self::Context) {
        self.hb(ctx);

        let addr = ctx.address();
        let room = self.room.clone();

        self.addr
            .send(SocketMessageConnect {
                room,
                addr: addr.recipient(),
            })
            .into_actor(self)
            .then(|res, act, new_ctx| {
                match res {
                    Ok(res) => {
                        act.id = res;
                        debug!("{} joined room {}", act.id, act.room);
                    }
                    _ => new_ctx.stop(),
                }
                fut::ready(())
            })
            .wait(ctx);
    }

    fn stopping(&mut self, _: &mut Self::Context) -> Running {
        debug!("{} disconnected from {}", self.id, self.room);
        self.addr.do_send(SocketMessageDisconnect { id: self.id });
        Running::Stop
    }
}

impl Handler<SocketMessageStr> for MahjongWebsocketSession {
    type Result = ();

    fn handle(&mut self, msg: SocketMessageStr, ctx: &mut Self::Context) {
        ctx.text(msg.0);
    }
}

impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for MahjongWebsocketSession {
    fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
        let msg = match msg {
            Err(_) => {
                ctx.stop();
                return;
            }
            Ok(msg) => msg,
        };

        match msg {
            ws::Message::Ping(msg) => {
                self.hb = Instant::now();
                ctx.pong(&msg);
            }
            ws::Message::Pong(_) => {
                self.hb = Instant::now();
            }
            ws::Message::Text(text) => {
                let message = serde_json::from_str::<SocketMessage>(&text);
                if message.is_err() {
                    return;
                }

                if let Ok(SocketMessage::ListRooms) = message {
                    self.addr
                        .send(SocketMessageListRooms)
                        .into_actor(self)
                        .then(|res, _, new_ctx| {
                            if let Ok(rooms) = res {
                                for room in rooms {
                                    let room =
                                        serde_json::to_string(&SocketMessage::Name(room)).unwrap();

                                    new_ctx.text(room);
                                }
                            }
                            fut::ready(())
                        })
                        .wait(ctx)
                }
            }
            ws::Message::Close(reason) => {
                ctx.close(reason);
                ctx.stop();
            }
            ws::Message::Continuation(_) => {
                ctx.stop();
            }
            _ => {}
        }
    }
}