Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
dr7ana committed Sep 28, 2023
1 parent 4aca09e commit 6cb8adb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
46 changes: 38 additions & 8 deletions include/quic/btstream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace oxen::quic
using time_point = std::chrono::steady_clock::time_point;

// timeout is used for sent requests awaiting responses
inline constexpr std::chrono::milliseconds TIMEOUT{10s};
inline constexpr std::chrono::seconds DEFAULT_TIMEOUT{10s};

// request sizes
inline constexpr long long MAX_REQ_LEN = 10_M;
Expand Down Expand Up @@ -81,13 +81,14 @@ namespace oxen::quic

template <typename... Opt>
sent_request(BTRequestStream& bp, std::string_view d, int64_t rid, Opt&&... opts) :
req_id{rid}, return_sender{bp}, req_time{get_time()}, expiry{req_time}
req_id{rid}, return_sender{bp}, total_len{d.length()}, req_time{get_time()}, expiry{req_time}
{
((void)handle_req_opts(std::forward<Opt>(opts)), ...);
if (total_len > MAX_REQ_LEN)
throw std::invalid_argument{"Request body too long!"};

total_len = d.length();
((void)handle_req_opts(std::forward<Opt>(opts)), ...);
data = oxenc::bt_serialize(d);
expiry += (timeout) ? *timeout : TIMEOUT;
expiry += timeout.value_or(DEFAULT_TIMEOUT);
}

bool is_expired(std::chrono::steady_clock::time_point tp) const { return expiry < tp; }
Expand Down Expand Up @@ -133,6 +134,17 @@ namespace oxen::quic
return std::dynamic_pointer_cast<BTRequestStream>(shared_from_this());
}

/** API: ::command
Invokes a remote RPC endpoint.
Parameters:
std::string endpoint - remote RPC endpoint to be called
std::string body - remote RPC endpoint body to be called
Opt&&... opts:
std::function<void(message)> cb - callback to be executed if expecting response
std::chrono::milliseconds timeout - request timeout (defaults to 10 seconds)
*/
template <typename... Opt>
void command(std::string endpoint, std::string body, Opt&&... opts)
{
Expand All @@ -142,7 +154,6 @@ namespace oxen::quic

if (req)
{
// if we have a cb, then this is a request; else, it is a command
if (req->cb)
{
send(req->view());
Expand Down Expand Up @@ -176,8 +187,27 @@ namespace oxen::quic

void process_incoming(std::string_view req);

std::shared_ptr<sent_request> make_command(
std::string endpoint, std::string body, std::function<void(message)> = nullptr);
template <typename... Opt>
std::shared_ptr<sent_request> make_command(std::string endpoint, std::string body, Opt&&... opts)
{
oxenc::bt_list_producer btlp;
auto rid = ++next_rid;

try
{
btlp.append("C");
btlp.append(rid);
btlp.append(endpoint);
btlp.append(body);

return std::make_shared<sent_request>(*this, std::move(btlp).str(), rid, std::forward<Opt>(opts)...);
}
catch (const std::exception& e)
{
log::critical(bp_cat, "Invalid outgoing command encoding: {}", e.what());
return nullptr;
}
}

std::optional<sent_request> make_response(int64_t rid, std::string body, bool error = false);

Expand Down
23 changes: 0 additions & 23 deletions src/btstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,29 +183,6 @@ namespace oxen::quic
}
}

std::shared_ptr<sent_request> BTRequestStream::make_command(
std::string endpoint, std::string body, std::function<void(message)> func)
{
oxenc::bt_list_producer btlp;
auto rid = ++next_rid;

try
{
btlp.append("C");
btlp.append(rid);
btlp.append(endpoint);
btlp.append(body);

return std::make_shared<sent_request>(*this, std::move(btlp).str(), rid, func);
}
catch (...)
{
log::critical(bp_cat, "Invalid outgoing command encoding!");
}

return nullptr;
}

std::optional<sent_request> BTRequestStream::make_response(int64_t rid, std::string body, bool error)
{
oxenc::bt_list_producer btlp;
Expand Down

0 comments on commit 6cb8adb

Please sign in to comment.