baoshiwei
6 天以前 5bf14aed888cd0e258e325c65f14022dad02985b
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
mod tokio_utils;
mod pipe_server;
mod ws_client;
use crate::ws_client::LATEST_WS_DATA;
 
 
 
use std::sync::Mutex;
use tauri::async_runtime::spawn;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio_utils::{set_complete,setup,SetupState};
 
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
use crate::pipe_server::start_pipe_server;
 
#[tauri::command]
fn get_latest_ws_data() -> Option<String> {
    LATEST_WS_DATA.lock().unwrap().clone()
}
#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}
 
#[tauri::command]
async fn start_pipe_server_command(pipe_name: String) -> Result<(), String> {
    start_pipe_server(&pipe_name)
        .await
        .map_err(|e| e.to_string())
}
 
#[tauri::command]
async fn send_pipe_message(pipe_name: String, message: String) -> Result<String, String> {
    let pipe_path: String = format!(r"\\.\pipe\{}", pipe_name);
    
    let mut client = tokio::net::windows::named_pipe::ClientOptions::new()
        .open(&pipe_path)
        .map_err(|e| e.to_string())?;
 
    client.write_all(message.as_bytes()).await.map_err(|e| e.to_string())?;
    
    let mut buf = [0u8; 1024];
    let n = client.read(&mut buf).await.map_err(|e| e.to_string())?;
    
    String::from_utf8(buf[..n].to_vec())
        .map_err(|e| e.to_string())
}
 
 
 
// #[cfg_attr(mobile, tauri::mobile_entry_point)]
// pub fn run() {
//     tauri::Builder::default()
//         .plugin(tauri_plugin_opener::init())
//         .invoke_handler(tauri::generate_handler![greet])
//         .run(tauri::generate_context!())
//         .expect("error while running tauri application");
// }
 
// Our main entrypoint in a version 2 mobile compatible app
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    // Don't write code before Tauri starts, write it in the
    // setup hook instead!
    tauri::Builder::default()
        // Register a `State` to be managed by Tauri
        // We need write access to it so we wrap it in a `Mutex`
        .manage(Mutex::new(SetupState {
            frontend_task: false,
            backend_task: false,
        }))
        // Add a command we can use to check
        .invoke_handler(tauri::generate_handler![greet, set_complete, start_pipe_server_command, send_pipe_message, get_latest_ws_data])
        // Use the setup hook to execute setup related tasks
        // Runs before the main loop, so no windows are yet created
        .setup(|app| {
            // 启动 WebSocket 客户端(异步任务)
            ws_client::start_ws_client();
            // Spawn setup as a non-blocking task so the windows can be
            // created and ran while it executes
            spawn(setup(app.handle().clone()));
            // The hook expects an Ok result
            Ok(())
        })
        // Run the app
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}