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
use std::io::{Read, Write};
use bytes::{Buf, BytesMut};
use crate::{
codec::{
FrameCodec, FrameConfig, FrameReadState, FrameRecv, FrameSend, FrameWriteState, Split,
},
errors::WsError,
frame::OpCode,
protocol::standard_handshake_resp_check,
Message,
};
macro_rules! impl_recv {
() => {
pub fn receive(&mut self) -> Result<Message<BytesMut>, WsError> {
let frame = self.frame_codec.receive()?;
let (header, mut data) = frame.parts();
let op_code = header.opcode();
let close_code = if op_code == OpCode::Close {
Some(data.get_u16())
} else {
None
};
Ok(Message {
code: op_code,
data,
close_code,
})
}
};
}
macro_rules! impl_send {
() => {
pub fn send<'a, T: Into<Message<&'a [u8]>>>(&mut self, msg: T) -> Result<(), WsError> {
let msg: Message<&'a [u8]> = msg.into();
if let Some(close_code) = msg.close_code {
if msg.code == OpCode::Close {
let mut data = close_code.to_be_bytes().to_vec();
data.extend_from_slice(msg.data);
self.frame_codec.send(msg.code, &data)
} else {
self.frame_codec.send(msg.code, msg.data)
}
} else {
self.frame_codec.send(msg.code, msg.data)
}
}
pub fn flush(&mut self) -> Result<(), WsError> {
self.frame_codec.flush()
}
};
}
pub struct BytesRecv<S: Read> {
frame_codec: FrameRecv<S>,
}
impl<S: Read> BytesRecv<S> {
pub fn new(stream: S, state: FrameReadState) -> Self {
Self {
frame_codec: FrameRecv::new(stream, state),
}
}
impl_recv! {}
}
pub struct BytesSend<S: Write> {
frame_codec: FrameSend<S>,
}
impl<S: Write> BytesSend<S> {
pub fn new(stream: S, state: FrameWriteState) -> Self {
Self {
frame_codec: FrameSend::new(stream, state),
}
}
impl_send! {}
}
pub struct BytesCodec<S: Read + Write> {
frame_codec: FrameCodec<S>,
}
impl<S: Read + Write> BytesCodec<S> {
pub fn new(stream: S) -> Self {
Self {
frame_codec: FrameCodec::new(stream),
}
}
pub fn new_with(stream: S, config: FrameConfig) -> Self {
Self {
frame_codec: FrameCodec::new_with(stream, config),
}
}
pub fn factory(_req: http::Request<()>, stream: S) -> Result<Self, WsError> {
let config = FrameConfig {
mask_send_frame: false,
..Default::default()
};
Ok(Self::new_with(stream, config))
}
pub fn check_fn(key: String, resp: http::Response<()>, stream: S) -> Result<Self, WsError> {
standard_handshake_resp_check(key.as_bytes(), &resp)?;
Ok(Self::new_with(stream, FrameConfig::default()))
}
pub fn stream_mut(&mut self) -> &mut S {
self.frame_codec.stream_mut()
}
impl_recv! {}
impl_send! {}
}
impl<R, W, S> BytesCodec<S>
where
R: Read,
W: Write,
S: Read + Write + Split<R = R, W = W>,
{
pub fn split(self) -> (BytesRecv<R>, BytesSend<W>) {
let FrameCodec {
stream,
read_state,
write_state,
} = self.frame_codec;
let (read, write) = stream.split();
(
BytesRecv::new(read, read_state),
BytesSend::new(write, write_state),
)
}
}