2018-01-10 19:12:34 +01:00
|
|
|
use std::mem;
|
|
|
|
use futures::{Async, Poll};
|
|
|
|
use futures::sync::oneshot::Sender;
|
|
|
|
use futures::unsync::oneshot;
|
2018-01-14 01:17:33 +01:00
|
|
|
use smallvec::SmallVec;
|
2018-01-10 19:12:34 +01:00
|
|
|
|
|
|
|
use actix::{Actor, ActorState, ActorContext, AsyncContext,
|
2018-01-27 19:04:56 +01:00
|
|
|
Address, SyncAddress, Handler, ResponseType, SpawnHandle, MessageResult};
|
2018-01-10 19:12:34 +01:00
|
|
|
use actix::fut::ActorFuture;
|
2018-01-31 21:57:02 +01:00
|
|
|
use actix::dev::{ContextImpl, Envelope, ToEnvelope, RemoteEnvelope};
|
2018-01-10 19:12:34 +01:00
|
|
|
|
|
|
|
use body::{Body, Binary};
|
2018-01-27 19:04:56 +01:00
|
|
|
use error::{Error, ErrorInternalServerError};
|
2018-01-10 19:12:34 +01:00
|
|
|
use httprequest::HttpRequest;
|
2018-01-10 20:13:29 +01:00
|
|
|
use context::{Frame as ContextFrame, ActorHttpContext, Drain};
|
2018-01-10 19:12:34 +01:00
|
|
|
|
2018-01-10 20:13:29 +01:00
|
|
|
use ws::frame::Frame;
|
|
|
|
use ws::proto::{OpCode, CloseCode};
|
2018-01-10 19:12:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
/// Http actor execution context
|
|
|
|
pub struct WebsocketContext<A, S=()> where A: Actor<Context=WebsocketContext<A, S>>,
|
|
|
|
{
|
|
|
|
inner: ContextImpl<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,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, S> ActorContext for WebsocketContext<A, S> where A: Actor<Context=Self>
|
|
|
|
{
|
|
|
|
fn stop(&mut self) {
|
|
|
|
self.inner.stop();
|
|
|
|
}
|
|
|
|
fn terminate(&mut self) {
|
|
|
|
self.inner.terminate()
|
|
|
|
}
|
|
|
|
fn state(&self) -> ActorState {
|
|
|
|
self.inner.state()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, S> AsyncContext<A> for WebsocketContext<A, S> where A: Actor<Context=Self>
|
|
|
|
{
|
|
|
|
fn spawn<F>(&mut self, fut: F) -> SpawnHandle
|
|
|
|
where F: ActorFuture<Item=(), Error=(), Actor=A> + 'static
|
|
|
|
{
|
|
|
|
self.inner.spawn(fut)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wait<F>(&mut self, fut: F)
|
|
|
|
where F: ActorFuture<Item=(), Error=(), Actor=A> + 'static
|
|
|
|
{
|
|
|
|
self.inner.wait(fut)
|
|
|
|
}
|
|
|
|
|
2018-01-23 01:55:50 +01:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[inline]
|
|
|
|
fn waiting(&self) -> bool {
|
2018-01-25 05:17:14 +01:00
|
|
|
self.inner.waiting() || self.inner.state() == ActorState::Stopping ||
|
2018-01-24 00:39:53 +01: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)
|
|
|
|
}
|
|
|
|
|
2018-01-31 21:57:02 +01:00
|
|
|
#[doc(hidden)]
|
2018-01-10 19:12:34 +01:00
|
|
|
#[inline]
|
2018-01-31 21:57:02 +01:00
|
|
|
fn local_address(&mut self) -> Address<A> {
|
2018-01-10 19:12:34 +01:00
|
|
|
self.inner.unsync_address()
|
|
|
|
}
|
|
|
|
|
2018-01-31 21:57:02 +01:00
|
|
|
#[doc(hidden)]
|
2018-01-10 19:12:34 +01:00
|
|
|
#[inline]
|
|
|
|
fn sync_address(&mut self) -> SyncAddress<A> {
|
|
|
|
self.inner.sync_address()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, S: 'static> WebsocketContext<A, S> where A: Actor<Context=Self> {
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn new(req: HttpRequest<S>, actor: A) -> WebsocketContext<A, S> {
|
|
|
|
WebsocketContext::from_request(req).actor(actor)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_request(req: HttpRequest<S>) -> WebsocketContext<A, S> {
|
|
|
|
WebsocketContext {
|
|
|
|
inner: ContextImpl::new(None),
|
2018-01-14 01:17:33 +01:00
|
|
|
stream: None,
|
2018-01-10 19:12:34 +01:00
|
|
|
request: req,
|
|
|
|
disconnected: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn actor(mut self, actor: A) -> WebsocketContext<A, S> {
|
|
|
|
self.inner.set_actor(actor);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, S> WebsocketContext<A, S> where A: Actor<Context=Self> {
|
|
|
|
|
|
|
|
/// Write payload
|
|
|
|
#[inline]
|
|
|
|
fn write<B: Into<Binary>>(&mut self, data: B) {
|
|
|
|
if !self.disconnected {
|
2018-01-14 01:17:33 +01:00
|
|
|
self.add_frame(ContextFrame::Chunk(Some(data.into())));
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send text frame
|
|
|
|
pub fn text(&mut self, text: &str) {
|
2018-01-10 20:13:29 +01:00
|
|
|
let mut frame = Frame::message(Vec::from(text), OpCode::Text, true);
|
2018-01-10 19:12:34 +01:00
|
|
|
let mut buf = Vec::new();
|
|
|
|
frame.format(&mut buf).unwrap();
|
|
|
|
|
|
|
|
self.write(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send binary frame
|
|
|
|
pub fn binary<B: Into<Binary>>(&mut self, data: B) {
|
2018-01-10 20:13:29 +01:00
|
|
|
let mut frame = Frame::message(data, OpCode::Binary, true);
|
2018-01-10 19:12:34 +01:00
|
|
|
let mut buf = Vec::new();
|
|
|
|
frame.format(&mut buf).unwrap();
|
|
|
|
|
|
|
|
self.write(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send ping frame
|
|
|
|
pub fn ping(&mut self, message: &str) {
|
2018-01-10 20:13:29 +01:00
|
|
|
let mut frame = Frame::message(Vec::from(message), OpCode::Ping, true);
|
2018-01-10 19:12:34 +01:00
|
|
|
let mut buf = Vec::new();
|
|
|
|
frame.format(&mut buf).unwrap();
|
|
|
|
|
|
|
|
self.write(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send pong frame
|
|
|
|
pub fn pong(&mut self, message: &str) {
|
2018-01-10 20:13:29 +01:00
|
|
|
let mut frame = Frame::message(Vec::from(message), OpCode::Pong, true);
|
2018-01-10 19:12:34 +01:00
|
|
|
let mut buf = Vec::new();
|
|
|
|
frame.format(&mut buf).unwrap();
|
|
|
|
|
|
|
|
self.write(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send close frame
|
|
|
|
pub fn close(&mut self, code: CloseCode, reason: &str) {
|
2018-01-10 20:13:29 +01:00
|
|
|
let mut frame = Frame::close(code, reason);
|
2018-01-10 19:12:34 +01:00
|
|
|
let mut buf = Vec::new();
|
|
|
|
frame.format(&mut buf).unwrap();
|
|
|
|
self.write(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns drain future
|
|
|
|
pub fn drain(&mut self) -> Drain<A> {
|
|
|
|
let (tx, rx) = oneshot::channel();
|
|
|
|
self.inner.modify();
|
2018-01-14 01:17:33 +01:00
|
|
|
self.add_frame(ContextFrame::Drain(tx));
|
2018-01-10 19:12:34 +01:00
|
|
|
Drain::new(rx)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if connection still open
|
|
|
|
#[inline]
|
|
|
|
pub fn connected(&self) -> bool {
|
|
|
|
!self.disconnected
|
|
|
|
}
|
2018-01-14 01:17:33 +01:00
|
|
|
|
|
|
|
fn add_frame(&mut self, frame: ContextFrame) {
|
|
|
|
if self.stream.is_none() {
|
|
|
|
self.stream = Some(SmallVec::new());
|
|
|
|
}
|
|
|
|
self.stream.as_mut().map(|s| s.push(frame));
|
|
|
|
}
|
2018-01-28 18:47:46 +01:00
|
|
|
|
|
|
|
/// Handle of the running future
|
|
|
|
///
|
|
|
|
/// SpawnHandle is the handle returned by `AsyncContext::spawn()` method.
|
|
|
|
pub fn handle(&self) -> SpawnHandle {
|
|
|
|
self.inner.curr_handle()
|
|
|
|
}
|
2018-01-10 19:12:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, S> ActorHttpContext for WebsocketContext<A, S> where A: Actor<Context=Self>, S: 'static {
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn disconnected(&mut self) {
|
|
|
|
self.disconnected = true;
|
|
|
|
self.stop();
|
|
|
|
}
|
|
|
|
|
2018-01-25 05:12:49 +01:00
|
|
|
fn poll(&mut self) -> Poll<Option<SmallVec<[ContextFrame; 4]>>, Error> {
|
2018-01-10 19:12:34 +01:00
|
|
|
let ctx: &mut WebsocketContext<A, S> = unsafe {
|
|
|
|
mem::transmute(self as &mut WebsocketContext<A, S>)
|
|
|
|
};
|
|
|
|
|
2018-02-10 02:20:28 +01:00
|
|
|
if self.inner.alive() && self.inner.poll(ctx).is_err() {
|
|
|
|
return Err(ErrorInternalServerError("error").into())
|
2018-01-10 19:12:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// frames
|
2018-01-14 01:17:33 +01:00
|
|
|
if let Some(data) = self.stream.take() {
|
|
|
|
Ok(Async::Ready(Some(data)))
|
2018-01-10 19:12:34 +01:00
|
|
|
} else if self.inner.alive() {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
} else {
|
|
|
|
Ok(Async::Ready(None))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, S> ToEnvelope<A> for WebsocketContext<A, S>
|
|
|
|
where A: Actor<Context=WebsocketContext<A, S>>,
|
|
|
|
{
|
|
|
|
#[inline]
|
2018-01-31 21:57:02 +01:00
|
|
|
fn pack<M>(msg: M, tx: Option<Sender<MessageResult<M>>>) -> Envelope<A>
|
2018-01-10 19:12:34 +01:00
|
|
|
where A: Handler<M>,
|
|
|
|
M: ResponseType + Send + 'static, M::Item: Send, M::Error: Send {
|
2018-01-27 19:04:56 +01:00
|
|
|
RemoteEnvelope::envelope(msg, tx).into()
|
2018-01-10 19:12:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, S> From<WebsocketContext<A, S>> for Body
|
|
|
|
where A: Actor<Context=WebsocketContext<A, S>>, S: 'static
|
|
|
|
{
|
|
|
|
fn from(ctx: WebsocketContext<A, S>) -> Body {
|
|
|
|
Body::Actor(Box::new(ctx))
|
|
|
|
}
|
|
|
|
}
|