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
42
43
44
45
46
47
48
49
50
//! MinetestConnection
//!
//!
//!
use std::net::SocketAddr;

use crate::peer::peer::Peer;
use crate::wire::command::*;
use crate::wire::types::*;
use anyhow::bail;
use anyhow::Result;

/// This is owned by the driver
pub struct MinetestConnection {
    peer: Peer,
}

impl MinetestConnection {
    pub fn new(peer: Peer) -> Self {
        Self { peer: peer }
    }

    pub fn remote_addr(&self) -> SocketAddr {
        self.peer.remote_addr()
    }

    /// Send a command to the client
    pub async fn send(&self, command: ToClientCommand) -> Result<()> {
        self.peer.send(Command::ToClient(command)).await
    }

    pub async fn send_access_denied(&self, code: AccessDeniedCode) -> Result<()> {
        self.send(AccessDeniedSpec { code }.into()).await
    }

    /// Await a command from the peer
    /// Returns (channel, reliable flag, Command)
    /// Returns None when the peer is disconnected
    pub async fn recv(&mut self) -> Result<ToServerCommand> {
        match self.peer.recv().await? {
            Command::ToServer(command) => Ok(command),
            Command::ToClient(_) => {
                bail!("Received wrong direction command from SocketPeer")
            }
        }
    }
}

/// This is owned by the MinetestServer
pub struct MinetestConnectionRecord {}