-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ipc.cpp
More file actions
85 lines (78 loc) · 3.09 KB
/
Copy pathtest_ipc.cpp
File metadata and controls
85 lines (78 loc) · 3.09 KB
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
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include <stdio.h>
#include <string>
static HANDLE g_pipe = INVALID_HANDLE_VALUE;
static std::string g_buf;
static bool send_json(const std::string &s)
{
if (g_pipe == INVALID_HANDLE_VALUE) return false;
DWORD wrote = 0;
std::string m = s + "\n";
WriteFile(g_pipe, m.data(), (DWORD)m.size(), &wrote, NULL);
return true;
}
static void drain_all(void)
{
DWORD avail = 0;
while (g_pipe != INVALID_HANDLE_VALUE &&
PeekNamedPipe(g_pipe, NULL, 0, NULL, &avail, NULL) && avail > 0) {
char tmp[65536];
DWORD rd = 0;
if (!ReadFile(g_pipe, tmp, sizeof tmp, &rd, NULL) || rd == 0) break;
g_buf.append(tmp, rd);
}
/* 按大括号平衡切分连续 JSON 对象 */
int depth = 0, start = -1;
for (size_t i = 0; i < g_buf.size(); i++) {
char c = g_buf[i];
if (c == '{') { if (depth == 0) start = (int)i; depth++; }
else if (c == '}') { depth--; if (depth == 0 && start >= 0) {
std::string msg = g_buf.substr(start, i - start + 1);
g_buf.erase(0, i + 1);
printf("MSG: %.*s\n", (int)msg.size(), msg.c_str());
return drain_all(); /* 重新扫描 */
} }
}
g_buf.clear();
}
int main(void)
{
setvbuf(stdout, NULL, _IONBF, 0);
printf("START\n");
wchar_t cmd[] = L"\"C:\\Users\\37672\\Downloads\\bootstrapper\\mpv.exe\" --no-terminal --idle=yes --input-ipc-server=\\\\.\\pipe\\test_mpv_ipc";
STARTUPINFOW si = {0};
PROCESS_INFORMATION pi;
si.cb = sizeof si;
if (!CreateProcessW(NULL, cmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL,
L"C:\\Users\\37672\\Downloads\\bootstrapper", &si, &pi)) {
printf("CREATE PROCESS FAILED %lu\n", GetLastError());
return 1;
}
for (int i = 0; i < 50; i++) {
g_pipe = CreateFileW(L"\\\\.\\pipe\\test_mpv_ipc", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (g_pipe != INVALID_HANDLE_VALUE) break;
Sleep(100);
}
if (g_pipe == INVALID_HANDLE_VALUE) { printf("PIPE CONNECT FAILED\n"); return 1; }
printf("PIPE OK\n");
Sleep(300);
send_json("{\"command\":[\"loadfile\",\"C:/Users/37672/Desktop/backup/Bang.mp3\",\"replace\"],\"request_id\":1}");
send_json("{\"command\":[\"observe_property\",1,\"time-pos\"],\"request_id\":2}");
send_json("{\"command\":[\"observe_property\",2,\"duration\"],\"request_id\":3}");
Sleep(1500); drain_all();
send_json("{\"command\":[\"set_property\",\"pause\",true],\"request_id\":4}");
Sleep(300); drain_all();
send_json("{\"command\":[\"seek\",5,\"absolute\"],\"request_id\":5}");
Sleep(300); drain_all();
send_json("{\"command\":[\"set_property\",\"pause\",false],\"request_id\":6}");
Sleep(1200); drain_all();
send_json("{\"command\":[\"set_property\",\"volume\",55],\"request_id\":7}");
Sleep(300); drain_all();
send_json("{\"command\":[\"quit\"],\"request_id\":8}");
CloseHandle(g_pipe);
printf("DONE\n");
return 0;
}