From 467bb561ec3f71b2cd7b0b01c5894b12b9a22a52 Mon Sep 17 00:00:00 2001 From: amate Date: Sun, 25 Aug 2019 16:47:32 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=BB[fix]=20IPC=E6=9C=89=E5=8A=B9=E6=99=82?= =?UTF-8?q?=E3=80=81=E5=A4=96=E9=83=A8=E3=83=97=E3=83=AD=E3=82=BB=E3=82=B9?= =?UTF-8?q?=E3=81=A8=E3=81=AE=E9=80=9A=E4=BF=A1=E3=81=AB=E3=82=B3=E3=82=B1?= =?UTF-8?q?=E3=82=8B=E3=81=AE=E3=82=92=E4=BF=AE=E6=AD=A3(=E3=83=9E?= =?UTF-8?q?=E3=83=AB=E3=83=81=E3=82=B9=E3=83=AC=E3=83=83=E3=83=89=E3=81=A7?= =?UTF-8?q?=E5=90=84=E7=A8=AE=E9=96=A2=E6=95=B0=E3=81=8C=E5=AE=9F=E8=A1=8C?= =?UTF-8?q?=E3=81=95=E3=82=8C=E3=82=8B=E3=81=93=E3=81=A8=E3=81=8C=E3=81=82?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AA=E3=81=AE=E3=81=A7=E3=80=81?= =?UTF-8?q?=E6=8E=92=E4=BB=96=E5=88=B6=E5=BE=A1=E3=82=92=E5=85=A5=E3=82=8C?= =?UTF-8?q?=E3=81=9F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ・[change] 名前付きパイプの設定を、バイトストリームモードへ変更(上のバグのせいで変なことしてた) ・[fix] 外部プロセスにて、func_info_getが失敗することを考慮に入れてなかったのを修正 ・[change] 名前付きパイプの名前をランダムに変更するようにした --- InputPipeMain/InputPipeMain.cpp | 71 ++++++++++++++++++----------- InputPipePlugin/input.cpp | 79 ++++++++++++++++++++++++++++----- Readme.txt | 11 +++++ Share/Common.h | 15 ++++--- Share/IPC.cpp | 40 ++++++++++------- Share/Logger.cpp | 7 ++- 6 files changed, 163 insertions(+), 60 deletions(-) diff --git a/InputPipeMain/InputPipeMain.cpp b/InputPipeMain/InputPipeMain.cpp index 877d1e7..b6ef2a8 100644 --- a/InputPipeMain/InputPipeMain.cpp +++ b/InputPipeMain/InputPipeMain.cpp @@ -134,6 +134,9 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, return 0; } + // for Debug + CallFunc lastCallFunc; + for (;;) { std::vector readData = namedPipe.Read(kToWindDataHeaderSize); if (readData.size() == 0) { @@ -141,16 +144,19 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, break; } ToWinputData* toData = (ToWinputData*)readData.data(); + lastCallFunc = toData->header.callFunc; + std::vector dataBody = namedPipe.Read(toData->header.paramSize); switch (toData->header.callFunc) { case CallFunc::kOpen: { LPSTR file = (LPSTR)dataBody.data(); INPUT_HANDLE ih = g_winputPluginTable->func_open(file); - //INFO_LOG << L"kOpen: " << ih; + INFO_LOG << L"kOpen: " << ih; - auto fromData = GenerateFromInputData(ih, 0); + auto fromData = GenerateFromInputData(CallFunc::kOpen, ih, 0); namedPipe.Write((const BYTE*)fromData.get(), FromWinputDataTotalSize(*fromData)); + //INFO_LOG << L"Write: " << FromWinputDataTotalSize(*fromData) << L" bytes"; } break; @@ -158,10 +164,11 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, { StandardParamPack* spp = (StandardParamPack*)dataBody.data(); BOOL b = g_winputPluginTable->func_close(spp->ih); - //INFO_LOG << L"kClose: " << spp->ih; + INFO_LOG << L"kClose: " << spp->ih; - auto fromData = GenerateFromInputData(b, 0); + auto fromData = GenerateFromInputData(CallFunc::kClose, b, 0); namedPipe.Write((const BYTE*)fromData.get(), FromWinputDataTotalSize(*fromData)); + //INFO_LOG << L"Write: " << FromWinputDataTotalSize(*fromData) << L" bytes"; } break; @@ -171,20 +178,28 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, StandardParamPack* spp = (StandardParamPack*)dataBody.data(); INPUT_INFO inputInfo = {}; BOOL b = g_winputPluginTable->func_info_get(spp->ih, &inputInfo); - //INFO_LOG << L"kInfoGet: " << spp->ih; - - int totalInputInfoSize = sizeof(INPUT_INFO) + inputInfo.format_size + inputInfo.audio_format_size; - std::vector entireInputInfo(totalInputInfoSize); - errno_t e = ::memcpy_s(entireInputInfo.data(), totalInputInfoSize, &inputInfo, sizeof(INPUT_INFO)); - e = ::memcpy_s(entireInputInfo.data() + sizeof(INPUT_INFO), - inputInfo.format_size, - inputInfo.format, inputInfo.format_size); - e = ::memcpy_s(entireInputInfo.data() + sizeof(INPUT_INFO) + inputInfo.format_size, - inputInfo.audio_format_size, - inputInfo.audio_format, inputInfo.audio_format_size); - - auto fromData = GenerateFromInputData(b, entireInputInfo); - namedPipe.Write((const BYTE*)fromData.get(), FromWinputDataTotalSize(*fromData)); + assert(b); + INFO_LOG << L"kInfoGet: " << spp->ih; + if (b) { + int totalInputInfoSize = sizeof(INPUT_INFO) + inputInfo.format_size + inputInfo.audio_format_size; + std::vector entireInputInfo(totalInputInfoSize); + errno_t e = ::memcpy_s(entireInputInfo.data(), totalInputInfoSize, &inputInfo, sizeof(INPUT_INFO)); + e = ::memcpy_s(entireInputInfo.data() + sizeof(INPUT_INFO), + inputInfo.format_size, + inputInfo.format, inputInfo.format_size); + e = ::memcpy_s(entireInputInfo.data() + sizeof(INPUT_INFO) + inputInfo.format_size, + inputInfo.audio_format_size, + inputInfo.audio_format, inputInfo.audio_format_size); + + auto fromData = GenerateFromInputData(CallFunc::kInfoGet, b, entireInputInfo.data(), totalInputInfoSize); + namedPipe.Write((const BYTE*)fromData.get(), FromWinputDataTotalSize(*fromData)); + //INFO_LOG << L"Write: " << FromWinputDataTotalSize(*fromData) << L" bytes"; + + } else { + auto fromData = GenerateFromInputData(CallFunc::kInfoGet, b, 0); + namedPipe.Write((const BYTE*)fromData.get(), FromWinputDataTotalSize(*fromData)); + //INFO_LOG << L"Write: " << FromWinputDataTotalSize(*fromData) << L" bytes"; + } } break; @@ -198,12 +213,14 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, int readBytes = g_winputPluginTable->func_read_video(spp->ih, spp->param1, g_readVideoBuffer.data()); //INFO_LOG << L"kReadVideo: " << spp->ih; + namedPipe.Write((const BYTE*)&toData->header.callFunc, sizeof(toData->header.callFunc)); std::int32_t totalSize = sizeof(int) + readBytes; namedPipe.Write((const BYTE*)&totalSize, sizeof(totalSize)); namedPipe.Write((const BYTE*)&readBytes, sizeof(readBytes)); namedPipe.Write((const BYTE*)g_readVideoBuffer.data(), readBytes); - //auto fromData = GenerateFromInputData(readBytes, g_readVideoBuffer); + //auto fromData = GenerateFromInputData(toData->header.callFunc, readBytes, g_readVideoBuffer.data(), readBytes); //namedPipe.Write((const BYTE*)fromData.get(), FromWinputDataTotalSize(*fromData)); + //INFO_LOG << L"Write: " << FromWinputDataTotalSize(*fromData) << L" bytes"; } break; @@ -216,25 +233,29 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, g_readAudioBuffer.resize(requestReadBytes); } int readSample = g_winputPluginTable->func_read_audio(spp->ih, spp->param1, spp->param2, g_readAudioBuffer.data()); - //INFO_LOG << L"kReadAudio: " << spp->ih; + assert(readSample > 0); + //INFO_LOG << L"kReadAudio: " << spp->ih << L" readSample: " << readSample; + const int readBufferSize = PerAudioSampleBufferSize * readSample; - std::int32_t totalSize = sizeof(int) + g_readAudioBuffer.size(); + namedPipe.Write((const BYTE*)& toData->header.callFunc, sizeof(toData->header.callFunc)); + std::int32_t totalSize = sizeof(int) + readBufferSize; namedPipe.Write((const BYTE*)& totalSize, sizeof(totalSize)); namedPipe.Write((const BYTE*)& readSample, sizeof(readSample)); - namedPipe.Write((const BYTE*)g_readAudioBuffer.data(), g_readAudioBuffer.size()); - //auto fromData = GenerateFromInputData(readBytes, g_readAudioBuffer); + namedPipe.Write((const BYTE*)g_readAudioBuffer.data(), readBufferSize); + //auto fromData = GenerateFromInputData(toData->header.callFunc, readSample, g_readAudioBuffer.data(), readBufferSize); //namedPipe.Write((const BYTE*)fromData.get(), FromWinputDataTotalSize(*fromData)); + //INFO_LOG << L"Write: " << FromWinputDataTotalSize(*fromData) << L" bytes"; } break; case CallFunc::kIsKeyframe: { - //INFO_LOG << L"kIsKeyframe"; + INFO_LOG << L"kIsKeyframe"; StandardParamPack* spp = (StandardParamPack*)dataBody.data(); BOOL b = g_winputPluginTable->func_is_keyframe(spp->ih, spp->param1); - auto fromData = GenerateFromInputData(b, 0); + auto fromData = GenerateFromInputData(CallFunc::kIsKeyframe, b, 0); namedPipe.Write((const BYTE*)fromData.get(), FromWinputDataTotalSize(*fromData)); } break; diff --git a/InputPipePlugin/input.cpp b/InputPipePlugin/input.cpp index e07d8a3..a506d98 100644 --- a/InputPipePlugin/input.cpp +++ b/InputPipePlugin/input.cpp @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include //#include //#pragma comment(lib, "Vfw32.lib") @@ -30,7 +32,7 @@ BindProcess g_bindProcess; LPCWSTR kPipeName = LR"(\\.\pipe\InputPipePlugin)"; NamedPipe g_namedPipe; -std::vector g_lastInfoGetData; +//std::vector g_lastInfoGetData; Config m_config; @@ -42,9 +44,13 @@ struct FrameAudioVideoBufferSize std::unordered_map g_mapFrameBufferSize; +std::unordered_map> g_mapInputHandleInfoGetData; + using HandleCache = std::tuple; std::vector g_vecHandleCache; +std::mutex g_mtxIPC; + // for Logger std::string LogFileName() { @@ -129,7 +135,16 @@ BOOL func_init( void ) if (m_config.bEnableIPC) { INFO_LOG << "EnableIPC"; - std::wstring pipeName = std::wstring(kPipeName) + std::to_wstring((uint64_t)g_hModule); + + enum { kRandamStrLength = 64 }; + std::random_device randdev; + WCHAR tempstr[] = L"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + std::uniform_int_distribution dist(0, std::size(tempstr) - 1); + std::wstring randamString = L"_"; + for (int i = 0; i < kRandamStrLength; ++i) { + randamString += tempstr[dist(randdev)]; + } + std::wstring pipeName = std::wstring(kPipeName) + randamString; bool ret = g_namedPipe.CreateNamedPipe(pipeName); // INFO_LOG << L"CreateNamedPipe: " << pipeName << L" ret: " << ret; @@ -173,6 +188,7 @@ BOOL func_exit( void ) INPUT_HANDLE func_open( LPSTR file ) { INFO_LOG << L"func_open: " << CodeConvert::UTF16fromShiftJIS(file); + std::lock_guard lock(g_mtxIPC); #ifndef NO_REMOTE if (m_config.bEnableHandleCache) { @@ -201,6 +217,8 @@ INPUT_HANDLE func_open( LPSTR file ) std::vector headerData = g_namedPipe.Read(kFromWinputDataHeaderSize); FromWinputData* fromData = (FromWinputData*)headerData.data(); + //INFO_LOG << L"Read: " << fromData->returnSize << L" bytes"; + assert(fromData->callFunc == CallFunc::kOpen); std::vector readBody = g_namedPipe.Read(fromData->returnSize); INPUT_HANDLE ih2 = *(INPUT_HANDLE*)readBody.data(); INFO_LOG << ih2; @@ -229,6 +247,7 @@ INPUT_HANDLE func_open( LPSTR file ) BOOL func_close( INPUT_HANDLE ih ) { INFO_LOG << L"func_close: " << ih; + std::lock_guard lock(g_mtxIPC); #ifndef NO_REMOTE if (m_config.bEnableHandleCache) { auto itfound = std::find_if(g_vecHandleCache.begin(), g_vecHandleCache.end(), @@ -252,12 +271,19 @@ BOOL func_close( INPUT_HANDLE ih ) } } if (m_config.bEnableIPC) { + auto itfound = g_mapInputHandleInfoGetData.find(ih); + if (itfound != g_mapInputHandleInfoGetData.end()) { + g_mapInputHandleInfoGetData.erase(itfound); + } + StandardParamPack spp = { ih }; auto toData = GenerateToInputData(CallFunc::kClose, spp); g_namedPipe.Write((const BYTE*)toData.get(), ToWinputDataTotalSize(*toData)); std::vector headerData = g_namedPipe.Read(kFromWinputDataHeaderSize); FromWinputData* fromData = (FromWinputData*)headerData.data(); + //INFO_LOG << L"Read: " << fromData->returnSize << L" bytes"; + assert(fromData->callFunc == CallFunc::kClose); std::vector readBody = g_namedPipe.Read(fromData->returnSize); auto retData = ParseFromInputData(readBody); @@ -298,26 +324,46 @@ BOOL func_close( INPUT_HANDLE ih ) BOOL func_info_get( INPUT_HANDLE ih,INPUT_INFO *iip ) { INFO_LOG << L"func_info_get"; + std::lock_guard lock(g_mtxIPC); #ifndef NO_REMOTE if (m_config.bEnableIPC) { + auto itfound = g_mapInputHandleInfoGetData.find(ih); + if (itfound != g_mapInputHandleInfoGetData.end()) { + INFO_LOG << L"InfoGetData cache found!"; + *iip = *reinterpret_cast(itfound->second.get()); + return TRUE; + } + StandardParamPack spp = { ih }; auto toData = GenerateToInputData(CallFunc::kInfoGet, spp); g_namedPipe.Write((const BYTE*)toData.get(), ToWinputDataTotalSize(*toData)); std::vector headerData = g_namedPipe.Read(kFromWinputDataHeaderSize); FromWinputData* fromData = (FromWinputData*)headerData.data(); + INFO_LOG << L"Read: " << fromData->returnSize << L" bytes"; + assert(fromData->callFunc == CallFunc::kInfoGet); std::vector readBody = g_namedPipe.Read(fromData->returnSize); auto retData = ParseFromInputData(readBody); - *iip = *(INPUT_INFO*)retData.second; - iip->format = (BITMAPINFOHEADER*)(retData.second + sizeof(INPUT_INFO)); - iip->audio_format = (WAVEFORMATEX*)(retData.second + sizeof(INPUT_INFO) + iip->format_size); - g_lastInfoGetData = std::move(readBody); - - int OneFrameBufferSize = iip->format->biWidth * iip->format->biHeight * (iip->format->biBitCount / 8) + kVideoBufferSurplusBytes; - int PerAudioSampleBufferSize = iip->audio_format->nChannels * (iip->audio_format->wBitsPerSample / 8); - g_mapFrameBufferSize.emplace(ih, FrameAudioVideoBufferSize{ OneFrameBufferSize , PerAudioSampleBufferSize }); - - INFO_LOG << L"OneFrameBufferSize: " << OneFrameBufferSize << L" PerAudioSampleBufferSize: " << PerAudioSampleBufferSize; + if (retData.first) { + auto tempInputInfo = (INPUT_INFO*)retData.second; + const int infoGetDataSize = sizeof(INPUT_INFO) + tempInputInfo->format_size + tempInputInfo->audio_format_size; + auto infoGetData = std::make_unique(infoGetDataSize); + memcpy_s(infoGetData.get(), infoGetDataSize, tempInputInfo, infoGetDataSize); + + auto igData = reinterpret_cast(infoGetData.get()); + igData->format = (BITMAPINFOHEADER*)(infoGetData.get() + sizeof(INPUT_INFO)); + igData->audio_format = (WAVEFORMATEX*)(infoGetData.get() + sizeof(INPUT_INFO) + igData->format_size); + *iip = *igData; + g_mapInputHandleInfoGetData.emplace(ih, std::move(infoGetData)); + + const int OneFrameBufferSize = iip->format->biWidth * iip->format->biHeight * (iip->format->biBitCount / 8); + const int PerAudioSampleBufferSize = iip->audio_format->nChannels * (iip->audio_format->wBitsPerSample / 8); + g_mapFrameBufferSize.emplace(ih, FrameAudioVideoBufferSize{ OneFrameBufferSize , PerAudioSampleBufferSize }); + + INFO_LOG << L"OneFrameBufferSize: " << OneFrameBufferSize << L" PerAudioSampleBufferSize: " << PerAudioSampleBufferSize; + } else { + ERROR_LOG << L"func_info_get failed, ih: " << ih; + } return retData.first; } else { BOOL b = g_winputPluginTable->func_info_get(ih, iip); @@ -360,6 +406,7 @@ BOOL func_info_get( INPUT_HANDLE ih,INPUT_INFO *iip ) int func_read_video( INPUT_HANDLE ih,int frame,void *buf ) { //INFO_LOG << L"func_read_video" << L" frame: " << frame; + std::lock_guard lock(g_mtxIPC); #ifndef NO_REMOTE if (m_config.bEnableIPC) { const int OneFrameBufferSize = g_mapFrameBufferSize[ih].OneFrameBufferSize; @@ -371,6 +418,8 @@ int func_read_video( INPUT_HANDLE ih,int frame,void *buf ) std::vector headerData = g_namedPipe.Read(kFromWinputDataHeaderSize); FromWinputData* fromData = (FromWinputData*)headerData.data(); + //INFO_LOG << L"Read: " << fromData->returnSize << L" bytes"; + assert(fromData->callFunc == CallFunc::kReadVideo); int readBytes = 0; int nRet = g_namedPipe.Read((BYTE*)& readBytes, sizeof(readBytes)); assert(nRet == sizeof(readBytes)); @@ -413,6 +462,7 @@ int func_read_video( INPUT_HANDLE ih,int frame,void *buf ) int func_read_audio(INPUT_HANDLE ih, int start, int length, void* buf) { //INFO_LOG << L"func_read_audio: " << ih << L" start: " << start << L" length: " << length; + std::lock_guard lock(g_mtxIPC); #ifndef NO_REMOTE if (m_config.bEnableIPC) { @@ -424,10 +474,13 @@ int func_read_audio(INPUT_HANDLE ih, int start, int length, void* buf) std::vector headerData = g_namedPipe.Read(kFromWinputDataHeaderSize); FromWinputData* fromData = (FromWinputData*)headerData.data(); + //INFO_LOG << L"Read: " << fromData->returnSize << L" bytes"; + assert(fromData->callFunc == CallFunc::kReadAudio); int readSample = 0; int nRet = g_namedPipe.Read((BYTE*)& readSample, sizeof(readSample)); assert(nRet == sizeof(readSample)); const int audioBufferSize = fromData->returnSize - sizeof(int); + assert(audioBufferSize >= 0); nRet = g_namedPipe.Read((BYTE*)buf, audioBufferSize); assert(nRet == audioBufferSize); return readSample; @@ -466,6 +519,7 @@ int func_read_audio(INPUT_HANDLE ih, int start, int length, void* buf) BOOL func_is_keyframe(INPUT_HANDLE ih, int frame) { // INFO_LOG << L"func_is_keyframe" << L" frame:" << frame; + std::lock_guard lock(g_mtxIPC); #ifndef NO_REMOTE if (m_config.bEnableIPC) { @@ -475,6 +529,7 @@ BOOL func_is_keyframe(INPUT_HANDLE ih, int frame) std::vector headerData = g_namedPipe.Read(kFromWinputDataHeaderSize); FromWinputData* fromData = (FromWinputData*)headerData.data(); + assert(fromData->callFunc == CallFunc::kIsKeyframe); std::vector readBody = g_namedPipe.Read(fromData->returnSize); auto retData = ParseFromInputData(readBody); return retData.first; diff --git a/Readme.txt b/Readme.txt index 62d0c17..8d06f5c 120000 --- a/Readme.txt +++ b/Readme.txt @@ -72,3 +72,14 @@ http://www.boost.org/ ◆WTL http://sourceforge.net/projects/wtl/ + + +■更新履歴 +v1.1 +・[fix] IPC有効時、外部プロセスとの通信にコケるのを修正(マルチスレッドで各種関数が実行されることがあるようなので、排他制御を入れた) +・[change] 名前付きパイプの設定を、バイトストリームモードへ変更(上のバグのせいで変なことしてた) +・[fix] 外部プロセスにて、func_info_getが失敗することを考慮に入れてなかったのを修正 +・[change] 名前付きパイプの名前をランダムに変更するようにした + +v1.0 +・完成 diff --git a/Share/Common.h b/Share/Common.h index 4de4c79..01396ec 100644 --- a/Share/Common.h +++ b/Share/Common.h @@ -24,7 +24,7 @@ BOOL func_config(HWND hwnd, HINSTANCE dll_hinst); //////////////////////////////////////////////////////////////// -#define PLUGIN_VERSION "1.0" +#define PLUGIN_VERSION "1.1" constexpr int kVideoBufferSurplusBytes = 0x3FF; @@ -52,7 +52,7 @@ struct Config //////////////////////////////////////////////////////////////// -enum class CallFunc : char +enum class CallFunc : std::int32_t { kOpen = 1, kClose, @@ -107,6 +107,7 @@ std::shared_ptr GenerateToInputData(CallFunc callFunc, RetParam1T struct FromWinputData { + CallFunc callFunc; std::int32_t returnSize; unsigned char returnData[1]; }; @@ -118,13 +119,14 @@ inline int FromWinputDataTotalSize(const FromWinputData& data) { } template -std::shared_ptr GenerateFromInputData(RetT ret, RetParamT retParam) +std::shared_ptr GenerateFromInputData(CallFunc callFunc, RetT ret, RetParamT retParam) { size_t returnSize = sizeof(ret) + sizeof(retParam); std::shared_ptr fromData((FromWinputData*)new BYTE[kToWindDataHeaderSize + returnSize], [](FromWinputData* p) { delete[] (BYTE*)p; }); + fromData->callFunc = callFunc; fromData->returnSize = returnSize; memcpy_s(fromData->returnData, sizeof(ret), &ret, sizeof(ret)); memcpy_s(fromData->returnData + sizeof(ret), sizeof(retParam), &retParam, sizeof(retParam)); @@ -132,16 +134,17 @@ std::shared_ptr GenerateFromInputData(RetT ret, RetParamT retPar } template -std::shared_ptr GenerateFromInputData(RetT ret, const std::vector& retParam) +std::shared_ptr GenerateFromInputData(CallFunc callFunc, RetT ret, const BYTE* retParam, int retParamSize) { - size_t returnSize = sizeof(ret) + retParam.size(); + size_t returnSize = sizeof(ret) + retParamSize; std::shared_ptr fromData((FromWinputData*)new BYTE[kToWindDataHeaderSize + returnSize], [](FromWinputData* p) { delete[](BYTE*)p; }); + fromData->callFunc = callFunc; fromData->returnSize = returnSize; memcpy_s(fromData->returnData, sizeof(ret), &ret, sizeof(ret)); - memcpy_s(fromData->returnData + sizeof(ret), retParam.size(), retParam.data(), retParam.size()); + memcpy_s(fromData->returnData + sizeof(ret), retParamSize, retParam, retParamSize); return fromData; } diff --git a/Share/IPC.cpp b/Share/IPC.cpp index 358e020..8f6f188 100644 --- a/Share/IPC.cpp +++ b/Share/IPC.cpp @@ -3,6 +3,7 @@ #include #include #include +#include "Logger.h" //////////////////////////////////////////////////////////////////////////////////// // BindProcess @@ -67,7 +68,7 @@ bool NamedPipe::CreateNamedPipe(const std::wstring& pipeName) m_hPipe = ::CreateNamedPipe(pipeName.c_str(), PIPE_ACCESS_DUPLEX, // read/write access PIPE_TYPE_MESSAGE | // message type pipe - PIPE_READMODE_MESSAGE | // message-read mode + PIPE_READMODE_BYTE | // byte-read mode PIPE_WAIT, // blocking mode kMaxInstance, kBuffSize, kBuffSize, 0, nullptr); if (m_hPipe == INVALID_HANDLE_VALUE) { @@ -87,17 +88,17 @@ bool NamedPipe::OpenNamedPipe(const std::wstring& pipeName) } // The pipe connected; change to message-read mode. - DWORD dwMode = PIPE_READMODE_MESSAGE; - BOOL fSuccess = ::SetNamedPipeHandleState( - m_hPipe, // pipe handle - &dwMode, // new pipe mode - NULL, // don't set maximum bytes - NULL); // don't set maximum time - if (!fSuccess) { - assert(false); - //_tprintf(TEXT("SetNamedPipeHandleState failed. GLE=%d\n"), GetLastError()); - return false; - } + //DWORD dwMode = PIPE_READMODE_MESSAGE; + //BOOL fSuccess = ::SetNamedPipeHandleState( + // m_hPipe, // pipe handle + // &dwMode, // new pipe mode + // NULL, // don't set maximum bytes + // NULL); // don't set maximum time + //if (!fSuccess) { + // assert(false); + // //_tprintf(TEXT("SetNamedPipeHandleState failed. GLE=%d\n"), GetLastError()); + // return false; + //} return true; } @@ -152,19 +153,26 @@ int NamedPipe::Read(BYTE* data, const int length) #endif DWORD readyBytes = 0; BOOL bSuccess = ::ReadFile(m_hPipe, writeDataPos, restReadBytes, &readyBytes, nullptr); - restReadBytes -= readyBytes; - totalReadBytes = length - restReadBytes; - DWORD error = GetLastError(); - if ((!bSuccess || readyBytes == 0) && error != ERROR_MORE_DATA) { + if ((!bSuccess || readyBytes == 0)) { if (error == ERROR_BROKEN_PIPE) { //_tprintf(TEXT("InstanceThread: client disconnected.\n"), GetLastError()); + ERROR_LOG << L"NamedPipe::Read failed: client disconnected."; } else { //_tprintf(TEXT("InstanceThread ReadFile failed, GLE=%d.\n"), GetLastError()); + ERROR_LOG << L"NamedPipe::Read failed: GetLastError: " << GetLastError(); } + //if (bSuccess && readyBytes == 0) { + // WARN_LOG << L"NamedPipe::Read crazy?"; // Ȃ ReadFile ɐ readBytes 0 ̏ꍇ炵... + // ::Sleep(10); + // continue; + //} + assert(false); return totalReadBytes; } else { + restReadBytes -= readyBytes; + totalReadBytes = length - restReadBytes; writeDataPos += readyBytes; // ݈ʒuXV } diff --git a/Share/Logger.cpp b/Share/Logger.cpp index c1b20ce..9ae883c 100644 --- a/Share/Logger.cpp +++ b/Share/Logger.cpp @@ -14,6 +14,10 @@ namespace sinks = boost::log::sinks; namespace keywords = boost::log::keywords; namespace logging = boost::log; +#ifdef _DEBUG +#define USE_CONSOLE +#endif + //std::string LogFileName() //{ // return "info.log"; @@ -77,8 +81,9 @@ BOOST_LOG_GLOBAL_LOGGER_INIT(my_logger, logger_t) sink_text->imbue(std::locale(std::locale(), new std::codecvt_utf8_utf16)); core->add_sink(sink_text); -#ifdef _DEBUG +#ifdef USE_CONSOLE // ÕR\[o͂ݒ + ::AllocConsole(); int cp; cp = GetConsoleOutputCP();