mirror of
https://github.com/fafhrd91/actix-web
synced 2025-01-18 22:01:50 +01:00
move websocket code to submodule
This commit is contained in:
parent
d85081b64e
commit
3f3dcf413b
@ -107,9 +107,6 @@ mod pipeline;
|
|||||||
mod server;
|
mod server;
|
||||||
mod worker;
|
mod worker;
|
||||||
mod channel;
|
mod channel;
|
||||||
mod wsframe;
|
|
||||||
mod wsproto;
|
|
||||||
mod wscontext;
|
|
||||||
mod h1;
|
mod h1;
|
||||||
mod h2;
|
mod h2;
|
||||||
mod h1writer;
|
mod h1writer;
|
||||||
|
@ -13,18 +13,17 @@ use actix::dev::{queue, AsyncContextApi,
|
|||||||
use body::{Body, Binary};
|
use body::{Body, Binary};
|
||||||
use error::{Error, Result, ErrorInternalServerError};
|
use error::{Error, Result, ErrorInternalServerError};
|
||||||
use httprequest::HttpRequest;
|
use httprequest::HttpRequest;
|
||||||
use context::{Frame, ActorHttpContext, Drain};
|
use context::{Frame as ContextFrame, ActorHttpContext, Drain};
|
||||||
|
|
||||||
use wsframe;
|
use ws::frame::Frame;
|
||||||
use wsproto::*;
|
use ws::proto::{OpCode, CloseCode};
|
||||||
pub use wsproto::CloseCode;
|
|
||||||
|
|
||||||
|
|
||||||
/// Http actor execution context
|
/// Http actor execution context
|
||||||
pub struct WebsocketContext<A, S=()> where A: Actor<Context=WebsocketContext<A, S>>,
|
pub struct WebsocketContext<A, S=()> where A: Actor<Context=WebsocketContext<A, S>>,
|
||||||
{
|
{
|
||||||
inner: ContextImpl<A>,
|
inner: ContextImpl<A>,
|
||||||
stream: VecDeque<Frame>,
|
stream: VecDeque<ContextFrame>,
|
||||||
request: HttpRequest<S>,
|
request: HttpRequest<S>,
|
||||||
disconnected: bool,
|
disconnected: bool,
|
||||||
}
|
}
|
||||||
@ -108,7 +107,7 @@ impl<A, S> WebsocketContext<A, S> where A: Actor<Context=Self> {
|
|||||||
#[inline]
|
#[inline]
|
||||||
fn write<B: Into<Binary>>(&mut self, data: B) {
|
fn write<B: Into<Binary>>(&mut self, data: B) {
|
||||||
if !self.disconnected {
|
if !self.disconnected {
|
||||||
self.stream.push_back(Frame::Payload(Some(data.into())));
|
self.stream.push_back(ContextFrame::Payload(Some(data.into())));
|
||||||
} else {
|
} else {
|
||||||
warn!("Trying to write to disconnected response");
|
warn!("Trying to write to disconnected response");
|
||||||
}
|
}
|
||||||
@ -128,7 +127,7 @@ impl<A, S> WebsocketContext<A, S> where A: Actor<Context=Self> {
|
|||||||
|
|
||||||
/// Send text frame
|
/// Send text frame
|
||||||
pub fn text(&mut self, text: &str) {
|
pub fn text(&mut self, text: &str) {
|
||||||
let mut frame = wsframe::Frame::message(Vec::from(text), OpCode::Text, true);
|
let mut frame = Frame::message(Vec::from(text), OpCode::Text, true);
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
frame.format(&mut buf).unwrap();
|
frame.format(&mut buf).unwrap();
|
||||||
|
|
||||||
@ -137,7 +136,7 @@ impl<A, S> WebsocketContext<A, S> where A: Actor<Context=Self> {
|
|||||||
|
|
||||||
/// Send binary frame
|
/// Send binary frame
|
||||||
pub fn binary<B: Into<Binary>>(&mut self, data: B) {
|
pub fn binary<B: Into<Binary>>(&mut self, data: B) {
|
||||||
let mut frame = wsframe::Frame::message(data, OpCode::Binary, true);
|
let mut frame = Frame::message(data, OpCode::Binary, true);
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
frame.format(&mut buf).unwrap();
|
frame.format(&mut buf).unwrap();
|
||||||
|
|
||||||
@ -146,7 +145,7 @@ impl<A, S> WebsocketContext<A, S> where A: Actor<Context=Self> {
|
|||||||
|
|
||||||
/// Send ping frame
|
/// Send ping frame
|
||||||
pub fn ping(&mut self, message: &str) {
|
pub fn ping(&mut self, message: &str) {
|
||||||
let mut frame = wsframe::Frame::message(Vec::from(message), OpCode::Ping, true);
|
let mut frame = Frame::message(Vec::from(message), OpCode::Ping, true);
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
frame.format(&mut buf).unwrap();
|
frame.format(&mut buf).unwrap();
|
||||||
|
|
||||||
@ -155,7 +154,7 @@ impl<A, S> WebsocketContext<A, S> where A: Actor<Context=Self> {
|
|||||||
|
|
||||||
/// Send pong frame
|
/// Send pong frame
|
||||||
pub fn pong(&mut self, message: &str) {
|
pub fn pong(&mut self, message: &str) {
|
||||||
let mut frame = wsframe::Frame::message(Vec::from(message), OpCode::Pong, true);
|
let mut frame = Frame::message(Vec::from(message), OpCode::Pong, true);
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
frame.format(&mut buf).unwrap();
|
frame.format(&mut buf).unwrap();
|
||||||
|
|
||||||
@ -164,7 +163,7 @@ impl<A, S> WebsocketContext<A, S> where A: Actor<Context=Self> {
|
|||||||
|
|
||||||
/// Send close frame
|
/// Send close frame
|
||||||
pub fn close(&mut self, code: CloseCode, reason: &str) {
|
pub fn close(&mut self, code: CloseCode, reason: &str) {
|
||||||
let mut frame = wsframe::Frame::close(code, reason);
|
let mut frame = Frame::close(code, reason);
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
frame.format(&mut buf).unwrap();
|
frame.format(&mut buf).unwrap();
|
||||||
self.write(buf);
|
self.write(buf);
|
||||||
@ -174,7 +173,7 @@ impl<A, S> WebsocketContext<A, S> where A: Actor<Context=Self> {
|
|||||||
pub fn drain(&mut self) -> Drain<A> {
|
pub fn drain(&mut self) -> Drain<A> {
|
||||||
let (tx, rx) = oneshot::channel();
|
let (tx, rx) = oneshot::channel();
|
||||||
self.inner.modify();
|
self.inner.modify();
|
||||||
self.stream.push_back(Frame::Drain(tx));
|
self.stream.push_back(ContextFrame::Drain(tx));
|
||||||
Drain::new(rx)
|
Drain::new(rx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -213,7 +212,7 @@ impl<A, S> ActorHttpContext for WebsocketContext<A, S> where A: Actor<Context=Se
|
|||||||
self.stop();
|
self.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Option<Frame>, Error> {
|
fn poll(&mut self) -> Poll<Option<ContextFrame>, Error> {
|
||||||
let ctx: &mut WebsocketContext<A, S> = unsafe {
|
let ctx: &mut WebsocketContext<A, S> = unsafe {
|
||||||
mem::transmute(self as &mut WebsocketContext<A, S>)
|
mem::transmute(self as &mut WebsocketContext<A, S>)
|
||||||
};
|
};
|
@ -4,7 +4,7 @@ use std::iter::FromIterator;
|
|||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
|
|
||||||
use body::Binary;
|
use body::Binary;
|
||||||
use wsproto::{OpCode, CloseCode};
|
use ws::proto::{OpCode, CloseCode};
|
||||||
|
|
||||||
|
|
||||||
fn apply_mask(buf: &mut [u8], mask: &[u8; 4]) {
|
fn apply_mask(buf: &mut [u8], mask: &[u8; 4]) {
|
@ -55,10 +55,14 @@ use error::{Error, WsHandshakeError};
|
|||||||
use httprequest::HttpRequest;
|
use httprequest::HttpRequest;
|
||||||
use httpresponse::{ConnectionType, HttpResponse, HttpResponseBuilder};
|
use httpresponse::{ConnectionType, HttpResponse, HttpResponseBuilder};
|
||||||
|
|
||||||
use wsframe;
|
mod frame;
|
||||||
use wsproto::*;
|
mod proto;
|
||||||
pub use wsproto::CloseCode;
|
mod context;
|
||||||
pub use wscontext::WebsocketContext;
|
|
||||||
|
use ws::frame::Frame;
|
||||||
|
use ws::proto::{hash_key, OpCode};
|
||||||
|
pub use ws::proto::CloseCode;
|
||||||
|
pub use ws::context::WebsocketContext;
|
||||||
|
|
||||||
const SEC_WEBSOCKET_ACCEPT: &str = "SEC-WEBSOCKET-ACCEPT";
|
const SEC_WEBSOCKET_ACCEPT: &str = "SEC-WEBSOCKET-ACCEPT";
|
||||||
const SEC_WEBSOCKET_KEY: &str = "SEC-WEBSOCKET-KEY";
|
const SEC_WEBSOCKET_KEY: &str = "SEC-WEBSOCKET-KEY";
|
||||||
@ -207,7 +211,7 @@ impl Stream for WsStream {
|
|||||||
}
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match wsframe::Frame::parse(&mut self.buf) {
|
match Frame::parse(&mut self.buf) {
|
||||||
Ok(Some(frame)) => {
|
Ok(Some(frame)) => {
|
||||||
// trace!("WsFrame {}", frame);
|
// trace!("WsFrame {}", frame);
|
||||||
let (_finished, opcode, payload) = frame.unpack();
|
let (_finished, opcode, payload) = frame.unpack();
|
Loading…
x
Reference in New Issue
Block a user