Skip to content

Commit

Permalink
Improve internal implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasy-peak committed Aug 22, 2024
1 parent 2cb1c6c commit ff74aa3
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 42 deletions.
64 changes: 59 additions & 5 deletions out/bi_web/include/fantasy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace fantasy {
class HelloWorldClient final {
public:
HelloWorldClient(const frpc::ChannelConfig& config,
std::function<void(std::string)> error)
const std::function<void(std::string)>& error)
: m_channel(std::make_unique<frpc::BiChannel>(
config,
error,
Expand All @@ -68,7 +68,7 @@ class HelloWorldClient final {
HelloWorldClient(const frpc::ChannelConfig& config,
const std::shared_ptr<zmq::context_t>& context_ptr,
const std::shared_ptr<zmq::socket_t>& socket_ptr,
std::function<void(std::string)> error)
const std::function<void(std::string)>& error)
: m_channel(std::make_unique<frpc::BiChannel>(
config,
context_ptr,
Expand All @@ -80,7 +80,7 @@ class HelloWorldClient final {

HelloWorldClient(const frpc::ChannelConfig& config,
const std::shared_ptr<zmq::context_t>& context_ptr,
std::function<void(std::string)> error)
const std::function<void(std::string)>& error)
: m_channel(std::make_unique<frpc::BiChannel>(
config,
context_ptr,
Expand All @@ -89,6 +89,11 @@ class HelloWorldClient final {
m_error(error) {
}

HelloWorldClient(const HelloWorldClient&) = delete;
HelloWorldClient& operator=(const HelloWorldClient&) = delete;
HelloWorldClient(HelloWorldClient&&) = delete;
HelloWorldClient& operator=(HelloWorldClient&&) = delete;

void start() {
m_channel->start();
}
Expand Down Expand Up @@ -178,7 +183,7 @@ class HelloWorldClient final {
std::optional<std::string> date,
frpc::DateTime date_time) mutable {
auto handler_ptr =
std::make_shared<Handler>(std::move(handler));
std::make_shared<Handler>(std::forward<Handler>(handler));
this->hello_world(
std::move(bank_info),
std::move(bank_name),
Expand Down Expand Up @@ -239,7 +244,7 @@ class HelloWorldClient final {
frpc::DateTime date_time,
const auto& timeout) mutable {
auto handler_ptr =
std::make_shared<Handler>(std::move(handler));
std::make_shared<Handler>(std::forward<Handler>(handler));
this->hello_world(
std::move(bank_info),
std::move(bank_name),
Expand Down Expand Up @@ -473,6 +478,9 @@ class HelloWorldClient final {
};

struct HelloWorldServerHandler {
HelloWorldServerHandler() = default;
virtual ~HelloWorldServerHandler() = default;

virtual void hello_world(
BankInfo bank_info,
std::string bank_name,
Expand All @@ -485,6 +493,9 @@ struct HelloWorldServerHandler {
};

struct AsioCoroHelloWorldServerHandler {
AsioCoroHelloWorldServerHandler() = default;
virtual ~AsioCoroHelloWorldServerHandler() = default;

#ifdef __cpp_impl_coroutine
virtual asio::awaitable<void> hello_world(
BankInfo bank_info,
Expand All @@ -509,6 +520,9 @@ struct AsioCoroHelloWorldServerHandler {
};

struct FrpcCoroHelloWorldServerHandler {
FrpcCoroHelloWorldServerHandler() = default;
virtual ~FrpcCoroHelloWorldServerHandler() = default;

#ifdef __cpp_impl_coroutine
virtual frpc::Task<void> hello_world(
BankInfo bank_info,
Expand Down Expand Up @@ -603,6 +617,11 @@ class HelloWorldServer final {
#endif
}

HelloWorldServer(const HelloWorldServer&) = delete;
HelloWorldServer& operator=(const HelloWorldServer&) = delete;
HelloWorldServer(HelloWorldServer&&) = delete;
HelloWorldServer& operator=(HelloWorldServer&&) = delete;

auto& socket() {
return m_channel->socket();
}
Expand Down Expand Up @@ -843,11 +862,17 @@ MSGPACK_ADD_ENUM(fantasy::HelloWorldSenderHelloWorldReceiver)
namespace fantasy {

struct HelloWorldReceiverHandler {
HelloWorldReceiverHandler() = default;
virtual ~HelloWorldReceiverHandler() = default;

virtual void hello_world(std::string in) noexcept = 0;
virtual void notice(int32_t in, std::string info) noexcept = 0;
};

struct AsioCoroHelloWorldReceiverHandler {
AsioCoroHelloWorldReceiverHandler() = default;
virtual ~AsioCoroHelloWorldReceiverHandler() = default;

#ifdef __cpp_impl_coroutine
virtual asio::awaitable<void> hello_world(std::string in) noexcept = 0;
virtual asio::awaitable<void> notice(int32_t in,
Expand All @@ -859,6 +884,9 @@ struct AsioCoroHelloWorldReceiverHandler {
};

struct FrpcCoroHelloWorldReceiverHandler {
FrpcCoroHelloWorldReceiverHandler() = default;
virtual ~FrpcCoroHelloWorldReceiverHandler() = default;

#ifdef __cpp_impl_coroutine
virtual frpc::Task<void> hello_world(std::string in) noexcept = 0;
virtual frpc::Task<void> notice(int32_t in, std::string info) noexcept = 0;
Expand Down Expand Up @@ -931,6 +959,11 @@ class HelloWorldReceiver final {
#endif
}

HelloWorldReceiver(const HelloWorldReceiver&) = delete;
HelloWorldReceiver& operator=(const HelloWorldReceiver&) = delete;
HelloWorldReceiver(HelloWorldReceiver&&) = delete;
HelloWorldReceiver& operator=(HelloWorldReceiver&&) = delete;

void start() {
m_channel->start();
}
Expand Down Expand Up @@ -1126,6 +1159,11 @@ class HelloWorldSender final {
~HelloWorldSender() {
}

HelloWorldSender(const HelloWorldSender&) = delete;
HelloWorldSender& operator=(const HelloWorldSender&) = delete;
HelloWorldSender(HelloWorldSender&&) = delete;
HelloWorldSender& operator=(HelloWorldSender&&) = delete;

static auto create(frpc::ChannelConfig& config) {
if ((config.socktype != zmq::socket_type::pub) &&
config.socktype != zmq::socket_type::push)
Expand Down Expand Up @@ -1291,6 +1329,11 @@ class StreamClient final {
m_pool_ptr->stop();
}

StreamClient(const StreamClient&) = delete;
StreamClient& operator=(const StreamClient&) = delete;
StreamClient(StreamClient&&) = delete;
StreamClient& operator=(StreamClient&&) = delete;

void start() {
m_channel->start();
}
Expand Down Expand Up @@ -1430,13 +1473,19 @@ class StreamClient final {
};

struct StreamServerHandler {
StreamServerHandler() = default;
virtual ~StreamServerHandler() = default;

virtual void hello_world(
std::shared_ptr<asio::experimental::concurrent_channel<
void(frpc::error_code, std::string)>>,
std::shared_ptr<frpc::Stream<void(std::string)>>) noexcept = 0;
};

struct CoroStreamServerHandler {
CoroStreamServerHandler() = default;
virtual ~CoroStreamServerHandler() = default;

#ifdef __cpp_impl_coroutine
virtual asio::awaitable<void> hello_world(
std::shared_ptr<asio::experimental::concurrent_channel<
Expand Down Expand Up @@ -1502,6 +1551,11 @@ class StreamServer final {
m_pool_ptr->stop();
}

StreamServer(const StreamServer&) = delete;
StreamServer& operator=(const StreamServer&) = delete;
StreamServer(StreamServer&&) = delete;
StreamServer& operator=(StreamServer&&) = delete;

decltype(auto) socket() {
return m_channel->socket();
}
Expand Down
11 changes: 9 additions & 2 deletions out/bi_web/include/impl/asio_context_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#ifndef _FRPC_CONTEXT_POOL_H_
#define _FRPC_CONTEXT_POOL_H_

#include <list>
#include <thread>

#ifdef FRPC_USE_BOOST_ASIO
#include <boost/asio.hpp>
#include <boost/asio/experimental/concurrent_channel.hpp>
Expand Down Expand Up @@ -38,12 +41,16 @@ class ContextPool final {

void start() {
for (auto& context : m_io_contexts)
m_threads.emplace_back(std::jthread([&] { context->run(); }));
m_threads.emplace_back(std::thread([&] { context->run(); }));
}

void stop() {
for (auto& context_ptr : m_io_contexts)
context_ptr->stop();
for (auto& thread : m_threads) {
if (thread.joinable())
thread.join();
}
}

asio::io_context& getIoContext() {
Expand All @@ -62,7 +69,7 @@ class ContextPool final {
std::vector<std::shared_ptr<asio::io_context>> m_io_contexts;
std::list<asio::any_io_executor> m_work;
std::atomic_uint64_t m_next_io_context;
std::vector<std::jthread> m_threads;
std::vector<std::thread> m_threads;
};

} // namespace frpc
Expand Down
5 changes: 1 addition & 4 deletions out/bi_web/include/impl/coroutine.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
#define _FRPC_COROUTINE_H_

#include <algorithm>
#include <atomic>
#include <cassert>
#include <condition_variable>
#include <coroutine>
#include <exception>
#include <future>
#include <mutex>
#include <optional>
#include <type_traits>
#include <functional>

namespace frpc {

Expand Down
2 changes: 2 additions & 0 deletions out/bi_web/include/impl/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include <functional>

#include <msgpack.hpp>
#include <zmq.hpp>
#include <uuid/uuid.h>
#include <nlohmann/json.hpp>

namespace nlohmann {

Expand Down
Loading

0 comments on commit ff74aa3

Please sign in to comment.