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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use std::net::SocketAddr;

use byteorder::{BigEndian, WriteBytesExt};

use naia_client_socket::{ClientSocket, Config as SocketConfig, MessageSender, SocketEvent};
pub use naia_shared::{
    Config, EntityType, Event, EventType, LocalEntityKey, ManagerType, Manifest, PacketReader,
    PacketType, PacketWriter, Timer, Timestamp,
};

use super::{
    client_entity_message::ClientEntityMessage, client_event::ClientEvent, error::NaiaClientError,
    server_connection::ServerConnection, Packet,
};
use crate::client_connection_state::{
    ClientConnectionState, ClientConnectionState::AwaitingChallengeResponse,
};

/// Client can send/receive events to/from a server, and has a pool of in-scope
/// entities that are synced with the server
#[derive(Debug)]
pub struct NaiaClient<T: EventType, U: EntityType> {
    manifest: Manifest<T, U>,
    server_address: SocketAddr,
    config: Config,
    socket: ClientSocket,
    sender: MessageSender,
    server_connection: Option<ServerConnection<T, U>>,
    pre_connection_timestamp: Option<Timestamp>,
    pre_connection_digest: Option<Box<[u8]>>,
    handshake_timer: Timer,
    connection_state: ClientConnectionState,
    auth_event: Option<T>,
}

impl<T: EventType, U: EntityType> NaiaClient<T, U> {
    /// Create a new client, given the server's address, a shared manifest, an
    /// optional Config, and an optional Authentication event
    pub fn new(
        server_address: SocketAddr,
        manifest: Manifest<T, U>,
        config: Option<Config>,
        auth: Option<T>,
    ) -> Self {
        let mut config = match config {
            Some(config) => config,
            None => Config::default(),
        };
        config.heartbeat_interval /= 2;

        let socket_config = SocketConfig::default();
        let mut client_socket = ClientSocket::connect(server_address, Some(socket_config));

        let mut handshake_timer = Timer::new(config.send_handshake_interval);
        handshake_timer.ring_manual();
        let message_sender = client_socket.get_sender();

        NaiaClient {
            server_address,
            manifest,
            socket: client_socket,
            sender: message_sender,
            config,
            handshake_timer,
            server_connection: None,
            pre_connection_timestamp: None,
            pre_connection_digest: None,
            connection_state: AwaitingChallengeResponse,
            auth_event: auth,
        }
    }

