pub struct NetcodeInterface<const SIZE: usize> { /* private fields */ }Expand description
The interface for netcode.
Runs Tokio and iroh under the hood in a separate thread. So, methods must be called from the context of a Tokio runtime. The procedure for operation is as follows.
A new NetcodeInterface should be created on
the two players’ machines. The first, the “server,” must provide a oneshot
sender that receives a newly generated ticket. The second, the “client,”
must provide a ticket string from that server.
The server moves second and the client moves first.
If it is the user’s turn, they may:
send_turnonce- it will no longer be the user’s turn
If it is not the user’s turn, they may:
try_recv_turnrepeatedly- if it returns
Ok, it will be the user’s turn.
Turns are represented as byte buffers of a constant size. Both players’ buffer sizes must be the same.
Deviations from this procedure are undefined behavior.
Implementations§
Source§impl<const SIZE: usize> NetcodeInterface<SIZE>
impl<const SIZE: usize> NetcodeInterface<SIZE>
Sourcepub fn new(config: Config) -> Self
pub fn new(config: Config) -> Self
Create a new interface.
See the struct’s docs for invariants.
Examples found in repository?
57async fn main() -> Result<(), String> {
58 if is_client()? {
59 // create a send side & send a ping
60 let mut netcode = NetcodeInterface::new(Config::Ticket(ticket()?));
61 netcode.send_turn(b"ping");
62 println!("Client sent ping");
63
64 assert_eq!(
65 b"pong",
66 &wait_for(NetcodeInterface::try_recv_turn, &mut netcode).await
67 );
68 println!("Client recieved pong");
69
70 let mut counter = 0;
71
72 loop {
73 let bytes = [0, 0, 0, counter];
74 netcode.send_turn(&bytes);
75 println!("Client sent {bytes:?}");
76
77 assert_eq!(
78 &bytes,
79 &wait_for(NetcodeInterface::try_recv_turn, &mut netcode).await
80 );
81 println!("Client got {bytes:?} back");
82
83 counter += 1;
84 }
85 } else {
86 // create the receive side
87 let (send, recv) = oneshot::channel();
88 let mut netcode = NetcodeInterface::new(Config::TicketSender(send));
89
90 println!(
91 "hosting ping_echo. another player may join with \n\n\
92 cargo run --example ping_echo client --ticket={}",
93 recv.await.unwrap()
94 );
95
96 assert_eq!(
97 b"ping",
98 &wait_for(NetcodeInterface::try_recv_turn, &mut netcode).await
99 );
100 println!("Server received ping");
101
102 netcode.send_turn(b"pong");
103 println!("Server sent pong");
104
105 loop {
106 let bytes = wait_for(NetcodeInterface::try_recv_turn, &mut netcode).await;
107 println!("Server received: {:?}", &bytes);
108
109 netcode.send_turn(&bytes);
110 println!("Server echoed");
111
112 sleep(Duration::from_secs(1)).await;
113 }
114 }
115}Sourcepub fn send_turn(&mut self, turn: &[u8; SIZE])
pub fn send_turn(&mut self, turn: &[u8; SIZE])
Send a turn to the other player.
See the struct’s docs for invariants.
Examples found in repository?
57async fn main() -> Result<(), String> {
58 if is_client()? {
59 // create a send side & send a ping
60 let mut netcode = NetcodeInterface::new(Config::Ticket(ticket()?));
61 netcode.send_turn(b"ping");
62 println!("Client sent ping");
63
64 assert_eq!(
65 b"pong",
66 &wait_for(NetcodeInterface::try_recv_turn, &mut netcode).await
67 );
68 println!("Client recieved pong");
69
70 let mut counter = 0;
71
72 loop {
73 let bytes = [0, 0, 0, counter];
74 netcode.send_turn(&bytes);
75 println!("Client sent {bytes:?}");
76
77 assert_eq!(
78 &bytes,
79 &wait_for(NetcodeInterface::try_recv_turn, &mut netcode).await
80 );
81 println!("Client got {bytes:?} back");
82
83 counter += 1;
84 }
85 } else {
86 // create the receive side
87 let (send, recv) = oneshot::channel();
88 let mut netcode = NetcodeInterface::new(Config::TicketSender(send));
89
90 println!(
91 "hosting ping_echo. another player may join with \n\n\
92 cargo run --example ping_echo client --ticket={}",
93 recv.await.unwrap()
94 );
95
96 assert_eq!(
97 b"ping",
98 &wait_for(NetcodeInterface::try_recv_turn, &mut netcode).await
99 );
100 println!("Server received ping");
101
102 netcode.send_turn(b"pong");
103 println!("Server sent pong");
104
105 loop {
106 let bytes = wait_for(NetcodeInterface::try_recv_turn, &mut netcode).await;
107 println!("Server received: {:?}", &bytes);
108
109 netcode.send_turn(&bytes);
110 println!("Server echoed");
111
112 sleep(Duration::from_secs(1)).await;
113 }
114 }
115}