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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use common_net::msg::{ClientType, ServerGeneral, ServerMsg};
use network::{ConnectAddr, Message, Participant, Stream, StreamError, StreamParams};
use serde::{de::DeserializeOwned, Serialize};
use specs::Component;
use std::{net::SocketAddr, sync::atomic::AtomicBool};

/// Client handles ALL network related information of everything that connects
/// to the server Client DOES NOT handle game states
/// Client DOES NOT handle network information that is only relevant to some
/// "things" connecting to the server (there is currently no such case). First a
/// Client connects to the game, when it registers, it gets the `Player`
/// component, when it enters the game it gets the `InGame` component.
pub struct Client {
    pub client_type: ClientType,
    pub participant: Option<Participant>,
    pub current_ip_addrs: Vec<SocketAddr>,
    connected_from_addr: ConnectAddr,
    pub last_ping: f64,
    pub login_msg_sent: AtomicBool,
    pub locale: Option<String>,

    //TODO: Consider splitting each of these out into their own components so all the message
    //processing systems can run in parallel with each other (though it may turn out not to
    //matter that much).
    general_stream: Stream,
    ping_stream: Stream,
    register_stream: Stream,
    character_screen_stream: Stream,
    in_game_stream: Stream,
    terrain_stream: Stream,

    general_stream_params: StreamParams,
    ping_stream_params: StreamParams,
    register_stream_params: StreamParams,
    character_screen_stream_params: StreamParams,
    in_game_stream_params: StreamParams,
    terrain_stream_params: StreamParams,
}

pub struct PreparedMsg {
    stream_id: u8,
    message: Message,
}

impl Component for Client {
    type Storage = specs::DenseVecStorage<Self>;
}

impl Client {
    pub(crate) fn new(
        client_type: ClientType,
        participant: Participant,
        connected_from: ConnectAddr,
        last_ping: f64,
        locale: Option<String>,
        general_stream: Stream,
        ping_stream: Stream,
        register_stream: Stream,
        character_screen_stream: Stream,
        in_game_stream: Stream,
        terrain_stream: Stream,
    ) -> Self {
        let general_stream_params = general_stream.params();
        let ping_stream_params = ping_stream.params();
        let register_stream_params = register_stream.params();
        let character_screen_stream_params = character_screen_stream.params();
        let in_game_stream_params = in_game_stream.params();
        let terrain_stream_params = terrain_stream.params();
        Client {
            client_type,
            participant: Some(participant),
            current_ip_addrs: connected_from.socket_addr().into_iter().collect(),
            connected_from_addr: connected_from,
            last_ping,
            locale,
            login_msg_sent: AtomicBool::new(false),
            general_stream,
            ping_stream,
            register_stream,
            character_screen_stream,
            in_game_stream,
            terrain_stream,
            general_stream_params,
            ping_stream_params,
            register_stream_params,
            character_screen_stream_params,
            in_game_stream_params,
            terrain_stream_params,
        }
    }

    pub(crate) fn connected_from_addr(&self) -> &ConnectAddr { &self.connected_from_addr }

    pub(crate) fn send<M: Into<ServerMsg>>(&self, msg: M) -> Result<(), StreamError> {
        // TODO: hack to avoid locking stream mutex while serializing the message,
        // remove this when the mutexes on the Streams are removed
        let prepared = self.prepare(msg);
        self.send_prepared(&prepared)
        /*match msg.into() {
            ServerMsg::Info(m) => self.register_stream.lock().unwrap().send(m),
            ServerMsg::Init(m) => self.register_stream.lock().unwrap().send(m),
            ServerMsg::RegisterAnswer(m) => self.register_stream.lock().unwrap().send(m),
            ServerMsg::General(g) => {
                match g {
                    //Character Screen related
                    ServerGeneral::CharacterDataLoadResult(_)
                    | ServerGeneral::CharacterListUpdate(_)
                    | ServerGeneral::CharacterActionError(_)
                    | ServerGeneral::CharacterCreated(_)
                    | ServerGeneral::CharacterEdited(_)
                    | ServerGeneral::CharacterSuccess => {
                        self.character_screen_stream.lock().unwrap().send(g)
                    },
                    //In-game related
                    ServerGeneral::GroupUpdate(_)
                    | ServerGeneral::Invite { .. }
                    | ServerGeneral::InvitePending(_)
                    | ServerGeneral::InviteComplete { .. }
                    | ServerGeneral::ExitInGameSuccess
                    | ServerGeneral::InventoryUpdate(_, _)
                    | ServerGeneral::SetViewDistance(_)
                    | ServerGeneral::SiteEconomy(_)
                    | ServerGeneral::Outcomes(_)
                    | ServerGeneral::Knockback(_)
                    | ServerGeneral::UpdatePendingTrade(_, _, _)
                    | ServerGeneral::FinishedTrade(_)
                    | ServerGeneral::WeatherUpdate(_) => {
                        self.in_game_stream.lock().unwrap().send(g)
                    },
                    //Ingame related, terrain
                    ServerGeneral::TerrainChunkUpdate { .. }
                    | ServerGeneral::LodZoneUpdate { .. }
                    | ServerGeneral::TerrainBlockUpdates(_) => {
                        self.terrain_stream.lock().unwrap().send(g)
                    },
                    // Always possible
                    ServerGeneral::PlayerListUpdate(_)
                    | ServerGeneral::ChatMsg(_)
                    | ServerGeneral::ChatMode(_)
                    | ServerGeneral::SetPlayerEntity(_)
                    | ServerGeneral::TimeOfDay(_, _)
                    | ServerGeneral::EntitySync(_)
                    | ServerGeneral::CompSync(_)
                    | ServerGeneral::CreateEntity(_)
                    | ServerGeneral::DeleteEntity(_)
                    | ServerGeneral::Disconnect(_)
                    | ServerGeneral::Notification(_) => self.general_stream.lock().unwrap().send(g),
                }
            },
            ServerMsg::Ping(m) => self.ping_stream.lock().unwrap().send(m),
        }*/
    }

    /// Like `send` but any errors are explicitly ignored.
    pub(crate) fn send_fallible<M: Into<ServerMsg>>(&self, msg: M) { let _ = self.send(msg); }

    pub(crate) fn send_prepared(&self, msg: &PreparedMsg) -> Result<(), StreamError> {
        match msg.stream_id {
            0 => self.register_stream.send_raw(&msg.message),
            1 => self.character_screen_stream.send_raw(&msg.message),
            2 => self.in_game_stream.send_raw(&msg.message),
            3 => self.general_stream.send_raw(&msg.message),
            4 => self.ping_stream.send_raw(&msg.message),
            5 => self.terrain_stream.send_raw(&msg.message),
            _ => unreachable!("invalid stream id"),
        }
    }

