Skip to content

Commit 22a253a

Browse files
committed
Supplement similar demo under linux.
1 parent e229f78 commit 22a253a

File tree

6 files changed

+85
-1
lines changed

6 files changed

+85
-1
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ if (LIBIPC_BUILD_DEMOS)
6262
if (MSVC)
6363
add_subdirectory(demo/win_service/service)
6464
add_subdirectory(demo/win_service/client)
65+
else()
66+
add_subdirectory(demo/linux_service/service)
67+
add_subdirectory(demo/linux_service/client)
6568
endif()
6669
endif()
6770

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

demo/linux_service/client/main.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// \brief To create a basic command line program.
2+
3+
#include <stdio.h>
4+
#include <thread>
5+
#include <chrono>
6+
7+
#include "libipc/ipc.h"
8+
9+
int main(int argc, char *argv[]) {
10+
printf("My Sample Client: Entry\n");
11+
ipc::channel ipc_r{"service ipc r", ipc::receiver};
12+
ipc::channel ipc_w{"service ipc w", ipc::sender};
13+
while (1) {
14+
auto msg = ipc_r.recv();
15+
if (msg.empty()) {
16+
printf("My Sample Client: message recv error\n");
17+
return -1;
18+
}
19+
printf("My Sample Client: message recv: [%s]\n", (char const *)msg.data());
20+
while (!ipc_w.send("Copy.")) {
21+
printf("My Sample Client: message send error\n");
22+
std::this_thread::sleep_for(std::chrono::seconds(1));
23+
}
24+
printf("My Sample Client: message send [Copy]\n");
25+
}
26+
printf("My Sample Client: Exit\n");
27+
return 0;
28+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
project(linux_service)
2+
3+
file(GLOB SRC_FILES ./*.cpp)
4+
file(GLOB HEAD_FILES ./*.h)
5+
6+
add_executable(${PROJECT_NAME} ${SRC_FILES} ${HEAD_FILES})
7+
8+
target_link_libraries(${PROJECT_NAME} ipc)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/// \brief To create a basic Windows Service in C++.
2+
/// \see https://www.codeproject.com/Articles/499465/Simple-Windows-Service-in-Cplusplus
3+
4+
#include <stdio.h>
5+
#include <string>
6+
#include <thread>
7+
#include <chrono>
8+
9+
#include "libipc/ipc.h"
10+
11+
int main(int argc, char *argv[]) {
12+
printf("My Sample Service: Main: Entry\n");
13+
14+
ipc::channel ipc_r{"service ipc r", ipc::sender};
15+
ipc::channel ipc_w{"service ipc w", ipc::receiver};
16+
17+
while (1) {
18+
if (!ipc_r.send("Hello, World!")) {
19+
printf("My Sample Service: send failed.\n");
20+
}
21+
else {
22+
printf("My Sample Service: send [Hello, World!]\n");
23+
auto msg = ipc_w.recv(1000);
24+
if (msg.empty()) {
25+
printf("My Sample Service: recv error\n");
26+
} else {
27+
printf("%s\n", (std::string{"My Sample Service: recv ["} + msg.get<char const *>() + "]").c_str());
28+
}
29+
}
30+
std::this_thread::sleep_for(std::chrono::seconds(3));
31+
}
32+
33+
printf("My Sample Service: Main: Exit\n");
34+
return 0;
35+
}

src/libipc/platform/posix/shm_posix.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ id_t acquire(char const * name, std::size_t size, unsigned mode) {
4949
ipc::error("fail acquire: name is empty\n");
5050
return nullptr;
5151
}
52-
ipc::string op_name = ipc::string{"__IPC_SHM__"} + name;
52+
// For portable use, a shared memory object should be identified by name of the form /somename.
53+
// see: https://man7.org/linux/man-pages/man3/shm_open.3.html
54+
ipc::string op_name = ipc::string{"/__IPC_SHM__"} + name;
5355
// Open the object for read-write access.
5456
int flag = O_RDWR;
5557
switch (mode) {

0 commit comments

Comments
 (0)