diff --git a/src/ImageBuffer.cpp b/src/ImageBuffer.cpp index 2c7f52d..4b84b1a 100644 --- a/src/ImageBuffer.cpp +++ b/src/ImageBuffer.cpp @@ -54,6 +54,7 @@ void ImageBuffer::PushHumanImage(std::vector data, uint32 width, uint32 h human_.width = width; human_.height = height; human_.comp = comp; + human_.updated = true; } void ImageBuffer::UpdateHuman(Texture2D* texture) { @@ -62,6 +63,10 @@ void ImageBuffer::UpdateHuman(Texture2D* texture) { } ScopeLock lock(&human_cs_); + if (!human_.updated) { + return; + } + human_.updated = false; texture->Update(human_.width, human_.height, human_.comp, human_.data.data()); } diff --git a/src/ImageBuffer.h b/src/ImageBuffer.h index 48a2716..7edbac6 100644 --- a/src/ImageBuffer.h +++ b/src/ImageBuffer.h @@ -41,6 +41,7 @@ private: uint32 width; uint32 height; uint32 comp; + bool updated{false}; }; CriticalSection human_cs_; Image human_; diff --git a/src/Ipc/Ipc.cpp b/src/Ipc/Ipc.cpp index a9a0bd7..08ebd35 100644 --- a/src/Ipc/Ipc.cpp +++ b/src/Ipc/Ipc.cpp @@ -26,7 +26,7 @@ Ipc::~Ipc() { } bool Ipc::listen() { - if (reciver_stop_.load(std::memory_order_acquire)) { + if (!reciver_stop_.load(std::memory_order_acquire)) { return false; } @@ -38,7 +38,7 @@ bool Ipc::listen() { } void Ipc::stop() { - if (!reciver_stop_.load(std::memory_order_acquire)) { + if (reciver_stop_.load(std::memory_order_acquire)) { return; } @@ -60,6 +60,23 @@ bool Ipc::send(const char* data, unsigned int size) { return sender_->send(data, size); } + +bool Ipc::isConnected() { + /* if (receiver_) { + return receiver_->is_connected(); + }*/ + return false; +} + + +void Ipc::reConnect() { + sender_->reconnect(ipc::sender); + if (receiver_) { + receiver_->disconnect(); + receiver_->reconnect(ipc::receiver); + } +} + void Ipc::registReadCallback(const std::shared_ptr& reader) { std::lock_guard lock(mutex_); @@ -88,9 +105,9 @@ void Ipc::doReciver(std::weak_ptr wThis) { receiver_ = std::make_unique(writer_name_.c_str(), ipc::receiver); while (!reciver_stop_.load(std::memory_order_acquire)) { - ipc::buff_t buffer = receiver_->recv(); + ipc::buff_t buffer = receiver_->recv(5); if (buffer.empty()) { - break; + continue; } const char* data = buffer.get(); onReciveer(data, static_cast(buffer.size())); diff --git a/src/Ipc/Ipc.h b/src/Ipc/Ipc.h index 29242a7..c6d7084 100644 --- a/src/Ipc/Ipc.h +++ b/src/Ipc/Ipc.h @@ -19,6 +19,8 @@ public: bool listen(); void stop(); bool send(const char* data, unsigned int size); + bool isConnected(); + void reConnect(); void registReadCallback(const std::shared_ptr& reader); void unregistReadCallback(const std::shared_ptr& reader); @@ -39,7 +41,7 @@ private: std::unique_ptr sender_; std::unique_ptr receiver_; std::thread reciver_thread_; - std::atomic_bool reciver_stop_{false}; + std::atomic_bool reciver_stop_{true}; std::mutex mutex_; using ReaderCallbackList = std::vector>; diff --git a/src/Ipc/IpcMoudle.cpp b/src/Ipc/IpcMoudle.cpp index be98360..0ca7bd8 100644 --- a/src/Ipc/IpcMoudle.cpp +++ b/src/Ipc/IpcMoudle.cpp @@ -45,11 +45,10 @@ void OnParseImageData(const char* data, size_t size) { } else if (0x03 == identifier) { ImageBuffer::Get()->PushImage(ImageBuffer::IBType::FaceMask, std::move(img_data), width, height, bit_depth); } - // Further processing of img_bytes can be done here } bool IpcMoudle::Initialize() { - if (!initialize("ipc_sender", "ipc_sender")) { + if (!initialize("human_render", "human_product")) { ERRORLOG("ipc initialize failed"); return false; } @@ -64,9 +63,28 @@ bool IpcMoudle::Initialize() { ERRORLOG("ipc listen failed"); return false; } + lastHeartbeatTime_ = std::chrono::steady_clock::now(); return true; } void IpcMoudle::Uninitialize() { uninitialize(); } + +bool IpcMoudle::Send(const char* data, unsigned int size) { + return send(data, size); +} + +void IpcMoudle::OnFrame() { + auto now = std::chrono::steady_clock::now(); + auto duration = std::chrono::duration_cast(now - lastHeartbeatTime_); + + if (duration.count() >= 2) { + constexpr char heartbeat[] = "heartbeat"; + if (!send("heartbeat", sizeof(heartbeat) / sizeof(heartbeat[0]))) { + ERRORLOG("send heartbeat failed"); + reConnect(); + } + lastHeartbeatTime_ = now; + } +} diff --git a/src/Ipc/IpcMoudle.h b/src/Ipc/IpcMoudle.h index 41edd81..2e3a61b 100644 --- a/src/Ipc/IpcMoudle.h +++ b/src/Ipc/IpcMoudle.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "Core/Singleton.h" #include "Core/Constant.h" @@ -11,5 +13,13 @@ public: virtual ~IpcMoudle() = default; bool Initialize() override; void Uninitialize() override; + + bool Send(const char* data, unsigned int size); + + void OnFrame(); + +private: + std::chrono::time_point lastHeartbeatTime_; + }; diff --git a/src/Ipc/ipclib.cpp b/src/Ipc/ipclib.cpp index 84d55ae..6f0928e 100644 --- a/src/Ipc/ipclib.cpp +++ b/src/Ipc/ipclib.cpp @@ -67,3 +67,14 @@ bool __stdcall listen() { g_readCallback->setCallback(callback); return true; } + + + void __stdcall reConnect() { + assert(g_ipc); + return g_ipc->reConnect(); + } + + bool __stdcall isConnect() { + assert(g_ipc); + return g_ipc->isConnected(); + } diff --git a/src/Ipc/ipclib.h b/src/Ipc/ipclib.h index 6f3ffb3..cb4f149 100644 --- a/src/Ipc/ipclib.h +++ b/src/Ipc/ipclib.h @@ -16,6 +16,10 @@ extern "C" { bool __stdcall send(const char* data, unsigned int size); bool __stdcall setReaderCallback(ReaderCallbackFunc callback); + void __stdcall reConnect(); + + bool __stdcall isConnect(); + #ifdef __cplusplus } #endif // __cplusplus diff --git a/src/Main.cpp b/src/Main.cpp index ada3ffd..2bdc0fb 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -153,6 +153,8 @@ static int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevI view = glm::lookAt(glm::vec3(0, 0, 10), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0)); projection = glm::perspective(glm::radians(60.f), ratio, 1.f, 1000.f); + IpcMoudle::Get()->OnFrame(); + ImageBuffer::Get()->Update(ImageBuffer::IBType::Human, &huaman); glBindVertexArray(VAO); @@ -170,6 +172,9 @@ static int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevI //glDrawArrays(GL_TRIANGLES, 0, 6); //glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + constexpr char quit[] = "quit"; + IpcMoudle::Get()->Send(quit, sizeof(quit)/ sizeof(quit[0])); + glfwDestroyWindow(window); glfwTerminate();