-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e9723ab
commit f8c2999
Showing
7 changed files
with
152 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#pragma once | ||
#include <asyncpp/dispatcher.h> | ||
#include <asyncpp/grpc/calldata_interface.h> | ||
#include <asyncpp/threadsafe_queue.h> | ||
#include <grpcpp/alarm.h> | ||
#include <grpcpp/completion_queue.h> | ||
|
||
#include <memory> | ||
#include <thread> | ||
|
||
namespace asyncpp::grpc { | ||
class client_cq : public dispatcher, private grpc::calldata_interface { | ||
std::thread m_thread; | ||
threadsafe_queue<std::function<void()>> m_dispatched; | ||
std::atomic_flag m_alarm_set; | ||
::grpc::Alarm m_alarm; | ||
::grpc::CompletionQueue m_cq; | ||
|
||
void handle_event(size_t evt, bool ok) noexcept override; | ||
|
||
public: | ||
client_cq(); | ||
client_cq(const client_cq&) = delete; | ||
client_cq(client_cq&&) = delete; | ||
client_cq& operator=(const client_cq&) = delete; | ||
client_cq& operator=(client_cq&&) = delete; | ||
~client_cq(); | ||
|
||
void push(std::function<void()> fn) override; | ||
|
||
::grpc::CompletionQueue& cq() noexcept { return m_cq; } | ||
const ::grpc::CompletionQueue& cq() const noexcept { return m_cq; } | ||
|
||
static std::shared_ptr<client_cq> get_default(); | ||
}; | ||
} // namespace asyncpp::grpc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <asyncpp/grpc/client_cq.h> | ||
#include <asyncpp/ptr_tag.h> | ||
|
||
namespace asyncpp::grpc { | ||
void client_cq::handle_event(size_t, bool) noexcept { | ||
m_alarm_set.clear(); | ||
auto task = m_dispatched.pop(); | ||
while (task) { | ||
if (*task) (*task)(); | ||
task = m_dispatched.pop(); | ||
} | ||
} | ||
|
||
client_cq::client_cq() { | ||
m_thread = std::thread([this]() { | ||
#ifdef __linux__ | ||
pthread_setname_np(pthread_self(), "grpc_client_cq"); | ||
#endif | ||
void* tag = nullptr; | ||
bool ok = false; | ||
while (this->m_cq.Next(&tag, &ok)) { | ||
auto [i, t] = ptr_untag<calldata_interface>(tag); | ||
if (!i) continue; | ||
i->handle_event(t, ok); | ||
} | ||
}); | ||
} | ||
|
||
client_cq::~client_cq() { | ||
m_cq.Shutdown(); | ||
if (m_thread.joinable()) m_thread.join(); | ||
} | ||
|
||
void client_cq::push(std::function<void()> fn) { | ||
m_dispatched.emplace(std::move(fn)); | ||
if (!m_alarm_set.test_and_set()) { m_alarm.Set(&m_cq, gpr_time_0(GPR_CLOCK_REALTIME), ptr_tag<0, calldata_interface>(this)); } | ||
} | ||
|
||
std::shared_ptr<client_cq> client_cq::get_default() { | ||
static auto instance = std::make_shared<client_cq>(); | ||
return instance; | ||
} | ||
} // namespace asyncpp::grpc |
Oops, something went wrong.