2018-05-31 19:43:14 +02:00
|
|
|
extern crate actix;
|
|
|
|
|
2018-07-04 13:04:23 +02:00
|
|
|
use bytes::Bytes;
|
2018-05-30 01:32:39 +02:00
|
|
|
use futures::sync::oneshot::{self, Sender};
|
2018-07-04 13:04:23 +02:00
|
|
|
use futures::{Async, Future, Poll, Stream};
|
2018-01-14 01:17:33 +01:00
|
|
|
use smallvec::SmallVec;
|
2018-01-10 19:12:34 +01:00
|
|
|
|
2018-07-04 13:04:23 +02:00
|
|
|
use self::actix::dev::{
|
|
|
|
AsyncContextParts, ContextFut, ContextParts, Envelope, Mailbox, StreamHandler,
|
|
|
|
ToEnvelope,
|
|
|
|
};
|
2018-05-31 19:43:14 +02:00
|
|
|
use self::actix::fut::ActorFuture;
|
|
|
|
use self::actix::{
|
2018-07-04 13:04:23 +02:00
|
|
|
Actor, ActorContext, ActorState, Addr, AsyncContext, Handler,
|
|
|
|
Message as ActixMessage, SpawnHandle,
|
2018-05-17 21:20:20 +02:00
|
|
|
};
|
2018-01-10 19:12:34 +01:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
use body::{Binary, Body};
|
|
|
|
use context::{ActorHttpContext, Drain, Frame as ContextFrame};
|
2018-07-04 13:04:23 +02:00
|
|
|
use error::{Error, ErrorInternalServerError, PayloadError};
|
2018-01-10 19:12:34 +01:00
|
|
|
use httprequest::HttpRequest;
|
|
|
|
|
2018-02-10 09:05:20 +01:00
|
|
|
use ws::frame::Frame;
|
2018-04-21 22:50:27 +02:00
|
|
|
use ws::proto::{CloseReason, OpCode};
|
2018-07-04 13:04:23 +02:00
|
|
|
use ws::{Message, ProtocolError, WsStream, WsWriter};
|
2018-01-10 19:12:34 +01:00
|
|
|
|
2018-03-30 00:00:18 +02:00
|
|
|
/// Execution context for `WebSockets` actors
|
2018-04-14 01:02:01 +02:00
|
|
|
pub struct WebsocketContext<A, S = ()>
|
|
|
|
where
|
|
|
|
A: Actor<Context = WebsocketContext<A, S>>,
|
2018-01-10 19:12:34 +01:00
|
|
|
{
|
2018-07-04 13:04:23 +02:00
|
|
|
inner: ContextParts<A>,
|
2018-01-25 05:12:49 +01:00
|
|
|
stream: Option<SmallVec<[ContextFrame; 4]>>,
|
2018-01-10 19:12:34 +01:00
|
|
|
request: HttpRequest<S>,
|
|
|
|
disconnected: bool,
|
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
impl<A, S> ActorContext for WebsocketContext<A, S>
|
|
|
|
where
|
|
|
|
A: Actor<Context = Self>,
|
2018-01-10 19:12:34 +01:00
|
|
|
{
|
|
|
|
fn stop(&mut self) {
|
|
|
|
self.inner.stop();
|
|
|
|
}
|
|
|
|
fn terminate(&mut self) {
|
|
|
|
self.inner.terminate()
|
|
|
|
}
|
|
|
|
fn state(&self) -> ActorState {
|
|
|
|
self.inner.state()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
impl<A, S> AsyncContext<A> for WebsocketContext<A, S>
|
|
|
|
where
|
|
|
|
A: Actor<Context = Self>,
|
2018-01-10 19:12:34 +01:00
|
|
|
{
|
|
|
|
fn spawn<F>(&mut self, fut: F) -> SpawnHandle
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
|
|
|
F: ActorFuture<Item = (), Error = (), Actor = A> + 'static,
|
2018-01-10 19:12:34 +01:00
|
|
|
{
|
|
|
|
self.inner.spawn(fut)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wait<F>(&mut self, fut: F)
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
|
|
|
F: ActorFuture<Item = (), Error = (), Actor = A> + 'static,
|
2018-01-10 19:12:34 +01:00
|
|
|
{
|
|
|
|
self.inner.wait(fut)
|
|
|
|
}
|
|
|
|
|
2018-01-23 01:55:50 +01:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[inline]
|
|
|
|
fn waiting(&self) -> bool {
|
2018-05-17 21:20:20 +02:00
|
|
|
self.inner.waiting()
|
|
|
|
|| self.inner.state() == ActorState::Stopping
|
2018-04-14 01:02:01 +02:00
|
|
|
|| self.inner.state() == ActorState::Stopped
|
2018-01-23 01:55:50 +01:00
|
|
|
}
|
|
|
|
|
2018-01-10 19:12:34 +01:00
|
|
|
fn cancel_future(&mut self, handle: SpawnHandle) -> bool {
|
|
|
|
self.inner.cancel_future(handle)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-05-29 19:31:37 +02:00
|
|
|
fn address(&self) -> Addr<A> {
|
2018-05-27 14:02:49 +02:00
|
|
|
self.inner.address()
|
2018-01-10 19:12:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
impl<A, S: 'static> WebsocketContext<A, S>
|
|
|
|
where
|
|
|
|
A: Actor<Context = Self>,
|
|
|
|
{
|
2018-01-10 19:12:34 +01:00
|
|
|
#[inline]
|
2018-06-02 15:51:58 +02:00
|
|
|
/// Create a new Websocket context from a request and an actor
|
2018-07-04 17:01:27 +02:00
|
|
|
pub fn create<P>(req: HttpRequest<S>, actor: A, stream: WsStream<P>) -> Body
|
2018-07-04 13:04:23 +02:00
|
|
|
where
|
|
|
|
A: StreamHandler<Message, ProtocolError>,
|
|
|
|
P: Stream<Item = Bytes, Error = PayloadError> + 'static,
|
|
|
|
{
|
|
|
|
let mb = Mailbox::default();
|
|
|
|
let mut ctx = WebsocketContext {
|
|
|
|
inner: ContextParts::new(mb.sender_producer()),
|
2018-01-14 01:17:33 +01:00
|
|
|
stream: None,
|
2018-01-10 19:12:34 +01:00
|
|
|
request: req,
|
|
|
|
disconnected: false,
|
2018-07-04 13:04:23 +02:00
|
|
|
};
|
|
|
|
ctx.add_stream(stream);
|
|
|
|
|
|
|
|
Body::Actor(Box::new(WebsocketContextFut::new(ctx, actor, mb)))
|
2018-01-10 19:12:34 +01:00
|
|
|
}
|
|
|
|
|
2018-06-16 23:10:44 +02:00
|
|
|
/// Create a new Websocket context
|
2018-07-04 13:04:23 +02:00
|
|
|
pub fn with_factory<F, P>(req: HttpRequest<S>, stream: WsStream<P>, f: F) -> Body
|
2018-06-16 23:10:44 +02:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> A + 'static,
|
2018-07-04 13:04:23 +02:00
|
|
|
A: StreamHandler<Message, ProtocolError>,
|
|
|
|
P: Stream<Item = Bytes, Error = PayloadError> + 'static,
|
2018-06-16 23:10:44 +02:00
|
|
|
{
|
2018-07-04 13:04:23 +02:00
|
|
|
let mb = Mailbox::default();
|
2018-06-16 23:10:44 +02:00
|
|
|
let mut ctx = WebsocketContext {
|
2018-07-04 13:04:23 +02:00
|
|
|
inner: ContextParts::new(mb.sender_producer()),
|
2018-06-16 23:10:44 +02:00
|
|
|
stream: None,
|
|
|
|
request: req,
|
|
|
|
disconnected: false,
|
|
|
|
};
|
2018-07-04 13:04:23 +02:00
|
|
|
ctx.add_stream(stream);
|
2018-06-16 23:10:44 +02:00
|
|
|
|
|
|
|
let act = f(&mut ctx);
|
2018-07-04 13:04:23 +02:00
|
|
|
Body::Actor(Box::new(WebsocketContextFut::new(ctx, act, mb)))
|
2018-01-10 19:12:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
impl<A, S> WebsocketContext<A, S>
|
|
|
|
where
|
|
|
|
A: Actor<Context = Self>,
|
|
|
|
{
|
2018-01-10 19:12:34 +01:00
|
|
|
/// Write payload
|
|
|
|
#[inline]
|
2018-02-10 09:05:20 +01:00
|
|
|
fn write(&mut self, data: Binary) {
|
2018-01-10 19:12:34 +01:00
|
|
|
if !self.disconnected {
|
2018-02-10 05:43:14 +01:00
|
|
|
if self.stream.is_none() {
|
|
|
|
self.stream = Some(SmallVec::new());
|
|
|
|
}
|
|
|
|
let stream = self.stream.as_mut().unwrap();
|
2018-02-10 09:05:20 +01:00
|
|
|
stream.push(ContextFrame::Chunk(Some(data)));
|
2018-01-10 19:12:34 +01:00
|
|
|
} else {
|
|
|
|
warn!("Trying to write to disconnected response");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Shared application state
|
|
|
|
#[inline]
|
|
|
|
pub fn state(&self) -> &S {
|
|
|
|
self.request.state()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Incoming request
|
|
|
|
#[inline]
|
|
|
|
pub fn request(&mut self) -> &mut HttpRequest<S> {
|
|
|
|
&mut self.request
|
|
|
|
}
|
|
|
|
|
2018-05-02 07:25:31 +02:00
|
|
|
/// Returns drain future
|
|
|
|
pub fn drain(&mut self) -> Drain<A> {
|
|
|
|
let (tx, rx) = oneshot::channel();
|
|
|
|
self.add_frame(ContextFrame::Drain(tx));
|
|
|
|
Drain::new(rx)
|
|
|
|
}
|
|
|
|
|
2018-05-09 14:48:06 +02:00
|
|
|
/// Send text frame
|
|
|
|
#[inline]
|
2018-05-09 15:05:16 +02:00
|
|
|
pub fn text<T: Into<Binary>>(&mut self, text: T) {
|
2018-05-09 14:48:06 +02:00
|
|
|
self.write(Frame::message(text.into(), OpCode::Text, true, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send binary frame
|
|
|
|
#[inline]
|
2018-05-09 15:05:16 +02:00
|
|
|
pub fn binary<B: Into<Binary>>(&mut self, data: B) {
|
2018-05-09 14:48:06 +02:00
|
|
|
self.write(Frame::message(data, OpCode::Binary, true, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send ping frame
|
|
|
|
#[inline]
|
2018-05-09 15:05:16 +02:00
|
|
|
pub fn ping(&mut self, message: &str) {
|
2018-05-09 14:48:06 +02:00
|
|
|
self.write(Frame::message(
|
|
|
|
Vec::from(message),
|
|
|
|
OpCode::Ping,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send pong frame
|
|
|
|
#[inline]
|
2018-05-09 15:05:16 +02:00
|
|
|
pub fn pong(&mut self, message: &str) {
|
2018-05-09 14:48:06 +02:00
|
|
|
self.write(Frame::message(
|
|
|
|
Vec::from(message),
|
|
|
|
OpCode::Pong,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send close frame
|
|
|
|
#[inline]
|
2018-05-09 15:05:16 +02:00
|
|
|
pub fn close(&mut self, reason: Option<CloseReason>) {
|
2018-05-09 14:48:06 +02:00
|
|
|
self.write(Frame::close(reason, false));
|
|
|
|
}
|
|
|
|
|
2018-05-02 07:25:31 +02:00
|
|
|
/// Check if connection still open
|
|
|
|
#[inline]
|
|
|
|
pub fn connected(&self) -> bool {
|
|
|
|
!self.disconnected
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn add_frame(&mut self, frame: ContextFrame) {
|
|
|
|
if self.stream.is_none() {
|
|
|
|
self.stream = Some(SmallVec::new());
|
|
|
|
}
|
|
|
|
if let Some(s) = self.stream.as_mut() {
|
|
|
|
s.push(frame)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle of the running future
|
|
|
|
///
|
|
|
|
/// SpawnHandle is the handle returned by `AsyncContext::spawn()` method.
|
|
|
|
pub fn handle(&self) -> SpawnHandle {
|
|
|
|
self.inner.curr_handle()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, S> WsWriter for WebsocketContext<A, S>
|
|
|
|
where
|
|
|
|
A: Actor<Context = Self>,
|
|
|
|
S: 'static,
|
|
|
|
{
|
2018-01-10 19:12:34 +01:00
|
|
|
/// Send text frame
|
2018-02-10 05:43:14 +01:00
|
|
|
#[inline]
|
2018-05-09 14:48:06 +02:00
|
|
|
fn send_text<T: Into<Binary>>(&mut self, text: T) {
|
|
|
|
self.text(text)
|
2018-01-10 19:12:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Send binary frame
|
2018-02-10 05:43:14 +01:00
|
|
|
#[inline]
|
2018-05-09 14:48:06 +02:00
|
|
|
fn send_binary<B: Into<Binary>>(&mut self, data: B) {
|
|
|
|
self.binary(data)
|
2018-01-10 19:12:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Send ping frame
|
2018-02-10 05:43:14 +01:00
|
|
|
#[inline]
|
2018-05-09 14:48:06 +02:00
|
|
|
fn send_ping(&mut self, message: &str) {
|
|
|
|
self.ping(message)
|
2018-01-10 19:12:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Send pong frame
|
2018-02-10 05:43:14 +01:00
|
|
|
#[inline]
|
2018-05-09 14:48:06 +02:00
|
|
|
fn send_pong(&mut self, message: &str) {
|
|
|
|
self.pong(message)
|
2018-01-10 19:12:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Send close frame
|
2018-02-10 05:43:14 +01:00
|
|
|
#[inline]
|
2018-05-09 14:48:06 +02:00
|
|
|
fn send_close(&mut self, reason: Option<CloseReason>) {
|
|
|
|
self.close(reason)
|
2018-01-10 19:12:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-04 13:04:23 +02:00
|
|
|
impl<A, S> AsyncContextParts<A> for WebsocketContext<A, S>
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
|
|
|
A: Actor<Context = Self>,
|
2018-07-04 13:04:23 +02:00
|
|
|
{
|
|
|
|
fn parts(&mut self) -> &mut ContextParts<A> {
|
|
|
|
&mut self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct WebsocketContextFut<A, S>
|
|
|
|
where
|
|
|
|
A: Actor<Context = WebsocketContext<A, S>>,
|
|
|
|
{
|
|
|
|
fut: ContextFut<A, WebsocketContext<A, S>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, S> WebsocketContextFut<A, S>
|
|
|
|
where
|
|
|
|
A: Actor<Context = WebsocketContext<A, S>>,
|
|
|
|
{
|
|
|
|
fn new(ctx: WebsocketContext<A, S>, act: A, mailbox: Mailbox<A>) -> Self {
|
|
|
|
let fut = ContextFut::new(ctx, act, mailbox);
|
|
|
|
WebsocketContextFut { fut }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, S> ActorHttpContext for WebsocketContextFut<A, S>
|
|
|
|
where
|
|
|
|
A: Actor<Context = WebsocketContext<A, S>>,
|
2018-04-14 01:02:01 +02:00
|
|
|
S: 'static,
|
|
|
|
{
|
2018-01-10 19:12:34 +01:00
|
|
|
#[inline]
|
|
|
|
fn disconnected(&mut self) {
|
2018-07-04 13:04:23 +02:00
|
|
|
self.fut.ctx().disconnected = true;
|
|
|
|
self.fut.ctx().stop();
|
2018-01-10 19:12:34 +01:00
|
|
|
}
|
|
|
|
|
2018-01-25 05:12:49 +01:00
|
|
|
fn poll(&mut self) -> Poll<Option<SmallVec<[ContextFrame; 4]>>, Error> {
|
2018-07-04 13:04:23 +02:00
|
|
|
if self.fut.alive() && self.fut.poll().is_err() {
|
2018-04-14 01:02:01 +02:00
|
|
|
return Err(ErrorInternalServerError("error"));
|
2018-01-10 19:12:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// frames
|
2018-07-04 13:04:23 +02:00
|
|
|
if let Some(data) = self.fut.ctx().stream.take() {
|
2018-01-14 01:17:33 +01:00
|
|
|
Ok(Async::Ready(Some(data)))
|
2018-07-04 13:04:23 +02:00
|
|
|
} else if self.fut.alive() {
|
2018-01-10 19:12:34 +01:00
|
|
|
Ok(Async::NotReady)
|
|
|
|
} else {
|
|
|
|
Ok(Async::Ready(None))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 14:02:49 +02:00
|
|
|
impl<A, M, S> ToEnvelope<A, M> for WebsocketContext<A, S>
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
|
|
|
A: Actor<Context = WebsocketContext<A, S>> + Handler<M>,
|
2018-07-04 13:04:23 +02:00
|
|
|
M: ActixMessage + Send + 'static,
|
2018-04-14 01:02:01 +02:00
|
|
|
M::Result: Send,
|
2018-01-10 19:12:34 +01:00
|
|
|
{
|
2018-05-27 14:02:49 +02:00
|
|
|
fn pack(msg: M, tx: Option<Sender<M::Result>>) -> Envelope<A> {
|
|
|
|
Envelope::new(msg, tx)
|
2018-01-10 19:12:34 +01:00
|
|
|
}
|
|
|
|
}
|