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
use std::net::SocketAddr;
use anyhow::bail;
use super::socket::MinetestSocket;
use crate::peer::peer::Peer;
use crate::wire::command::*;
pub struct MinetestClient {
remote_peer: Peer,
}
impl MinetestClient {
pub async fn connect(connect_to: SocketAddr) -> anyhow::Result<Self> {
let bind_addr = if connect_to.is_ipv4() {
"0.0.0.0:0".parse()?
} else {
"[::]:0".parse()?
};
let mut socket = MinetestSocket::new(bind_addr, false).await?;
let remote_peer = socket.add_peer(connect_to).await;
Ok(Self { remote_peer })
}
pub async fn recv(&mut self) -> anyhow::Result<ToClientCommand> {
match self.remote_peer.recv().await? {
Command::ToClient(cmd) => Ok(cmd),
Command::ToServer(_) => bail!("Invalid packet direction"),
}
}
pub async fn send(&mut self, command: ToServerCommand) -> anyhow::Result<()> {
self.remote_peer.send(Command::ToServer(command)).await
}
}