|
| 1 | +[](https://crates.io/crates/message-io) |
| 2 | +[](https://www.apache.org/licenses/LICENSE-2.0.txt) |
| 3 | +[](https://crates.io/crates/message-io) |
| 4 | + |
| 5 | +# `message-io` |
| 6 | +`message-io` is an asynchronous network message library for building clients and servers. |
| 7 | +This library offers an event-based API over an abstraction network transport layer. |
| 8 | + |
| 9 | +This library can be used but it is still growing, so if you see any bug or strange behaviour, please put an issue! |
| 10 | +Of course, any contribution is welcome! |
| 11 | + |
| 12 | +## For who is this project? |
| 13 | +- People who want to make an application that needs to communicate over tcp/udp protocols. |
| 14 | +- People who want to make a multiplayer game (server and/or client). |
| 15 | +- People who don't want to deal with concurrence or socket connection problems. |
| 16 | +- People who want to push the effort in the messages among the apps, not in how to transport them. |
| 17 | + |
| 18 | +## Features |
| 19 | +- Asynchronous: internal poll event with non-blocking sockets. |
| 20 | +- Really easy API: |
| 21 | + - Abstraction from transport layer: Do not thinks about sockets, only thing about data messages. |
| 22 | + - Only two main entities: an extensible event-queue to manage all events. |
| 23 | + and a network manager to manage the connections, and send/receive data. |
| 24 | + - Forget concurrence problem: "One thread to rule them all", |
| 25 | +- Significant performance: one thread for manage all internal conenctions over a OS poll, binary serialization, small overhead over OS sockets). |
| 26 | + |
| 27 | +## Getting started |
| 28 | +Add to your `Cargo.toml` |
| 29 | +``` |
| 30 | +message-io = "0.1" |
| 31 | +``` |
| 32 | + |
| 33 | +### Documentation |
| 34 | +- [Basic concepts](#basic-concepts) |
| 35 | +- [API documentation](https://docs.rs/message-io/) |
| 36 | + |
| 37 | +### Example |
| 38 | +The following example is simplest server that reads `HelloServer` message |
| 39 | +and responses with a `HelloClient` message. |
| 40 | +It is capable to manage several client connections and listen from 2 differents ports and interfaces. |
| 41 | + |
| 42 | +```rust |
| 43 | +use message_io::event_queue::{EventQueue}; |
| 44 | +use message_io::network_manager::{NetworkManager, NetEvent, TransportProtocol}; |
| 45 | +use serde::{Serialize, Deserialize}; |
| 46 | + |
| 47 | +#[derive(Serialize, Deserialize)] |
| 48 | +enum Message { |
| 49 | + HelloServer, |
| 50 | + HelloClient, |
| 51 | + // Other messages here |
| 52 | +} |
| 53 | + |
| 54 | +enum Event { |
| 55 | + Network(NetEvent<Message>), |
| 56 | + // Other user events here |
| 57 | +} |
| 58 | + |
| 59 | +fn main() { |
| 60 | + let mut event_queue = EventQueue::new(); |
| 61 | + |
| 62 | + // Create NetworkManager, the callback will push the network event into the event queue |
| 63 | + let network_sender = event_queue.sender().clone(); |
| 64 | + let mut network = NetworkManager::new(move |net_event| network_sender.send(Event::Network(net_event))); |
| 65 | + |
| 66 | + // Listen TCP messages at ports 3001 and 3002. |
| 67 | + network.listen("127.0.0.1:3001".parse().unwrap(), TransportProtocol::Tcp).unwrap(); |
| 68 | + network.listen("0.0.0.0.0:3002".parse().unwrap(), TransportProtocol::Tcp).unwrap(); |
| 69 | + |
| 70 | + loop { |
| 71 | + match event_queue.receive() { // Read the next event or wait until have it. |
| 72 | + Event::Network(net_event) => match net_event { |
| 73 | + NetEvent::Message(message, endpoint) => match message { |
| 74 | + Message::HelloServer => network.send(endpoint, Message::HelloClient).unwrap(), |
| 75 | + _ => (), // Other messages here |
| 76 | + }, |
| 77 | + NetEvent::AddedEndpoint(_endpoint) => println!("Client connected"), |
| 78 | + NetEvent::RemovedEndpoint(_endpoint) => println!("Client disconnected"), |
| 79 | + }, |
| 80 | + // Other events here |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +## Basic concepts |
| 87 | +The library has two main pieces: |
| 88 | + |
| 89 | +- **`EventQueue`**: |
| 90 | +Is a generic and synchronized queue where all the system events are sent. |
| 91 | +The user must be read these events in its main thread in order to dispatch actions. |
| 92 | + |
| 93 | +<p align="center"> |
| 94 | + <img src="https://docs.google.com/drawings/d/e/2PACX-1vQr06OL40IWagXWHoyytUIlR1SHoahYE0Pkj6r0HmokaUMW4ojC5MV2OViFO9m-2jDqrDokPJ62oSzg/pub?w=837&h=313"/> |
| 95 | +</p> |
| 96 | + |
| 97 | +- **`NetworkManager`**: |
| 98 | +It is an abstraction layer of the transport protocols that works over non-blocking sockets. |
| 99 | +It allows to create/remove connections, send and receive messages (defined by the user). |
| 100 | + |
| 101 | +To manage the connections the `NetworkManager` offers an *`Endpoint`* that is an unique identifier of the connection |
| 102 | +that can be used to remove, send or identify input messages. |
| 103 | + |
| 104 | +<p align="center"> |
| 105 | + <img src="https://docs.google.com/drawings/d/e/2PACX-1vS3y1BKwPHjoFqtHm2pqfmvxr0JRQIzeRJim9s2UOrOIS74cGwlyqxnH4_DHVXTverziCjPzl6FtQMe/pub?w=586&h=273"/> |
| 106 | +</p> |
| 107 | + |
| 108 | +The power comes when both pieces joins together, allowing to process all actions from one thread. |
| 109 | +To reach this, the user have to connect the `NetworkManager` to the `EventQueue` sending `NetEvent` produced by the first one. |
| 110 | + |
| 111 | +<p align="center"> |
| 112 | + <img src="https://docs.google.com/drawings/d/e/2PACX-1vT6IuBVr4mLbdNfs2yZayqqUJ04PsuqG27Ce3Vdr0ZG8ItX3slISoKVxyndybaYPIS5oFZ6N4TljrKQ/pub?w=701&h=383"/> |
| 113 | +</p> |
| 114 | + |
| 115 | +## Test yourself! |
| 116 | +Clone the repository and test the `basic`example that you can found in [`examples/basic`](examples/basic): |
| 117 | + |
| 118 | +Run the server: |
| 119 | +``` |
| 120 | +cargo run --example basic server [tcp/udp] |
| 121 | +``` |
| 122 | +In other terminals, run one or more clients: |
| 123 | +``` |
| 124 | +cargo run --example basic client [tcp/udp] |
| 125 | +``` |
| 126 | +(By default, if no protocol is specified, `tcp` is used) |
| 127 | + |
0 commit comments