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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use anyhow::{bail, ensure, Result};
use aqueue::Actor;
use futures_util::stream::SplitSink;
use futures_util::SinkExt;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::net::TcpStream;
use tokio_tungstenite::tungstenite::Message;
use tokio_tungstenite::WebSocketStream;
use crate::stream::MaybeTlsStream;

pub struct WSPeer {
    addr: SocketAddr,
    sender: Option<SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, Message>>,
}

impl WSPeer {
    /// 创建一个TCP PEER
    #[inline]
    pub fn new(
        addr: SocketAddr,
        sender: SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, Message>,
    ) -> Arc<Actor<WSPeer>> {
        Arc::new(Actor::new(WSPeer {
            addr,
            sender: Some(sender),
        }))
    }
    /// 是否断线
    #[inline]
    fn is_disconnect(&self) -> bool {
        self.sender.is_none()
    }

    /// 发送
    #[inline]
    async fn send_message(&mut self, message: Message) -> Result<()> {
        if let Some(ref mut sender) = self.sender {
            sender.send(message).await?;
            Ok(())
        } else {
            bail!("ConnectionReset")
        }
    }

    /// 发送
    #[inline]
    async fn send_vec(&mut self, buff: Vec<u8>) -> Result<usize> {
        if let Some(ref mut sender) = self.sender {
            let len = buff.len();
            sender.send(Message::Binary(buff)).await?;
            Ok(len)
        } else {
            bail!("ConnectionReset")
        }
    }

    /// 发送
    #[inline]
    async fn send<'a>(&'a mut self, buff: &'a [u8]) -> Result<usize> {
        if let Some(ref mut sender) = self.sender {
            sender.send(Message::binary(buff)).await?;
            Ok(buff.len())
        } else {
            bail!("ConnectionReset")
        }
    }

    /// flush
    #[inline]
    async fn flush(&mut self) -> Result<()> {
        if let Some(ref mut sender) = self.sender {
            sender.flush().await?;
            Ok(())
        } else {
            bail!("ConnectionReset")
        }
    }

    /// 掐线
    #[inline]
    async fn disconnect(&mut self) -> Result<()> {
        if let Some(mut sender) = self.sender.take() {
            sender.close().await?;
            Ok(())
        } else {
            Ok(())
        }
    }
}

#[async_trait::async_trait]
pub trait IPeer: Sync + Send {
    fn addr(&self) -> SocketAddr;
    async fn is_disconnect(&self) -> Result<bool>;
    async fn send_message(&self, message: Message) -> Result<()>;
    async fn send(&self, buff: Vec<u8>) -> Result<usize>;
    async fn send_all(&self, buff: Vec<u8>) -> Result<()>;
    async fn send_ref(&self, buff: &[u8]) -> Result<usize>;
    async fn send_all_ref(&self, buff: &[u8]) -> Result<()>;
    async fn flush(&self) -> Result<()>;
    async fn disconnect(&self) -> Result<()>;
}

#[async_trait::async_trait]
impl IPeer for Actor<WSPeer> {
    #[inline]
    fn addr(&self) -> SocketAddr {
        unsafe { self.deref_inner().addr }
    }

    #[inline]
    async fn is_disconnect(&self) -> Result<bool> {
        self.inner_call(|inner| async move { Ok(inner.get().is_disconnect()) })
            .await
    }
    #[inline]
    async fn send_message(&self, message: Message) -> Result<()> {
        self.inner_call(|inner| async move { inner.get_mut().send_message(message).await })
            .await
    }

    #[inline]
    async fn send(&self, buff: Vec<u8>) -> Result<usize> {
        ensure!(!buff.is_empty(), "send buff is null");
        self.inner_call(|inner| async move { inner.get_mut().send_vec(buff).await })
            .await
    }
    #[inline]
    async fn send_all(&self, buff: Vec<u8>) -> Result<()> {
        ensure!(!buff.is_empty(), "send buff is null");
        self.inner_call(|inner| async move {
            inner.get_mut().send_vec(buff).await?;
            Ok(())
        })
        .await
    }
    #[inline]
    async fn send_ref(&self, buff: &[u8]) -> Result<usize> {
        self.inner_call(|inner| async move { inner.get_mut().send(buff).await })
            .await
    }

    #[inline]
    async fn send_all_ref(&self, buff: &[u8]) -> Result<()> {
        self.inner_call(|inner| async move {
            inner.get_mut().send(buff).await?;
            Ok(())
        })
        .await
    }

    #[inline]
    async fn flush(&self) -> Result<()> {
        self.inner_call(|inner| async move { inner.get_mut().flush().await })
            .await
    }

    #[inline]
    async fn disconnect(&self) -> Result<()> {
        self.inner_call(|inner| async move { inner.get_mut().disconnect().await })
            .await
    }
}