minetest_protocol/services/
conn.rs

1//! MinetestConnection
2//!
3//!
4//!
5use std::net::SocketAddr;
6
7use crate::peer::peer::Peer;
8use crate::wire::command::*;
9use crate::wire::types::*;
10use anyhow::bail;
11use anyhow::Result;
12
13/// This is owned by the driver
14pub struct MinetestConnection {
15    peer: Peer,
16}
17
18impl MinetestConnection {
19    pub fn new(peer: Peer) -> Self {
20        Self { peer: peer }
21    }
22
23    pub fn remote_addr(&self) -> SocketAddr {
24        self.peer.remote_addr()
25    }
26
27    /// Send a command to the client
28    pub async fn send(&self, command: ToClientCommand) -> Result<()> {
29        self.peer.send(Command::ToClient(command)).await
30    }
31
32    pub async fn send_access_denied(&self, code: AccessDeniedCode) -> Result<()> {
33        self.send(AccessDeniedSpec { code }.into()).await
34    }
35
36    /// Await a command from the peer
37    /// Returns (channel, reliable flag, Command)
38    /// Returns None when the peer is disconnected
39    pub async fn recv(&mut self) -> Result<ToServerCommand> {
40        match self.peer.recv().await? {
41            Command::ToServer(command) => Ok(command),
42            Command::ToClient(_) => {
43                bail!("Received wrong direction command from SocketPeer")
44            }
45        }
46    }
47}
48
49/// This is owned by the MinetestServer
50pub struct MinetestConnectionRecord {}