2025-02-23 17:07:09 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2025-02-24 01:43:08 +00:00
|
|
|
type Id = u64;
|
|
|
|
|
2025-02-23 18:06:07 +00:00
|
|
|
pub(crate) struct Args {
|
|
|
|
pub(crate) port: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Args {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self { port: 9000 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-23 17:07:09 +00:00
|
|
|
pub(crate) struct App {
|
2025-02-24 02:29:05 +00:00
|
|
|
client_streams: BTreeMap<Id, Framed<TcpStream, LengthDelimitedCodec>>,
|
2025-02-24 01:43:08 +00:00
|
|
|
clients: BTreeMap<Id, Client>,
|
2025-02-23 17:07:09 +00:00
|
|
|
listener: TcpListener,
|
2025-02-24 01:43:08 +00:00
|
|
|
next_client_id: Id,
|
2025-02-23 17:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl App {
|
2025-02-23 18:06:07 +00:00
|
|
|
pub(crate) async fn new(args: Args) -> Result<Self> {
|
|
|
|
let listener = TcpListener::bind(("0.0.0.0", args.port)).await?;
|
2025-02-23 17:07:09 +00:00
|
|
|
|
|
|
|
Ok(Self {
|
2025-02-24 02:29:05 +00:00
|
|
|
client_streams: Default::default(),
|
2025-02-23 17:07:09 +00:00
|
|
|
clients: Default::default(),
|
|
|
|
listener,
|
|
|
|
next_client_id: 1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn poll_run(&mut self, cx: &mut Context<'_>) -> Poll<Result<()>> {
|
|
|
|
let previous_client_count = self.clients.len();
|
|
|
|
|
|
|
|
let mut new_messages = vec![];
|
2025-02-24 01:43:08 +00:00
|
|
|
let mut clients_to_delete = vec![];
|
|
|
|
for (id, client) in &mut self.clients {
|
|
|
|
let id = *id;
|
2025-02-24 02:29:05 +00:00
|
|
|
let stream = self
|
|
|
|
.client_streams
|
|
|
|
.get_mut(&id)
|
|
|
|
.context("Logic error: Client has no stream")?;
|
|
|
|
let mut stream = pin!(stream);
|
2025-02-23 17:07:09 +00:00
|
|
|
|
2025-02-24 02:29:05 +00:00
|
|
|
match <_ as futures_sink::Sink<Bytes>>::poll_ready(stream.as_mut(), cx) {
|
|
|
|
Poll::Pending => {}
|
|
|
|
Poll::Ready(Err(err)) => {
|
|
|
|
return Poll::Ready(
|
|
|
|
Err(err).context("Can't check network write half for readiness"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Poll::Ready(Ok(())) => {
|
|
|
|
if let Some(frame) = client.poll_send() {
|
|
|
|
if let Err(err) = stream.as_mut().start_send(frame) {
|
2025-02-23 17:07:09 +00:00
|
|
|
return Poll::Ready(Err(err).context("start_send"));
|
|
|
|
}
|
2025-02-24 02:29:05 +00:00
|
|
|
match <_ as futures_sink::Sink<Bytes>>::poll_flush(stream.as_mut(), cx) {
|
|
|
|
Poll::Pending => {}
|
|
|
|
Poll::Ready(Err(err)) => {
|
|
|
|
return Poll::Ready(Err(err).context("poll_flush"));
|
|
|
|
}
|
|
|
|
Poll::Ready(Ok(())) => {}
|
|
|
|
}
|
2025-02-23 17:07:09 +00:00
|
|
|
tracing::debug!("Started send");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-24 02:29:05 +00:00
|
|
|
if let Some(ChatLine { id, line }) = client.poll_inbox() {
|
|
|
|
cx.waker().wake_by_ref();
|
|
|
|
new_messages.push(ToClient::ChatLine { id, line });
|
|
|
|
}
|
|
|
|
|
|
|
|
match stream.as_mut().poll_next(cx) {
|
2025-02-23 17:07:09 +00:00
|
|
|
Poll::Pending => {}
|
2025-02-24 02:29:05 +00:00
|
|
|
Poll::Ready(None) => clients_to_delete.push(id),
|
|
|
|
Poll::Ready(Some(Err(err))) => {
|
|
|
|
tracing::error!(?err, "stream.poll_next error");
|
2025-02-23 17:07:09 +00:00
|
|
|
}
|
2025-02-24 02:29:05 +00:00
|
|
|
Poll::Ready(Some(Ok(frame))) => {
|
|
|
|
cx.waker().wake_by_ref();
|
|
|
|
if let Err(err) = client.handle_frame(frame.into()) {
|
|
|
|
return Poll::Ready(Err(err).context("client.handle_frame"));
|
2025-02-23 17:07:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-24 01:43:08 +00:00
|
|
|
for id in clients_to_delete {
|
|
|
|
tracing::info!(?id, "Closing client");
|
|
|
|
self.clients.remove(&id);
|
|
|
|
}
|
2025-02-23 17:07:09 +00:00
|
|
|
|
2025-02-24 01:43:08 +00:00
|
|
|
for client in &mut self.clients.values_mut() {
|
2025-02-23 17:07:09 +00:00
|
|
|
for msg in &new_messages {
|
2025-02-24 02:29:05 +00:00
|
|
|
client.handle_outgoing(msg)?;
|
2025-02-23 17:07:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.listener.poll_accept(cx) {
|
|
|
|
Poll::Pending => {}
|
|
|
|
Poll::Ready(Err(err)) => return Poll::Ready(Err(err.into())),
|
|
|
|
Poll::Ready(Ok((stream, _addr))) => {
|
|
|
|
cx.waker().wake_by_ref();
|
2025-02-24 02:29:05 +00:00
|
|
|
let id = self.next_client_id;
|
|
|
|
self.next_client_id += 1;
|
2025-02-23 17:07:09 +00:00
|
|
|
let client = Client {
|
2025-02-24 02:29:05 +00:00
|
|
|
id,
|
|
|
|
inbox: Default::default(),
|
2025-02-23 17:07:09 +00:00
|
|
|
outbox: Default::default(),
|
2025-02-23 18:06:07 +00:00
|
|
|
sequence: 0,
|
2025-02-23 17:07:09 +00:00
|
|
|
};
|
2025-02-24 02:29:05 +00:00
|
|
|
let stream = Framed::new(stream, LengthDelimitedCodec::new());
|
2025-02-23 17:07:09 +00:00
|
|
|
tracing::info!(id = client.id, "Accepted client");
|
2025-02-24 02:29:05 +00:00
|
|
|
self.clients.insert(id, client);
|
|
|
|
self.client_streams.insert(id, stream);
|
2025-02-23 17:07:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.clients.len() != previous_client_count {
|
|
|
|
tracing::info!(client_count = self.clients.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
}
|
2025-02-23 18:06:07 +00:00
|
|
|
|
2025-02-24 02:29:05 +00:00
|
|
|
struct ChatLine {
|
|
|
|
id: Id,
|
|
|
|
line: String,
|
|
|
|
}
|
|
|
|
|
2025-02-23 18:06:07 +00:00
|
|
|
struct Client {
|
|
|
|
id: u64,
|
2025-02-24 02:29:05 +00:00
|
|
|
inbox: VecDeque<ChatLine>,
|
|
|
|
outbox: VecDeque<Bytes>,
|
2025-02-23 18:06:07 +00:00
|
|
|
sequence: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Client {
|
2025-02-24 02:29:05 +00:00
|
|
|
fn handle_frame(&mut self, frame: Bytes) -> Result<()> {
|
|
|
|
match rmp_serde::from_slice(&frame)? {
|
|
|
|
ToServer::ChatLine { line, sequence } => {
|
|
|
|
if sequence != self.sequence {
|
|
|
|
tracing::error!(
|
|
|
|
expected = self.sequence,
|
|
|
|
actual = sequence,
|
|
|
|
"Sequence mismatch"
|
|
|
|
);
|
|
|
|
bail!("Sequence mismatch");
|
|
|
|
}
|
|
|
|
self.sequence += 1;
|
|
|
|
self.inbox.push_back(ChatLine { id: self.id, line });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_outgoing(&mut self, msg: &ToClient) -> Result<()> {
|
|
|
|
if !self.outbox.is_empty() {
|
|
|
|
bail!("Outbox full");
|
|
|
|
}
|
|
|
|
let bytes = rmp_serde::to_vec(msg)?.into();
|
|
|
|
self.outbox.push_back(bytes);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_inbox(&mut self) -> Option<ChatLine> {
|
|
|
|
self.inbox.pop_front()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_send(&mut self) -> Option<Bytes> {
|
|
|
|
self.outbox.pop_front()
|
2025-02-23 18:06:07 +00:00
|
|
|
}
|
|
|
|
}
|