Supplement similar demo under linux.

This commit is contained in:
mutouyun 2023-09-23 14:19:12 +08:00
parent 2d8d6facc3
commit b6c25760cf
6 changed files with 85 additions and 1 deletions

View File

@ -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()

View 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)

View 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;
}

View 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)

View 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;
}

View File

@ -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) {