Rework low level UDP layer

This commit is contained in:
QuantumEntangledAndy committed 2023-08-14 11:52:18 +07:00
1 parent f23f28c12e
commit a8a306301b
3 files changed
+174 -226

No files matched your search

@@ -55,10 +55,12 @@ impl BcConnection {
let runtime = tokio::runtime::Builder::new_current_thread()
.build()
.unwrap();
runtime.block_on(
let result = runtime.block_on(
PollSender::new(thread_poll_commander)
.send_all(&mut source.map(|bc| Ok(PollCommand::Bc(Box::new(bc))))),
)
);
log::info!("PollSender Bc {:?}", result);
result
});
rx_thread.spawn(async move {
handle.await??;
@@ -166,6 +168,18 @@ enum PollCommand {
Disconnect,
}
impl std::fmt::Debug for PollCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
PollCommand::Bc(_) => f.write_str("PollCommand::Bc"),
PollCommand::AddHandler(_, _) => f.write_str("PollCommand::AddHandler"),
PollCommand::RemoveHandler(_) => f.write_str("PollCommand::RemoveHandler"),
PollCommand::AddSubscriber(_, _, _) => f.write_str("PollCommand::AddSubscriber"),
PollCommand::Disconnect => f.write_str("PollCommand::Disconnect"),
}
}
}
struct Poller {
subscribers: Subscriber,
sink: Sender<Result<Bc>>,
@@ -9,20 +9,21 @@ use futures::{
sink::{Sink, SinkExt},
stream::{IntoAsyncRead, Stream, StreamExt, TryStreamExt},
};
use log::*;
use rand::{seq::SliceRandom, thread_rng};
use std::collections::BTreeMap;
use std::collections::VecDeque;
use std::io::{Error as IoError, ErrorKind, Result as IoResult};
use std::net::SocketAddr;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use tokio::sync::mpsc::channel;
use tokio::{
net::UdpSocket,
time::{interval, Duration, Instant, Interval},
};
use tokio_stream::wrappers::ReceiverStream;
use tokio_util::compat::{Compat, FuturesAsyncReadCompatExt};
use tokio_util::sync::PollSender;
use tokio_util::{
codec::{Decoder, Encoder, Framed},
udp::UdpFramed,
@@ -82,7 +83,7 @@ impl UdpSource {
debug: bool,
) -> Result<Self> {
let bcudp_source = BcUdpSource::new_from_socket(stream, addr).await?;
let payload_source = bcudp_source.into_payload_source(client_id, camera_id);
let payload_source = bcudp_source.into_payload_source(client_id, camera_id).await;
let async_read = payload_source.into_async_read().compat();
let codex = if debug {
BcCodex::new_with_debug(Credentials::new(username, password))
@@ -160,22 +161,12 @@ impl BcUdpSource {
})
}
pub(crate) fn into_payload_source(self, client_id: i32, camera_id: i32) -> UdpPayloadSource {
UdpPayloadSource {
inner: self,
client_id,
camera_id,
packets_sent: 0,
packets_want: 0,
sent: Default::default(),
recieved: Default::default(),
state: State::Normal,
send_buffer: Default::default(),
ack_interval: interval(Duration::from_millis(10)), // Offical Client does ack every 10ms
resend_interval: interval(Duration::from_millis(500)), // Offical Client does resend every 500ms
ack_latency: Default::default(),
flush_state: FlushState::Ready,
}
pub(crate) async fn into_payload_source(
self,
client_id: i32,
camera_id: i32,
) -> UdpPayloadSource {
UdpPayloadSource::new(self, client_id, camera_id).await
}
}
@@ -268,21 +259,21 @@ impl AckLatency {
}
}
enum FlushState {
Ready,
Flush,
pub(crate) struct UdpPayloadSource {
inner_stream: ReceiverStream<IoResult<Vec<u8>>>,
inner_sink: PollSender<Vec<u8>>,
}
pub(crate) struct UdpPayloadSource {
struct UdpPayloadInner {
inner: BcUdpSource,
thread_stream: PollSender<IoResult<Vec<u8>>>,
thread_sink: ReceiverStream<Vec<u8>>,
client_id: i32,
camera_id: i32,
packets_sent: u32,
packets_want: u32,
sent: BTreeMap<u32, UdpData>,
recieved: BTreeMap<u32, Vec<u8>>,
state: State,
send_buffer: VecDeque<BcUdp>,
/// Offical Client does ack every 10ms if we don't also do this the camera
/// seems to think we have a poor connection and will abort
/// This `ack_interval` controls how ofen we do this
@@ -291,10 +282,95 @@ pub(crate) struct UdpPayloadSource {
/// This `resend_interval` controls how ofen we do this
resend_interval: Interval,
ack_latency: AckLatency,
flush_state: FlushState,
}
impl UdpPayloadInner {
fn new(
inner: BcUdpSource,
thread_stream: PollSender<IoResult<Vec<u8>>>,
thread_sink: ReceiverStream<Vec<u8>>,
client_id: i32,
camera_id: i32,
) -> Self {
Self {
inner,
thread_stream,
thread_sink,
client_id,
camera_id,
packets_sent: 0,
packets_want: 0,
sent: Default::default(),
recieved: Default::default(),
ack_interval: interval(Duration::from_millis(10)), // Offical Client does ack every 10ms
resend_interval: interval(Duration::from_millis(500)), // Offical Client does resend every 500ms
ack_latency: Default::default(),
}
}
async fn run(&mut self) -> Result<()> {
let camera_addr = self.inner.addr;
tokio::select! {
_ = self.ack_interval.tick() => {
let ack = BcUdp::Ack(self.build_send_ack());
self.inner.feed((ack, camera_addr)).await?;
Result::Ok(())
},
_ = self.resend_interval.tick() => {
for (_, resend) in self.sent.iter() {
self.inner.feed((BcUdp::Data(resend.clone()), camera_addr)).await?;
}
Ok(())
},
v = self.thread_sink.next() => {
// Incomming from application
// Outgoing on socket
let item = v.ok_or(Error::DroppedConnection)?;
for chunk in item.chunks(MTU - UDPDATA_HEADER_SIZE) {
let udp_data = UdpData {
connection_id: self.camera_id,
packet_id: self.packets_sent,
payload: chunk.to_vec(),
};
self.packets_sent += 1;
self.sent.insert(udp_data.packet_id, udp_data.clone());
self.inner.feed((BcUdp::Data(udp_data), camera_addr)).await?;
}
Ok(())
}
v = self.inner.next() => {
// Incomming from socket
// Outgoing to application
let (item, addr) = v.ok_or(Error::DroppedConnection)??;
if addr == camera_addr {
match item {
BcUdp::Discovery(_disc) => {},
BcUdp::Ack(ack) => {
if ack.connection_id == self.client_id {
self.handle_ack(ack);
}
},
BcUdp::Data(data) => {
if data.connection_id == self.client_id {
let packet_id = data.packet_id;
if packet_id >= self.packets_want {
// error!("packets_want: {}", this.packets_want);
self.recieved.insert(packet_id, data.payload);
}
}
},
}
}
Ok(())
}
}?;
while let Some(payload) = self.recieved.remove(&self.packets_want) {
self.packets_want += 1;
self.thread_stream.send(Ok(payload)).await?;
}
self.inner.flush().await?;
Ok(())
}
impl UdpPayloadSource {
fn build_send_ack(&self) -> UdpAck {
if self.packets_want > 0 {
let mut first_missing: u32 = self.packets_want;
@@ -346,22 +422,39 @@ impl UdpPayloadSource {
}
self.ack_latency.feed_ack();
}
}
impl UdpPayloadSource {
async fn new(inner: BcUdpSource, client_id: i32, camera_id: i32) -> Self {
let (inner_sink, thread_sink) = channel(100);
let (thread_stream, inner_stream) = channel(100);
fn maintanence(&mut self, cx: &mut Context<'_>) {
// Check for periodic resends
// log::info!("Maintaince");
if self.resend_interval.poll_tick(cx).is_ready() {
for (_, resend) in self.sent.iter() {
self.send_buffer.push_back(BcUdp::Data(resend.clone()));
let mut payload_inner = UdpPayloadInner::new(
inner,
PollSender::new(thread_stream),
ReceiverStream::new(thread_sink),
client_id,
camera_id,
);
tokio::task::spawn(async move {
loop {
match payload_inner.run().await {
Ok(()) => {}
Err(e) => {
// log::error!("payload_inner: {:?}", e);
// Pass error up
let _ = payload_inner
.thread_stream
.send(Err(IoError::new(ErrorKind::Other, e.clone())))
.await;
return Result::<()>::Err(e);
}
}
}
}
if self.ack_interval.poll_tick(cx).is_ready()
// && self.packets_want > 0
{
// log::info!("Pushing Ack");
let ack = BcUdp::Ack(self.build_send_ack());
self.send_buffer.push_back(ack);
self.state = State::Flushing;
});
UdpPayloadSource {
inner_stream: ReceiverStream::new(inner_stream),
inner_sink: PollSender::new(inner_sink),
}
}
}
@@ -369,113 +462,16 @@ impl UdpPayloadSource {
impl Stream for UdpPayloadSource {
type Item = IoResult<Vec<u8>>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let camera_addr = self.inner.addr;
let this = self.get_mut();
// log::info!("this.state: {:?}", this.state);
match this.state {
State::Normal => {
// this.state = State::YieldNow;
// Handle resend events
this.maintanence(cx);
// Data ready to go
if let Some(payload) = this.recieved.remove(&this.packets_want) {
this.packets_want += 1;
// error!("packets_want: {}", this.packets_want);
return Poll::Ready(Some(Ok(payload)));
}
// Normal behaviors
match this.inner.poll_next_unpin(cx) {
Poll::Ready(Some(Ok((
BcUdp::Data(UdpData {
connection_id,
packet_id,
payload,
}),
addr,
)))) if connection_id == this.client_id
&& addr == camera_addr
&& packet_id >= this.packets_want =>
{
if packet_id == this.packets_want {
this.packets_want += 1;
// error!("packets_want: {}", this.packets_want);
let ack = BcUdp::Ack(this.build_send_ack());
this.send_buffer.push_back(ack);
return Poll::Ready(Some(Ok(payload)));
} else {
this.recieved.insert(packet_id, payload);
}
}
Poll::Ready(Some(Ok((
BcUdp::Data(UdpData {
connection_id,
packet_id,
..
}),
addr,
)))) if connection_id == this.client_id
&& addr == camera_addr
&& packet_id < this.packets_want =>
{
trace!("UdpPayloadSource.RecievedPacket: Old data: {}", packet_id);
}
Poll::Ready(Some(Ok((
BcUdp::Ack(ack @ UdpAck { connection_id, .. }),
addr,
)))) if connection_id == this.client_id && addr == camera_addr => {
this.handle_ack(ack);
// Rather then immediatly flush wait for the next call
// this.state = State::Flushing;
}
Poll::Ready(Some(Err(e))) => {
return Poll::Ready(Some(Err(IoError::new(ErrorKind::Other, e))));
}
Poll::Ready(None) => {
return Poll::Ready(None);
}
Poll::Pending => {
return Poll::Pending;
}
Poll::Ready(Some(Ok((bcudp, addr)))) => {
trace!(
"UdpPayloadSource.RecievedPacker: UnexpectedData {:?} from {}",
bcudp,
addr
);
} // _ => {
// trace!("UDPSource.RecievedPacker: Other?");
// // Repeat/unintersting packet
// }
}
}
State::Flushing => {
// log::info!("Fushing: {}", this.send_buffer.len());
if this.send_buffer.is_empty() {
this.state = State::Normal;
} else {
match this.poll_flush_unpin(cx) {
Poll::Ready(Ok(())) => {
this.state = State::Normal;
}
Poll::Ready(Err(e)) => {
return Poll::Ready(Some(Err(e)));
}
Poll::Pending => {
return Poll::Pending;
}
}
}
}
State::Closed => {
return Poll::Ready(Some(Err(IoError::from(ErrorKind::ConnectionAborted))));
}
State::YieldNow => {
this.state = State::Normal;
}
delegate! {
to Pin::new(&mut self.inner_stream) {
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>;
}
}
delegate! {
to self.inner_stream {
fn size_hint(&self) -> (usize, Option<usize>);
}
cx.waker().wake_by_ref();
Poll::Pending
}
}
@@ -486,97 +482,35 @@ impl Sink<Vec<u8>> for UdpPayloadSource {
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<std::result::Result<(), Self::Error>> {
let this = self.get_mut();
this.inner
self.get_mut()
.inner_sink
.poll_ready_unpin(cx)
.map_err(|e| IoError::new(ErrorKind::Other, e))
}
fn start_send(self: Pin<&mut Self>, item: Vec<u8>) -> std::result::Result<(), Self::Error> {
let this = self.get_mut();
for chunk in item.chunks(MTU - UDPDATA_HEADER_SIZE) {
let udp_data = UdpData {
connection_id: this.camera_id,
packet_id: this.packets_sent,
payload: chunk.to_vec(),
};
this.packets_sent += 1;
this.send_buffer.push_back(BcUdp::Data(udp_data));
}
// Push through whats on the buffer
for first in this.send_buffer.drain(..) {
if let BcUdp::Data(data) = &first {
let id = data.packet_id;
this.sent.insert(id, data.clone());
}
let addr = this.inner.addr;
Pin::new(&mut this.inner)
.start_send((first, addr))
.map_err(|e| IoError::new(ErrorKind::Other, e))?;
}
Ok(())
self.get_mut()
.inner_sink
.start_send_unpin(item)
.map_err(|e| IoError::new(ErrorKind::Other, e))
}
fn poll_flush(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<std::result::Result<(), Self::Error>> {
// log::info!("poll_flush");
let this = self.get_mut();
let res = match this.flush_state {
FlushState::Ready => match this.poll_ready_unpin(cx) {
Poll::Ready(Ok(())) => {
for first in this.send_buffer.drain(..) {
if let BcUdp::Data(data) = &first {
let id = data.packet_id;
this.sent.insert(id, data.clone());
}
let addr = this.inner.addr;
Pin::new(&mut this.inner)
.start_send((first, addr))
.map_err(|e| IoError::new(ErrorKind::Other, e))?;
}
this.flush_state = FlushState::Flush;
cx.waker().wake_by_ref();
Poll::Pending
}
poll => poll,
},
FlushState::Flush => {
match this
.inner
.poll_flush_unpin(cx)
.map_err(|e| IoError::new(ErrorKind::Other, e))
{
Poll::Ready(Ok(())) => {
this.flush_state = FlushState::Ready;
Poll::Ready(Ok(()))
}
poll => poll,
}
}
};
log::trace!("poll_flush end: {:?}", res);
res
self.get_mut()
.inner_sink
.poll_flush_unpin(cx)
.map_err(|e| IoError::new(ErrorKind::Other, e))
}
fn poll_close(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<std::result::Result<(), Self::Error>> {
let this = self.get_mut();
if let State::Closed = this.state {
return Poll::Ready(Ok(()));
}
match this.poll_flush_unpin(cx) {
Poll::Ready(Ok(())) => {
this.state = State::Closed;
this.inner
.poll_close_unpin(cx)
.map_err(|e| IoError::new(ErrorKind::Other, e))
}
poll => poll,
}
self.get_mut()
.inner_sink
.poll_close_unpin(cx)
.map_err(|e| IoError::new(ErrorKind::Other, e))
}
}
+1 -1
View File
@@ -17,7 +17,7 @@ pub(crate) fn timeout<F>(future: F) -> tokio::time::Timeout<F>
where
F: std::future::Future,
{
tokio::time::timeout(tokio::time::Duration::from_secs(5), future)
tokio::time::timeout(tokio::time::Duration::from_secs(15), future)
}
pub(crate) enum AddressOrUid {