Skip to content

Commit b78ff60

Browse files
committed
initial commit
EXPERIMENTAL drpc c++ support. A client can be made; a server not yet. This uses ASIO for supporting both asynchronous and synchronous communications.
0 parents  commit b78ff60

21 files changed

+2887
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build/

CMakeLists.txt

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright (c) 2021 Storj Labs, Inc.
2+
# See LICENSE for copying information.
3+
4+
cmake_minimum_required(VERSION 3.16)
5+
project(
6+
drpc_cpp
7+
DESCRIPTION "DRPC implementation in C++20"
8+
LANGUAGES CXX)
9+
10+
#set(CMAKE_DEBUG_TARGET_PROPERTIES INCLUDE_DIRECTORIES)
11+
#set(CMAKE_VERBOSE_MAKEFILE on)
12+
13+
if(CMAKE_CXX_COMPILER_ID MATCHES GNU)
14+
add_compile_options(-fcoroutines -pthread)
15+
endif()
16+
add_compile_options(-Wall -Wextra -pedantic -Werror)
17+
18+
add_subdirectory(src)
19+
20+
# Only do the following if this is the main project, and not included in something else
21+
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
22+
set(CMAKE_CXX_STANDARD 20)
23+
24+
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
25+
26+
add_subdirectory(examples)
27+
28+
# Testing only available if this is the main app
29+
enable_testing()
30+
endif()
31+
32+
# Testing only available if this is the main app
33+
# Emergency override MODERN_CMAKE_BUILD_TESTING provided as well
34+
if((CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR MODERN_CMAKE_BUILD_TESTING)
35+
AND BUILD_TESTING)
36+
add_subdirectory(tests)
37+
endif()

CODE_OF_CONDUCT.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Code of Conduct
2+
Storj strives to create a welcoming and thriving environment. In order to ensure that every community member is respected and supported, we’ve adopted some general community guidelines.
3+
4+
## Our Pledge
5+
6+
We value diversity and inclusiveness in our community and encourage participation by fostering an open and welcoming environment. We as contributors and maintainers pledge to keep our community respectful and inclusive. By participating in our project, you pledge to keep our community a harassment-free experience for everyone, regardless of race, color, religion, gender, sexual orientation, national origin, age, disability, pregnancy, marital status, body size, appearance, education, level of experience, education, or socioeconomic status.
7+
8+
## Our Standards
9+
10+
### Examples of behavior that contributes to creating a positive environment
11+
include:
12+
13+
* Using welcoming and inclusive language
14+
* Being respectful of differing viewpoints and experiences
15+
* Gracefully accepting constructive criticism
16+
* Focusing on what is best for the community
17+
* Showing empathy towards other community members
18+
19+
### Examples of unacceptable behavior by participants include:
20+
21+
* The use of sexualized language or imagery and unwelcome sexual attention or
22+
advances
23+
* Trolling, insulting/derogatory comments, and personal or political attacks
24+
* Public or private harassment
25+
* Publishing others' private information, such as a physical or electronic
26+
address, without explicit permission
27+
* Other conduct which could reasonably be considered inappropriate in a
28+
professional setting
29+
30+
## Our Responsibilities
31+
32+
Project maintainers are responsible for clarifying the standards of acceptable behavior and agree to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
33+
34+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned with this Code of Conduct, or to ban temporarily or permanently any contributor for any behavior that they deem inappropriate, threatening, offensive, or harmful.
35+
36+
## Scope
37+
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community.Examples of representing a project or community include using an official project email address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
38+
39+
## Enforcement
40+
41+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported anonymously or by contacting the project team at [email protected]. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
42+
43+
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership or maintainers.
44+
45+
## Attribution
46+
47+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
48+
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
49+
50+
[homepage]: https://www.contributor-covenant.org
51+
52+
For answers to common questions about this code of conduct, see
53+
https://www.contributor-covenant.org/faq

