2025-09-18 16:04:07 +08:00
|
|
|
|
import * as http from 'http';
|
|
|
|
|
|
import * as vscode from 'vscode';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 构建JSON数据
|
|
|
|
|
|
* @param filePath 文件路径
|
|
|
|
|
|
* @param content 文件内容
|
|
|
|
|
|
* @returns JSON字符串
|
|
|
|
|
|
*/
|
|
|
|
|
|
function buildJsonData(filePath: string, content: string): string {
|
|
|
|
|
|
const data: { [key: string]: string } = {};
|
|
|
|
|
|
data[filePath] = content;
|
|
|
|
|
|
|
|
|
|
|
|
return JSON.stringify(data);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 发送文件内容到本地C++服务
|
|
|
|
|
|
* @param filePath 文件路径
|
|
|
|
|
|
* @param content 文件内容
|
|
|
|
|
|
* @returns Promise<boolean> 是否发送成功
|
|
|
|
|
|
*/
|
|
|
|
|
|
export async function sendFileToCppService(filePath: string, content: string): Promise<boolean> {
|
2025-09-18 16:10:04 +08:00
|
|
|
|
// 从配置中获取端口号,默认为26000
|
|
|
|
|
|
const config = vscode.workspace.getConfiguration('squirrel');
|
|
|
|
|
|
const localServicePort = config.get<number>('localServicePort', 26000);
|
|
|
|
|
|
|
2025-09-18 16:04:07 +08:00
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
|
const jsonData = buildJsonData(filePath, content);
|
|
|
|
|
|
const postData = jsonData;
|
|
|
|
|
|
|
|
|
|
|
|
const options: http.RequestOptions = {
|
|
|
|
|
|
hostname: '127.0.0.1',
|
2025-09-18 16:10:04 +08:00
|
|
|
|
port: localServicePort,
|
2025-09-18 16:04:07 +08:00
|
|
|
|
path: '/send_data',
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
|
'Content-Length': Buffer.byteLength(postData)
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const req = http.request(options, (res) => {
|
|
|
|
|
|
let data = '';
|
|
|
|
|
|
|
|
|
|
|
|
res.on('data', (chunk) => {
|
|
|
|
|
|
data += chunk;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
res.on('end', () => {
|
|
|
|
|
|
console.log(`[Squirrel] 已发送文件到C++服务: ${filePath}, 状态码: ${res.statusCode}`);
|
|
|
|
|
|
if (res.statusCode === 200) {
|
|
|
|
|
|
resolve(true);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
console.error(`[Squirrel] 发送失败,状态码: ${res.statusCode}, 响应: ${data}`);
|
|
|
|
|
|
resolve(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
req.on('error', (error) => {
|
|
|
|
|
|
console.error(`[Squirrel] 发送文件到C++服务失败: ${error.message}`);
|
|
|
|
|
|
resolve(false);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
req.write(postData);
|
|
|
|
|
|
req.end();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|