mirror of
https://github.com/sxyazi/yazi.git
synced 2026-05-13 08:16:40 +00:00
feat: merge the sender and severity in the DDS payload into the same field to simplify the protocol (#871)
This commit is contained in:
parent
1b030e0f52
commit
d04b549f4e
6 changed files with 39 additions and 26 deletions
|
|
@ -76,7 +76,7 @@ impl<'a> Body<'a> {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
pub fn with_severity(self, severity: u8) -> Payload<'a> {
|
||||
pub fn with_severity(self, severity: u16) -> Payload<'a> {
|
||||
Payload::new(self).with_severity(severity)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,13 +9,12 @@ use crate::{body::Body, ID};
|
|||
#[derive(Debug)]
|
||||
pub struct Payload<'a> {
|
||||
pub receiver: u64,
|
||||
pub severity: u8,
|
||||
pub sender: u64,
|
||||
pub body: Body<'a>,
|
||||
}
|
||||
|
||||
impl<'a> Payload<'a> {
|
||||
pub(super) fn new(body: Body<'a>) -> Self { Self { receiver: 0, severity: 0, sender: *ID, body } }
|
||||
pub(super) fn new(body: Body<'a>) -> Self { Self { receiver: 0, sender: *ID, body } }
|
||||
|
||||
pub(super) fn flush(&self) { writeln!(std::io::stdout(), "{self}").ok(); }
|
||||
|
||||
|
|
@ -38,8 +37,8 @@ impl<'a> Payload<'a> {
|
|||
self
|
||||
}
|
||||
|
||||
pub(super) fn with_severity(mut self, severity: u8) -> Self {
|
||||
self.severity = severity;
|
||||
pub(super) fn with_severity(mut self, severity: u16) -> Self {
|
||||
self.sender = severity as u64;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
|
@ -55,22 +54,19 @@ impl FromStr for Payload<'_> {
|
|||
type Err = anyhow::Error;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let mut parts = s.splitn(5, ',');
|
||||
let mut parts = s.splitn(4, ',');
|
||||
|
||||
let kind = parts.next().ok_or_else(|| anyhow!("empty kind"))?;
|
||||
|
||||
let receiver =
|
||||
parts.next().and_then(|s| s.parse().ok()).ok_or_else(|| anyhow!("invalid receiver"))?;
|
||||
|
||||
let severity =
|
||||
parts.next().and_then(|s| s.parse().ok()).ok_or_else(|| anyhow!("invalid severity"))?;
|
||||
|
||||
let sender =
|
||||
parts.next().and_then(|s| s.parse().ok()).ok_or_else(|| anyhow!("invalid sender"))?;
|
||||
|
||||
let body = parts.next().ok_or_else(|| anyhow!("empty body"))?;
|
||||
|
||||
Ok(Self { receiver, severity, sender, body: Body::from_str(kind, body)? })
|
||||
Ok(Self { receiver, sender, body: Body::from_str(kind, body)? })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -92,7 +88,7 @@ impl Display for Payload<'_> {
|
|||
};
|
||||
|
||||
if let Ok(s) = result {
|
||||
write!(f, "{},{},{},{},{s}", self.body.kind(), self.receiver, self.severity, self.sender)
|
||||
write!(f, "{},{},{},{s}", self.body.kind(), self.receiver, self.sender)
|
||||
} else {
|
||||
Err(std::fmt::Error)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -80,7 +80,7 @@ impl Pubsub {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn pub_static(severity: u8, body: Body) {
|
||||
pub fn pub_static(severity: u16, body: Body) {
|
||||
let (kind, peers) = (body.kind(), PEERS.read());
|
||||
if peers.values().any(|c| c.able(kind)) {
|
||||
Client::push(body.with_severity(severity));
|
||||
|
|
@ -100,7 +100,7 @@ impl Pubsub {
|
|||
Self::pub_(BodyCd::dummy(tab));
|
||||
}
|
||||
if PEERS.read().values().any(|p| p.able("cd")) {
|
||||
Client::push(BodyCd::borrowed(tab, url).with_severity(10));
|
||||
Client::push(BodyCd::borrowed(tab, url).with_severity(100));
|
||||
}
|
||||
if BOOT.local_events.contains("cd") {
|
||||
BodyCd::borrowed(tab, url).with_receiver(*ID).flush();
|
||||
|
|
@ -112,7 +112,7 @@ impl Pubsub {
|
|||
Self::pub_(BodyHover::dummy(tab));
|
||||
}
|
||||
if PEERS.read().values().any(|p| p.able("hover")) {
|
||||
Client::push(BodyHover::borrowed(tab, url).with_severity(20));
|
||||
Client::push(BodyHover::borrowed(tab, url).with_severity(200));
|
||||
}
|
||||
if BOOT.local_events.contains("hover") {
|
||||
BodyHover::borrowed(tab, url).with_receiver(*ID).flush();
|
||||
|
|
@ -136,7 +136,7 @@ impl Pubsub {
|
|||
Self::pub_(BodyYank::dummy());
|
||||
}
|
||||
if PEERS.read().values().any(|p| p.able("yank")) {
|
||||
Client::push(BodyYank::borrowed(cut, urls).with_severity(30));
|
||||
Client::push(BodyYank::borrowed(cut, urls).with_severity(300));
|
||||
}
|
||||
if BOOT.local_events.contains("yank") {
|
||||
BodyYank::borrowed(cut, urls).with_receiver(*ID).flush();
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ impl Server {
|
|||
let Some(id) = id else { continue };
|
||||
let Some(kind) = parts.next() else { continue };
|
||||
let Some(receiver) = parts.next().and_then(|s| s.parse().ok()) else { continue };
|
||||
let Some(severity) = parts.next().and_then(|s| s.parse::<u8>().ok()) else { continue };
|
||||
let Some(sender) = parts.next().and_then(|s| s.parse::<u64>().ok()) else { continue };
|
||||
|
||||
let clients = CLIENTS.read();
|
||||
let clients: Vec<_> = if receiver == 0 {
|
||||
|
|
@ -61,9 +61,9 @@ impl Server {
|
|||
continue;
|
||||
}
|
||||
|
||||
if receiver == 0 && severity > 0 {
|
||||
if receiver == 0 && sender > 0 && sender <= u16::MAX as u64 {
|
||||
let Some(body) = parts.next() else { continue };
|
||||
STATE.add(format!("{}_{severity}_{kind}", Body::tab(kind, body)), &line);
|
||||
if !STATE.set(kind, sender as u16, body) { continue }
|
||||
}
|
||||
|
||||
line.push('\n');
|
||||
|
|
@ -95,6 +95,10 @@ impl Server {
|
|||
let Ok(payload) = Payload::from_str(&s) else { return };
|
||||
let Body::Hi(hi) = payload.body else { return };
|
||||
|
||||
if payload.sender <= u16::MAX as u64 {
|
||||
return; // The kind of static messages cannot be "hi"
|
||||
}
|
||||
|
||||
if id.is_none() {
|
||||
if let Some(ref state) = *STATE.read() {
|
||||
state.values().for_each(|s| _ = tx.send(s.clone()));
|
||||
|
|
|
|||
|
|
@ -23,11 +23,25 @@ impl Deref for State {
|
|||
}
|
||||
|
||||
impl State {
|
||||
pub fn add(&self, key: String, value: &str) {
|
||||
if let Some(ref mut inner) = *self.inner.write() {
|
||||
inner.insert(key, value.to_owned());
|
||||
self.last.store(timestamp_us(), Ordering::Relaxed);
|
||||
pub fn set(&self, kind: &str, severity: u16, body: &str) -> bool {
|
||||
let Some(inner) = &mut *self.inner.write() else { return false };
|
||||
let key = format!("{}_{severity}_{kind}", Body::tab(kind, body));
|
||||
|
||||
if body == "null" {
|
||||
return inner
|
||||
.remove(&key)
|
||||
.map(|_| self.last.store(timestamp_us(), Ordering::Relaxed))
|
||||
.is_some();
|
||||
}
|
||||
|
||||
let value = format!("{kind},0,{severity},{body}\n");
|
||||
if inner.get(&key).is_some_and(|s| *s == value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
inner.insert(key, value);
|
||||
self.last.store(timestamp_us(), Ordering::Relaxed);
|
||||
true
|
||||
}
|
||||
|
||||
pub async fn load_or_create(&self) {
|
||||
|
|
@ -69,11 +83,10 @@ impl State {
|
|||
|
||||
let mut inner = HashMap::new();
|
||||
while buf.read_line(&mut line).await? > 0 {
|
||||
let mut parts = line.splitn(5, ',');
|
||||
let mut parts = line.splitn(4, ',');
|
||||
let Some(kind) = parts.next() else { continue };
|
||||
let Some(_) = parts.next() else { continue };
|
||||
let Some(severity) = parts.next().and_then(|s| s.parse::<u8>().ok()) else { continue };
|
||||
let Some(_) = parts.next() else { continue };
|
||||
let Some(severity) = parts.next().and_then(|s| s.parse::<u16>().ok()) else { continue };
|
||||
let Some(body) = parts.next() else { continue };
|
||||
inner.insert(format!("{}_{severity}_{kind}", Body::tab(kind, body)), mem::take(&mut line));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue