Skip to content

Commit

Permalink
🐞 修改 Windows 平台命名管道析构函数
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoxi-scut committed Dec 7, 2024
1 parent d04f855 commit 4076b83
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
2 changes: 1 addition & 1 deletion modules/algorithm/src/optimal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ std::vector<double> lsqnonlinRKF(const FuncNds &funcs, const std::vector<double>

#else

std::vector<double> lsqnonlinRKF(const FuncNds &, const std::vector<double> &, const OptimalOptions &)
std::vector<double> lsqnonlinRKF(const FuncNds &, const std::vector<double> &, RobustMode, const OptimalOptions &)
{
RMVL_Error(RMVL_StsBadFunc, "this function must be used with libopencv_core.so, please recompile "
"RMVL by setting \"WITH_OPENCV=ON\" and \"WITH_EIGEN3=ON\" in CMake");
Expand Down
4 changes: 2 additions & 2 deletions modules/algorithm/test/test_dsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ TEST(DSPTest, dft)
auto X = rm::dft(x);
auto Gx = rm::Gx(X, rm::GxType::Amp);
// 获取幅度谱最大值对应的频率
int max_it = std::max_element(Gx.begin(), Gx.end()) - Gx.begin();
EXPECT_EQ(max_it, f);
std::ptrdiff_t max_it = std::max_element(Gx.begin(), Gx.end()) - Gx.begin();
EXPECT_EQ(max_it, static_cast<std::ptrdiff_t>(f));
}

} // namespace rm_test
32 changes: 16 additions & 16 deletions modules/core/src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

#ifndef _WIN32
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <unistd.h>
#endif

#include "io_impl.hpp"
Expand Down Expand Up @@ -94,27 +94,27 @@ PipeServer::Impl::Impl(std::string_view name) : _name("\\\\.\\pipe\\"s + name.da

PipeClient::Impl::Impl(std::string_view name)
{
std::string pipe_name = "\\\\.\\pipe\\"s + name.data();
_handle = CreateFileA(
("\\\\.\\pipe\\"s + name.data()).c_str(), // 命名管道名称
GENERIC_READ | GENERIC_WRITE, // 读写权限
0, // 不共享
nullptr, // 默认安全属性
OPEN_EXISTING, // 打开已存在的管道
0, // 默认属性
nullptr); // 无模板文件
pipe_name.c_str(), // 命名管道名称
GENERIC_READ | GENERIC_WRITE, // 读写权限
0, // 不共享
nullptr, // 默认安全属性
OPEN_EXISTING, // 打开已存在的管道
0, // 默认属性
nullptr); // 无模板文件
if (_handle == INVALID_HANDLE_VALUE)
{
ERROR_("Failed to open named pipe");
return;
}
DWORD mode = PIPE_READMODE_MESSAGE;
if (!SetNamedPipeHandleState(_handle, &mode, nullptr, nullptr))
{
ERROR_("Failed to set named pipe handle state");
CloseHandle(_handle);
return;
}
return;
}

PipeServer::Impl::~Impl()
{
if (!DisconnectNamedPipe(_handle))
ERROR_("Failed to disconnect named pipe");
closePipe(_handle);
}

bool PipeServer::Impl::read(std::string &data) { return readPipe(_handle, data); }
Expand Down
25 changes: 22 additions & 3 deletions modules/core/test/test_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
* @brief 数据 IO 与通信模块单元测试
* @version 1.0
* @date 2024-09-18
*
*
* @copyright Copyright 2024 (c), zhaoxi
*
*
*/

#include <fstream>
#ifdef _WIN32
#include <thread>
#endif

#include <gtest/gtest.h>

Expand All @@ -36,6 +39,22 @@ TEST(IOTest, corners_io)

TEST(IOTest, pipe_io)
{
#ifdef _WIN32
std::thread t([&]() {
rm::PipeServer server("test_pipe");
EXPECT_TRUE(server.write("Hello"));
std::string msg{};
EXPECT_TRUE(server.read(msg));
EXPECT_EQ(msg, "world");
});
std::this_thread::sleep_for(std::chrono::milliseconds(100));
rm::PipeClient client("test_pipe");
std::string msg{};
EXPECT_TRUE(client.read(msg));
EXPECT_EQ(msg, "Hello");
EXPECT_TRUE(client.write("world"));
t.join();
#else
rm::PipeServer server("test_pipe");
rm::PipeClient client("test_pipe");
EXPECT_TRUE(server.write("hello"));
Expand All @@ -45,7 +64,7 @@ TEST(IOTest, pipe_io)
EXPECT_TRUE(client.write("world"));
EXPECT_TRUE(server.read(data));
EXPECT_EQ(data, "world");
#endif
}


} // namespace rm_test

0 comments on commit 4076b83

Please sign in to comment.