Skip to content

Commit 06e1045

Browse files
committed
libmultiprocess initial commit
0 parents  commit 06e1045

File tree

16 files changed

+3695
-0
lines changed

16 files changed

+3695
-0
lines changed

CMakeLists.txt

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Copyright (c) 2019 The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
cmake_minimum_required(VERSION 3.0)
6+
project("Libmultiprocess" CXX)
7+
include(CTest)
8+
find_package(Boost)
9+
find_package(CapnProto)
10+
11+
capnp_generate_cpp(MP_PROXY_SRCS MP_PROXY_HDRS include/mp/proxy.capnp)
12+
13+
set(MP_PUBLIC_HEADERS
14+
${MP_PROXY_HDRS}
15+
include/mp/proxy.h
16+
include/mp/proxy-io.h
17+
include/mp/proxy-types.h
18+
include/mp/proxy.capnp
19+
include/mp/proxy.h
20+
include/mp/util.h)
21+
22+
add_library(multiprocess STATIC
23+
${MP_PROXY_SRCS}
24+
${MP_PUBLIC_HEADERS}
25+
src/mp/proxy.cpp
26+
src/mp/util.cpp)
27+
target_include_directories(multiprocess PUBLIC
28+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
29+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
30+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
31+
$<INSTALL_INTERFACE:include>
32+
${CAPNP_INCLUDE_DIRECTORY}
33+
${Boost_INCLUDE_DIR})
34+
set_target_properties(multiprocess PROPERTIES PUBLIC_HEADER "${MP_PUBLIC_HEADERS}")
35+
install(TARGETS multiprocess EXPORT Multiprocess ARCHIVE DESTINATION lib PUBLIC_HEADER DESTINATION include/mp)
36+
37+
add_executable(mpgen src/mp/gen.cpp)
38+
target_include_directories(mpgen PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
39+
target_include_directories(mpgen PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include>)
40+
target_link_libraries(mpgen PRIVATE CapnProto::capnp-rpc)
41+
target_link_libraries(mpgen PRIVATE -L${capnp_LIBRARY_DIRS} capnpc)
42+
install(TARGETS mpgen EXPORT Multiprocess RUNTIME DESTINATION bin)
43+
44+
configure_file(pkgconfig/libmultiprocess.pc.in "${CMAKE_CURRENT_BINARY_DIR}/libmultiprocess.pc" @ONLY)
45+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libmultiprocess.pc" DESTINATION "pkgconfig")
46+
47+
install(EXPORT Multiprocess DESTINATION lib/cmake/Multiprocess)
48+
49+
set(CAPNPC_IMPORT_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include")
50+
capnp_generate_cpp(FOO_PROXY_SRCS FOO_PROXY_HDRS src/mp/test/foo.capnp)
51+
52+
53+
if(BUILD_TESTING)
54+
add_custom_command(
55+
OUTPUT
56+
src/mp/test/foo.capnp.proxy.h
57+
src/mp/test/foo.capnp.proxy-server.c++
58+
src/mp/test/foo.capnp.proxy-client.c++
59+
src/mp/test/foo.capnp.proxy-types.c++
60+
src/mp/test/foo.capnp.proxy-types.h
61+
PRE_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory src/mp/test
62+
COMMAND ${CMAKE_COMMAND} -E chdir src "${CMAKE_CURRENT_BINARY_DIR}/mpgen" mp/test/foo "${CMAKE_CURRENT_SOURCE_DIR}/src/mp/test/foo.capnp" "${CMAKE_CURRENT_SOURCE_DIR}/include" "${capnp_PREFIX}/include"
63+
DEPENDS src/mp/test/foo.capnp mpgen
64+
)
65+
add_executable(mptest EXCLUDE_FROM_ALL
66+
${FOO_PROXY_HDRS}
67+
${FOO_PROXY_SRCS}
68+
${MP_PROXY_HDRS}
69+
src/mp/test/foo.capnp.proxy.h
70+
src/mp/test/foo.capnp.proxy-server.c++
71+
src/mp/test/foo.capnp.proxy-client.c++
72+
src/mp/test/foo.capnp.proxy-types.c++
73+
src/mp/test/foo.capnp.proxy-types.h
74+
src/mp/test/foo-types.h
75+
src/mp/test/foo.h
76+
src/mp/test/test.cpp
77+
)
78+
target_include_directories(mptest PUBLIC
79+
${CAPNP_INCLUDE_DIRECTORY}
80+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
81+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
82+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
83+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/src>
84+
)
85+
target_link_libraries(mptest PRIVATE CapnProto::capnp-rpc)
86+
target_link_libraries(mptest PRIVATE CapnProto::kj-test)
87+
target_link_libraries(mptest PRIVATE multiprocess)
88+
add_test(NAME mptest COMMAND mptest)
89+
endif()

COPYING

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2009-2019 The Bitcoin Core developers
4+
Copyright (c) 2009-2019 Bitcoin Developers
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# libmultiprocess
2+
3+
## Summary
4+
5+
C++ library and code generator making it easy to call functions and reference objects in different processes.
6+
7+
## Description
8+
9+
Given an interface description of an object with one or more methods, libmultiprocess generates:
10+
11+
* A C++ `ProxyClient` class with an implementation of each interface method that sends a request over a socket, waits for a response, and returns the result.
12+
* A C++ `ProxyServer` class that listens for requests over a socket and calls a wrapped C++ object implementating the same interface to actually execute the requests.
13+
14+
The function call ⇆ request translation supports input and output arguments, standard types like `unique_ptr`, `vector`, `map`, and `optional`, and bidirectional calls between processes through interface pointer and `std::function` arguments.
15+
16+
If the wrapped C++ object inherits from an abstract base class declaring virtual methods, the generated `ProxyClient` objects can inherit from the same class, allowing interprocess calls to replace local calls without changes to existing code.
17+
18+
There is also optional support for thread mapping, so each thread making interprocess calls can have a dedicated thread processing requests from it, and callbacks from processing threads are executed on corresponding request threads (so recursive mutexes and thread names function as expected in callbacks).
19+
20+
## Example
21+
22+
A simple interface description can be found at [src/mp/test/foo.capnp](src/mp/test/foo.capnp), implementation in [src/mp/test/foo.h](src/mp/test/foo.h), and usage in [src/mp/test/test.cpp](src/mp/test/test.cpp).
23+
24+
## Future directions
25+
26+
_libmultiprocess_ uses the [Cap'n Proto](https://capnproto.org) interface description language and protocol, but it could be extended or changed to use a different IDL/protocol like [gRPC](https://grpc.io). The nice thing about _Cap'n Proto_ compared to _gRPC_ and most other lower level protocols is that it allows interface pointers (_Services_ in gRPC parlance) to be passed as method arguments and return values, so object references and bidirectional requests work out of the box. Supporting a lower-level protocol would require writing adding maps and tracking code to proxy objects.
27+
28+
_libmultiprocess_ is currently compatible with sandboxing but could add platform-specific sandboxing support or integration with a sandboxing library like [SAPI](https://github.com/google/sandboxed-api).
29+
30+
## Installation
31+
32+
Installation currently requires boost[*] and Cap'n Proto:
33+
34+
```sh
35+
apt install libboost-dev libcapnp-dev capnproto
36+
brew install boost capnp
37+
dnf install boost-devel capnproto
38+
39+
Installation steps are:
40+
41+
```sh
42+
mkdir build
43+
cd build
44+
cmake ..
45+
make
46+
make test
47+
make install
48+
```
49+
50+
[*] The boost dependency should be eliminated; it is solely for `boost::optional`.

0 commit comments

Comments
 (0)