mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
implement sigchat SignalWS read(timeout)
This commit is contained in:
parent
535ee5de11
commit
146666cb0e
2 changed files with 56 additions and 16 deletions
|
|
@ -11,6 +11,7 @@ use locales::t;
|
||||||
use modals::Modals;
|
use modals::Modals;
|
||||||
use signal_ws::SignalWS;
|
use signal_ws::SignalWS;
|
||||||
use std::io::{Error, ErrorKind};
|
use std::io::{Error, ErrorKind};
|
||||||
|
use std::time::Duration;
|
||||||
pub use trust_mode::TrustMode;
|
pub use trust_mode::TrustMode;
|
||||||
use tungstenite::Message;
|
use tungstenite::Message;
|
||||||
|
|
||||||
|
|
@ -152,7 +153,7 @@ impl Manager {
|
||||||
match SignalWS::new_provision(&host) {
|
match SignalWS::new_provision(&host) {
|
||||||
Ok(mut ws) => {
|
Ok(mut ws) => {
|
||||||
log::info!("provisioning websocket established to {host}");
|
log::info!("provisioning websocket established to {host}");
|
||||||
match ws.read() {
|
match ws.read(Some(Duration::from_millis(5000))) {
|
||||||
Ok(Message::Binary(uuid)) => {
|
Ok(Message::Binary(uuid)) => {
|
||||||
log::info!("received Provisioning UUID message from host");
|
log::info!("received Provisioning UUID message from host");
|
||||||
let uuid = libsignal::ProvisioningUuid::decode(uuid).id.clone();
|
let uuid = libsignal::ProvisioningUuid::decode(uuid).id.clone();
|
||||||
|
|
@ -173,7 +174,7 @@ impl Manager {
|
||||||
Some(device_link_uri.as_str()),
|
Some(device_link_uri.as_str()),
|
||||||
)
|
)
|
||||||
.expect("qrcode failed");
|
.expect("qrcode failed");
|
||||||
match ws.read() {
|
match ws.read(Some(Duration::from_millis(5000))) {
|
||||||
Ok(Message::Binary(registration)) => {
|
Ok(Message::Binary(registration)) => {
|
||||||
log::info!("Registration message received from host");
|
log::info!("Registration message received from host");
|
||||||
ws.close();
|
ws.close();
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ use rustls::{ClientConnection, StreamOwned};
|
||||||
use std::io::{Error, ErrorKind};
|
use std::io::{Error, ErrorKind};
|
||||||
use std::net::TcpStream;
|
use std::net::TcpStream;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
use std::thread;
|
||||||
|
use std::time::Duration;
|
||||||
use tls::Tls;
|
use tls::Tls;
|
||||||
use tungstenite::{Message, WebSocket};
|
use tungstenite::{Message, WebSocket};
|
||||||
|
|
||||||
|
|
@ -47,22 +49,59 @@ impl SignalWS {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(&mut self) -> Result<Message, Error> {
|
/// Reads a msg from the websocket with optional timeout
|
||||||
let msg = if let Ok(mut ws) =self. ws.lock() {
|
///
|
||||||
match ws.read() {
|
/// Hint: timeout = None is more efficient than Some(Duration(1)
|
||||||
Ok(msg) => Ok(msg),
|
///
|
||||||
Err(e) => {
|
/// # Arguments
|
||||||
log::warn!("{e}");
|
///
|
||||||
Err(Error::new(ErrorKind::Other, "error on reading ws"))
|
/// * `timeout` - a duration before the read operation times-out and returns
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
/// a message read from the websocket or ErrorKind::TimedOut
|
||||||
|
///
|
||||||
|
pub fn read(&mut self, timeout: Option<Duration>) -> Result<Message, Error> {
|
||||||
|
match timeout {
|
||||||
|
Some(duration) => {
|
||||||
|
let (tx, rx) = std::sync::mpsc::channel();
|
||||||
|
let ws = self.ws.clone();
|
||||||
|
thread::spawn(move || {
|
||||||
|
if let Ok(mut ws) = ws.lock() {
|
||||||
|
tx.send(ws.read()).unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
match rx.recv_timeout(duration) {
|
||||||
|
Ok(rx_msg) => match rx_msg {
|
||||||
|
Ok(msg) => Ok(msg),
|
||||||
|
Err(e) => {
|
||||||
|
log::warn!("{e}");
|
||||||
|
Err(Error::new(ErrorKind::Other, "error on reading ws"))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
log::warn!("{e}");
|
||||||
|
Err(Error::new(ErrorKind::TimedOut, e))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
None => {
|
||||||
Err(Error::new(
|
let msg = if let Ok(mut ws) = self.ws.lock() {
|
||||||
ErrorKind::Other,
|
match ws.read() {
|
||||||
"failed to get lock on websocket",
|
Ok(msg) => Ok(msg),
|
||||||
))
|
Err(e) => {
|
||||||
};
|
log::warn!("{e}");
|
||||||
msg
|
Err(Error::new(ErrorKind::Other, "error on reading ws"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err(Error::new(
|
||||||
|
ErrorKind::Other,
|
||||||
|
"failed to get lock on websocket",
|
||||||
|
))
|
||||||
|
};
|
||||||
|
msg
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Make a websocket connection to host server
|
/// Make a websocket connection to host server
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue