nzr-api: https://xkcd.com/927/
This commit is contained in:
parent
6fe1ed02aa
commit
f1dd375e2f
1 changed files with 42 additions and 3 deletions
|
@ -1,4 +1,4 @@
|
||||||
use std::{io, net::SocketAddr, pin::Pin};
|
use std::{fmt, io, pin::Pin};
|
||||||
|
|
||||||
use futures::SinkExt;
|
use futures::SinkExt;
|
||||||
use tarpc::tokio_util::codec::{FramedWrite, LengthDelimitedCodec};
|
use tarpc::tokio_util::codec::{FramedWrite, LengthDelimitedCodec};
|
||||||
|
@ -10,6 +10,45 @@ use tracing::instrument;
|
||||||
|
|
||||||
use super::EventMessage;
|
use super::EventMessage;
|
||||||
|
|
||||||
|
/// Representation of multiple types of SocketAddrs, because you can't have just
|
||||||
|
/// one!
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum SocketAddr {
|
||||||
|
#[cfg(unix)]
|
||||||
|
TokioUnix(tokio::net::unix::SocketAddr),
|
||||||
|
#[cfg(unix)]
|
||||||
|
Unix(std::os::unix::net::SocketAddr),
|
||||||
|
Net(std::net::SocketAddr),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for SocketAddr {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
#[cfg(unix)]
|
||||||
|
Self::TokioUnix(addr) => std::fmt::Debug::fmt(addr, f),
|
||||||
|
#[cfg(unix)]
|
||||||
|
Self::Unix(addr) => std::fmt::Debug::fmt(addr, f),
|
||||||
|
Self::Net(addr) => addr.fmt(f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! as_sockaddr {
|
||||||
|
($id:ident, $t:ty) => {
|
||||||
|
impl From<$t> for SocketAddr {
|
||||||
|
fn from(value: $t) -> Self {
|
||||||
|
Self::$id(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
as_sockaddr!(TokioUnix, tokio::net::unix::SocketAddr);
|
||||||
|
#[cfg(unix)]
|
||||||
|
as_sockaddr!(Unix, std::os::unix::net::SocketAddr);
|
||||||
|
as_sockaddr!(Net, std::net::SocketAddr);
|
||||||
|
|
||||||
/// Represents a connection to a client. Instead of being owned by the server
|
/// Represents a connection to a client. Instead of being owned by the server
|
||||||
/// struct, a [`tokio::sync::broadcast::Receiver`] is used to get the serialized
|
/// struct, a [`tokio::sync::broadcast::Receiver`] is used to get the serialized
|
||||||
/// message and pass it to the client.
|
/// message and pass it to the client.
|
||||||
|
@ -79,7 +118,7 @@ impl EventServer {
|
||||||
pub async fn spawn<T: AsyncWrite + Send + 'static>(
|
pub async fn spawn<T: AsyncWrite + Send + 'static>(
|
||||||
&self,
|
&self,
|
||||||
inner: T,
|
inner: T,
|
||||||
client_addr: SocketAddr,
|
client_addr: impl Into<SocketAddr>,
|
||||||
) -> io::Result<()> {
|
) -> io::Result<()> {
|
||||||
// Sender<T> doesn't have a try_subscribe, so this is our last-ditch
|
// Sender<T> doesn't have a try_subscribe, so this is our last-ditch
|
||||||
// effort to avoid a panic
|
// effort to avoid a panic
|
||||||
|
@ -87,7 +126,7 @@ impl EventServer {
|
||||||
todo!("Too many connections!");
|
todo!("Too many connections!");
|
||||||
}
|
}
|
||||||
|
|
||||||
EventEmitter::new(inner, client_addr, self.channel.subscribe()).run();
|
EventEmitter::new(inner, client_addr.into(), self.channel.subscribe()).run();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue