first commit
This commit is contained in:
77
test/test_ipc.cpp
Normal file
77
test/test_ipc.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <fmt/core.h>
|
||||
#include <thread>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "ipc/api.hpp"
|
||||
|
||||
int
|
||||
main(int /* argc */, char** /* argv */)
|
||||
{
|
||||
using ipc::Ipc;
|
||||
using fmt::format;
|
||||
using fmt::print;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
int verbose = 0;
|
||||
|
||||
// custom name
|
||||
#if defined(_WIN32)
|
||||
auto name = format("custom_fifo_ipc_{}", getpid());
|
||||
#else
|
||||
auto name = format("/tmp/custom_fifo_ipc_{}", getpid());
|
||||
#endif
|
||||
print("\n{}\n\n", name);
|
||||
ipc::init(name, 0600);
|
||||
|
||||
std::thread server_thread([name, verbose] {
|
||||
print("[server] pre-start\n");
|
||||
std::this_thread::sleep_for(1s);
|
||||
|
||||
auto server = Ipc(name, true, verbose);
|
||||
print("[server] start\n");
|
||||
|
||||
int value;
|
||||
double result;
|
||||
while(1) {
|
||||
{
|
||||
server.read(value);
|
||||
print("[server] read int({})\n", value);
|
||||
}
|
||||
|
||||
{
|
||||
// result = value * value;
|
||||
result = std::sqrt(value);
|
||||
server.write(result);
|
||||
print("[server] write double({:.2e})\n", result);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
print("[client] pre-start\n");
|
||||
auto client = Ipc(name, false, verbose);
|
||||
print("[client] start\n");
|
||||
|
||||
double answer;
|
||||
auto hline = "\n------------------------\n";
|
||||
for (int i = 2; i <= 5; ++i) {
|
||||
std::this_thread::sleep_for(500ms);
|
||||
print(hline);
|
||||
|
||||
{
|
||||
client.write(i);
|
||||
print("[client] write int({})\n", i);
|
||||
}
|
||||
|
||||
{
|
||||
client.read(answer);
|
||||
print("[client] read double({:.2e})\n", answer);
|
||||
}
|
||||
}
|
||||
|
||||
print(hline);
|
||||
print("[client] finish the work\n\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user