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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// <<*>> Revenge of snowflake.
// -^v- SASWE
// @ This source code is licensed under a dual license model:
// 1. MPL-2.0 for non-commercial use only
// 2. Commercial License for commercial use
// See LICENSE and LICENSE_COMMERCIAL files for details.
use mlua::{Lua, Table};
use std::io::{self, BufRead, Write};
use std::sync::{Arc, Mutex};
/// 消息传输管理器
/// 负责处理标准输入输出与 Lua 运行时之间的消息传递
pub struct TransmitManager {
// 存储从 stdin 接收的消息队列
message_queue: Arc<Mutex<Vec<String>>>,
// 用于从 stdin 读取线程
reader_handle: Option<std::thread::JoinHandle<()>>,
}
impl TransmitManager {
/// 创建新的传输管理器实例
pub fn new() -> Self {
let message_queue = Arc::new(Mutex::new(Vec::new()));
// 克隆引用用于 stdin 读取线程
let queue_clone = Arc::clone(&message_queue);
// 启动后台线程持续读取 stdin
let reader_handle = Some(std::thread::spawn(move || {
let stdin = io::stdin();
let mut handle = stdin.lock();
let mut buffer = String::new();
loop {
buffer.clear();
match handle.read_line(&mut buffer) {
Ok(0) => {
// EOF 到达
eprintln!("[TRANSMIT] stdin EOF");
break;
}
Ok(_) => {
// 读取到一行数据,根据平台自动处理换行符
// Windows: CRLF (\r\n), Unix/Linux: LF (\n)
let line = buffer.trim_end_matches('\n').trim_end_matches('\r').to_string();
// 只处理以 TOSCRIPT 开头的消息
if line.starts_with("TOSCRIPT:") {
// 移除 TOSCRIPT:前缀,只保留实际消息内容
let message = line.strip_prefix("TOSCRIPT:").unwrap().to_string();
if !message.is_empty() {
eprintln!("[TRANSMIT] 接收到消息:{}", message);
let mut queue = queue_clone.lock().unwrap();
queue.push(message);
}
} else {
eprintln!("[TRANSMIT] 忽略非 TOSCRIPT 消息:{}", line);
}
}
Err(e) => {
eprintln!("[TRANSMIT] 读取 stdin 错误:{}", e);
break;
}
}
}
}));
TransmitManager {
message_queue,
reader_handle,
}
}
/// 获取下一条消息(非阻塞)
/// 如果没有消息则返回 None
pub fn get_message(&self) -> Option<String> {
let mut queue = self.message_queue.lock().unwrap();
if queue.is_empty() {
None
} else {
Some(queue.remove(0))
}
}
/// 获取所有可用消息(非阻塞)
pub fn get_all_messages(&self) -> Vec<String> {
let mut queue = self.message_queue.lock().unwrap();
std::mem::take(&mut *queue)
}
/// 等待并获取消息(阻塞,带超时)
/// timeout_ms: 超时时间(毫秒),None 表示无限等待
pub fn wait_for_message(&self, timeout_ms: Option<u64>) -> Option<String> {
let start = std::time::Instant::now();
loop {
{
let mut queue = self.message_queue.lock().unwrap();
if !queue.is_empty() {
return Some(queue.remove(0));
}
}
// 检查是否超时
if let Some(timeout) = timeout_ms {
if start.elapsed() >= std::time::Duration::from_millis(timeout) {
return None;
}
}
// 短暂休眠避免 CPU 占用过高
std::thread::sleep(std::time::Duration::from_millis(10));
}
}
/// 发送消息到 stdout(以 SWMSG 前缀标识)
/// 这会被 runtime.rtmsg.send(...) 调用
pub fn send_message(&self, message: &str) -> io::Result<()> {
let stdout = io::stdout();
let mut handle = stdout.lock();
// 写入 SWMSG 前缀和消息
writeln!(handle, "SWMSG:{}", message)?;
handle.flush()?;
eprintln!("[TRANSMIT] 发送消息:{}", message);
Ok(())
}
/// 初始化 Lua API,暴露给 Lua 脚本使用
/// 创建 runtime.rtmsg 表,包含 get、get_all、send、wait 方法
pub fn setup_lua_api(&self, lua: &Lua, runtime_table: &Table) -> Result<(), mlua::Error> {
let transmit_manager = Arc::clone(&self.message_queue);
// 创建 rtmsg 表
let rtmsg = lua.create_table()?;
// rtmsg.get() - 获取单条消息
let queue_get = Arc::clone(&transmit_manager);
rtmsg.set("get", lua.create_function(move |_, _: ()| -> Result<Option<String>, mlua::Error> {
let mut queue = queue_get.lock().unwrap();
if queue.is_empty() {
Ok(None)
} else {
Ok(Some(queue.remove(0)))
}
})?)?;
// rtmsg.get_all() - 获取所有消息
let queue_get_all = Arc::clone(&transmit_manager);
rtmsg.set("get_all", lua.create_function(move |_, _: ()| -> Result<Vec<String>, mlua::Error> {
let mut queue = queue_get_all.lock().unwrap();
Ok(std::mem::take(&mut *queue))
})?)?;
// rtmsg.wait(timeout_ms) - 等待消息(可选超时)
let queue_wait = Arc::clone(&transmit_manager);
rtmsg.set("wait", lua.create_function(move |_, timeout_ms: Option<u64>| -> Result<Option<String>, mlua::Error> {
let start = std::time::Instant::now();
loop {
{
let mut queue = queue_wait.lock().unwrap();
if !queue.is_empty() {
return Ok(Some(queue.remove(0)));
}
}
// 检查是否超时
if let Some(timeout) = timeout_ms {
if start.elapsed() >= std::time::Duration::from_millis(timeout) {
return Ok(None);
}
}
// 短暂休眠
std::thread::sleep(std::time::Duration::from_millis(10));
}
})?)?;
// rtmsg.send(message) - 发送消息到 stdout
rtmsg.set("send", lua.create_function(|_, message: String| -> Result<(), mlua::Error> {
let stdout = io::stdout();
let mut handle = stdout.lock();
writeln!(handle, "SWMSG:{}", message)
.map_err(|e| mlua::Error::RuntimeError(format!("发送消息失败:{}", e)))?;
handle.flush()
.map_err(|e| mlua::Error::RuntimeError(format!("刷新输出失败:{}", e)))?;
eprintln!("[TRANSMIT] Lua 发送消息:{}", message);
Ok(())
})?)?;
// 将 rtmsg 设置为 runtime 的子表
runtime_table.set("rtmsg", rtmsg)?;
Ok(())
}
}
impl Default for TransmitManager {
fn default() -> Self {
Self::new()
}
}
/// 初始化传输模块并设置 Lua API
/// 这个函数应该在 runtime.rs 中被调用
pub fn init_transmit(lua: &Lua, runtime_table: &Table) -> Result<Arc<TransmitManager>, mlua::Error> {
let manager = TransmitManager::new();
manager.setup_lua_api(lua, runtime_table)?;
Ok(Arc::new(manager))
}