1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
use std::time::Duration; use crate::logic::ServerLogic; use async_channel::Sender; use async_handle::Handle; use basws_shared::{ protocol::ServerResponse, protocol::{InstallationConfig, WsBatchResponse}, timing::current_timestamp, timing::NetworkTiming, }; pub struct ConnectedClient<L> where L: ServerLogic + ?Sized, { data: Handle<ConnectedClientData<L>>, } impl<L> Clone for ConnectedClient<L> where L: ServerLogic, { fn clone(&self) -> Self { Self { data: self.data.clone(), } } } struct ConnectedClientData<L> where L: ServerLogic + ?Sized, { client: Handle<L::Client>, pub installation: Option<InstallationConfig>, pub(crate) nonce: Option<[u8; 32]>, sender: Sender<WsBatchResponse<L::Response>>, pub account: Option<Handle<L::Account>>, pub network_timing: NetworkTiming, } impl<L> ConnectedClient<L> where L: ServerLogic + 'static, { pub(crate) fn new(client: L::Client, sender: Sender<WsBatchResponse<L::Response>>) -> Self { Self::new_with_installation(None, client, sender) } pub(crate) fn new_with_installation( installation: Option<InstallationConfig>, client: L::Client, sender: Sender<WsBatchResponse<L::Response>>, ) -> Self { Self { data: Handle::new(ConnectedClientData { sender, client: Handle::new(client), nonce: None, account: None, installation, network_timing: Default::default(), }), } } pub(crate) async fn send(&self, response: WsBatchResponse<L::Response>) -> anyhow::Result<()> { let data = self.data.read().await; Ok(data.sender.send(response).await?) } pub async fn send_response(&self, response: L::Response) -> anyhow::Result<()> { let data = self.data.read().await; Ok(data .sender .send(WsBatchResponse::from_response(response)) .await?) } pub async fn installation(&self) -> Option<InstallationConfig> { let data = self.data.read().await; data.installation } pub async fn set_installation(&self, installation: InstallationConfig) { let mut data = self.data.write().await; data.installation = Some(installation); } pub async fn client(&self) -> Handle<L::Client> { let data = self.data.read().await; data.client.clone() } pub async fn map_client<F: FnOnce(&L::Client) -> R, R>(&self, map_fn: F) -> R { let data = self.data.read().await; let client = data.client.read().await; map_fn(&client) } pub async fn map_client_mut<F: FnOnce(&mut L::Client) -> R, R>(&self, map_fn: F) -> R { let data = self.data.read().await; let mut client = data.client.write().await; map_fn(&mut client) } pub async fn account(&self) -> Option<Handle<L::Account>> { let data = self.data.read().await; data.account.clone() } pub async fn set_account(&self, account: Handle<L::Account>) { let mut data = self.data.write().await; data.account = Some(account); } pub async fn nonce(&self) -> Option<[u8; 32]> { let data = self.data.read().await; data.nonce } pub(crate) async fn set_nonce(&self, nonce: [u8; 32]) { let mut data = self.data.write().await; data.nonce = Some(nonce); } pub(crate) async fn update_network_timing(&self, original_timestamp: f64, timestamp: f64) { let mut data = self.data.write().await; data.network_timing.update(original_timestamp, timestamp) } pub(crate) async fn ping_loop(&self, ping_duration: Duration) -> anyhow::Result<()> { loop { let ping = { let data = self.data.read().await; ServerResponse::Ping { average_roundtrip: data.network_timing.average_roundtrip, average_server_timestamp_delta: data .network_timing .average_server_timestamp_delta, timestamp: current_timestamp(), } }; self.send(WsBatchResponse { request_id: None, results: vec![ping], }) .await?; tokio::time::sleep(ping_duration).await } } }