Initial migration to MTP [Broken]
This commit is contained in:
parent
acc2534834
commit
da86bdaf5a
10 changed files with 693 additions and 563 deletions
|
|
@ -2,12 +2,12 @@ use crate::log;
|
|||
use crate::util::file_util::load_file_vec;
|
||||
use crate::util::logger::PrintType;
|
||||
use dashmap::DashMap;
|
||||
use mtp::codec::{CommunicationType, CommunicationValue, DataType, DataValue};
|
||||
use mtp::transport::{Host, Policy, Receiver, SendMode, Sender, host};
|
||||
use once_cell::sync::Lazy;
|
||||
use std::net::{IpAddr, Ipv4Addr};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::futures;
|
||||
use ttp_core::{CommunicationType, CommunicationValue, DataTypes, DataValue};
|
||||
use ttp_native::{Host, Policy, Receiver, SendMode, Sender};
|
||||
|
||||
pub struct TauriConnection {
|
||||
pub user_id: i64,
|
||||
|
|
@ -20,7 +20,8 @@ pub async fn start(port: u16) -> Result<(), Box<dyn std::error::Error>> {
|
|||
let cert_pem = load_file_vec("certs", "transport_cert.pem").expect("Error loading Pemfile");
|
||||
let key_pem = load_file_vec("certs", "transport_key.pem").expect("Error loading Keyfile");
|
||||
|
||||
let mut host: Host = ttp_native::host(
|
||||
let mut host: Host = host(
|
||||
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
|
||||
port,
|
||||
cert_pem,
|
||||
key_pem,
|
||||
|
|
@ -59,45 +60,41 @@ async fn handle_connection(sender: Sender, receiver: &mut Receiver) {
|
|||
let sender = Arc::new(sender);
|
||||
|
||||
while let Ok(cv) = receiver.receive().await {
|
||||
match cv.get_type() {
|
||||
CommunicationType::tauri_identification => {
|
||||
let user_id = cv.get_data(DataTypes::user_id).as_number().unwrap_or(0);
|
||||
if user_id != 0 {
|
||||
current_user_id = user_id;
|
||||
let conn = Arc::new(TauriConnection {
|
||||
user_id,
|
||||
sender: sender.clone(),
|
||||
});
|
||||
if cv.is_type(CommunicationType::TauriIdentification) {
|
||||
let user_id = cv.get_data(DataType::UserId).as_number().unwrap_or(0) as i64;
|
||||
if user_id != 0 {
|
||||
current_user_id = user_id;
|
||||
let conn = Arc::new(TauriConnection {
|
||||
user_id,
|
||||
sender: sender.clone(),
|
||||
});
|
||||
|
||||
TAURI_CONNECTIONS
|
||||
.entry(user_id)
|
||||
.and_modify(|conns| {
|
||||
conns.retain(|c| !Arc::ptr_eq(&c.sender.handle(), &sender.handle()));
|
||||
conns.push(conn.clone());
|
||||
})
|
||||
.or_insert_with(|| vec![conn]);
|
||||
TAURI_CONNECTIONS
|
||||
.entry(user_id)
|
||||
.and_modify(|conns| {
|
||||
conns.retain(|c| !Arc::ptr_eq(&c.sender.handle(), &sender.handle()));
|
||||
conns.push(conn.clone());
|
||||
})
|
||||
.or_insert_with(|| vec![conn]);
|
||||
|
||||
let response =
|
||||
CommunicationValue::new(CommunicationType::success).with_id(cv.get_id());
|
||||
if let Err(e) = sender.send(&response).await {
|
||||
log!(
|
||||
PrintType::General,
|
||||
"Failed to send tauri success response: {}",
|
||||
e
|
||||
);
|
||||
} else {
|
||||
log!(user_id, PrintType::Client, "Tauri device registered");
|
||||
}
|
||||
}
|
||||
}
|
||||
CommunicationType::ping => {
|
||||
let response =
|
||||
CommunicationValue::new(CommunicationType::pong).with_id(cv.get_id());
|
||||
if let Err(_) = sender.send(&response).await {
|
||||
break;
|
||||
CommunicationValue::new(CommunicationType::Success).with_id(cv.get_id());
|
||||
if let Err(e) = sender.send(&response).await {
|
||||
log!(
|
||||
PrintType::General,
|
||||
"Failed to send tauri success response: {}",
|
||||
e
|
||||
);
|
||||
} else {
|
||||
log!(user_id, PrintType::Client, "Tauri device registered");
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
} else if cv.is_type(CommunicationType::Ping) {
|
||||
let response =
|
||||
CommunicationValue::new(CommunicationType::Pong).with_id(cv.get_id());
|
||||
if let Err(_) = sender.send(&response).await {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -116,8 +113,8 @@ pub async fn send_notification(user_id: i64, sender_id: i64) {
|
|||
};
|
||||
|
||||
if let Some(conns) = conns_opt {
|
||||
let cv = CommunicationValue::new(CommunicationType::push_notification)
|
||||
.add_data(DataTypes::sender_id, DataValue::Number(sender_id));
|
||||
let cv = CommunicationValue::new(CommunicationType::PushNotification)
|
||||
.add_typed_default(DataType::SenderId, DataValue::SignedNumber(sender_id as i128));
|
||||
|
||||
let mut remove_needed = false;
|
||||
for conn in conns.iter() {
|
||||
|
|
@ -141,8 +138,8 @@ pub async fn remove_notification(user_id: i64, sender_id: i64) {
|
|||
};
|
||||
|
||||
if let Some(conns) = conns_opt {
|
||||
let cv = CommunicationValue::new(CommunicationType::read_notification)
|
||||
.add_data(DataTypes::sender_id, DataValue::Number(sender_id));
|
||||
let cv = CommunicationValue::new(CommunicationType::ReadNotification)
|
||||
.add_typed_default(DataType::SenderId, DataValue::SignedNumber(sender_id as i128));
|
||||
|
||||
let mut remove_needed = false;
|
||||
for conn in conns.iter() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue