11use crate :: common:: command:: FormatRequestData ;
22use crate :: common:: config:: WebDriverConfig ;
3- use crate :: error:: WebDriverResult ;
3+ use crate :: error:: { WebDriverError , WebDriverResult } ;
44use crate :: http:: connection_async:: WebDriverHttpClientAsync ;
55use crate :: webdrivercommands:: WebDriverCommands ;
6- use crate :: SessionId ;
6+ use crate :: { RequestData , SessionId } ;
77use async_trait:: async_trait;
8- use std:: sync:: Arc ;
8+ use futures:: channel:: mpsc:: { unbounded, UnboundedReceiver , UnboundedSender } ;
9+ use futures:: channel:: oneshot;
10+ use futures:: SinkExt ;
11+ use futures:: StreamExt ;
12+
13+ #[ derive( Debug ) ]
14+ pub enum SessionMessage {
15+ Request ( RequestData , oneshot:: Sender < WebDriverResult < serde_json:: Value > > ) ,
16+ }
17+
18+ pub fn spawn_session_task (
19+ conn : Box < dyn WebDriverHttpClientAsync > ,
20+ ) -> UnboundedSender < SessionMessage > {
21+ let ( tx, rx) = unbounded ( ) ;
22+
23+ #[ cfg( feature = "async-std-runtime" ) ]
24+ {
25+ async_std:: task:: spawn ( session_runner ( rx, conn) ) ;
26+ }
27+
28+ #[ cfg( all( feature = "tokio-runtime" , not( feature = "async-std-runtime" ) ) ) ]
29+ {
30+ tokio:: spawn ( session_runner ( rx, conn) ) ;
31+ }
32+
33+ tx
34+ }
35+
36+ async fn session_runner (
37+ mut rx : UnboundedReceiver < SessionMessage > ,
38+ conn : Box < dyn WebDriverHttpClientAsync > ,
39+ ) {
40+ // This will return None when the sender hangs up.
41+ while let Some ( msg) = rx. next ( ) . await {
42+ match msg {
43+ SessionMessage :: Request ( data, tx) => {
44+ let ret = conn. execute ( data) . await ;
45+ tx. send ( ret) . expect ( "Failed to send response" ) ;
46+ }
47+ }
48+ }
49+ }
950
1051#[ derive( Debug ) ]
1152pub struct WebDriverSession {
1253 session_id : SessionId ,
13- conn : Arc < dyn WebDriverHttpClientAsync > ,
54+ tx : UnboundedSender < SessionMessage > ,
1455 config : WebDriverConfig ,
1556}
1657
1758impl WebDriverSession {
18- pub fn new ( session_id : SessionId , conn : Arc < dyn WebDriverHttpClientAsync > ) -> Self {
59+ pub fn new ( session_id : SessionId , tx : UnboundedSender < SessionMessage > ) -> Self {
1960 Self {
2061 session_id,
21- conn ,
62+ tx ,
2263 config : WebDriverConfig :: new ( ) ,
2364 }
2465 }
@@ -39,7 +80,21 @@ impl WebDriverSession {
3980 & self ,
4081 request : Box < dyn FormatRequestData + Send + Sync > ,
4182 ) -> WebDriverResult < serde_json:: Value > {
42- self . conn . execute ( request. format_request ( & self . session_id ) ) . await
83+ let ( ret_tx, ret_rx) = oneshot:: channel ( ) ;
84+ self . tx
85+ . clone ( )
86+ . send ( SessionMessage :: Request ( request. format_request ( & self . session_id ) , ret_tx) )
87+ . await
88+ . map_err ( |e| {
89+ WebDriverError :: UnknownResponse ( format ! ( "Failed to send request to server: {}" , e) )
90+ } ) ?;
91+
92+ match ret_rx. await {
93+ Ok ( x) => x,
94+ Err ( oneshot:: Canceled ) => Err ( WebDriverError :: UnknownResponse (
95+ "Failed to get response from server" . to_string ( ) ,
96+ ) ) ,
97+ }
4398 }
4499}
45100
0 commit comments