Represent Chunks as iterables of Bytes

This commit is contained in:
Tangent Wantwight 2020-09-11 19:34:27 -04:00
parent 2fb8408ebc
commit 9cc7d8064d
3 changed files with 10 additions and 18 deletions

View File

@ -92,20 +92,6 @@ impl Iterator for Chunk {
} }
} }
// impl Buf???
impl Chunk {
/// converts this chunk of data into a Bytes object, perhaps to send over the network
pub fn into_bytes(self) -> Bytes {
match self {
Chunk::Headers {bytes, ..} => bytes,
Chunk::ClusterHead(cluster_head) => cluster_head.bytes.freeze(),
Chunk::ClusterBody {bytes, ..} => bytes,
Chunk::Empty => Bytes::new(),
}
}
}
#[derive(Debug)] #[derive(Debug)]
enum ChunkerState { enum ChunkerState {
BuildingHeader(Cursor<Vec<u8>>), BuildingHeader(Cursor<Vec<u8>>),

View File

@ -20,6 +20,7 @@ use hyper::{
CONTENT_TYPE CONTENT_TYPE
} }
}; };
use stream::iter;
use warp::{ use warp::{
self, self,
Filter, Filter,
@ -42,16 +43,17 @@ use webmetro::{
ChunkTimecodeFixer, ChunkTimecodeFixer,
}, },
stream_parser::StreamEbml stream_parser::StreamEbml
}; , chunk::Chunk};
const BUFFER_LIMIT: usize = 2 * 1024 * 1024; const BUFFER_LIMIT: usize = 2 * 1024 * 1024;
fn get_stream(channel: Handle) -> impl Stream<Item = Result<Bytes, WebmetroError>> { fn get_stream(channel: Handle) -> impl Stream<Item = Result<Bytes, WebmetroError>> {
let mut timecode_fixer = ChunkTimecodeFixer::new(); let mut timecode_fixer = ChunkTimecodeFixer::new();
Listener::new(channel).map(|c| Ok(c)) Listener::new(channel).map(|c| Result::<Chunk, WebmetroError>::Ok(c))
.map_ok(move |chunk| timecode_fixer.process(chunk)) .map_ok(move |chunk| timecode_fixer.process(chunk))
.find_starting_point() .find_starting_point()
.map_ok(|webm_chunk| webm_chunk.into_bytes()) .map_ok(|webm_chunk| iter(webm_chunk).map(Result::<Bytes, WebmetroError>::Ok))
.try_flatten()
} }
fn post_stream(channel: Handle, stream: impl Stream<Item = Result<impl Buf, warp::Error>> + Unpin) -> impl Stream<Item = Result<Bytes, WebmetroError>> { fn post_stream(channel: Handle, stream: impl Stream<Item = Result<impl Buf, warp::Error>> + Unpin) -> impl Stream<Item = Result<Bytes, WebmetroError>> {

View File

@ -1,7 +1,9 @@
use bytes::Bytes;
use clap::{App, Arg, ArgMatches, SubCommand}; use clap::{App, Arg, ArgMatches, SubCommand};
use futures::prelude::*; use futures::prelude::*;
use hyper::{client::HttpConnector, Body, Client, Request}; use hyper::{client::HttpConnector, Body, Client, Request};
use std::io::{stdout, Write}; use std::io::{stdout, Write};
use stream::iter;
use super::stdin_stream; use super::stdin_stream;
use webmetro::{ use webmetro::{
@ -26,6 +28,7 @@ type BoxedChunkStream = Box<dyn Stream<Item = Result<Chunk, WebmetroError>> + Se
#[tokio::main] #[tokio::main]
pub async fn run(args: &ArgMatches) -> Result<(), WebmetroError> { pub async fn run(args: &ArgMatches) -> Result<(), WebmetroError> {
// build pipeline
let mut timecode_fixer = ChunkTimecodeFixer::new(); let mut timecode_fixer = ChunkTimecodeFixer::new();
let mut chunk_stream: BoxedChunkStream = Box::new( let mut chunk_stream: BoxedChunkStream = Box::new(
stdin_stream() stdin_stream()
@ -44,7 +47,8 @@ pub async fn run(args: &ArgMatches) -> Result<(), WebmetroError> {
} }
let chunk_stream = chunk_stream let chunk_stream = chunk_stream
.map_ok(|webm_chunk| webm_chunk.into_bytes()) .map_ok(|webm_chunk| iter(webm_chunk).map(Result::<Bytes, WebmetroError>::Ok))
.try_flatten()
.map_err(|err| { .map_err(|err| {
warn!("{}", &err); warn!("{}", &err);
err err