baoshiwei
2025-05-21 832991a036bcb0d99a66d2d1f059253136ddd622
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
import { invoke } from '@tauri-apps/api/core';
 
export async function startPipeServer(pipeName: string): Promise<void> {
  try {
    await invoke('start_pipe_server_command', { pipeName });
    console.log(`Pipe server started on \\\.\\pipe\\${pipeName}`);
  } catch (err) {
    console.error('Failed to start pipe server:', err);
  }
}
 
export async function sendToPipe(pipeName: string, message: string): Promise<string> {
  // 添加重试机制
  const maxRetries = 3;
  let retryCount = 0;
  let lastError: any;
  
  while (retryCount < maxRetries) {
    try {
      return await invoke('send_pipe_message', { pipeName, message });
    } catch (err) {
      lastError = err;
      console.warn(`管道通信失败,正在重试 (${retryCount + 1}/${maxRetries}):`, err);
      retryCount++;
      
      // 等待一段时间再重试
      if (retryCount < maxRetries) {
        await new Promise(resolve => setTimeout(resolve, 500 * retryCount));
      }
    }
  }
  
  // 所有重试都失败,抛出最后一个错误
  throw lastError;
}