Skip to content

Commit

Permalink
Changed README Examples (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimonPost authored Apr 12, 2019
1 parent e4ae10e commit d2aa41a
Showing 1 changed file with 35 additions and 39 deletions.
74 changes: 35 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ These are the features this crate provides:
- UDP-based Protocol
- Connection Tracking
- Automatic Fragmentation
- Packets Types: Unreliable and Sequenced, Reliable and Unordered, Sequenced and Ordered.
- Reliability Options: Unreliable and Reliable
- Arranging Options: Sequenced, Unordered, and Ordered.
- Arranging Streams
- Protocol Versioning
- RTT Estimation
Expand All @@ -66,7 +67,7 @@ Add the laminar package to your `Cargo.toml` file.

```toml
[dependencies]
laminar = "0.1"
laminar = "0.2"
```

### Useful Links
Expand All @@ -85,61 +86,56 @@ This is an example of how to use the UDP API.
_Send packets_

```rust
use laminar::{DeliveryMethod, Packet};
use laminar::net::{UdpSocket, NetworkConfig};
use laminar::{Socket, Packet};

// Create the necessary config, you can edit it or just use the default.
let config = NetworkConfig::default();

// Setup an udp socket and bind it to the client address.
let mut udp_socket = UdpSocket::bind("127.0.0.1:12346", config).unwrap();
// create the socket
let (mut socket, packet_sender, _) = Socket::bind("127.0.0.1:12345")?;

// our data
let bytes = vec![...];

// Create a packet that can be send with the given destination and raw data.
let packet = Packet::new(destination, bytes, DeliveryMethod::Unreliable);

// Or we could also use the function syntax for more clarity:
let packet = Packet::unreliable(destination, bytes);
let packet = Packet::reliable_unordered(destination, bytes);

// Send the packet to the endpoint we earlier placed into the packet.
udp_socket.send(packet);
// You can create packets with different reliabilities
let unreliable = Packet::unreliable(destination, bytes);
let reliable = Packet::reliable_unordered(destination, bytes);

// We can specify on which stream and how to order our packets, checkout our book and documentation for more information
let unreliable = Packet::unreliable_sequenced(destination, bytes, Some(1));
let reliable_sequenced = Packet::reliable_sequenced(destination, bytes, Some(2));
let reliable_ordered = Packet::reliable_ordered(destination, bytes, Some(3));

// send the created packets
packet_sender.send(unreliable_sequenced).unwrap();
packet_sender.send(reliable).unwrap();
packet_sender.send(unreliable_sequenced).unwrap();
packet_sender.send(reliable_sequenced).unwrap();
packet_sender.send(reliable_ordered).unwrap();
```

_Receive Packets_

```rust
use laminar::net::{UdpSocket, NetworkConfig};
use std::net::SocketAddr;
// Create the necessarily config, you can edit it or just use the default.
let config = NetworkConfig::default();
use laminar::{SocketEvent, Socket};

// Setup an udp socket and bind it to the client address.
let mut udp_socket = UdpSocket::bind("127.0.0.1:12345", config).unwrap();
// create the socket
let (mut socket, _, packet_receiver) = Socket::bind("127.0.0.1:12346")?;

// Start receiving (blocks the current thread), use `udp_socket.set_nonblocking()` for not blocking the current thread.
let result = udp_socket.recv();
// wait until a socket event occurs
let result = packet_receiver.recv();

match result {
Ok(Some(packet)) => {
let endpoint: SocketAddr = packet.addr();
let received_data: &[u8] = packet.payload();

// You can deserialize your bytes here into the data you have passed it when sending.

println!("Received packet from: {:?} with length {}", endpoint, received_data.len());
}
Ok(None) => {
println!("This could happen when we have not received all the data from this packet yet");
Ok(socket_event) => {
match socket_event {
SocketEvent::Packet(packet) => {
let endpoint: SocketAddr = packet.addr();
let received_data: &[u8] = packet.payload();
},
SocketEvent::Connect(connect_event) => { /* a client connected */ },
SocketEvent::Timeout(timeout_event) => { /* a client timed out */},
}
}
Err(e) => {
// We get an error if something went wrong, like the address is already in use.
println!("Something went wrong when receiving, error: {:?}", e);
}
}

```

## Authors
Expand Down

0 comments on commit d2aa41a

Please sign in to comment.