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
//! Public response-sending helpers for `WireframeApp`.
//!
//! This module keeps outbound response serialization and write helpers out of
//! the inbound frame-dispatch path while preserving the public API.
use bytes::{Bytes, BytesMut};
use futures::SinkExt;
use tokio::io::{self, AsyncWrite, AsyncWriteExt};
use tokio_util::codec::{Encoder, Framed, LengthDelimitedCodec};
use super::{
builder::WireframeApp,
envelope::Packet,
error::SendError,
outbound_encoding::encode_message_frame,
};
use crate::{
codec::{FrameCodec, LengthDelimitedFrameCodec},
message::EncodeWith,
serializer::Serializer,
};
impl<S, C, E, F> WireframeApp<S, C, E, F>
where
S: Serializer + Send + Sync,
C: Send + 'static,
E: Packet,
F: FrameCodec,
{
/// Serialize `msg` and write it to `stream` using the configured codec.
///
/// This helper writes directly to the stream and therefore does not run
/// the configured [`crate::hooks::WireframeProtocol`] hooks, including
/// `before_send`.
///
/// # Errors
///
/// Returns a [`SendError`] if serialization or writing fails.
pub async fn send_response<W, M>(
&self,
stream: &mut W,
msg: &M,
) -> std::result::Result<(), SendError>
where
W: AsyncWrite + Unpin,
M: EncodeWith<S>,
{
let outbound_frame = encode_message_frame(self.serializer.as_ref(), &self.codec, msg)?;
let mut encoder = self.codec.encoder();
let mut encoded_buf = BytesMut::new();
encoder
.encode(outbound_frame.frame, &mut encoded_buf)
.map_err(SendError::Io)?;
stream
.write_all(&encoded_buf)
.await
.map_err(SendError::Io)?;
stream.flush().await.map_err(SendError::Io)
}
/// Serialize `msg` and send it through an existing framed stream.
///
/// This helper bypasses the application response pipeline and does not run
/// the configured [`crate::hooks::WireframeProtocol`] hooks, including
/// `before_send`.
///
/// # Errors
///
/// Returns a [`SendError`] if serialization or sending fails.
pub async fn send_response_framed_with_codec<W, M, Cc>(
&self,
framed: &mut Framed<W, Cc>,
msg: &M,
) -> std::result::Result<(), SendError>
where
W: AsyncWrite + Unpin,
M: EncodeWith<S>,
Cc: Encoder<F::Frame, Error = io::Error>,
{
let encoded = encode_message_frame(self.serializer.as_ref(), &self.codec, msg)?;
framed.send(encoded.frame).await.map_err(SendError::Io)
}
}
impl<S, C, E> WireframeApp<S, C, E, LengthDelimitedFrameCodec>
where
S: Serializer + Send + Sync,
C: Send + 'static,
E: Packet,
{
/// Construct a length-delimited codec capped by the application's buffer
/// capacity.
#[must_use]
pub fn length_codec(&self) -> LengthDelimitedCodec {
LengthDelimitedCodec::builder()
.max_frame_length(self.codec.max_frame_length())
.new_codec()
}
/// Serialize `msg` and send it through an existing framed stream.
///
/// This helper bypasses the application response pipeline and does not run
/// the configured [`crate::hooks::WireframeProtocol`] hooks, including
/// `before_send`.
///
/// # Errors
///
/// Returns a [`SendError`] if serialization or sending fails.
pub async fn send_response_framed<W, M>(
&self,
framed: &mut Framed<W, LengthDelimitedCodec>,
msg: &M,
) -> std::result::Result<(), SendError>
where
W: AsyncWrite + Unpin,
M: EncodeWith<S>,
{
let bytes = self
.serializer
.serialize(msg)
.map_err(SendError::Serialize)?;
// `LengthDelimitedCodec` performs the length-prefix encoding in
// `framed.send`. Do not use `encode_message_frame` here: wrapping the
// payload with `self.codec` would add a second application frame around
// a path that intentionally sends the raw serialized message.
// TODO: Remove this `Vec<u8>` to `Bytes` conversion when the
// zero-copy serializer migration in
// https://github.com/leynos/wireframe/issues/538 is complete.
//
// `Bytes::from`, not `bytes.into()`: `tokio-util` 0.7.20 also encodes
// `&[u8]`, so `send` has nothing to infer the item from.
framed.send(Bytes::from(bytes)).await.map_err(SendError::Io)
}
}