Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: resurrect UdpFramed #3044

Merged
merged 5 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion tokio-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ categories = ["asynchronous"]
default = []

# Shorthand for enabling everything
full = ["codec", "compat", "io", "time"]
full = ["codec", "compat", "io", "time", "net"]

net = ["tokio/net"]
compat = ["futures-io",]
codec = ["tokio/stream"]
time = ["tokio/time","slab"]
Expand Down
8 changes: 3 additions & 5 deletions tokio-util/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@ macro_rules! cfg_compat {
}
}

/*
macro_rules! cfg_udp {
macro_rules! cfg_net {
($($item:item)*) => {
$(
#[cfg(all(feature = "udp", feature = "codec"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "udp", feature = "codec"))))]
#[cfg(all(feature = "net", feature = "codec"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "net", feature = "codec"))))]
Darksonn marked this conversation as resolved.
Show resolved Hide resolved
$item
)*
}
}
*/

macro_rules! cfg_io {
($($item:item)*) => {
Expand Down
7 changes: 1 addition & 6 deletions tokio-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,9 @@ cfg_codec! {
pub mod codec;
}

/*
Disabled due to removal of poll_ functions on UdpSocket.

See https://github.com/tokio-rs/tokio/issues/2830
cfg_udp! {
cfg_net! {
pub mod udp;
}
*/

cfg_compat! {
pub mod compat;
Expand Down
24 changes: 11 additions & 13 deletions tokio-util/src/udp/frame.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use crate::codec::{Decoder, Encoder};

use tokio::{net::UdpSocket, stream::Stream};
use tokio::{io::ReadBuf, net::UdpSocket, stream::Stream};

use bytes::{BufMut, BytesMut};
use futures_core::ready;
use futures_sink::Sink;
use std::io;
use std::mem::MaybeUninit;
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use std::pin::Pin;
use std::task::{Context, Poll};
use std::{io, mem::MaybeUninit};

/// A unified `Stream` and `Sink` interface to an underlying `UdpSocket`, using
/// the `Encoder` and `Decoder` traits to encode and decode frames.
Expand Down Expand Up @@ -41,6 +40,9 @@ pub struct UdpFramed<C> {
current_addr: Option<SocketAddr>,
}

const INITIAL_RD_CAPACITY: usize = 64 * 1024;
const INITIAL_WR_CAPACITY: usize = 8 * 1024;

impl<C: Decoder + Unpin> Stream for UdpFramed<C> {
type Item = Result<(C::Item, SocketAddr), C::Error>;

Expand Down Expand Up @@ -69,13 +71,12 @@ impl<C: Decoder + Unpin> Stream for UdpFramed<C> {
let addr = unsafe {
// Convert `&mut [MaybeUnit<u8>]` to `&mut [u8]` because we will be
// writing to it via `poll_recv_from` and therefore initializing the memory.
let buf: &mut [u8] =
&mut *(pin.rd.bytes_mut() as *mut [MaybeUninit<u8>] as *mut [u8]);

let res = ready!(Pin::new(&mut pin.socket).poll_recv_from(cx, buf));
let buf = &mut *(pin.rd.bytes_mut() as *mut _ as *mut [MaybeUninit<u8>]);
let mut read = ReadBuf::uninit(buf);
let res = ready!(Pin::new(&mut pin.socket).poll_recv_from(cx, &mut read));
Darksonn marked this conversation as resolved.
Show resolved Hide resolved

let (n, addr) = res?;
pin.rd.advance_mut(n);
let addr = res?;
pin.rd.advance_mut(read.filled().len());
addr
};

Expand Down Expand Up @@ -148,15 +149,12 @@ impl<I, C: Encoder<I> + Unpin> Sink<(I, SocketAddr)> for UdpFramed<C> {
}
}

const INITIAL_RD_CAPACITY: usize = 64 * 1024;
const INITIAL_WR_CAPACITY: usize = 8 * 1024;

impl<C> UdpFramed<C> {
/// Create a new `UdpFramed` backed by the given socket and codec.
///
/// See struct level documentation for more details.
pub fn new(socket: UdpSocket, codec: C) -> UdpFramed<C> {
UdpFramed {
Self {
socket,
codec,
out_addr: SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), 0)),
Expand Down
2 changes: 1 addition & 1 deletion tokio-util/src/udp/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! UDP framing

mod frame;
pub use self::frame::UdpFramed;
pub use frame::UdpFramed;
2 changes: 0 additions & 2 deletions tokio-util/tests/udp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/*
#![warn(rust_2018_idioms)]

use tokio::{net::UdpSocket, stream::StreamExt};
Expand Down Expand Up @@ -101,4 +100,3 @@ async fn send_framed_lines_codec() -> std::io::Result<()> {

Ok(())
}
*/