|
| 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 | +} |
0 commit comments