Merge branch 'master' of https://github.com/QuantumEntangledAndy/neolink into fix/ptz-stop

This commit is contained in:
QuantumEntangledAndy committed 2023-05-13 16:52:40 +07:00
commit 6e334392da
25 files changed
+2305 -746

No files matched your search

+17 -6
View File
@@ -7,7 +7,7 @@ use crate::bc::model::*;
use crate::bc::xml::*;
use crate::{Credentials, Error, Result};
use bytes::BytesMut;
use std::io;
use nom::AsBytes;
use tokio_util::codec::{Decoder, Encoder};
pub(crate) struct BcCodex {
@@ -54,14 +54,25 @@ impl Decoder for BcCodex {
if buf.is_empty() {
Ok(None)
} else {
Err(io::Error::new(
io::ErrorKind::Other,
format!("bytes remaining on BC stream: {:X?}", buf),
)
.into())
log::debug!(
"bytes remaining on BC stream: {:X?}",
buf.as_bytes().chunks(25).next()
);
// Right after this we seem to get an issue with the camera dropping us
// Needs probing
// F0, DE, BC, A, 3, 0, 0, 0, 88, 6, 0, 0, 0, 1, 4, 0, C8, 0, 0, 0, 0, 0, 0, 0, 30, 31, 64, 63, 48,
// 32, 36, 34, 6A, 6, 0, 0, 0, 0, 0, 0, D8, F5, C7, 86, 56, 0, 0, 0, 0, 0, 0, 1, 21, 9A, FC, 22, 7F, 6, AE, F6, 15, FF, E5, 71, 4, 2F, 24, 61, 15, 96, F0, BF, 83, DE, 10, BE, B4, 2E, 3
// 9, 76, 56, 92, 7E, 48, 79, 20, 9A, DC, 1B, BB, AC, 22, 60, 5C, 72, B5, 3D, 8, E0, 34, 43, 3F, 2E, A7, 81, A8, 11, 75, 7F, 58, 3E, 8, 54, 91, 43, 21, EC, 6B, D6, 1A, D5, CB, D5, 6C,
// 8C, 2E, 6E, A3, 51, C3, A4, F0, CF, 2B, 61, 81, D0, 1C, A1, 76, EE, BF, 7A, D5, D8, D1, C4, D, B0, 45, EE, 3E, 93, 9A, CE, 5F, AB, 75, 55, AC, 9D, 66, DE, 23, 6D, 5F, 25, 57, DA, F5
//, E, 7F, 8D, 30, A7, 66, C4, 60, 76, 41, D0, 6A, 23, E, A9, C5, 51, EE, F6, DD, 19, E7, A8, 96, 9F, 2B, AF, 31, 90, 9D, FC, BE
Ok(None)
}
}
}
// match self.decode(buf)? {
// Some(frame) => Ok(Some(frame)),
// None => Ok(None),
// }
}
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>> {
+155 -110
View File
@@ -5,10 +5,10 @@ use futures::stream::{Stream, StreamExt};
use log::*;
use std::collections::btree_map::Entry;
use std::collections::BTreeMap;
use std::sync::Arc;
use tokio::sync::mpsc::{channel, Sender};
use tokio::task::yield_now;
use tokio_stream::wrappers::ReceiverStream;
use tokio_util::sync::PollSender;
use tokio::{sync::RwLock, task::JoinSet};
@@ -16,9 +16,9 @@ type MsgHandler = Box<dyn Fn(&Bc) -> Option<Bc> + Send + Sync>;
#[derive(Default)]
struct Subscriber {
/// Subscribers based on their Num
num: RwLock<BTreeMap<u16, Sender<Result<Bc>>>>,
num: BTreeMap<u16, Sender<Result<Bc>>>,
/// Subscribers based on their ID
id: RwLock<BTreeMap<u32, MsgHandler>>,
id: BTreeMap<u32, MsgHandler>,
}
pub(crate) type BcConnSink = Box<dyn Sink<Bc, Error = Error> + Send + Sync + Unpin>;
@@ -31,35 +31,48 @@ pub(crate) type BcConnSource = Box<dyn Stream<Item = Result<Bc>> + Send + Sync +
/// There can be only one subscriber per kind of message at a time.
pub struct BcConnection {
sink: Sender<Result<Bc>>,
subscribers: Arc<Subscriber>,
#[allow(dead_code)] // Not dead we just need to hold a reference to keep it alive
poll_commander: Sender<PollCommand>,
rx_thread: RwLock<JoinSet<Result<()>>>,
}
impl BcConnection {
pub async fn new(mut sink: BcConnSink, mut source: BcConnSource) -> Result<BcConnection> {
let subscribers: Arc<Subscriber> = Default::default();
let subs = subscribers.clone();
pub async fn new(mut sink: BcConnSink, source: BcConnSource) -> Result<BcConnection> {
let (sinker, sinker_rx) = channel::<Result<Bc>>(100);
let mut rx_thread = JoinSet::new();
let sink_thread = sinker.clone();
let (poll_commander, poll_commanded) = channel(200);
let mut poller = Poller {
subscribers: Default::default(),
sink: sinker.clone(),
reciever: ReceiverStream::new(poll_commanded),
};
let mut rx_thread = JoinSet::<Result<()>>::new();
let thread_poll_commander = poll_commander.clone();
let handle = tokio::task::spawn_blocking(move || {
let runtime = tokio::runtime::Builder::new_current_thread()
.build()
.unwrap();
runtime.block_on(
PollSender::new(thread_poll_commander)
.send_all(&mut source.map(|bc| Ok(PollCommand::Bc(Box::new(bc))))),
)
});
rx_thread.spawn(async move {
loop {
yield_now().await; // Give other tasks a chance to run before pulling next packet
let packet = source.next().await;
let bc = match packet {
Some(res) => res,
None => continue,
};
Self::poll(bc, &subs, &sink_thread).await?
}
handle.await??;
Ok(())
});
rx_thread.spawn(async move { sink.send_all(&mut ReceiverStream::new(sinker_rx)).await });
rx_thread.spawn(async move {
loop {
poller.run().await?;
}
});
Ok(BcConnection {
sink: sinker,
subscribers,
poll_commander,
rx_thread: RwLock::new(rx_thread),
})
}
@@ -71,18 +84,9 @@ impl BcConnection {
pub async fn subscribe(&self, msg_num: u16) -> Result<BcSubscription> {
let (tx, rx) = channel(100);
match self.subscribers.num.write().await.entry(msg_num) {
Entry::Vacant(vac_entry) => {
vac_entry.insert(tx);
}
Entry::Occupied(mut occ_entry) => {
if occ_entry.get().is_closed() {
occ_entry.insert(tx);
} else {
return Err(Error::SimultaneousSubscription { msg_num });
}
}
};
self.poll_commander
.send(PollCommand::AddSubscriber(msg_num, tx))
.await?;
Ok(BcSubscription::new(rx, msg_num as u32, self))
}
@@ -93,90 +97,18 @@ impl BcConnection {
where
T: Fn(&Bc) -> Option<Bc> + Send + Sync + 'static,
{
match self.subscribers.id.write().await.entry(msg_id) {
Entry::Vacant(vac_entry) => {
vac_entry.insert(Box::new(handler));
}
Entry::Occupied(_) => {
return Err(Error::SimultaneousSubscriptionId { msg_id });
}
};
self.poll_commander
.send(PollCommand::AddHandler(msg_id, Box::new(handler)))
.await?;
Ok(())
}
/// Stop a message handler created using [`handle_msg`]
#[allow(dead_code)] // Currently unused but added for future use
pub async fn unhandle_msg(&self, msg_id: u32) -> Result<()> {
self.subscribers.id.write().await.remove(&msg_id);
Ok(())
}
async fn poll(
response: Result<Bc>,
subscribers: &Subscriber,
sink: &Sender<Result<Bc>>,
) -> Result<()> {
match response {
Ok(response) => {
let msg_num = response.meta.msg_num;
let msg_id = response.meta.msg_id;
let mut remove_it_num = false;
match (
subscribers.id.read().await.get(&msg_id),
subscribers.num.read().await.get(&msg_num),
) {
(Some(occ), _) => {
if let Some(reply) = occ(&response) {
assert!(reply.meta.msg_num == response.meta.msg_num);
sink.send(Ok(reply)).await?;
}
}
(None, Some(occ)) => {
if occ.capacity() == 0 {
warn!("Reaching limit of channel");
warn!(
"Remaining: {} of {} message space for {} (ID: {})",
occ.capacity(),
occ.max_capacity(),
&msg_num,
&msg_id
);
} else {
trace!(
"Remaining: {} of {} message space for {} (ID: {})",
occ.capacity(),
occ.max_capacity(),
&msg_num,
&msg_id
);
}
if occ.send(Ok(response)).await.is_err() {
remove_it_num = true;
}
}
(None, None) => {
debug!(
"Ignoring uninteresting message id {} (number: {})",
msg_id, msg_num
);
trace!("Contents: {:?}", response);
}
}
if remove_it_num {
subscribers.num.write().await.remove(&msg_num);
}
}
Err(e) => {
for sub in subscribers.num.read().await.values() {
let _ = sub.send(Err(e.clone())).await;
}
subscribers.num.write().await.clear();
subscribers.id.write().await.clear();
return Err(e);
}
}
self.poll_commander
.send(PollCommand::RemoveHandler(msg_id))
.await?;
Ok(())
}
@@ -198,3 +130,116 @@ impl BcConnection {
Ok(())
}
}
enum PollCommand {
Bc(Box<Result<Bc>>),
AddHandler(u32, MsgHandler),
RemoveHandler(u32),
AddSubscriber(u16, Sender<Result<Bc>>),
}
struct Poller {
subscribers: Subscriber,
sink: Sender<Result<Bc>>,
reciever: ReceiverStream<PollCommand>,
}
impl Poller {
async fn run(&mut self) -> Result<()> {
while let Some(command) = self.reciever.next().await {
yield_now().await;
match command {
PollCommand::Bc(boxed_response) => match *boxed_response {
Ok(response) => {
let msg_num = response.meta.msg_num;
let msg_id = response.meta.msg_id;
let mut remove_it_num = false;
match (
self.subscribers.id.get(&msg_id),
self.subscribers.num.get(&msg_num),
) {
(Some(occ), _) => {
if let Some(reply) = occ(&response) {
assert!(reply.meta.msg_num == response.meta.msg_num);
self.sink.send(Ok(reply)).await?;
}
}
(None, Some(occ)) => {
if occ.capacity() == 0 {
warn!("Reaching limit of channel");
warn!(
"Remaining: {} of {} message space for {} (ID: {})",
occ.capacity(),
occ.max_capacity(),
&msg_num,
&msg_id
);
} else {
trace!(
"Remaining: {} of {} message space for {} (ID: {})",
occ.capacity(),
occ.max_capacity(),
&msg_num,
&msg_id
);
}
if occ.send(Ok(response)).await.is_err() {
remove_it_num = true;
}
}
(None, None) => {
debug!(
"Ignoring uninteresting message id {} (number: {})",
msg_id, msg_num
);
trace!("Contents: {:?}", response);
}
}
if remove_it_num {
self.subscribers.num.remove(&msg_num);
}
}
Err(e) => {
for sub in self.subscribers.num.values() {
let _ = sub.send(Err(e.clone())).await;
}
self.subscribers.num.clear();
self.subscribers.id.clear();
return Err(e);
}
},
PollCommand::AddHandler(msg_id, handler) => {
match self.subscribers.id.entry(msg_id) {
Entry::Vacant(vac_entry) => {
vac_entry.insert(handler);
}
Entry::Occupied(_) => {
return Err(Error::SimultaneousSubscriptionId { msg_id });
}
};
}
PollCommand::RemoveHandler(msg_id) => {
self.subscribers.id.remove(&msg_id);
}
PollCommand::AddSubscriber(msg_num, tx) => {
match self.subscribers.num.entry(msg_num) {
Entry::Vacant(vac_entry) => {
vac_entry.insert(tx);
}
Entry::Occupied(mut occ_entry) => {
if occ_entry.get().is_closed() {
occ_entry.insert(tx);
} else {
let _ = tx
.send(Err(Error::SimultaneousSubscription { msg_num }))
.await;
}
}
};
}
}
}
Ok(())
}
}
@@ -189,11 +189,16 @@ impl Discoverer {
F: Fn(UdpDiscovery, SocketAddr) -> Option<T>,
{
let mut reply = ReceiverStream::new(self.subscribe(0).await?);
loop {
let (reply, addr) = reply.next().await.ok_or(Error::ConnectionUnavaliable)??;
if let Some(result) = map(reply, addr) {
return Ok(result);
}
tokio::select! {
v = async {
loop {
let (reply, addr) = reply.next().await.ok_or(Error::ConnectionUnavaliable)??;
if let Some(result) = map(reply, addr) {
return Ok(result);
}
}
} => v,
_ = tokio::time::sleep(*MAXIMUM_WAIT) => Err::<T, Error>(Error::DiscoveryTimeout),
}
}
@@ -217,6 +222,7 @@ impl Discoverer {
where
F: Fn(UdpDiscovery, SocketAddr) -> Option<T>,
{
trace!("Retrying send");
disc.tid = 0; // Must be random to avoid simulatenous subscription errors
let mut set = FuturesUnordered::new();
for dest in dests.iter() {
@@ -225,6 +231,7 @@ impl Discoverer {
// Get what ever completes first
while let Some(result) = set.next().await {
trace!("Result: {}", result.is_ok());
if let Ok(ret) = result {
return Ok(ret);
}
@@ -356,15 +363,20 @@ impl Discoverer {
/// On success it returns the M2cQr that the p2p relay
/// server has about the UID
async fn uid_loopup(&self, uid: &str) -> Result<UidLookupResults> {
let mut addrs = vec![];
for p2p_relay in P2P_RELAY_HOSTNAMES.iter() {
addrs.append(
&mut format!("{}:9999", p2p_relay)
.to_socket_addrs()
.map(|i| i.collect::<Vec<SocketAddr>>())
.unwrap_or_else(|_| vec![]),
);
}
let task = tokio::task::spawn_blocking(move || {
let mut addrs = vec![];
for p2p_relay in P2P_RELAY_HOSTNAMES.iter() {
addrs.append(
&mut format!("{}:9999", p2p_relay)
.to_socket_addrs()
.map(|i| i.collect::<Vec<SocketAddr>>())
.unwrap_or_else(|_| vec![]),
);
}
addrs
});
let addrs = timeout(*MAXIMUM_WAIT, task).await??;
trace!("Uid lookup to: {:?}", addrs);
let msg = UdpDiscovery {
tid: 0,
payload: UdpXml {
@@ -375,6 +387,7 @@ impl Discoverer {
..Default::default()
},
};
trace!("Sending look up {:?}", msg);
let (packet, _) = self
.retry_send_multi(msg, addrs.as_slice(), |bc, addr| match bc {
UdpDiscovery {
@@ -388,7 +401,7 @@ impl Discoverer {
_ => None,
})
.await?;
trace!("Look up complete");
Ok(UidLookupResults {
reg: SocketAddr::new(packet.reg.ip.parse()?, packet.reg.port),
relay: SocketAddr::new(packet.relay.ip.parse()?, packet.relay.port),
@@ -1004,20 +1017,25 @@ impl Discovery {
// directly
#[allow(unused)]
pub(crate) async fn remote(uid: &str) -> Result<DiscoveryResult> {
trace!("Start remote");
let mut discoverer = Discoverer::new().await?;
let client_id = generate_cid();
trace!("client_id: {}", client_id);
let lookup = discoverer.uid_loopup(uid).await?;
trace!("lookup: {:?}", lookup);
let reg_addr = lookup.reg;
let relay_addr = lookup.relay;
let reg_result = discoverer.register_address(uid, client_id, &lookup).await?;
trace!("reg_result: {:?}", reg_result);
let connect_result = tokio::select! {
v = discoverer.client_initiated_dev(&reg_result) => {v},
v = discoverer.device_initiated_dev(&reg_result) => {v},
}?;
trace!("connect_result: {:?}", connect_result);
let (socket, keep_alive_tasks) = discoverer.into_socket().await;
Ok(DiscoveryResult {
@@ -198,6 +198,7 @@ impl Sink<(BcUdp, SocketAddr)> for BcUdpSource {
}
}
#[allow(dead_code)]
enum State {
Normal, // Normal recieve
Flushing, // Used to send ack packets and things in the buffer
@@ -298,15 +299,15 @@ impl Stream for UdpPayloadSource {
let mut this = self.get_mut();
match this.state {
State::Normal => {
this.state = State::YieldNow;
// 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)));
}
// Handle resend events
this.maintanence(cx);
// Normal behaviors
match this.inner.poll_next_unpin(cx) {
Poll::Ready(Some(Ok((
+6
View File
@@ -208,6 +208,12 @@ impl<T> From<tokio::sync::mpsc::error::SendError<T>> for Error {
}
}
impl<T> From<tokio_util::sync::PollSendError<T>> for Error {
fn from(_: tokio_util::sync::PollSendError<T>) -> Self {
Error::TokioBcSendError
}
}
impl From<cookie_factory::GenError> for Error {
fn from(k: cookie_factory::GenError) -> Self {
Error::GenError(std::sync::Arc::new(k))
+13 -8
View File
@@ -12,11 +12,15 @@ pub struct BcMediaCodex {
/// If true we will not search for the start of the next packet
/// in the event that the stream appears to be corrupted
strict: bool,
amount_skipped: usize,
}
impl BcMediaCodex {
pub(crate) fn new(strict: bool) -> Self {
Self { strict }
Self {
strict,
amount_skipped: 0,
}
}
}
@@ -46,18 +50,19 @@ impl Decoder for BcMediaCodex {
}
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>> {
let mut amount_skipped: usize = 0;
loop {
match { BcMedia::deserialize(src) } {
Ok(bc) => {
if amount_skipped > 0 {
debug!("Amount skipped to restore stream: {}", amount_skipped);
if self.amount_skipped > 0 {
debug!("Amount skipped to restore stream: {}", self.amount_skipped);
self.amount_skipped = 0;
}
return Ok(Some(bc));
}
Err(Error::NomIncomplete(_)) => {
if amount_skipped > 0 {
debug!("Amount skipped to restore stream: {}", amount_skipped);
if self.amount_skipped > 0 {
debug!("Amount skipped to restore stream: {}", self.amount_skipped);
self.amount_skipped = 0;
}
return Ok(None);
}
@@ -67,12 +72,12 @@ impl Decoder for BcMediaCodex {
} else if src.is_empty() {
return Ok(None);
} else {
if amount_skipped == 0 {
if self.amount_skipped == 0 {
debug!("Error in stream attempting to restore");
trace!(" Stream Error: {:?}", e);
}
// Drop the whole packet and wait for a packet that starts with magic
amount_skipped += src.len();
self.amount_skipped += src.len();
src.clear();
continue;
}