    pub(crate) fn prepare<M: Into<ServerMsg>>(&self, msg: M) -> PreparedMsg {
        match msg.into() {
            ServerMsg::Info(m) => PreparedMsg::new(0, &m, &self.register_stream_params),
            ServerMsg::Init(m) => PreparedMsg::new(0, &m, &self.register_stream_params),
            ServerMsg::RegisterAnswer(m) => PreparedMsg::new(0, &m, &self.register_stream_params),
            ServerMsg::General(g) => {
                match g {
                    // Character Screen related
                    ServerGeneral::CharacterDataLoadResult(_)
                    | ServerGeneral::CharacterListUpdate(_)
                    | ServerGeneral::CharacterActionError(_)
                    | ServerGeneral::CharacterCreated(_)
                    | ServerGeneral::CharacterEdited(_)
                    | ServerGeneral::CharacterSuccess
                    | ServerGeneral::SpectatorSuccess(_) => {
                        PreparedMsg::new(1, &g, &self.character_screen_stream_params)
                    },
                    // In-game related
                    ServerGeneral::GroupUpdate(_)
                    | ServerGeneral::Invite { .. }
                    | ServerGeneral::InvitePending(_)
                    | ServerGeneral::InviteComplete { .. }
                    | ServerGeneral::ExitInGameSuccess
                    | ServerGeneral::InventoryUpdate(_, _)
                    | ServerGeneral::GroupInventoryUpdate(_, _)
                    | ServerGeneral::SetViewDistance(_)
                    | ServerGeneral::Outcomes(_)
                    | ServerGeneral::Knockback(_)
                    | ServerGeneral::SiteEconomy(_)
                    | ServerGeneral::UpdatePendingTrade(_, _, _)
                    | ServerGeneral::FinishedTrade(_)
                    | ServerGeneral::MapMarker(_)
                    | ServerGeneral::WeatherUpdate(_)
                    | ServerGeneral::LocalWindUpdate(_)
                    | ServerGeneral::SpectatePosition(_)
                    | ServerGeneral::UpdateRecipes => {
                        PreparedMsg::new(2, &g, &self.in_game_stream_params)
                    },
                    // Terrain
                    ServerGeneral::TerrainChunkUpdate { .. }
                    | ServerGeneral::LodZoneUpdate { .. }
                    | ServerGeneral::TerrainBlockUpdates(_) => {
                        PreparedMsg::new(5, &g, &self.terrain_stream_params)
                    },
                    // Always possible
                    ServerGeneral::PlayerListUpdate(_)
                    | ServerGeneral::ChatMsg(_)
                    | ServerGeneral::ChatMode(_)
                    | ServerGeneral::SetPlayerEntity(_)
                    | ServerGeneral::TimeOfDay(_, _, _, _)
                    | ServerGeneral::EntitySync(_)
                    | ServerGeneral::CompSync(_, _)
                    | ServerGeneral::CreateEntity(_)
                    | ServerGeneral::DeleteEntity(_)
                    | ServerGeneral::Disconnect(_)
                    | ServerGeneral::Notification(_)
                    | ServerGeneral::SetPlayerRole(_)
                    | ServerGeneral::PluginData(_) => {
                        PreparedMsg::new(3, &g, &self.general_stream_params)
                    },
                }
            },
            ServerMsg::Ping(m) => PreparedMsg::new(4, &m, &self.ping_stream_params),
        }
    }

    pub(crate) fn terrain_params(&self) -> StreamParams { self.terrain_stream_params.clone() }

    /// Only used for Serialize Chunks in a SlowJob.
    /// TODO: find a more elegant version for this invariant
    pub(crate) fn prepare_chunk_update_msg(
        terrain_chunk_update: ServerGeneral,
        params: &StreamParams,
    ) -> PreparedMsg {
        if !matches!(
            terrain_chunk_update,
            ServerGeneral::TerrainChunkUpdate { .. }
        ) {
            unreachable!("You must not call this function without a terrain chunk update!")
        }
        PreparedMsg::new(5, &terrain_chunk_update, params)
    }

    pub(crate) fn recv<M: DeserializeOwned>(
        &mut self,
        stream_id: u8,
    ) -> Result<Option<M>, StreamError> {
        // TODO: are two systems using the same stream?? why is there contention here?
        match stream_id {
            0 => self.register_stream.try_recv(),
            1 => self.character_screen_stream.try_recv(),
            2 => self.in_game_stream.try_recv(),
            3 => self.general_stream.try_recv(),
            4 => self.ping_stream.try_recv(),
            5 => self.terrain_stream.try_recv(),
            _ => unreachable!("invalid stream id"),
        }
    }
}

impl PreparedMsg {
    fn new<M: Serialize + ?Sized>(id: u8, msg: &M, stream_params: &StreamParams) -> PreparedMsg {
        Self {
            stream_id: id,
            message: Message::serialize(&msg, stream_params.clone()),
        }
    }
}