@@ -4,14 +4,13 @@ use std::{fmt, rc::Rc};
44pub use crate :: ws:: { CloseCode , CloseReason , Frame , Message , WsSink } ;
55
66use crate :: http:: { StatusCode , body:: BodySize , h1} ;
7+ use crate :: io:: { DispatchItem , IoConfig , Reason } ;
78use crate :: service:: {
8- IntoServiceFactory , ServiceFactory , apply_fn , chain_factory, fn_factory_with_config,
9+ IntoServiceFactory , ServiceFactory , chain_factory, fn_factory_with_config, fn_service ,
910} ;
1011use crate :: web:: { HttpRequest , HttpResponse } ;
1112use crate :: ws:: { self , error:: HandshakeError , error:: WsError , handshake} ;
12- use crate :: {
13- SharedCfg , io:: DispatchItem , io:: IoConfig , rt, time:: Seconds , util:: Either , util:: Ready ,
14- } ;
13+ use crate :: { SharedCfg , rt, time:: Seconds } ;
1514
1615thread_local ! {
1716 static CFG : SharedCfg = SharedCfg :: new( "WS" )
@@ -29,44 +28,30 @@ where
2928{
3029 let inner_factory = Rc :: new ( chain_factory ( factory) . map_err ( WsError :: Service ) ) ;
3130
32- let factory = fn_factory_with_config ( move |sink : WsSink | {
33- let factory = inner_factory. clone ( ) ;
34-
35- async move {
36- let srv = factory. create ( sink. clone ( ) ) . await ?;
37- let sink = sink. clone ( ) ;
38-
39- Ok :: < _ , T :: InitError > ( apply_fn ( srv, move |req, srv| match req {
40- DispatchItem :: < ws:: Codec > :: Item ( item) => {
41- let s = if matches ! ( item, Frame :: Close ( _) ) {
42- Some ( sink. clone ( ) )
43- } else {
44- None
45- } ;
46- Either :: Left ( async move {
47- let result = srv. call ( item) . await ;
48- if let Some ( s) = s {
49- let _ = rt:: spawn ( async move { s. io ( ) . close ( ) } ) ;
50- }
51- result
52- } )
53- }
54- DispatchItem :: WBackPressureEnabled
55- | DispatchItem :: WBackPressureDisabled => Either :: Right ( Ready :: Ok ( None ) ) ,
56- DispatchItem :: KeepAliveTimeout => {
57- Either :: Right ( Ready :: Err ( WsError :: KeepAlive ) )
58- }
59- DispatchItem :: ReadTimeout => {
60- Either :: Right ( Ready :: Err ( WsError :: ReadTimeout ) )
61- }
62- DispatchItem :: DecoderError ( e) | DispatchItem :: EncoderError ( e) => {
63- Either :: Right ( Ready :: Err ( WsError :: Protocol ( e) ) )
64- }
65- DispatchItem :: Disconnect ( e) => {
66- Either :: Right ( Ready :: Err ( WsError :: Disconnected ( e) ) )
31+ let factory = fn_factory_with_config ( async move |sink : WsSink | {
32+ let srv = inner_factory. pipeline ( sink. clone ( ) ) . await ?;
33+ let sink = sink. clone ( ) ;
34+
35+ Ok :: < _ , T :: InitError > ( fn_service ( async move |req| match req {
36+ DispatchItem :: < ws:: Codec > :: Item ( item) => {
37+ let s = if matches ! ( item, Frame :: Close ( _) ) {
38+ Some ( sink. clone ( ) )
39+ } else {
40+ None
41+ } ;
42+ let result = srv. call ( item) . await ;
43+ if let Some ( s) = s {
44+ let _ = rt:: spawn ( async move { s. io ( ) . close ( ) } ) ;
6745 }
68- } ) )
69- }
46+ result
47+ }
48+ DispatchItem :: Control ( _) => Ok ( None ) ,
49+ DispatchItem :: Stop ( Reason :: KeepAliveTimeout ) => Err ( WsError :: KeepAlive ) ,
50+ DispatchItem :: Stop ( Reason :: ReadTimeout ) => Err ( WsError :: ReadTimeout ) ,
51+ DispatchItem :: Stop ( Reason :: Decoder ( e) )
52+ | DispatchItem :: Stop ( Reason :: Encoder ( e) ) => Err ( WsError :: Protocol ( e) ) ,
53+ DispatchItem :: Stop ( Reason :: Io ( e) ) => Err ( WsError :: Disconnected ( e) ) ,
54+ } ) )
7055 } ) ;
7156
7257 start_with ( req, factory) . await
0 commit comments