From 54cdb3c2f40216a795b20ca8ce24313cdd2eddba Mon Sep 17 00:00:00 2001 From: Vladimir Borisov Date: Mon, 26 Jul 2021 13:40:06 +0300 Subject: [PATCH] Move IPC erlated stuff in separate file --- src/stremio_app/ipc.rs | 54 +++++++++++++++++++ src/stremio_app/mod.rs | 4 +- src/stremio_app/stremio_app.rs | 52 +----------------- .../stremio_player/stremio_player.rs | 3 +- .../stremio_wevbiew/stremio_wevbiew.rs | 3 +- 5 files changed, 63 insertions(+), 53 deletions(-) create mode 100644 src/stremio_app/ipc.rs diff --git a/src/stremio_app/ipc.rs b/src/stremio_app/ipc.rs new file mode 100644 index 0000000..8305bce --- /dev/null +++ b/src/stremio_app/ipc.rs @@ -0,0 +1,54 @@ +use std::cell::RefCell; +use std::sync::mpsc; +use std::sync::{Arc, Mutex}; +use serde::{Deserialize, Serialize}; +use serde_json::{self, json}; + +pub type Channel = RefCell, Arc>>)>>; + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct RPCRequest { + pub id: u64, + pub args: Option>, +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct RPCResponseDataTransport { + pub properties: Vec>, + pub signals: Vec, + pub methods: Vec>, +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct RPCResponseData { + pub transport: RPCResponseDataTransport, +} + +#[derive(Default, Serialize, Deserialize, Debug, Clone)] +pub struct RPCResponse { + pub id: u64, + pub object: String, + #[serde(rename = "type")] + pub response_type: u32, + #[serde(skip_serializing_if = "Option::is_none")] + pub data: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub args: Option, +} + +impl RPCResponse { + pub fn visibility_change(visible: bool, visibility: u32, is_full_screen: bool) -> String { + let resp = RPCResponse { + id: 1, + object: "transport".to_string(), + response_type: 1, + args: Some(json!(["win-visibility-changed" ,{ + "visible": visible, + "visibility": visibility, + "isFullscreen": is_full_screen + }])), + ..Default::default() + }; + serde_json::to_string(&resp).expect("Cannot build response") + } +} diff --git a/src/stremio_app/mod.rs b/src/stremio_app/mod.rs index cdeff12..688cf13 100644 --- a/src/stremio_app/mod.rs +++ b/src/stremio_app/mod.rs @@ -5,4 +5,6 @@ pub use stremio_wevbiew::WebView; pub mod stremio_player; pub use stremio_player::Player; pub mod stremio_server; -pub use stremio_server::StremioServer; \ No newline at end of file +pub use stremio_server::StremioServer; +pub mod ipc; +pub use ipc::{Channel, RPCRequest, RPCResponse, RPCResponseData, RPCResponseDataTransport}; diff --git a/src/stremio_app/stremio_app.rs b/src/stremio_app/stremio_app.rs index 16f8441..e007f47 100644 --- a/src/stremio_app/stremio_app.rs +++ b/src/stremio_app/stremio_app.rs @@ -1,7 +1,6 @@ use native_windows_derive::NwgUi; use native_windows_gui as nwg; -use serde::{Deserialize, Serialize}; -use serde_json::{self, json}; +use serde_json; use std::cell::RefCell; use std::cmp; use std::sync::Arc; @@ -12,57 +11,10 @@ use winapi::um::winuser::{ WS_EX_WINDOWEDGE, WS_THICKFRAME, }; +use crate::stremio_app::ipc::{RPCRequest, RPCResponse, RPCResponseData, RPCResponseDataTransport}; use crate::stremio_app::stremio_player::Player; use crate::stremio_app::stremio_wevbiew::WebView; -////////////////////////////////////////// -#[derive(Serialize, Deserialize, Debug, Clone)] -struct RPCRequest { - id: u64, - args: Option>, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -struct RPCResponseDataTransport { - properties: Vec>, - signals: Vec, - methods: Vec>, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -struct RPCResponseData { - transport: RPCResponseDataTransport, -} - -#[derive(Default, Serialize, Deserialize, Debug, Clone)] -struct RPCResponse { - id: u64, - object: String, - #[serde(rename = "type")] - response_type: u32, - #[serde(skip_serializing_if = "Option::is_none")] - data: Option, - #[serde(skip_serializing_if = "Option::is_none")] - args: Option, -} - -impl RPCResponse { - fn visibility_change(visible: bool, visibility: u32, is_full_screen: bool) -> String { - let resp = RPCResponse { - id: 1, - object: "transport".to_string(), - response_type: 1, - args: Some(json!(["win-visibility-changed" ,{ - "visible": visible, - "visibility": visibility, - "isFullscreen": is_full_screen - }])), - ..Default::default() - }; - serde_json::to_string(&resp).expect("Cannot build response") - } -} -////////////////////////////////////////// #[derive(Default)] pub struct WindowStyle { pub full_screen: bool, diff --git a/src/stremio_app/stremio_player/stremio_player.rs b/src/stremio_app/stremio_player/stremio_player.rs index adfa64c..6c405bf 100644 --- a/src/stremio_app/stremio_player/stremio_player.rs +++ b/src/stremio_app/stremio_player/stremio_player.rs @@ -4,6 +4,7 @@ use std::cell::RefCell; use std::sync::mpsc; use std::sync::{Arc, Mutex}; use std::thread; +use crate::stremio_app::ipc; #[derive(Default, Serialize, Deserialize, Debug, Clone)] pub struct MpvEvent { @@ -48,7 +49,7 @@ impl MpvEvent { #[derive(Default)] pub struct Player { - pub channel: RefCell, Arc>>)>>, + pub channel: ipc::Channel, } impl PartialUi for Player { diff --git a/src/stremio_app/stremio_wevbiew/stremio_wevbiew.rs b/src/stremio_app/stremio_wevbiew/stremio_wevbiew.rs index 3208585..730756a 100644 --- a/src/stremio_app/stremio_wevbiew/stremio_wevbiew.rs +++ b/src/stremio_app/stremio_wevbiew/stremio_wevbiew.rs @@ -13,12 +13,13 @@ use urlencoding::decode; use webview2::Controller; use winapi::shared::windef::HWND__; use winapi::um::winuser::*; +use crate::stremio_app::ipc; #[derive(Default)] pub struct WebView { pub endpoint: Rc>, controller: Rc>, - pub channel: RefCell, Arc>>)>>, + pub channel: ipc::Channel, notice: nwg::Notice, compute: RefCell>>, message_queue: Arc>>,