use std::rc::Rc;
use serde::{de::DeserializeOwned, Serialize};
use super::RemoteAddr;
use crate::state::{with_node, ResourceId};
pub struct Connection {
rid: ResourceId,
remote: RemoteAddr,
remote_rid: ResourceId,
}
impl Connection {
pub(super) fn new(rid: ResourceId, remote: RemoteAddr, remote_rid: ResourceId) -> Self {
Self {
rid,
remote,
remote_rid,
}
}
pub fn split(self) -> (OwnedReader, OwnedWriter) {
let connection = Rc::new(self);
(
OwnedReader {
connection: connection.clone(),
},
OwnedWriter { connection },
)
}
pub fn remote(&self) -> RemoteAddr {
self.remote
}
pub fn is_closed(&self) -> bool {
with_node(|n| !n.is_connection_open(self.rid))
}
pub async fn recv<T>(&mut self) -> Option<T>
where
T: DeserializeOwned,
{
if self.is_closed() {
return None;
}
let bytes = with_node(|n| n.recv(self.rid)).await?;
Some(bincode::deserialize(&bytes).unwrap())
}
pub fn write<T>(&mut self, message: &T)
where
T: Serialize,
{
let bytes = bincode::serialize(message).expect("Serialization failed.");
with_node(|n| n.send(self.remote, self.remote_rid, bytes))
}
}
impl Drop for Connection {
fn drop(&mut self) {
with_node(|n| n.close_connection(self.rid, self.remote, self.remote_rid))
}
}
pub struct OwnedReader {
connection: Rc<Connection>,
}
pub struct OwnedWriter {
connection: Rc<Connection>,
}
impl OwnedReader {
pub fn remote(&self) -> RemoteAddr {
self.connection.remote
}
pub fn is_closed(&self) -> bool {
self.connection.is_closed()
}
pub async fn recv<T>(&mut self) -> Option<T>
where
T: DeserializeOwned,
{
if self.is_closed() {
return None;
}
let bytes = with_node(|n| n.recv(self.connection.rid)).await?;
Some(bincode::deserialize(&bytes).unwrap())
}
}
impl OwnedWriter {
pub fn remote(&self) -> RemoteAddr {
self.connection.remote
}
pub fn is_closed(&self) -> bool {
self.connection.is_closed()
}
pub fn write<T>(&mut self, message: &T)
where
T: Serialize,
{
let bytes = bincode::serialize(message).expect("Serialization failed.");
with_node(|n| n.send(self.connection.remote, self.connection.remote_rid, bytes))
}
}