mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 08:46:45 +08:00
Supplement similar demo under linux.
This commit is contained in:
parent
e229f78a15
commit
22a253a72f
@ -62,6 +62,9 @@ if (LIBIPC_BUILD_DEMOS)
|
||||
if (MSVC)
|
||||
add_subdirectory(demo/win_service/service)
|
||||
add_subdirectory(demo/win_service/client)
|
||||
else()
|
||||
add_subdirectory(demo/linux_service/service)
|
||||
add_subdirectory(demo/linux_service/client)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
|
||||
8
demo/linux_service/client/CMakeLists.txt
Normal file
8
demo/linux_service/client/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
project(linux_client)
|
||||
|
||||
file(GLOB SRC_FILES ./*.cpp)
|
||||
file(GLOB HEAD_FILES ./*.h)
|
||||
|
||||
add_executable(${PROJECT_NAME} ${SRC_FILES} ${HEAD_FILES})
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} ipc)
|
||||
28
demo/linux_service/client/main.cpp
Normal file
28
demo/linux_service/client/main.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
/// \brief To create a basic command line program.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
#include "libipc/ipc.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
printf("My Sample Client: Entry\n");
|
||||
ipc::channel ipc_r{"service ipc r", ipc::receiver};
|
||||
ipc::channel ipc_w{"service ipc w", ipc::sender};
|
||||
while (1) {
|
||||
auto msg = ipc_r.recv();
|
||||
if (msg.empty()) {
|
||||
printf("My Sample Client: message recv error\n");
|
||||
return -1;
|
||||
}
|
||||
printf("My Sample Client: message recv: [%s]\n", (char const *)msg.data());
|
||||
while (!ipc_w.send("Copy.")) {
|
||||
printf("My Sample Client: message send error\n");
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
printf("My Sample Client: message send [Copy]\n");
|
||||
}
|
||||
printf("My Sample Client: Exit\n");
|
||||
return 0;
|
||||
}
|
||||
8
demo/linux_service/service/CMakeLists.txt
Normal file
8
demo/linux_service/service/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
project(linux_service)
|
||||
|
||||
file(GLOB SRC_FILES ./*.cpp)
|
||||
file(GLOB HEAD_FILES ./*.h)
|
||||
|
||||
add_executable(${PROJECT_NAME} ${SRC_FILES} ${HEAD_FILES})
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} ipc)
|
||||
35
demo/linux_service/service/main.cpp
Normal file
35
demo/linux_service/service/main.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
/// \brief To create a basic Windows Service in C++.
|
||||
/// \see https://www.codeproject.com/Articles/499465/Simple-Windows-Service-in-Cplusplus
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
#include "libipc/ipc.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
printf("My Sample Service: Main: Entry\n");
|
||||
|
||||
ipc::channel ipc_r{"service ipc r", ipc::sender};
|
||||
ipc::channel ipc_w{"service ipc w", ipc::receiver};
|
||||
|
||||
while (1) {
|
||||
if (!ipc_r.send("Hello, World!")) {
|
||||
printf("My Sample Service: send failed.\n");
|
||||
}
|
||||
else {
|
||||
printf("My Sample Service: send [Hello, World!]\n");
|
||||
auto msg = ipc_w.recv(1000);
|
||||
if (msg.empty()) {
|
||||
printf("My Sample Service: recv error\n");
|
||||
} else {
|
||||
printf("%s\n", (std::string{"My Sample Service: recv ["} + msg.get<char const *>() + "]").c_str());
|
||||
}
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::seconds(3));
|
||||
}
|
||||
|
||||
printf("My Sample Service: Main: Exit\n");
|
||||
return 0;
|
||||
}
|
||||
@ -49,7 +49,9 @@ id_t acquire(char const * name, std::size_t size, unsigned mode) {
|
||||
ipc::error("fail acquire: name is empty\n");
|
||||
return nullptr;
|
||||
}
|
||||
ipc::string op_name = ipc::string{"__IPC_SHM__"} + name;
|
||||
// For portable use, a shared memory object should be identified by name of the form /somename.
|
||||
// see: https://man7.org/linux/man-pages/man3/shm_open.3.html
|
||||
ipc::string op_name = ipc::string{"/__IPC_SHM__"} + name;
|
||||
// Open the object for read-write access.
|
||||
int flag = O_RDWR;
|
||||
switch (mode) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user