mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
commit new demo
This commit is contained in:
parent
94ad05ce35
commit
68590dd2f3
11
demo/send_recv/CMakeLists.txt
Normal file
11
demo/send_recv/CMakeLists.txt
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
project(send_recv)
|
||||||
|
|
||||||
|
include_directories(
|
||||||
|
${LIBIPC_PROJECT_DIR}/3rdparty)
|
||||||
|
|
||||||
|
file(GLOB SRC_FILES ./*.cpp)
|
||||||
|
file(GLOB HEAD_FILES ./*.h)
|
||||||
|
|
||||||
|
add_executable(${PROJECT_NAME} ${SRC_FILES} ${HEAD_FILES})
|
||||||
|
|
||||||
|
target_link_libraries(${PROJECT_NAME} ipc)
|
||||||
46
demo/send_recv/main.cpp
Normal file
46
demo/send_recv/main.cpp
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
#include "libipc/ipc.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
void do_send(int size, int interval) {
|
||||||
|
ipc::channel ipc {"ipc", ipc::sender};
|
||||||
|
std::string buffer(size, 'A');
|
||||||
|
while (true) {
|
||||||
|
std::cout << "send size: " << buffer.size() + 1 << "\n";
|
||||||
|
ipc.send(buffer, 0/*tm*/);
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void do_recv(int interval) {
|
||||||
|
ipc::channel ipc {"ipc", ipc::receiver};
|
||||||
|
while (true) {
|
||||||
|
ipc::buff_t recv;
|
||||||
|
for (int k = 1; recv.empty(); ++k) {
|
||||||
|
std::cout << "recv waiting... " << k << "\n";
|
||||||
|
recv = ipc.recv(interval);
|
||||||
|
}
|
||||||
|
std::cout << "recv size: " << recv.size() << "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
int main(int argc, char ** argv) {
|
||||||
|
if (argc < 3) return -1;
|
||||||
|
std::string mode {argv[1]};
|
||||||
|
if (mode == "send") {
|
||||||
|
if (argc < 4) return -1;
|
||||||
|
do_send(std::stoi(argv[2]) /*size*/,
|
||||||
|
std::stoi(argv[3]) /*interval*/);
|
||||||
|
} else if (mode == "recv") {
|
||||||
|
do_recv(std::stoi(argv[2]) /*interval*/);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user