Skip to content

Commit 68590dd

Browse files
committed
commit new demo
1 parent 94ad05c commit 68590dd

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

demo/send_recv/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
project(send_recv)
2+
3+
include_directories(
4+
${LIBIPC_PROJECT_DIR}/3rdparty)
5+
6+
file(GLOB SRC_FILES ./*.cpp)
7+
file(GLOB HEAD_FILES ./*.h)
8+
9+
add_executable(${PROJECT_NAME} ${SRC_FILES} ${HEAD_FILES})
10+
11+
target_link_libraries(${PROJECT_NAME} ipc)

demo/send_recv/main.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
#include <iostream>
3+
#include <string>
4+
#include <thread>
5+
#include <chrono>
6+
7+
#include "libipc/ipc.h"
8+
9+
namespace {
10+
11+
void do_send(int size, int interval) {
12+
ipc::channel ipc {"ipc", ipc::sender};
13+
std::string buffer(size, 'A');
14+
while (true) {
15+
std::cout << "send size: " << buffer.size() + 1 << "\n";
16+
ipc.send(buffer, 0/*tm*/);
17+
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
18+
}
19+
}
20+
21+
void do_recv(int interval) {
22+
ipc::channel ipc {"ipc", ipc::receiver};
23+
while (true) {
24+
ipc::buff_t recv;
25+
for (int k = 1; recv.empty(); ++k) {
26+
std::cout << "recv waiting... " << k << "\n";
27+
recv = ipc.recv(interval);
28+
}
29+
std::cout << "recv size: " << recv.size() << "\n";
30+
}
31+
}
32+
33+
} // namespace
34+
35+
int main(int argc, char ** argv) {
36+
if (argc < 3) return -1;
37+
std::string mode {argv[1]};
38+
if (mode == "send") {
39+
if (argc < 4) return -1;
40+
do_send(std::stoi(argv[2]) /*size*/,
41+
std::stoi(argv[3]) /*interval*/);
42+
} else if (mode == "recv") {
43+
do_recv(std::stoi(argv[2]) /*interval*/);
44+
}
45+
return 0;
46+
}

0 commit comments

Comments
 (0)