use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::{Duration, Instant};
use bytes::Bytes;
use serde_json::{Value, json};
use tokio::time::Sleep;
use super::handles::{Handles, Resource};
use crate::host::{GuestEnd, Refused};
use crate::wire::ByteStore;
use crate::{
ChannelEvent, ChannelFuture, CloseFrame, Error, Limits, Message, Result, WebSocketFuture,
};
type Opening = Pin<Box<dyn Future<Output = Result<Option<Value>>> + Send + 'static>>;
pub(super) struct OpenChannel {
end: GuestEnd,
opening: Option<Opening>,
write: Option<Bytes>,
paused: bool,
forced: Option<CloseFrame>,
idle_timeout: Option<Duration>,
idle: Option<Pin<Box<Sleep>>>,
end_of_life: Option<Instant>,
lifetime: Option<Pin<Box<Sleep>>>,
socket: Option<SocketSignals>,
}
struct SocketSignals {
start_tls: bool,
}
impl OpenChannel {
fn force(&mut self, frame: CloseFrame) {
self.end.close(frame.clone());
self.forced.get_or_insert(frame);
}
fn new(end: GuestEnd) -> Self {
Self {
end,
opening: None,
write: None,
paused: false,
forced: None,
idle_timeout: None,
idle: None,
end_of_life: None,
lifetime: None,
socket: None,
}
}
fn ready_socket(&mut self) -> Result<&mut SocketSignals> {
if self.opening.is_some() || self.write.is_some() || self.end.is_ended() {
return Err(Error::InvalidRequest("socket is not ready".into()));
}
self.socket
.as_mut()
.ok_or_else(|| Error::InvalidRequest("not a socket".into()))
}
fn touch(&mut self) {
self.idle = None;
}
fn poll_write(&mut self, context: &mut Context<'_>) -> Poll<bool> {
while let Some(rest) = self.write.as_mut() {
if rest.is_empty() {
self.write = None;
return Poll::Ready(true);
}
let room = match self.end.poll_room(context) {
Poll::Pending => return Poll::Pending,
Poll::Ready(0) => {
self.write = None;
self.force(CloseFrame::new(
CloseFrame::MESSAGE_TOO_BIG,
"message exceeds the channel budget",
));
return Poll::Ready(false);
}
Poll::Ready(room) => room,
};
let piece = rest.split_to(room.min(rest.len()));
match self.end.send(Message::Binary(piece)) {
Ok(()) => self.touch(),
Err(_) => {
self.write = None;
return Poll::Ready(false);
}
}
}
Poll::Ready(false)
}
fn poll_budgets(&mut self, context: &mut Context<'_>) -> Option<&'static str> {
if let Some(timeout) = self.idle_timeout {
let idle = self
.idle
.get_or_insert_with(|| Box::pin(tokio::time::sleep(timeout)));
if idle.as_mut().poll(context).is_ready() {
return Some("idle timeout");
}
}
if let Some(end_of_life) = self.end_of_life {
let lifetime = self
.lifetime
.get_or_insert_with(|| Box::pin(tokio::time::sleep_until(end_of_life.into())));
if lifetime.as_mut().poll(context).is_ready() {
return Some("connection lifetime");
}
}
None
}
}
#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub(super) struct GuestMessage {
id: u64,
text: Option<String>,
bytes: Option<u64>,
}
#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub(super) struct GuestWrite {
id: u64,
bytes: u64,
}
#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub(super) struct GuestPause {
id: u64,
paused: bool,
}
#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub(super) struct GuestClose {
id: u64,
code: u16,
reason: String,
}
impl Handles {
pub fn open_connection(&mut self, end: GuestEnd, limits: &Limits) -> Result<u64> {
let end_of_life = limits
.connection_lifetime
.and_then(|lifetime| Instant::now().checked_add(lifetime));
self.insert(Resource::Channel(Box::new(OpenChannel {
idle_timeout: limits.connection_idle_timeout,
end_of_life,
..OpenChannel::new(end)
})))
}
pub fn open_requested(&mut self, end: GuestEnd, opening: ChannelFuture) -> Result<u64> {
self.insert(Resource::Channel(Box::new(OpenChannel {
opening: Some(Box::pin(async move { opening.await.map(|()| None) })),
..OpenChannel::new(end)
})))
}
pub fn open_socket(
&mut self,
end: GuestEnd,
opening: ChannelFuture,
start_tls: bool,
) -> Result<u64> {
self.insert(Resource::Channel(Box::new(OpenChannel {
opening: Some(Box::pin(async move { opening.await.map(|()| None) })),
socket: Some(SocketSignals { start_tls }),
..OpenChannel::new(end)
})))
}
pub fn end_socket(&mut self, id: u64) -> Result<Value> {
let channel = self.channel(id)?;
channel.ready_socket()?;
channel.end.end();
Ok(Value::Null)
}
pub fn start_socket_tls(&mut self, id: u64) -> Result<Value> {
let channel = self.channel(id)?;
let socket = channel.ready_socket()?;
if !socket.start_tls {
return Err(Error::InvalidRequest("socket cannot start TLS".into()));
}
socket.start_tls = false;
channel.end.start_tls();
Ok(Value::Null)
}
pub fn open_websocket(&mut self, end: GuestEnd, handshake: WebSocketFuture) -> Result<u64> {
let opening = async move {
let accepted = handshake.await?;
Ok(Some(json!({
"protocol": accepted.protocol,
"extensions": accepted.extensions,
})))
};
self.insert(Resource::Channel(Box::new(OpenChannel {
opening: Some(Box::pin(opening)),
..OpenChannel::new(end)
})))
}
fn channel(&mut self, id: u64) -> Result<&mut OpenChannel> {
self.channel_mut(id)
.ok_or_else(|| Error::InvalidRequest("unknown channel".into()))
}
pub fn send_channel(&mut self, message: GuestMessage, bytes: &mut ByteStore) -> Result<Value> {
let payload = match (message.text, message.bytes) {
(Some(text), None) => Message::Text(text),
(None, Some(handle)) => Message::Binary(bytes.take(handle)?),
_ => return Err(Error::InvalidRequest("invalid channel message".into())),
};
let channel = self.channel(message.id)?;
match channel.end.send(payload) {
Ok(()) => channel.touch(),
Err(Refused::Closed) => {}
Err(Refused::TooBig) => channel.force(CloseFrame::new(
CloseFrame::MESSAGE_TOO_BIG,
"message exceeds the channel budget",
)),
}
Ok(Value::Null)
}
pub fn write_channel(&mut self, write: GuestWrite, bytes: &mut ByteStore) -> Result<Value> {
let payload = bytes.take(write.bytes)?;
let channel = self.channel(write.id)?;
if channel.write.is_some() {
return Err(Error::InvalidRequest("a channel write is pending".into()));
}
channel.write = Some(payload);
Ok(Value::Null)
}
pub fn pause_channel(&mut self, pause: GuestPause) -> Result<Value> {
self.channel(pause.id)?.paused = pause.paused;
Ok(Value::Null)
}
pub fn close_channel(&mut self, close: GuestClose) -> Result<Value> {
let channel = self.channel(close.id)?;
let frame = CloseFrame::new(close.code, close.reason);
if channel.opening.take().is_some() {
channel.end.close(frame);
channel.forced.get_or_insert_with(CloseFrame::abnormal);
} else {
channel.end.close(frame);
}
Ok(Value::Null)
}
pub fn channel_buffered(&mut self, id: u64) -> Result<Value> {
Ok(json!(self.channel(id)?.end.buffered()))
}
pub fn force_channels(&mut self, frame: &CloseFrame) {
for (_, channel) in self.channels_mut() {
channel.force(frame.clone());
}
}
pub fn poll_channels(
&mut self,
context: &mut Context<'_>,
bytes: &mut ByteStore,
events: &mut Vec<Value>,
) -> Result<()> {
let mut ended = Vec::new();
for (id, channel) in self.channels_mut() {
if channel.forced.is_none()
&& let Some(reason) = channel.poll_budgets(context)
{
channel.force(CloseFrame::new(CloseFrame::GOING_AWAY, reason));
}
if let Some(frame) = channel.forced.take() {
events.push(close_event(id, &frame));
ended.push(id);
continue;
}
if let Some(opening) = channel.opening.as_mut() {
match opening.as_mut().poll(context) {
Poll::Pending => continue,
Poll::Ready(Ok(accepted)) => {
channel.opening = None;
let mut event = json!({ "channel": id, "open": true });
if let Some(accepted) = accepted {
event["accepted"] = accepted;
}
events.push(event);
}
Poll::Ready(Err(error)) => {
events.push(json!({
"channel": id,
"refused": { "code": error.code(), "message": error.guest_message() },
}));
ended.push(id);
continue;
}
}
}
if channel.write.is_some() && channel.poll_write(context) == Poll::Ready(true) {
events.push(json!({ "channel": id, "written": true }));
}
if let Some(frame) = channel.forced.take() {
events.push(close_event(id, &frame));
ended.push(id);
continue;
}
while !channel.paused {
match channel.end.poll_recv(context) {
Poll::Pending => break,
Poll::Ready(Some(ChannelEvent::Message(Message::Text(text)))) => {
channel.touch();
events.push(json!({ "channel": id, "text": text }));
}
Poll::Ready(Some(ChannelEvent::Message(Message::Binary(data)))) => {
channel.touch();
let handle = bytes.put(data)?;
events.push(json!({ "channel": id, "bytes": handle }));
}
Poll::Ready(Some(ChannelEvent::End)) => {
events.push(json!({ "channel": id, "end": true }));
}
Poll::Ready(Some(ChannelEvent::StartTls)) => {
events.push(json!({ "channel": id, "secure": true }));
}
Poll::Ready(Some(ChannelEvent::Close(frame))) => {
if !channel.end.is_closed() && frame.code != CloseFrame::ABNORMAL {
channel.end.close(frame.clone());
}
events.push(close_event(id, &frame));
ended.push(id);
break;
}
Poll::Ready(None) => {
ended.push(id);
break;
}
}
}
}
for id in ended {
self.release_channel(id);
}
Ok(())
}
}
fn close_event(id: u64, frame: &CloseFrame) -> Value {
json!({ "channel": id, "close": { "code": frame.code, "reason": frame.reason } })
}