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
use bevy::prelude::*;

use std::net::{SocketAddr, ToSocketAddrs};
use std::sync::mpsc::{Receiver, Sender};
use std::sync::Mutex;
use std::time::Duration;

use bytes::Bytes;

use laminar::{Config, Socket};

mod worker;

pub struct NetworkingPlugin;

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct SocketHandle {
    identifier: u32,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Connection {
    pub addr: SocketAddr,
    pub socket: SocketHandle,
}

#[derive(Debug)]
pub enum NetworkEvent {
    Connected(Connection),
    Disconnected(Connection),
    Message(Connection, Bytes),
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum NetworkDelivery {
    UnreliableUnordered,
    UnreliableSequenced(Option<u8>),
    ReliableUnordered,
    ReliableSequenced(Option<u8>),
    ReliableOrdered(Option<u8>),
}

pub struct NetworkResource {
    default_socket: Option<SocketHandle>,

    // future work, allow binding over multiple sockets
    // sockets: Vec<SocketHandle>,
    connections: Vec<Connection>,
    event_rx: Mutex<Receiver<NetworkEvent>>,
    message_tx: Mutex<Sender<Message>>,
    instruction_tx: Mutex<Sender<SocketInstruction>>,
}

impl Plugin for NetworkingPlugin {
    fn build(&self, app: &mut AppBuilder) {
        let network_resource = worker::start_worker_thread();

        app.add_event::<NetworkEvent>()
            .add_resource(network_resource)
            .add_system(process_network_events.system());
    }
}

impl NetworkResource {
    pub fn connections(&self) -> &Vec<Connection> {
        return &self.connections;
    }

    pub fn connections_for_socket(&self, socket: SocketHandle) -> Vec<Connection> {
        return self
            .connections
            .iter()
            .filter(|c| c.socket == socket)
            .map(|c| *c)
            .collect();
    }

    pub fn add_connection(&mut self, connection: Connection) {
        if self.has_connection(connection) {
            println!("Warning: attempted to add a connection that already exists");
            return;
        }

        self.connections.push(connection);
    }

    pub fn remove_connection(&mut self, connection: Connection) {
        let conn = self.connections.iter().position(|c| *c == connection);

        match conn {
            Some(idx) => {
                self.connections.remove(idx);
            }
            None => {
                println!("Warning: attempted to remove connection that doesn't exist");
            }
        }
    }

    pub fn has_connection(&self, connection: Connection) -> bool {
        self.connections.iter().any(|c| *c == connection)
    }

    pub fn bind<A: ToSocketAddrs>(&mut self, addr: A) -> Result<SocketHandle, ()> {
        if let Some(_) = self.default_socket {
            panic!("Already bound");
        }

        let mut cfg = Config::default();
        cfg.idle_connection_timeout = Duration::from_millis(2000);
        cfg.heartbeat_interval = Some(Duration::from_millis(1000));
        cfg.max_packets_in_flight = 2048;

        let handle = SocketHandle { identifier: 0 };
        let socket = Socket::bind_with_config(addr, cfg).expect("socket bind failed"); // todo

        let instruction = SocketInstruction::AddSocket(handle, socket);
        {
            let locked = self.instruction_tx.lock().expect("instruction lock failed");
            locked.send(instruction).expect("instruction send failed");
        }

        self.default_socket = Some(handle);

        return Ok(handle);
    }

    pub fn send(&self, addr: SocketAddr, message: &[u8], delivery: NetworkDelivery) {
        let socket = self.default_socket.unwrap(); // todo
        let msg = Message {
            destination: addr,
            delivery: delivery,
            socket_handle: socket,
            message: Bytes::copy_from_slice(message),
        };

        self.message_tx.lock().unwrap().send(msg).unwrap();
    }

    pub fn broadcast(&self, message: &[u8], delivery: NetworkDelivery) {
        let socket = self.default_socket.unwrap(); // todo

        let broadcast_to = self.connections_for_socket(socket);

        for conn in broadcast_to {
            let msg = Message {
                destination: conn.addr,
                delivery: delivery,
                socket_handle: socket,
                message: Bytes::copy_from_slice(message),
            };

            self.message_tx.lock().unwrap().send(msg).unwrap();
        }
    }
}

#[derive(Debug)]
struct Message {
    message: Bytes,
    delivery: NetworkDelivery,
    socket_handle: SocketHandle,
    destination: SocketAddr,
}

enum SocketInstruction {
    AddSocket(SocketHandle, Socket),
}

fn process_network_events(
    mut net: ResMut<NetworkResource>,
    mut network_events: ResMut<Events<NetworkEvent>>,
) {
    let mut added_connections: Vec<Connection> = Vec::new();
    let mut removed_connections: Vec<Connection> = Vec::new();

    {
        let locked = net.event_rx.lock().unwrap();

        while let Ok(event) = locked.try_recv() {
            match event {
                NetworkEvent::Connected(conn) => {
                    if !net.has_connection(conn) && !added_connections.contains(&conn) {
                        added_connections.push(conn);
                    }
                }
                NetworkEvent::Disconnected(conn) => {
                    if net.has_connection(conn) && !removed_connections.contains(&conn) {
                        removed_connections.push(conn);
                    }
                }
                _ => network_events.send(event),
            }
        }
    }

    for conn in added_connections {
        net.add_connection(conn);
        network_events.send(NetworkEvent::Connected(conn));
    }

    for conn in removed_connections {
        net.remove_connection(conn);
        network_events.send(NetworkEvent::Disconnected(conn));
    }
}