forked from rutice/NicoJK
-
Notifications
You must be signed in to change notification settings - Fork 7
/
JKStream.cpp
319 lines (307 loc) · 8.72 KB
/
JKStream.cpp
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "stdafx.h"
#include "JKStream.h"
CJKStream::CJKStream()
: bWorkerCreated_(false)
{
}
CJKStream::~CJKStream()
{
Close();
}
void CJKStream::BeginClose()
{
if (bWorkerCreated_) {
{
lock_recursive_mutex lock(workerLock_);
bStopWroker_ = true;
}
SetEvent(hWorkerEvent_);
}
}
void CJKStream::Close()
{
if (bWorkerCreated_) {
BeginClose();
if (WaitForSingleObject(hProcess_, 10000) == WAIT_TIMEOUT) {
TerminateProcess(hProcess_, 1);
}
HANDLE hWorkerThread = workerThread_.native_handle();
if (WaitForSingleObject(hWorkerThread, 10000) == WAIT_TIMEOUT) {
workerThread_.detach();
TerminateThread(hWorkerThread, 1);
CloseHandle(hWorkerThread);
} else {
workerThread_.join();
}
CloseHandle(hProcess_);
CloseHandle(hWorkerEvent_);
bWorkerCreated_ = false;
}
}
bool CJKStream::CreateWorker(HWND hwnd, UINT msg)
{
if (bWorkerCreated_) {
return true;
}
hWorkerEvent_ = CreateEvent(nullptr, FALSE, FALSE, nullptr);
if (hWorkerEvent_) {
workerThread_ = std::thread([this, hwnd, msg]() { WorkerThread(hwnd, msg); });
// 初期化を待つ
WaitForSingleObject(hWorkerEvent_, INFINITE);
if (bWorkerCreated_) {
lock_recursive_mutex lock(workerLock_);
bContinueWorker_ = true;
return true;
}
workerThread_.join();
CloseHandle(hWorkerEvent_);
}
return false;
}
bool CJKStream::CreateJKProcess(HANDLE &hProcess, HANDLE &hAsyncReadPipe, HANDLE &hWritePipe)
{
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = nullptr;
sa.bInheritHandle = TRUE;
HANDLE hStdInput;
if (CreatePipe(&hStdInput, &hWritePipe, &sa, 0)) {
// 標準出力は非同期にする
TCHAR pipeName[64];
_stprintf_s(pipeName, TEXT("\\\\.\\pipe\\anon_%08x_%08x"), GetCurrentProcessId(), GetCurrentThreadId());
HANDLE hStdOutput = CreateNamedPipe(pipeName, PIPE_ACCESS_OUTBOUND, 0, 1, 8192, 8192, 0, &sa);
if (hStdOutput != INVALID_HANDLE_VALUE) {
hAsyncReadPipe = CreateFile(pipeName, GENERIC_READ, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, nullptr);
if (hAsyncReadPipe != INVALID_HANDLE_VALUE) {
TCHAR jkcnslPath[MAX_PATH + 16];
if (GetLongModuleFileName(nullptr, jkcnslPath, MAX_PATH)) {
for (size_t i = _tcslen(jkcnslPath); i > 0 && !_tcschr(TEXT("/\\"), jkcnslPath[i - 1]); ) {
jkcnslPath[--i] = TEXT('\0');
}
_tcscat_s(jkcnslPath, TEXT("jkcnsl.exe"));
}
// 不正終了時に自力で落ちてもらうためにプロセスIDを渡す
TCHAR args[32];
_stprintf_s(args, TEXT(" -p %u"), GetCurrentProcessId());
STARTUPINFO si = {};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = hStdInput;
si.hStdOutput = hStdOutput;
// 標準エラー出力は捨てる
si.hStdError = CreateFile(TEXT("nul"), GENERIC_WRITE, 0, &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
PROCESS_INFORMATION pi;
if (CreateProcess(jkcnslPath, args, nullptr, nullptr, TRUE, CREATE_NO_WINDOW, nullptr, nullptr, &si, &pi)) {
if (si.hStdError != INVALID_HANDLE_VALUE) {
CloseHandle(si.hStdError);
}
CloseHandle(hStdOutput);
CloseHandle(hStdInput);
CloseHandle(pi.hThread);
hProcess = pi.hProcess;
return true;
}
if (si.hStdError != INVALID_HANDLE_VALUE) {
CloseHandle(si.hStdError);
}
CloseHandle(hAsyncReadPipe);
}
CloseHandle(hStdOutput);
}
CloseHandle(hWritePipe);
CloseHandle(hStdInput);
}
return false;
}
void CJKStream::WorkerThread(HWND hwnd, UINT msg)
{
HANDLE olEvents[] = {hWorkerEvent_, CreateEvent(nullptr, TRUE, TRUE, nullptr)};
if (!olEvents[1]) {
// 親スレッドに初期化失敗を通知
SetEvent(hWorkerEvent_);
return;
}
HANDLE hReadPipe, hWritePipe;
if (!CreateJKProcess(hProcess_, hReadPipe, hWritePipe)) {
// 親スレッドに初期化失敗を通知
SetEvent(hWorkerEvent_);
CloseHandle(olEvents[1]);
return;
}
bWorkerCreated_ = true;
bContinueWorker_ = false;
bStopWroker_ = false;
bOpened_ = false;
SetEvent(hWorkerEvent_);
// 親スレッドからの続行許可を待つ
for (;;) {
Sleep(0);
lock_recursive_mutex lock(workerLock_);
if (bContinueWorker_) {
break;
}
}
OVERLAPPED ol = {};
char olBuf[8192];
for (;;) {
DWORD dwRet = WaitForMultipleObjects(2, olEvents, FALSE, INFINITE);
if (dwRet == WAIT_OBJECT_0 + 1) {
bool bPost = false;
if (ol.hEvent) {
// 非同期読み込み完了
DWORD xferred;
if (GetOverlappedResult(hReadPipe, &ol, &xferred, FALSE)) {
lock_recursive_mutex lock(workerLock_);
if (bOpened_) {
recvBuf_.insert(recvBuf_.end(), olBuf, olBuf + xferred);
bPost = true;
}
}
}
ol.hEvent = olEvents[1];
while (ReadFile(hReadPipe, olBuf, sizeof(olBuf), nullptr, &ol)) {
DWORD xferred;
if (GetOverlappedResult(hReadPipe, &ol, &xferred, FALSE)) {
lock_recursive_mutex lock(workerLock_);
if (bOpened_) {
recvBuf_.insert(recvBuf_.end(), olBuf, olBuf + xferred);
bPost = true;
}
}
}
if (GetLastError() != ERROR_IO_PENDING) {
// エラーor閉じられた
ol.hEvent = nullptr;
break;
}
// 非同期読み込み開始
if (bPost) {
// ウィンドウに受信を通知
PostMessage(hwnd, msg, 0, 0);
}
} else {
lock_recursive_mutex lock(workerLock_);
if (bStopWroker_) {
DWORD dwWritten;
WriteFile(hWritePipe, "q\r\n", 3, &dwWritten, nullptr);
break;
}
if (bOpened_) {
if (bShutdown_ && !bShutdownSent_) {
DWORD dwWritten;
if (!WriteFile(hWritePipe, "c\r\n", 3, &dwWritten, nullptr) || dwWritten != 3) {
// エラーor閉じられた
break;
}
bShutdownSent_ = true;
}
if (sendBuf_.empty() == false && !bShutdownSent_) {
DWORD dwWritten;
if (!WriteFile(hWritePipe, sendBuf_.data(), static_cast<DWORD>(sendBuf_.size()), &dwWritten, nullptr) ||
dwWritten != sendBuf_.size()) {
// エラーor閉じられた
break;
}
sendBuf_.clear();
}
}
}
}
if (ol.hEvent) {
CancelIo(hReadPipe);
}
CloseHandle(hWritePipe);
CloseHandle(hReadPipe);
CloseHandle(olEvents[1]);
}
// 非同期通信を開始する
// すでに開始しているときは失敗するが、command=='+'のときは開いているストリームに送信データを追加する
bool CJKStream::Send(HWND hwnd, UINT msg, char command, const char *buf)
{
if (!strchr(buf, '\n') && !strchr(buf, '\r')) {
if (command == '+' && bWorkerCreated_ && bOpened_ && !bShutdown_) {
// 前のデータを送信済みのときだけ送信データを追加できる
lock_recursive_mutex lock(workerLock_);
if (sendBuf_.empty()) {
sendBuf_.push_back(command);
sendBuf_.insert(sendBuf_.end(), buf, buf + strlen(buf));
sendBuf_.push_back('\r');
sendBuf_.push_back('\n');
SetEvent(hWorkerEvent_);
return true;
}
} else if (command != '+' && (!bWorkerCreated_ || !bOpened_)) {
if (CreateWorker(hwnd, msg)) {
// ストリームを開く
lock_recursive_mutex lock(workerLock_);
recvBuf_.clear();
sendBuf_.clear();
sendBuf_.push_back(command);
sendBuf_.insert(sendBuf_.end(), buf, buf + strlen(buf));
sendBuf_.push_back('\r');
sendBuf_.push_back('\n');
bShutdown_ = false;
bShutdownSent_ = false;
bOpened_ = true;
SetEvent(hWorkerEvent_);
return true;
}
}
}
return false;
}
// ウィンドウメッセージを処理してデータを受信する
// 受信データはrecvBufに追記される
// 戻り値: 負値=切断した(-2=正常,-1=中断), 0=正常に処理した
int CJKStream::ProcessRecv(std::vector<char> &recvBuf)
{
if (bWorkerCreated_ && bOpened_) {
lock_recursive_mutex lock(workerLock_);
std::vector<char>::iterator it = recvBuf_.begin();
for (;;) {
// LF単位で受信
std::vector<char>::iterator itEnd = std::find(it, recvBuf_.end(), '\n');
if (itEnd == recvBuf_.end()) {
break;
}
++itEnd;
char c = *it;
if (c == '.' || c == '!' || c == '?') {
// プロセス側がストリームを閉じた
recvBuf_.erase(recvBuf_.begin(), itEnd);
bOpened_ = false;
return bShutdown_ || c != '.' ? -1 : -2;
}
if (c == '-') {
// 受信
for (++it; it != itEnd; ++it) {
// CRは無視
if (*it != '\r') {
recvBuf.push_back(*it);
}
}
}
it = itEnd;
}
recvBuf_.erase(recvBuf_.begin(), it);
if (WaitForSingleObject(workerThread_.native_handle(), 0) != WAIT_TIMEOUT) {
bOpened_ = false;
return -1;
}
}
return 0;
}
// 送受信停止を要求する
// 呼び出し後ProcessRecv()が負値を返すと完了(ストリームも閉じられる)
bool CJKStream::Shutdown()
{
if (bWorkerCreated_ && bOpened_) {
if (!bShutdown_) {
lock_recursive_mutex lock(workerLock_);
bShutdown_ = true;
SetEvent(hWorkerEvent_);
}
return true;
}
return false;
}