alielie
7 天以前 e57938fbda9bff702004038f76d009422968bff5
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using DeviceService.Service;
using DeviceService.Service.NamePipe;
using System.Collections.ObjectModel;
using System.IO.Pipes;
using System.Text;
using System.Windows;
using System.Windows.Input;
 
namespace DeviceService;
 
/// <summary>
/// Interaction logic for DebugWindow.xaml
/// </summary>
public partial class DebugWindow : Window
{
    private const string PipeName = "DeviceService";
 
    private class ClientItem
    {
        public ClientItem(IClient client, string name)
        {
            Client = client;
            Name = name;
        }
 
        public IClient Client { get; }
 
        public string Name { get; }
 
        public override string ToString() => Name;
    }
 
    private NamePipeService? _pipeService;
    private readonly ObservableCollection<ClientItem> _clients = new ObservableCollection<ClientItem>();
 
    public DebugWindow()
    {
        InitializeComponent();
        ClientList.ItemsSource = _clients;
        Loaded += DebugWindow_Loaded;
        Closed += DebugWindow_Closed;
    }
 
    private void DebugWindow_Loaded(object sender, RoutedEventArgs e)
    {
    }
 
    
 
    private string FindClientName(IClient client)
    {
        ClientItem? item = _clients.FirstOrDefault(c => ReferenceEquals(c.Client, client));
        return item?.Name ?? "未知客户端";
    }
 
    private async void SendTestButton_Click(object sender, RoutedEventArgs e)
    {
        string text = SendBox.Text;
        if (string.IsNullOrEmpty(text))
        {
            return;
        }
 
        SendTestButton.IsEnabled = false;
        try
        {
            string error = await Task.Run(() => SendViaPipe(text + "\r\n"));
            AppendLine(string.IsNullOrEmpty(error)
                ? $"[测试] 已发送:{text}"
                : $"[测试] 发送失败:{error}");
        }
        finally
        {
            SendTestButton.IsEnabled = true;
        }
    }
 
    private async void SendToClientButton_Click(object sender, RoutedEventArgs e)
    {
        if (ClientList.SelectedItem is not ClientItem item)
        {
            AppendLine("[发送] 请先在左侧列表中选择一个客户端。");
            return;
        }
 
        string text = SendBox.Text;
        if (string.IsNullOrEmpty(text))
        {
            return;
        }
 
        string name = item.Name;
        IClient client = item.Client;
 
        SendToClientButton.IsEnabled = false;
        try
        {
            string error = await Task.Run(() => client.Send(text, out string err) ? string.Empty : err);
            AppendLine(string.IsNullOrEmpty(error)
                ? $"[发送→{name}] {text}"
                : $"[发送失败→{name}] {error}");
        }
        finally
        {
            SendToClientButton.IsEnabled = true;
        }
    }
 
    private void SendBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            // 已选中客户端则定向发送,否则走测试通道
            if (ClientList.SelectedItem is ClientItem)
            {
                SendToClientButton_Click(sender, e);
            }
            else
            {
                SendTestButton_Click(sender, e);
            }
        }
    }
 
    /// <summary>
    /// 以客户端身份连接本进程的管道,按行协议写入一条 UTF8 消息(以 \n 结尾)。
    /// 返回空字符串表示成功,否则为错误信息。
    /// </summary>
    private static string SendViaPipe(string text)
    {
        try
        {
            using var pipe = new NamedPipeClientStream(".", PipeName, PipeDirection.InOut);
            pipe.Connect(2000);
 
            byte[] payload = Encoding.UTF8.GetBytes(text + "\n");
            pipe.Write(payload, 0, payload.Length);
            pipe.Flush();
            return string.Empty;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
 
    private void AppendLine(string text)
    {
        ReceiveBox.AppendText(text + Environment.NewLine);
        ReceiveBox.ScrollToEnd();
    }
 
    private void DebugWindow_Closed(object? sender, EventArgs e)
    {
    }
}