    /// Must be called regularly, performs updates to the connection, and
    /// retrieves event/entity updates sent by the Server
    pub fn receive(&mut self) -> Result<ClientEvent<T>, NaiaClientError> {
        // send handshakes, send heartbeats, timeout if need be
        match &mut self.server_connection {
            Some(connection) => {
                if connection.should_drop() {
                    self.server_connection = None;
                    self.pre_connection_timestamp = None;
                    self.pre_connection_digest = None;
                    self.connection_state = AwaitingChallengeResponse;
                    return Ok(ClientEvent::Disconnection);
                }
                if connection.should_send_heartbeat() {
                    NaiaClient::internal_send_with_connection(
                        &mut self.sender,
                        connection,
                        PacketType::Heartbeat,
                        Packet::empty(),
                    );
                }
                // send a packet
                if let Some(payload) = connection.get_outgoing_packet(&self.manifest) {
                    self.sender
                        .send(Packet::new_raw(payload))
                        .expect("send failed!");
                    connection.mark_sent();
                }
                // receive event
                if let Some(event) = connection.get_incoming_event() {
                    return Ok(ClientEvent::Event(event));
                }
                // receive entity message
                if let Some(message) = connection.get_incoming_entity_message() {
                    match message {
                        ClientEntityMessage::Create(local_key) => {
                            return Ok(ClientEvent::CreateEntity(local_key));
                        }
                        ClientEntityMessage::Delete(local_key) => {
                            return Ok(ClientEvent::DeleteEntity(local_key));
                        }
                        ClientEntityMessage::Update(local_key) => {
                            return Ok(ClientEvent::UpdateEntity(local_key));
                        }
                    }
                }
            }
            None => {
                if self.handshake_timer.ringing() {
                    match self.connection_state {
                        ClientConnectionState::AwaitingChallengeResponse => {
                            if self.pre_connection_timestamp.is_none() {
                                self.pre_connection_timestamp = Some(Timestamp::now());
                            }

                            let mut timestamp_bytes = Vec::new();
                            self.pre_connection_timestamp
                                .as_mut()
                                .unwrap()
                                .write(&mut timestamp_bytes);
                            NaiaClient::<T, U>::internal_send_connectionless(
                                &mut self.sender,
                                PacketType::ClientChallengeRequest,
                                Packet::new(timestamp_bytes),
                            );
                        }
                        ClientConnectionState::AwaitingConnectResponse => {
                            // write timestamp & digest into payload
                            let mut payload_bytes = Vec::new();
                            self.pre_connection_timestamp
                                .as_mut()
                                .unwrap()
                                .write(&mut payload_bytes);
                            for digest_byte in self.pre_connection_digest.as_ref().unwrap().as_ref()
                            {
                                payload_bytes.push(*digest_byte);
                            }
                            // write auth event object if there is one
                            if let Some(auth_event) = &mut self.auth_event {
                                let type_id = auth_event.get_type_id();
                                let naia_id = self.manifest.get_event_naia_id(&type_id); // get naia id
                                payload_bytes.write_u16::<BigEndian>(naia_id).unwrap(); // write naia id
                                auth_event.write(&mut payload_bytes);
                            }
                            NaiaClient::<T, U>::internal_send_connectionless(
                                &mut self.sender,
                                PacketType::ClientConnectRequest,
                                Packet::new(payload_bytes),
                            );
                        }
                        _ => {}
                    }

                    self.handshake_timer.reset();
                }
            }
        }

        // receive from socket
        let mut output: Option<Result<ClientEvent<T>, NaiaClientError>> = None;
        while output.is_none() {
            match self.socket.receive() {
                Ok(event) => match event {
                    SocketEvent::Packet(packet) => {
                        let packet_type = PacketType::get_from_packet(packet.payload());

                        let server_connection_wrapper = self.server_connection.as_mut();
                        if let Some(server_connection) = server_connection_wrapper {
                            server_connection.mark_heard();
                            let mut payload =
                                server_connection.process_incoming_header(packet.payload());

                            match packet_type {
                                PacketType::Data => {
                                    server_connection
                                        .process_incoming_data(&self.manifest, &mut payload);
                                    continue;
                                }
                                PacketType::Heartbeat => {
                                    continue;
                                }
                                _ => {}
                            }
                        } else {
                            match packet_type {
                                PacketType::ServerChallengeResponse => {
                                    if self.connection_state
                                        == ClientConnectionState::AwaitingChallengeResponse
                                    {
                                        if let Some(my_timestamp) = self.pre_connection_timestamp {
                                            let payload =
                                                naia_shared::utils::read_headerless_payload(
                                                    packet.payload(),
                                                );
                                            let mut reader = PacketReader::new(&payload);
                                            let payload_timestamp = Timestamp::read(&mut reader);

                                            if my_timestamp == payload_timestamp {
                                                let mut digest_bytes: Vec<u8> = Vec::new();
                                                for _ in 0..32 {
                                                    digest_bytes.push(reader.read_u8());
                                                }
                                                self.pre_connection_digest =
                                                    Some(digest_bytes.into_boxed_slice());
                                                self.connection_state =
                                                    ClientConnectionState::AwaitingConnectResponse;
                                            }
                                        }
                                    }

                                    continue;
                                }
                                PacketType::ServerConnectResponse => {
                                    self.server_connection = Some(ServerConnection::new(
                                        self.server_address,
                                        &self.config,
                                    ));
                                    self.connection_state = ClientConnectionState::Connected;
                                    output = Some(Ok(ClientEvent::Connection));
                                    continue;
                                }
                                _ => {}
                            }
                        }
                    }
                    SocketEvent::None => {
                        output = Some(Ok(ClientEvent::None));
                        continue;
                    }
                },
                Err(error) => {
                    output = Some(Err(NaiaClientError::Wrapped(Box::new(error))));
                    continue;
                }
            }
        }
        return output.unwrap();
    }

    /// Queues up an Event to be sent to the Server
    pub fn send_event(&mut self, event: &impl Event<T>) {
        if let Some(connection) = &mut self.server_connection {
            connection.queue_event(event);
        }
    }

    fn internal_send_with_connection(
        sender: &mut MessageSender,
        connection: &mut ServerConnection<T, U>,
        packet_type: PacketType,
        packet: Packet,
    ) {
        let new_payload = connection.process_outgoing_header(packet_type, packet.payload());
        sender
            .send(Packet::new_raw(new_payload))
            .expect("send failed!");
        connection.mark_sent();
    }

    fn internal_send_connectionless(
        sender: &mut MessageSender,
        packet_type: PacketType,
        packet: Packet,
    ) {
        let new_payload =
            naia_shared::utils::write_connectionless_payload(packet_type, packet.payload());
        sender
            .send(Packet::new_raw(new_payload))
            .expect("send failed!");
    }

    /// Get the address currently associated with the Server
    pub fn server_address(&self) -> SocketAddr {
        return self.socket.server_address();
    }

    /// Get a reference to an Entity currently in scope for the Client, given
    /// that Entity's Key
    pub fn get_entity(&self, key: LocalEntityKey) -> Option<&U> {
        return self
            .server_connection
            .as_ref()
            .unwrap()
            .get_local_entity(key);
    }

    /// Get the current measured Round Trip Time to the Server
    pub fn get_rtt(&self) -> f32 {
        return self.server_connection.as_ref().unwrap().get_rtt();
    }
}