LICENSE

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

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
C++ bindings, runtime library, and code generation for DRPC
2+
3+
## Links
4+
5+
* [DRPC on Githb](https://github.com/storj/drpc/)
6+
* [DRPC website](https://storj.github.io/drpc/)
7+
* [Quickstart documentation](https://storj.github.io/drpc/docs.html)
8+
* [Launch blog post](https://www.storj.io/blog/introducing-drpc-our-replacement-for-grpc)
9+
* [Examples](https://github.com/storj/drpc/tree/main/examples)
10+
11+
## Licensing
12+
13+
DRPC-C++ is licensed under the MIT/expat license. See the LICENSE file for more.

examples/CMakeLists.txt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2021 Storj Labs, Inc.
2+
# See LICENSE for copying information.
3+
4+
set(CMAKE_CXX_STANDARD 20)
5+
6+
find_package(Protobuf REQUIRED)
7+
8+
set(ProtoFiles pb/sesamestreet.proto)
9+
protobuf_generate_cpp(ProtoSources ProtoHeaders ${ProtoFiles})
10+
11+
add_executable(client client.cpp pb/sesamestreet_drpc.pb.h ${ProtoSources} ${ProtoHeaders})
12+
13+
target_link_libraries(client drpc)

examples/cert.pem

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIDFDCCAfwCCQCT23qfpphtOjANBgkqhkiG9w0BAQsFADBMMQswCQYDVQQGEwJV
3+
UzERMA8GA1UECAwITWljaGlnYW4xFDASBgNVBAcMC1NwcmluZ2ZpZWxkMRQwEgYD
4+
VQQDDAtleGFtcGxlLmNvbTAeFw0yMTA1MjQyMzAyNTFaFw0yMTA2MjMyMzAyNTFa
5+
MEwxCzAJBgNVBAYTAlVTMREwDwYDVQQIDAhNaWNoaWdhbjEUMBIGA1UEBwwLU3By
6+
aW5nZmllbGQxFDASBgNVBAMMC2V4YW1wbGUuY29tMIIBIjANBgkqhkiG9w0BAQEF
7+
AAOCAQ8AMIIBCgKCAQEAxLXg6gRZVfHH2WYFNZEqO1SRdE98kl6c4/BTSsFB+kQ8
8+
AgbkkZUujxYdF6yqOLJuCe5QIc4aIfq7YjM/MGNyFIgIPZIVmznrO8Yo0Q687Gpn
9+
LE7Z1rwDf0i4s+MioJV11St31zXMZh6wHozgHE5VmD/XY8bwkcnYAJ5/M7CU6I/v
10+
YJSwWvgWq91lE9cq9wDjpx12cq75AbaXBZb2Z3CO7Mee0u65At0AQolEH97NRFpo
11+
okbKGhmeas8YzvRxrd13uk0Rderm7haqGs1GQ5tmc+fch7eenKxVEGaGFa8CfBqE
12+
uiclJT9aKnLy8yYPNb4+sBYu2Pym65ar1RFDwvPyXQIDAQABMA0GCSqGSIb3DQEB
13+
CwUAA4IBAQC5p/apnKz1aNWUGHaqQEa+AYtDEHIIY5469H4ik7XAraFUlHIiyc9K
14+
GjG0bHFdUIGferWgW9nrqfTBUH6kqMVnl+IkbhjluZUjcn3ZfLiY8ZeHKWkz5b1U
15+
O7TAf0Pv6iblbeNNT5GUM/c3lEtF6XREm6z8AyrnwoXvgYVlnPy+E82+oOdAHPmH
16+
1pGriEvEYN4x1u7+QWyIuAiEg3lwnkPUAM8lCYA0WSDl5zcz4Ii5i1j86oLvrrc4
17+
aqT+WiPwhXoaYqYxqRO+h3PF/VaPg9IU08uzeaXCOXL/3enEdCkHfOvZzbR8Ejnv
18+
QF8E8lGLQWYi+mqdMJdzNgaluyJLTPRJ
19+
-----END CERTIFICATE-----

examples/client.cpp

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright (c) 2021 Storj Labs, Inc.
2+
// See LICENSE for copying information.
3+
4+
#include <deque>
5+
#include <iostream>
6+
#include <memory>
7+
#include <string>
8+
9+
#include <asio.hpp>
10+
#include <asio/ssl.hpp>
11+
12+
#include "drpc.h"
13+
#include "drpc/ssl.h"
14+
#include "pb/sesamestreet.pb.h"
15+
#include "pb/sesamestreet_drpc.pb.h"
16+
17+
namespace drpc::example {
18+
19+
namespace pb = ::sesamestreet;
20+
21+
using namespace std::literals::string_literals;
22+
23+
asio::awaitable<pb::Cookie::Type> async_secure_cookie_client(
24+
asio::io_service& io_service,
25+
std::string host,
26+
std::string service,
27+
pb::Cookie::Type type
28+
) {
29+
asio::ssl::context tls_context(asio::ssl::context::tlsv13);
30+
tls_context.load_verify_file("cert.pem");
31+
32+
auto ssl_stream = co_await drpc::ssl::async_ssl_connect(
33+
asio::ip::tcp::socket(io_service),
34+
std::move(tls_context),
35+
std::move(host),
36+
std::move(service),
37+
asio::use_awaitable
38+
);
39+
40+
pb::CookieMonsterAsyncClient s {std::move(*ssl_stream)};
41+
pb::Cookie cookie;
42+
cookie.set_type(type);
43+
44+
auto crumbs = co_await s.EatCookie(cookie);
45+
co_return crumbs.cookie().type();
46+
}
47+
48+
int async_example(const std::string& hostname, const std::string& service, const std::string& cookie_type_name) {
49+
asio::io_service io_service;
50+
pb::Cookie::Type cookie_type;
51+
if (!pb::Cookie::Type_Parse(cookie_type_name, &cookie_type)) {
52+
throw std::runtime_error("invalid cookie type \""s + cookie_type_name + "\"");
53+
}
54+
55+
auto result = asio::co_spawn(
56+
io_service,
57+
async_secure_cookie_client(io_service, hostname, service, cookie_type),
58+
asio::use_future
59+
);
60+
io_service.run();
61+
62+
auto crumbs_type = result.get();
63+
std::cout << "Got crumbs from: " << pb::Cookie_Type_Name(crumbs_type) << std::endl;
64+
65+
return 0;
66+
}
67+
68+
pb::Cookie::Type secure_cookie_client(
69+
asio::io_service& io_service,
70+
const std::string& host,
71+
const std::string& service,
72+
pb::Cookie::Type type
73+
) {
74+
asio::ssl::context tls_context(asio::ssl::context::tlsv13);
75+
tls_context.load_verify_file("cert.pem");
76+
77+
auto ssl_stream = drpc::ssl::ssl_connect(
78+
asio::ip::tcp::socket(io_service),
79+
std::move(tls_context),
80+
host,
81+
service
82+
);
83+
84+
pb::CookieMonsterSyncClient s {std::move(ssl_stream)};
85+
pb::Cookie cookie;
86+
cookie.set_type(type);
87+
88+
auto crumbs = s.EatCookie(cookie);
89+
return crumbs.cookie().type();
90+
}
91+
92+
int example(const std::string& hostname, const std::string& service, const std::string& cookie_type_name) {
93+
asio::io_service io_service;
94+
pb::Cookie::Type cookie_type;
95+
if (!pb::Cookie::Type_Parse(cookie_type_name, &cookie_type)) {
96+
throw std::runtime_error("invalid cookie type \""s + cookie_type_name + "\"");
97+
}
98+
99+
auto result = secure_cookie_client(io_service, hostname, service, cookie_type);
100+
101+
std::cout << "Got crumbs from: " << pb::Cookie_Type_Name(result) << std::endl;
102+
103+
return 0;
104+
}
105+
106+
} // namespace drpc::example
107+
108+
int main(int argc, char* argv[]) {
109+
if (argc < 4) {
110+
std::cerr << "Usage: " << argv[0] << " <localhost> <host> <cookietype>\n";
111+
return 1;
112+
}
113+
try {
114+
// change this from "async_example" to "example" to run the synchronous example
115+
return drpc::example::async_example(argv[1], argv[2], argv[3]);
116+
} catch (const std::exception& e) {
117+
std::cerr << "Failure: " << e.what();
118+
}
119+
}

examples/key.pem

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDEteDqBFlV8cfZ
3+
ZgU1kSo7VJF0T3ySXpzj8FNKwUH6RDwCBuSRlS6PFh0XrKo4sm4J7lAhzhoh+rti
4+
Mz8wY3IUiAg9khWbOes7xijRDrzsamcsTtnWvAN/SLiz4yKglXXVK3fXNcxmHrAe
5+
jOAcTlWYP9djxvCRydgAnn8zsJToj+9glLBa+Bar3WUT1yr3AOOnHXZyrvkBtpcF
6+
lvZncI7sx57S7rkC3QBCiUQf3s1EWmiiRsoaGZ5qzxjO9HGt3Xe6TRF16ubuFqoa
7+
zUZDm2Zz59yHt56crFUQZoYVrwJ8GoS6JyUlP1oqcvLzJg81vj6wFi7Y/KbrlqvV
8+
EUPC8/JdAgMBAAECggEBALslyvD56ECTQi68GDKphINj5urAgOUErMFTsPrLOMkw
9+
x+KpyiYTx3MMd+tBM8WzRGugVo6C9mvjjwlTdkfEJRIRhb2GUZYYGYwJ8GVNX0KR
10+
zMzjy+63e6BKgKdsblJXP/7qVx7iKS/y/F5kawejI14xRuu/prsdTKryIFT+J/3E
11+
357M+iJze/DsraWpvTncT8jE5DspM/65u55sKU5+A0pIgXsoVo8vJuGGS8F90Gek
12+
nkEjzgHMAaAmJNu+oIgKbhqftBDSRjpd39ntKcedHrJkFyofcz9Ng9FPi6x3Sqvz
13+
VXEcGrKcjCPkEGE2TOf3oIcTssZdak1acb1HgY2wKaECgYEA5/UrKBwtVhK7ECnv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