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
use anyhow::*;
use aqueue::Actor;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::io::{AsyncWriteExt, AsyncRead, AsyncWrite};
use tokio::io::WriteHalf;
pub struct TCPPeer<T> {
pub addr: SocketAddr,
pub sender: Option<WriteHalf<T>>,
}
impl<T> TCPPeer<T>
where T: AsyncRead + AsyncWrite + Send +'static {
#[inline]
pub fn new(addr: SocketAddr, sender: WriteHalf<T>) -> Arc<Actor<TCPPeer<T>>> {
Arc::new(Actor::new(TCPPeer {
addr,
sender: Some(sender),
}))
}
#[inline]
pub fn is_disconnect(&self) -> bool {
self.sender.is_none()
}
#[inline]
pub async fn send<'a>(
&'a mut self,
buff: &'a [u8],
) -> Result<usize> {
if let Some(ref mut sender) = self.sender {
Ok(sender.write(buff).await?)
} else {
bail!("ConnectionReset")
}
}
#[inline]
pub async fn send_all<'a>(
&'a mut self,
buff: &'a [u8],
) -> Result<()> {
if let Some(ref mut sender) = self.sender {
sender.write_all(buff).await?;
Ok(())
} else {
bail!("ConnectionReset")
}
}
#[inline]
pub async fn flush(&mut self)->Result<()>{
if let Some(ref mut sender) = self.sender {
sender.flush().await?;
Ok(())
} else {
bail!("ConnectionReset")
}
}
#[inline]
pub async fn disconnect(&mut self) -> Result<()> {
if let Some(mut sender) = self.sender.take() {
Ok(sender.shutdown().await?)
} else {
Ok(())
}
}
}
#[async_trait::async_trait]
pub trait IPeer {
fn addr(&self) -> SocketAddr;
async fn is_disconnect(&self) -> Result<bool>;
async fn send<'a>(&'a self, buff: &'a [u8])
-> Result<usize>;
async fn send_all<'a>(&'a self, buff: &'a [u8])
-> Result<()>;
async fn flush(&mut self)->Result<()>;
async fn disconnect(&self) -> Result<()>;
}
#[async_trait::async_trait]
impl<T> IPeer for Actor<TCPPeer<T>>
where T: AsyncRead + AsyncWrite+Send+'static {
#[inline]
fn addr(&self) -> SocketAddr {
unsafe { self.deref_inner().addr }
}
#[inline]
async fn is_disconnect(&self) -> Result<bool> {
self.inner_call(async move |inner| Ok(inner.get().is_disconnect()))
.await
}
#[inline]
async fn send<'a>(
&'a self,
buff: &'a [u8],
) -> Result<usize> {
ensure!(!buff.is_empty(), "send buff is null");
unsafe {
self.inner_call_ref(async move |inner| inner.get_mut().send(buff).await)
.await
}
}
async fn send_all<'a>(&'a self, buff: &'a [u8]) -> Result<()> {
ensure!(!buff.is_empty(), "send buff is null");
unsafe {
self.inner_call_ref(async move |inner| inner.get_mut().send_all(buff).await)
.await
}
}
async fn flush(&mut self) -> Result<()> {
self.inner_call(async move |inner| inner.get_mut().flush().await)
.await
}
#[inline]
async fn disconnect(&self) -> Result<()> {
self.inner_call(async move |inner| inner.get_mut().disconnect().await)
.await
}
}