mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +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 signal_ws::SignalWS;
|
||||
use std::io::{Error, ErrorKind};
|
||||
use std::time::Duration;
|
||||
pub use trust_mode::TrustMode;
|
||||
use tungstenite::Message;
|
||||
|
||||
|
|
@ -152,7 +153,7 @@ impl Manager {
|
|||
match SignalWS::new_provision(&host) {
|
||||
Ok(mut ws) => {
|
||||
log::info!("provisioning websocket established to {host}");
|
||||
match ws.read() {
|
||||
match ws.read(Some(Duration::from_millis(5000))) {
|
||||
Ok(Message::Binary(uuid)) => {
|
||||
log::info!("received Provisioning UUID message from host");
|
||||
let uuid = libsignal::ProvisioningUuid::decode(uuid).id.clone();
|
||||
|
|
@ -173,7 +174,7 @@ impl Manager {
|
|||
Some(device_link_uri.as_str()),
|
||||
)
|
||||
.expect("qrcode failed");
|
||||
match ws.read() {
|
||||
match ws.read(Some(Duration::from_millis(5000))) {
|
||||
Ok(Message::Binary(registration)) => {
|
||||
log::info!("Registration message received from host");
|
||||
ws.close();
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@ use rustls::{ClientConnection, StreamOwned};
|
|||
use std::io::{Error, ErrorKind};
|
||||
use std::net::TcpStream;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use tls::Tls;
|
||||
use tungstenite::{Message, WebSocket};
|
||||
|
||||
|
|
@ -47,22 +49,59 @@ impl SignalWS {
|
|||
};
|
||||
}
|
||||
|
||||
pub fn read(&mut self) -> Result<Message, Error> {
|
||||
let msg = if let Ok(mut ws) =self. ws.lock() {
|
||||
match ws.read() {
|
||||
Ok(msg) => Ok(msg),
|
||||
Err(e) => {
|
||||
log::warn!("{e}");
|
||||
Err(Error::new(ErrorKind::Other, "error on reading ws"))
|
||||
/// Reads a msg from the websocket with optional timeout
|
||||
///
|
||||
/// Hint: timeout = None is more efficient than Some(Duration(1)
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `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 {
|
||||
Err(Error::new(
|
||||
ErrorKind::Other,
|
||||
"failed to get lock on websocket",
|
||||
))
|
||||
};
|
||||
msg
|
||||
None => {
|
||||
let msg = if let Ok(mut ws) = self.ws.lock() {
|
||||
match ws.read() {
|
||||
Ok(msg) => Ok(msg),
|
||||
Err(e) => {
|
||||
log::warn!("{e}");
|
||||
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
|
||||
|
|
|
|||
Loading…
Reference in a new issue