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
304
305
306
307
308
309
310
311
312
313
314
// Copyright (c) 2015-2016 Ivo Wetzel

// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! **cobalt** is a networking library which provides [virtual connections
//! over UDP](http://gafferongames.com/networking-for-game-programmers/udp-vs-tcp/)
//! for both unreliable, reliable and optional in-order delivery of messages.
//!
//! It is primarily designed to be used as the basis of real-time, low latency,
//! single server / multi client systems.
//!
//! The library provides the underlying architecture required for handling and
//! maintaining virtual connections over UDP sockets and takes care of sending
//! raw messages over the established client-server connections with minimal
//! overhead.
//!
//! **cobalt** is also fully configurable and can be integrated in a variety of
//! ways, as can be seen in the examples below.
//!
//! ## Getting started
//!
//! **cobalt** supports a number of different integration strategies, the most
//! forward one being the use of so called *handlers*. The `Handler` trait acts
//! as a event proxy for both server and client events that are emitted from
//! the underlying tick loop inside the library.
//!
//!
//! ## Handler based integration
//!
//! The handler based approach suits itself especially well for the server side
//! where all updates on any connections will be performed inside a single tick
//! handler.
//!
//! ```
//! use std::collections::HashMap;
//! use cobalt::{Config, Connection, ConnectionID, Handler, Server, MessageKind};
//!
//! struct GameServer {
//!     tick: u8
//! }
//! impl Handler<Server> for GameServer {
//!
//!     fn bind(&mut self, server: &mut Server) {
//!         // Since this is a runnable doc, we'll just exit right away
//!         server.shutdown().unwrap();
//!     }
//!
//!     fn tick_connections(
//!         &mut self, _: &mut Server,
//!         connections: &mut HashMap<ConnectionID, Connection>
//!     ) {
//!
//!         for (_, conn) in connections.iter_mut() {
//!             // Receive player input
//!             for msg in conn.received() {
//!                 println!("Received message from client: {:?}", msg);
//!             }
//!         }
//!
//!         // Advance game state
//!         self.tick = self.tick.wrapping_add(1);
//!
//!         // Send state updates to players
//!         for (_, conn) in connections.iter_mut() {
//!             conn.send(MessageKind::Instant, b"Hello World".to_vec());
//!         }
//!
//!     }
//!
//!     fn shutdown(&mut self, _: &mut Server) {
//!         // Logging and things
//!     }
//!
//!     fn connection(&mut self, _: &mut Server, _: &mut Connection) {
//!         // Create Player, send MOTD etc.
//!     }
//!
//!     fn connection_lost(&mut self, _: &mut Server, _: &mut Connection) {
//!         // Remove Player
//!     }
//!
//! }
//!
//! let mut handler = GameServer {
//!     tick: 0
//! };
//! let mut server = Server::new(Config::default());
//! server.bind(&mut handler, "127.0.0.1:7156").ok();
//! ```
//!
//! The client version of the handler based approach looks almost
//! identical - except for a few methods sporting different names.
//!
//! This method is best used when separate logic / rendering threads are used.
//!
//! ```
//! use cobalt::{Config, Connection, Handler, Client, MessageKind};
//!
//! struct GameClient {
//!     tick: u8
//! };
//!
//! impl Handler<Client> for GameClient {
//!
//!     fn connect(&mut self, client: &mut Client) {
//!         // Since this is a runnable doc, we'll just exit right away
//!         client.close().unwrap();
//!     }
//!
//!     fn tick_connection(&mut self, _: &mut Client, conn: &mut Connection) {
//!
//!         // Receive incoming state from the server
//!         for msg in conn.received() {
//!             println!("Received message {:?}", msg);
//!         }
//!
//!         // Advance game state
//!         self.tick = self.tick.wrapping_add(1);
//!
//!         // Send some message to server
//!         conn.send(MessageKind::Instant, b"Hello World".to_vec());
//!
//!     }
//!
//!     fn close(&mut self, _: &mut Client) {
//!         // Exit game
//!     }
//!
//!     fn connection(&mut self, _: &mut Client, _: &mut Connection) {
//!         // Request map list and settings from server
//!     }
//!
//!     fn connection_failed(&mut self, _: &mut Client, _: &mut Connection) {
//!         // Failed to connect to the server
//!     }
//!
//!     fn connection_lost(&mut self, _: &mut Client, _: &mut Connection) {
//!         // Inform the user
//!     }
//!
//! }
//!
//! let mut handler = GameClient {
//!     tick: 0
//! };
//! let mut client = Client::new(Config::default());
//! client.connect(&mut handler, "127.0.0.1:7156").ok();
//! ```
//!
//! ## Stream based integration
//!
//! For situations where a event loop is already in place (e.g. game engines),
//! a stream based approach can be used in order to achieve easier integration
//! in single threaded scenarios.
//!
//! ```
//! use cobalt::{ClientEvent, ClientStream, Config, MessageKind};
//!
//! // Create a new stream
//! let mut stream = ClientStream::new(Config {
//!     send_rate: 15,
//!     .. Default::default()
//! });
//!
//! // Initiate the connection
//! stream.connect("127.0.0.1:5555").ok();
//!
//! // Inside of the existing event loop
//! // loop {
//!
//!     // Receive incoming messages
//!     while let Ok(event) = stream.receive() {
//!         match event {
//!             ClientEvent::Connection => println!("Connection established"),
//!             ClientEvent::ConnectionLost => println!("Connection lost"),
//!             ClientEvent::Tick => {
//!                 // Handle network related logic, advance actual in game time
//!             },
//!             ClientEvent::Message(payload) => println!("Received message: {:?}", payload),
//!             _ => {}
//!         }
//!     }
//!
//!     // Send some messages
//!     stream.send(MessageKind::Instant, b"Hello World".to_vec()).ok();
//!
//!     // Send outgoing messages
//!     stream.flush().ok();
//!
//! // }
//! ```
//!
//! ## Low level synchronous integration
//!
//! For times when even the stream based abstraction is too much, there's also
//! the option to use the underlying synchronous client implement on top of
//! which both the handler and stream based approaches are built.
//!
//! ```
//! use cobalt::{Client, Config, Handler, MessageKind};
//!
//! struct SyncHandler;
//! impl Handler<Client> for SyncHandler {}
//!
//! let mut handler = SyncHandler;
//! let mut client = Client::new(Config::default());
//! let mut state = client.connect_sync(
//!     &mut handler,
//!     "127.0.0.1:7156"
//!
//! ).unwrap();
//!
//! // Receive from the server
//! client.receive_sync(&mut handler, &mut state, 0);
//!
//! // Tick the connection
//! client.tick_sync(&mut handler, &mut state);
//!
//! // Send a message
//! state.send(MessageKind::Instant, b"Hello World".to_vec());
//!
//! // Send any pending messages via the connection
//! client.send_sync(&mut handler, &mut state);
//! ```
#![cfg_attr(feature="clippy", feature(plugin))]
#![cfg_attr(feature="clippy", plugin(clippy))]
#![deny(
    missing_docs,
    missing_debug_implementations, missing_copy_implementations,
    trivial_casts, trivial_numeric_casts,
    unsafe_code,
    unused_import_braces, unused_qualifications
)]
mod client;
mod client_stream;
mod server;
mod tick;

mod shared {
    pub mod binary_rate_limiter;
    pub mod config;
    pub mod connection;
    pub mod message_queue;
    pub mod udp_socket;
    pub mod stats;
}

mod traits {
    pub mod handler;
    pub mod rate_limiter;
    pub mod socket;
}

#[doc(inline)]
pub use shared::config::Config;

#[doc(inline)]
pub use shared::connection::{
    Connection,
    ConnectionID,
    ConnectionMap,
    ConnectionState
};

#[doc(inline)]
pub use shared::message_queue::MessageKind;

#[doc(inline)]
pub use shared::binary_rate_limiter::BinaryRateLimiter;

#[doc(inline)]
pub use shared::udp_socket::UdpSocket;

#[doc(inline)]
pub use shared::stats::Stats;

#[doc(inline)]
pub use traits::handler::Handler;

#[doc(inline)]
pub use traits::rate_limiter::RateLimiter;

#[doc(inline)]
pub use traits::socket::Socket;

#[doc(inline)]
pub use client::Client;

#[doc(inline)]
pub use client::ClientState;

#[doc(inline)]
pub use client_stream::ClientStream;

#[doc(inline)]
pub use client_stream::ClientEvent;

#[doc(inline)]
pub use server::Server;

#[cfg(test)]
mod tests {
    mod client;
    mod client_stream;
    mod connection;
    mod message_queue;
    mod server;
    mod mock;
}