pub struct FrameWriter<W> { /* private fields */ }Expand description
A writer of frames, object chunks, and pull bodies on a stream.
The writer enters the body state when it writes an ObjectHeader, or a
GetReply whose found is true.
write_object_dataandend_objectserve both body kinds.abandon_objectabandons an object of the push.abandon_bodyabandons a body of the pull.
The writer holds no object bytes and does not flush. The caller flushes
with flush, or wraps the stream in a buffered writer.
A frame is one write, and a chunk is two: the length and the data. A
caller wraps a raw socket or pipe in a buffered writer.
The futures of the writer are not cancel-safe. A call that is dropped before it completes can leave part of a frame or a chunk written. After a canceled call, the caller must drop the writer.
Implementations§
Source§impl<W: AsyncWrite + Unpin> FrameWriter<W>
impl<W: AsyncWrite + Unpin> FrameWriter<W>
Sourcepub fn new(inner: W) -> Self
pub fn new(inner: W) -> Self
Creates a writer with the limit MIN_FRAME_LIMIT.
Examples found in repository?
37async fn scripted_replies() -> io::Result<Vec<u8>> {
38 let msgs = [
39 Message::HelloReply(HelloReply {
40 version: 1,
41 mode: "archive".into(),
42 collection_id: None,
43 max_frame: MIN_FRAME_LIMIT,
44 max_have: 16_384,
45 encodings: vec![Encoding::Raw, Encoding::Deflate],
46 parallel_uploads: 1,
47 refs: vec![RefState {
48 name: "main".into(),
49 commit: None,
50 }],
51 }),
52 Message::ObjectsReply(ObjectsReply {
53 objects: 0,
54 payload_bytes: 0,
55 }),
56 ];
57 let mut w = FrameWriter::new(Vec::new());
58 for msg in &msgs {
59 w.write_message(msg).await.map_err(io::Error::other)?;
60 }
61 Ok(w.into_inner())
62}Sourcepub fn set_limit(&mut self, limit: u32)
pub fn set_limit(&mut self, limit: u32)
Sets the frame and chunk limit to the max-frame that the peer
announced.
The value is clamped to MIN_FRAME_LIMIT..=MAX_FRAME_LIMIT.
HelloReply::max_frame holds the
announced value. The writer applies the limit to each frame and each
chunk that it writes after the call.
Sourcepub async fn write_message(&mut self, msg: &Message) -> Result<()>
pub async fn write_message(&mut self, msg: &Message) -> Result<()>
Writes one message as a frame.
After an ObjectHeader, and after a GetReply whose found is
true, the writer is in the body state.
§Errors
If the call returns Error::Protocol or Error::LimitExceeded,
it writes nothing.
Error::Protocolif the writer is inside an object or a pull body.Error::Protocolifmsgholds a value that its kind cannot carry, for example aGetReplywhosefoundisfalsewith a length.Error::LimitExceededif the frame is over the limit.Error::Ioif a write to the stream fails.
Examples found in repository?
37async fn scripted_replies() -> io::Result<Vec<u8>> {
38 let msgs = [
39 Message::HelloReply(HelloReply {
40 version: 1,
41 mode: "archive".into(),
42 collection_id: None,
43 max_frame: MIN_FRAME_LIMIT,
44 max_have: 16_384,
45 encodings: vec![Encoding::Raw, Encoding::Deflate],
46 parallel_uploads: 1,
47 refs: vec![RefState {
48 name: "main".into(),
49 commit: None,
50 }],
51 }),
52 Message::ObjectsReply(ObjectsReply {
53 objects: 0,
54 payload_bytes: 0,
55 }),
56 ];
57 let mut w = FrameWriter::new(Vec::new());
58 for msg in &msgs {
59 w.write_message(msg).await.map_err(io::Error::other)?;
60 }
61 Ok(w.into_inner())
62}Sourcepub async fn write_object_data(&mut self, data: &[u8]) -> Result<()>
pub async fn write_object_data(&mut self, data: &[u8]) -> Result<()>
Writes bytes of the current object or pull body as chunks.
Each chunk holds at most the limit. Inside a body, an empty data
writes nothing. Each call with data writes one chunk or more, and each
chunk costs 4 bytes of framing and one read call on the peer. A caller
gives the object bytes in pieces of 64 KiB or more.
§Errors
Error::Protocolif the writer is not inside a body.Error::Ioif a write to the stream fails.
Sourcepub async fn end_object(&mut self) -> Result<()>
pub async fn end_object(&mut self) -> Result<()>
Ends the current object or pull body with a chunk of length 0.
§Errors
Error::Protocolif the writer is not inside a body.Error::Ioif a write to the stream fails.
Sourcepub async fn abandon_object(&mut self) -> Result<()>
pub async fn abandon_object(&mut self) -> Result<()>
Abandons the current object of the push.
The call writes the marker ABANDON and then an Abort message.
§Errors
Error::Protocolif the writer is not inside an object of the push, for example if it is inside a body of the pull.Error::Ioif a write to the stream fails.
Sourcepub async fn abandon_body(&mut self, error: &ErrorMessage) -> Result<()>
pub async fn abandon_body(&mut self, error: &ErrorMessage) -> Result<()>
Abandons the current body of the pull.
The call writes the marker ABANDON and then error as an Error
message. The caller gives the code, for example internal for a
failed read of the file.
§Errors
Error::Protocolif the writer is not inside a body of the pull, for example if it is inside an object of the push.Error::Protocoliferrordoes not encode. The call writes nothing.Error::LimitExceededif the frame oferroris over the limit. The call writes nothing.Error::Ioif a write to the stream fails.
Sourcepub fn into_inner(self) -> W
pub fn into_inner(self) -> W
Returns the underlying stream.
Examples found in repository?
37async fn scripted_replies() -> io::Result<Vec<u8>> {
38 let msgs = [
39 Message::HelloReply(HelloReply {
40 version: 1,
41 mode: "archive".into(),
42 collection_id: None,
43 max_frame: MIN_FRAME_LIMIT,
44 max_have: 16_384,
45 encodings: vec![Encoding::Raw, Encoding::Deflate],
46 parallel_uploads: 1,
47 refs: vec![RefState {
48 name: "main".into(),
49 commit: None,
50 }],
51 }),
52 Message::ObjectsReply(ObjectsReply {
53 objects: 0,
54 payload_bytes: 0,
55 }),
56 ];
57 let mut w = FrameWriter::new(Vec::new());
58 for msg in &msgs {
59 w.write_message(msg).await.map_err(io::Error::other)?;
60 }
61 Ok(w.into_inner())
62}