From 720a21f5fde223df72a7e8d5d1eb9a1f7b440cb6 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Wed, 26 Aug 2020 16:10:03 +0800 Subject: [PATCH] remove files --- include/codec.h | 49 -- include/connection.h | 370 ----------- include/const_vars.h | 43 -- include/cplusplus_14.h | 108 --- include/io_service_pool.h | 62 -- include/meta_util.hpp | 106 --- include/rest_rpc.hpp | 2 - include/router.h | 190 ------ include/rpc_client.hpp | 796 ---------------------- include/rpc_server.h | 227 ------- include/string_view.hpp | 1324 ------------------------------------- include/use_asio.hpp | 66 -- 12 files changed, 3343 deletions(-) delete mode 100644 include/codec.h delete mode 100644 include/connection.h delete mode 100644 include/const_vars.h delete mode 100644 include/cplusplus_14.h delete mode 100644 include/io_service_pool.h delete mode 100644 include/meta_util.hpp delete mode 100644 include/rest_rpc.hpp delete mode 100644 include/router.h delete mode 100644 include/rpc_client.hpp delete mode 100644 include/rpc_server.h delete mode 100644 include/string_view.hpp delete mode 100644 include/use_asio.hpp diff --git a/include/codec.h b/include/codec.h deleted file mode 100644 index 7623ad0..0000000 --- a/include/codec.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef REST_RPC_CODEC_H_ -#define REST_RPC_CODEC_H_ - -#include - -namespace rest_rpc { -namespace rpc_service { - - using buffer_type = msgpack::sbuffer; - struct msgpack_codec { - const static size_t init_size = 2 * 1024; - - template - static buffer_type pack_args(Args&&... args) { - buffer_type buffer(init_size); - msgpack::pack(buffer, std::forward_as_tuple(std::forward(args)...)); - return buffer; - } - - template::value>::type> - static std::string pack_args_str(Arg arg, Args&&... args) { - buffer_type buffer(init_size); - msgpack::pack(buffer, std::forward_as_tuple((int)arg, std::forward(args)...)); - return std::string(buffer.data(), buffer.size()); - } - - template - buffer_type pack(T&& t) const { - buffer_type buffer; - msgpack::pack(buffer, std::forward(t)); - return buffer; - } - - template - T unpack(char const* data, size_t length) { - try { - msgpack::unpack(&msg_, data, length); - return msg_.get().as(); - } catch (...) { throw std::invalid_argument("unpack failed: Args not match!"); } - } - - private: - msgpack::unpacked msg_; -}; -} // namespace rpc_service -} // namespace rest_rpc - -#endif // REST_RPC_CODEC_H_ \ No newline at end of file diff --git a/include/connection.h b/include/connection.h deleted file mode 100644 index aeb839b..0000000 --- a/include/connection.h +++ /dev/null @@ -1,370 +0,0 @@ -#ifndef REST_RPC_CONNECTION_H_ -#define REST_RPC_CONNECTION_H_ - -#include -#include -#include -#include -#include "use_asio.hpp" -#include "const_vars.h" -#include "router.h" -#include "cplusplus_14.h" - -using boost::asio::ip::tcp; - -namespace rest_rpc { - namespace rpc_service { - struct ssl_configure { - std::string cert_file; - std::string key_file; - }; - - class connection : public std::enable_shared_from_this, private asio::noncopyable { - public: - connection(boost::asio::io_service& io_service, std::size_t timeout_seconds) - : socket_(io_service), - body_(INIT_BUF_SIZE), - timer_(io_service), - timeout_seconds_(timeout_seconds), - has_closed_(false) { - } - - ~connection() { - close(); - } - - void start() { - if (is_ssl() && !has_shake_) { - async_handshake(); - } - else { - read_head(); - } - } - - tcp::socket& socket() { return socket_; } - - bool has_closed() const { return has_closed_; } - uint64_t request_id() const { - return req_id_; - } - - void response(uint64_t req_id, std::string data, request_type req_type = request_type::req_res) { - auto len = data.size(); - assert(len < MAX_BUF_LEN); - - std::unique_lock lock(write_mtx_); - write_queue_.emplace_back(message_type{ req_id, req_type, std::make_shared(std::move(data)) }); - if (write_queue_.size() > 1) { - return; - } - - write(); - } - - template - void pack_and_response(uint64_t req_id, T data) { - auto result = msgpack_codec::pack_args_str(result_code::OK, std::move(data)); - response(req_id, std::move(result)); - } - - void set_conn_id(int64_t id) { conn_id_ = id; } - - int64_t conn_id() const { return conn_id_; } - - const std::vector& body() const { - return body_; - } - - std::string remote_address() const { - if (has_closed_) { - return ""; - } - - return socket_.remote_endpoint().address().to_string(); - } - - void publish(const std::string& key, const std::string& data) { - auto result = msgpack_codec::pack_args_str(result_code::OK, key, data); - response(0, std::move(result), request_type::sub_pub); - } - - void set_callback(std::function)> callback) { - callback_ = std::move(callback); - } - - void init_ssl_context(const ssl_configure& ssl_conf) { -#ifdef CINATRA_ENABLE_SSL - unsigned long ssl_options = boost::asio::ssl::context::default_workarounds - | boost::asio::ssl::context::no_sslv2 - | boost::asio::ssl::context::single_dh_use; - try { - boost::asio::ssl::context ssl_context(boost::asio::ssl::context::sslv23); - ssl_context.set_options(ssl_options); - ssl_context.set_password_callback([](std::size_t size, - boost::asio::ssl::context_base::password_purpose purpose) {return "123456"; }); - - boost::system::error_code ec; - if (rpcfs::exists(ssl_conf.cert_file, ec)) { - ssl_context.use_certificate_chain_file(ssl_conf.cert_file); - } - - if (rpcfs::exists(ssl_conf.key_file, ec)) - ssl_context.use_private_key_file(ssl_conf.key_file, boost::asio::ssl::context::pem); - - //ssl_context_callback(ssl_context); - ssl_stream_ = std::make_unique>(socket_, ssl_context); - } - catch (const std::exception& e) { - print(e); - } -#endif - } - - private: - void read_head() { - reset_timer(); - auto self(this->shared_from_this()); - async_read_head([this, self](boost::system::error_code ec, std::size_t length) { - if (!socket_.is_open()) { - //LOG(INFO) << "socket already closed"; - return; - } - - if (!ec) { - //const uint32_t body_len = *((int*)(head_)); - //req_id_ = *((std::uint64_t*)(head_ + sizeof(int32_t))); - rpc_header* header = (rpc_header*)(head_); - req_id_ = header->req_id; - const uint32_t body_len = header->body_len; - req_type_ = header->req_type; - if (body_len > 0 && body_len < MAX_BUF_LEN) { - if (body_.size() < body_len) { body_.resize(body_len); } - read_body(body_len); - return; - } - - if (body_len == 0) { // nobody, just head, maybe as heartbeat. - cancel_timer(); - read_head(); - } - else { - print("invalid body len"); - close(); - } - } - else { - print(ec); - close(); - } - }); - } - - void read_body(std::size_t size) { - auto self(this->shared_from_this()); - async_read(size, [this, self](boost::system::error_code ec, std::size_t length) { - cancel_timer(); - - if (!socket_.is_open()) { - //LOG(INFO) << "socket already closed"; - return; - } - - if (!ec) { - read_head(); - if (req_type_ == request_type::req_res) { - router& _router = router::get(); - _router.route(body_.data(), length, this->shared_from_this()); - } - else if (req_type_ == request_type::sub_pub) { - try { - msgpack_codec codec; - auto p = codec.unpack>(body_.data(), length); - callback_(std::move(std::get<0>(p)), std::move(std::get<1>(p)), this->shared_from_this()); - } - catch (const std::exception& ex) { - print(ex); - } - } - } - else { - //LOG(INFO) << ec.message(); - } - }); - } - - void write() { - auto& msg = write_queue_.front(); - write_size_ = (uint32_t)msg.content->size(); - std::array write_buffers; - write_buffers[0] = boost::asio::buffer(&write_size_, sizeof(uint32_t)); - write_buffers[1] = boost::asio::buffer(&msg.req_id, sizeof(uint64_t)); - write_buffers[2] = boost::asio::buffer(&msg.req_type, sizeof(request_type)); - write_buffers[3] = boost::asio::buffer(msg.content->data(), write_size_); - - auto self = this->shared_from_this(); - async_write(write_buffers, - [this, self](boost::system::error_code ec, std::size_t length) { - on_write(ec, length); - }); - } - - void on_write(boost::system::error_code ec, std::size_t length) { - if (ec) { - print(ec); - close(false); - return; - } - - if (has_closed()) { return; } - - std::unique_lock lock(write_mtx_); - write_queue_.pop_front(); - - if (!write_queue_.empty()) { - write(); - } - } - - void async_handshake() { -#ifdef CINATRA_ENABLE_SSL - auto self = this->shared_from_this(); - ssl_stream_->async_handshake(boost::asio::ssl::stream_base::server, - [this, self](const boost::system::error_code& error) { - if (error) { - print(error); - close(); - return; - } - - has_shake_ = true; - read_head(); - }); -#endif - } - - bool is_ssl() const { -#ifdef CINATRA_ENABLE_SSL - return ssl_stream_ != nullptr; -#else - return false; -#endif - } - - template - void async_read_head(Handler handler) { - if (is_ssl()) { -#ifdef CINATRA_ENABLE_SSL - boost::asio::async_read(*ssl_stream_, boost::asio::buffer(head_, HEAD_LEN), std::move(handler)); -#endif - } - else { - boost::asio::async_read(socket_, boost::asio::buffer(head_, HEAD_LEN), std::move(handler)); - } - } - - template - void async_read(size_t size_to_read, Handler handler) { - if (is_ssl()) { -#ifdef CINATRA_ENABLE_SSL - boost::asio::async_read(*ssl_stream_, boost::asio::buffer(body_.data(), size_to_read), std::move(handler)); -#endif - } - else { - boost::asio::async_read(socket_, boost::asio::buffer(body_.data(), size_to_read), std::move(handler)); - } - } - - template - void async_write(const BufferType& buffers, Handler handler) { - if (is_ssl()) { -#ifdef CINATRA_ENABLE_SSL - boost::asio::async_write(*ssl_stream_, buffers, std::move(handler)); -#endif - } - else { - boost::asio::async_write(socket_, buffers, std::move(handler)); - } - } - - void reset_timer() { - if (timeout_seconds_ == 0) { return; } - - auto self(this->shared_from_this()); - timer_.expires_from_now(std::chrono::seconds(timeout_seconds_)); - timer_.async_wait([this, self](const boost::system::error_code& ec) { - if (has_closed()) { return; } - - if (ec) { return; } - - //LOG(INFO) << "rpc connection timeout"; - close(false); - }); - } - - void cancel_timer() { - if (timeout_seconds_ == 0) { return; } - - timer_.cancel(); - } - - void close(bool close_ssl = true) { -#ifdef CINATRA_ENABLE_SSL - if (close_ssl && ssl_stream_) { - boost::system::error_code ec; - ssl_stream_->shutdown(ec); - ssl_stream_ = nullptr; - } -#endif - if (has_closed_) { - return; - } - - boost::system::error_code ignored_ec; - socket_.shutdown(tcp::socket::shutdown_both, ignored_ec); - socket_.close(ignored_ec); - has_closed_ = true; - has_shake_ = false; - } - - template - void print(Args... args) { -#ifdef _DEBUG - std::initializer_list{( std::cout << args << ' ', 0)...}; - std::cout << "\n"; -#endif - } - - void print(const boost::system::error_code& ec) { - print(ec.value(), ec.message()); - } - - void print(const std::exception& ex) { - print(ex.what()); - } - - tcp::socket socket_; -#ifdef CINATRA_ENABLE_SSL - std::unique_ptr> ssl_stream_ = nullptr; -#endif - bool has_shake_ = false; - char head_[HEAD_LEN]; - std::vector body_; - std::uint64_t req_id_; - request_type req_type_; - - uint32_t write_size_ = 0; - std::mutex write_mtx_; - - asio::steady_timer timer_; - std::size_t timeout_seconds_; - int64_t conn_id_ = 0; - bool has_closed_; - - std::deque write_queue_; - std::function)> callback_; - }; - } // namespace rpc_service -} // namespace rest_rpc - -#endif // REST_RPC_CONNECTION_H_ diff --git a/include/const_vars.h b/include/const_vars.h deleted file mode 100644 index 7173a4e..0000000 --- a/include/const_vars.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once -#include - -namespace rest_rpc { - - enum class result_code : std::int16_t { - OK = 0, - FAIL = 1, - }; - - enum class error_code { - OK, - UNKNOWN, - FAIL, - TIMEOUT, - CANCEL, - BADCONNECTION, - }; - - enum class request_type : uint8_t { - req_res, - sub_pub - }; - - struct message_type { - std::uint64_t req_id; - request_type req_type; - std::shared_ptr content; - }; - - -#pragma pack (1) - struct rpc_header { - uint32_t body_len; - uint64_t req_id; - request_type req_type; - }; -#pragma pack () - - static const size_t MAX_BUF_LEN = 1048576 * 10; - static const size_t HEAD_LEN = 13; - static const size_t INIT_BUF_SIZE = 2 * 1024; -} \ No newline at end of file diff --git a/include/cplusplus_14.h b/include/cplusplus_14.h deleted file mode 100644 index 20425f0..0000000 --- a/include/cplusplus_14.h +++ /dev/null @@ -1,108 +0,0 @@ -#ifndef REST_RPC_CPLUSPLUS_14_H_ -#define REST_RPC_CPLUSPLUS_14_H_ - -#include -#include -#include - -#if __cplusplus == 201103L - -namespace std { -template -struct unique_if { - typedef unique_ptr single_object; -}; - -template -struct unique_if { - typedef unique_ptr unknown_bound; -}; - -template -struct unique_if { - typedef void known_bound; -}; - -template -typename unique_if::single_object make_unique(Args&&... args) { - return unique_ptr(new T(forward(args)...)); -} - -template -typename unique_if::unknown_bound make_unique(size_t n) { - typedef typename remove_extent::type U; - return unique_ptr(new U[n]()); -} - -template -typename unique_if::known_bound make_unique(Args&&...) = delete; - -template -struct index_sequence { - using type = index_sequence; - using value_type = size_t; - static constexpr std::size_t size() noexcept { return sizeof...(Ints); } -}; - -// -------------------------------------------------------------- - -template -struct _merge_and_renumber; - -template -struct _merge_and_renumber, index_sequence> - : index_sequence {}; - -// -------------------------------------------------------------- - -template -struct make_index_sequence : _merge_and_renumber::type, - typename make_index_sequence::type> {}; - -template<> -struct make_index_sequence<0> : index_sequence<> {}; -template<> -struct make_index_sequence<1> : index_sequence<0> {}; - -template -using index_sequence_for = make_index_sequence; - -template -using enable_if_t = typename enable_if::type; - -template -using remove_const_t = typename remove_const::type; - -template -using remove_reference_t = typename remove_reference::type; - -template -using tuple_element_t = typename tuple_element::type; - -template -using decay_t = typename decay::type; - -template -auto apply_helper(F&& f, Tuple&& tp, std::index_sequence) - -> decltype(std::forward(f)(std::get(std::forward(tp))...)) { - return std::forward(f)(std::get(std::forward(tp))...); -} - -template -auto apply(F&& f, Tuple&& tp) - -> decltype(apply_helper(std::forward(f), std::forward(tp), - std::make_index_sequence>::value>{})) { - return apply_helper(std::forward(f), std::forward(tp), - std::make_index_sequence>::value>{}); -} - -template -auto invoke(F&& f, Args&&... args) -> decltype(std::forward(f)(std::forward(args)...)) { - return std::forward(f)(std::forward(args)...); -} - -} // namespace std - -#endif - -#endif // REST_RPC_CPLUSPLUS_14_H_ diff --git a/include/io_service_pool.h b/include/io_service_pool.h deleted file mode 100644 index 219430d..0000000 --- a/include/io_service_pool.h +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef REST_RPC_IO_SERVICE_POOL_H_ -#define REST_RPC_IO_SERVICE_POOL_H_ - -#include -#include -#include "use_asio.hpp" - -namespace rest_rpc { -namespace rpc_service { -class io_service_pool : private asio::noncopyable { - public: - explicit io_service_pool(std::size_t pool_size) : next_io_service_(0) { - if (pool_size == 0) throw std::runtime_error("io_service_pool size is 0"); - - for (std::size_t i = 0; i < pool_size; ++i) { - io_service_ptr io_service(new boost::asio::io_service); - work_ptr work(new boost::asio::io_service::work(*io_service)); - io_services_.push_back(io_service); - work_.push_back(work); - } - } - - void run() { - std::vector> threads; - for (std::size_t i = 0; i < io_services_.size(); ++i) { - threads.emplace_back( - std::make_shared([](io_service_ptr svr) { svr->run(); }, io_services_[i])); - } - - for (std::size_t i = 0; i < threads.size(); ++i) threads[i]->join(); - } - - void stop() { - for (std::size_t i = 0; i < io_services_.size(); ++i) { - io_services_[i]->stop(); - } - } - - boost::asio::io_service& get_io_service() { - boost::asio::io_service& io_service = *io_services_[next_io_service_]; - ++next_io_service_; - if (next_io_service_ == io_services_.size()) next_io_service_ = 0; - return io_service; - } - - private: - typedef std::shared_ptr io_service_ptr; - typedef std::shared_ptr work_ptr; - - /// The pool of io_services. - std::vector io_services_; - - /// The work that keeps the io_services running. - std::vector work_; - - /// The next io_service to use for a connection. - std::size_t next_io_service_; -}; -} // namespace rpc_service -} // namespace rest_rpc - -#endif // REST_RPC_IO_SERVICE_POOL_H_ \ No newline at end of file diff --git a/include/meta_util.hpp b/include/meta_util.hpp deleted file mode 100644 index 0fa1562..0000000 --- a/include/meta_util.hpp +++ /dev/null @@ -1,106 +0,0 @@ -#ifndef REST_RPC_META_UTIL_HPP -#define REST_RPC_META_UTIL_HPP - -#include "cplusplus_14.h" - -namespace rest_rpc{ - - template - void for_each(const std::tuple& t, Func&& f, std::index_sequence) { - (void)std::initializer_list { (f(std::get(t)), void(), 0)...}; - } - - template - void for_each_i(const std::tuple& t, Func&& f, std::index_sequence) { - (void)std::initializer_list { (f(std::get(t), std::integral_constant{}), void(), 0)...}; - } - - template - struct function_traits; - - template - struct function_traits - { - public: - enum { arity = sizeof...(Args)+1 }; - typedef Ret function_type(Arg, Args...); - typedef Ret return_type; - using stl_function_type = std::function; - typedef Ret(*pointer)(Arg, Args...); - - typedef std::tuple tuple_type; - typedef std::tuple>, std::remove_const_t>...> bare_tuple_type; - using args_tuple = std::tuple>...>; - using args_tuple_2nd = std::tuple>...>; - }; - - template - struct function_traits { - public: - enum { arity = 0 }; - typedef Ret function_type(); - typedef Ret return_type; - using stl_function_type = std::function; - typedef Ret(*pointer)(); - - typedef std::tuple<> tuple_type; - typedef std::tuple<> bare_tuple_type; - using args_tuple = std::tuple; - using args_tuple_2nd = std::tuple; - }; - - template - struct function_traits : function_traits{}; - - template - struct function_traits> : function_traits{}; - - template - struct function_traits : function_traits{}; - - template - struct function_traits : function_traits{}; - - template - struct function_traits : function_traits{}; - - template - using remove_const_reference_t = std::remove_const_t>; - - template - auto make_tuple_from_sequence(std::index_sequence)->decltype(std::make_tuple(Is...)) { - std::make_tuple(Is...); - } - - template - constexpr auto make_tuple_from_sequence()->decltype(make_tuple_from_sequence(std::make_index_sequence{})) { - return make_tuple_from_sequence(std::make_index_sequence{}); - } - - namespace detail { - template - void tuple_switch(const std::size_t i, Tuple&& t, F&& f, std::index_sequence) { - (void)std::initializer_list { - (i == Is && ( - (void)std::forward(f)(std::integral_constant{}), 0))... - }; - } - } // namespace detail - - template - inline void tuple_switch(const std::size_t i, Tuple&& t, F&& f) { - constexpr auto N = - std::tuple_size>::value; - - detail::tuple_switch(i, std::forward(t), std::forward(f), - std::make_index_sequence{}); - } - - template - using nth_type_of = std::tuple_element_t>; - - template - using last_type_of = nth_type_of; -} - -#endif //REST_RPC_META_UTIL_HPP diff --git a/include/rest_rpc.hpp b/include/rest_rpc.hpp deleted file mode 100644 index 4a7f6ae..0000000 --- a/include/rest_rpc.hpp +++ /dev/null @@ -1,2 +0,0 @@ -#include "rest_rpc/rpc_server.h" -#include "rest_rpc/rpc_client.hpp" \ No newline at end of file diff --git a/include/router.h b/include/router.h deleted file mode 100644 index 7693159..0000000 --- a/include/router.h +++ /dev/null @@ -1,190 +0,0 @@ -#ifndef REST_RPC_ROUTER_H_ -#define REST_RPC_ROUTER_H_ - -#include -#include "use_asio.hpp" -#include "codec.h" -#include "meta_util.hpp" - -namespace rest_rpc { - enum class ExecMode { sync, async }; - const constexpr ExecMode Async = ExecMode::async; - - namespace rpc_service { - class connection; - - class router : asio::noncopyable { - public: - static router& get() { - static router instance; - return instance; - } - - template - void register_handler(std::string const& name, Function f) { - return register_nonmember_func(name, std::move(f)); - } - - template - void register_handler(std::string const& name, const Function& f, Self* self) { - return register_member_func(name, f, self); - } - - void remove_handler(std::string const& name) { this->map_invokers_.erase(name); } - - template - void route(const char* data, std::size_t size, std::weak_ptr conn) { - auto conn_sp = conn.lock(); - if (!conn_sp) { - return; - } - - auto req_id = conn_sp->request_id(); - std::string result; - try { - msgpack_codec codec; - auto p = codec.unpack>(data, size); - auto& func_name = std::get<0>(p); - auto it = map_invokers_.find(func_name); - if (it == map_invokers_.end()) { - result = codec.pack_args_str(result_code::FAIL, "unknown function: " + func_name); - conn_sp->response(req_id, std::move(result)); - return; - } - - ExecMode model; - it->second(conn, data, size, result, model); - if (model == ExecMode::sync) { - if (result.size() >= MAX_BUF_LEN) { - result = codec.pack_args_str(result_code::FAIL, "the response result is out of range: more than 10M " + func_name); - } - conn_sp->response(req_id, std::move(result)); - } - } - catch (const std::exception & ex) { - msgpack_codec codec; - result = codec.pack_args_str(result_code::FAIL, ex.what()); - conn_sp->response(req_id, std::move(result)); - } - } - - router() = default; - - private: - router(const router&) = delete; - router(router&&) = delete; - - template - static typename std::result_of, Args...)>::type call_helper( - const F & f, const std::index_sequence&, std::tuple tup, std::weak_ptr ptr) { - return f(ptr, std::move(std::get(tup))...); - } - - template - static - typename std::enable_if, Args...)>::type>::value>::type - call(const F & f, std::weak_ptr ptr, std::string & result, std::tuple tp) { - call_helper(f, std::make_index_sequence{}, std::move(tp), ptr); - result = msgpack_codec::pack_args_str(result_code::OK); - } - - template - static - typename std::enable_if, Args...)>::type>::value>::type - call(const F & f, std::weak_ptr ptr, std::string & result, std::tuple tp) { - auto r = call_helper(f, std::make_index_sequence{}, std::move(tp), ptr); - msgpack_codec codec; - result = msgpack_codec::pack_args_str(result_code::OK, r); - } - - template - static typename std::result_of, Args...)>::type call_member_helper( - const F & f, Self * self, const std::index_sequence&, - std::tuple tup, std::weak_ptr ptr = std::shared_ptr{ nullptr }) { - return (*self.*f)(ptr, std::move(std::get(tup))...); - } - - template - static typename std::enable_if< - std::is_void, Args...)>::type>::value>::type - call_member(const F & f, Self * self, std::weak_ptr ptr, std::string & result, - std::tuple tp) { - call_member_helper(f, self, typename std::make_index_sequence{}, std::move(tp), ptr); - result = msgpack_codec::pack_args_str(result_code::OK); - } - - template - static typename std::enable_if< - !std::is_void, Args...)>::type>::value>::type - call_member(const F & f, Self * self, std::weak_ptr ptr, std::string & result, - std::tuple tp) { - auto r = - call_member_helper(f, self, typename std::make_index_sequence{}, std::move(tp), ptr); - result = msgpack_codec::pack_args_str(result_code::OK, r); - } - - template - struct invoker { - template - static inline void apply(const Function& func, std::weak_ptr conn, const char* data, size_t size, - std::string& result, ExecMode& exe_model) { - using args_tuple = typename function_traits::args_tuple_2nd; - exe_model = ExecMode::sync; - msgpack_codec codec; - try { - auto tp = codec.unpack(data, size); - call(func, conn, result, std::move(tp)); - exe_model = model; - } - catch (std::invalid_argument & e) { - result = codec.pack_args_str(result_code::FAIL, e.what()); - } - catch (const std::exception & e) { - result = codec.pack_args_str(result_code::FAIL, e.what()); - } - } - - template - static inline void apply_member(const Function& func, Self* self, std::weak_ptr conn, - const char* data, size_t size, std::string& result, - ExecMode& exe_model) { - using args_tuple = typename function_traits::args_tuple_2nd; - exe_model = ExecMode::sync; - msgpack_codec codec; - try { - auto tp = codec.unpack(data, size); - call_member(func, self, conn, result, std::move(tp)); - exe_model = model; - } - catch (std::invalid_argument & e) { - result = codec.pack_args_str(result_code::FAIL, e.what()); - } - catch (const std::exception & e) { - result = codec.pack_args_str(result_code::FAIL, e.what()); - } - } - }; - - template - void register_nonmember_func(std::string const& name, Function f) { - this->map_invokers_[name] = { std::bind(&invoker::template apply, std::move(f), std::placeholders::_1, - std::placeholders::_2, std::placeholders::_3, - std::placeholders::_4, std::placeholders::_5) }; - } - - template - void register_member_func(const std::string& name, const Function& f, Self* self) { - this->map_invokers_[name] = { std::bind(&invoker::template apply_member, - f, self, std::placeholders::_1, std::placeholders::_2, - std::placeholders::_3, std::placeholders::_4, - std::placeholders::_5) }; - } - - std::unordered_map, const char*, size_t, std::string&, ExecMode& model)>> - map_invokers_; - }; - } // namespace rpc_service -} // namespace rest_rpc - -#endif // REST_RPC_ROUTER_H_ diff --git a/include/rpc_client.hpp b/include/rpc_client.hpp deleted file mode 100644 index ea71d92..0000000 --- a/include/rpc_client.hpp +++ /dev/null @@ -1,796 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include "use_asio.hpp" -#include "client_util.hpp" -#include "const_vars.h" -#include "meta_util.hpp" - -using namespace rest_rpc::rpc_service; - -namespace rest_rpc { - class req_result { - public: - req_result() = default; - req_result(string_view data) : data_(data.data(), data.length()) {} - bool success() const { - return !has_error(data_); - } - - template - T as() { - if (has_error(data_)) { - throw std::logic_error(get_error_msg(data_)); - } - - return get_result(data_); - } - - void as() { - if (has_error(data_)) { - throw std::logic_error(get_error_msg(data_)); - } - } - - private: - std::string data_; - }; - - enum class CallModel { - future, - callback - }; - const constexpr auto FUTURE = CallModel::future; - - const constexpr size_t DEFAULT_TIMEOUT = 5000; //milliseconds - - class rpc_client : private asio::noncopyable { - public: - rpc_client() : socket_(ios_), work_(ios_), - deadline_(ios_), body_(INIT_BUF_SIZE) { - thd_ = std::make_shared([this] { - ios_.run(); - }); - } - - rpc_client(const std::string& host, unsigned short port) : socket_(ios_), work_(ios_), - deadline_(ios_), host_(host), port_(port), body_(INIT_BUF_SIZE) { - thd_ = std::make_shared([this] { - ios_.run(); - }); - } - - ~rpc_client() { - close(); - stop(); - } - - void run(){ - thd_->join(); - } - - void set_connect_timeout(size_t milliseconds) { - connect_timeout_ = milliseconds; - } - - void set_reconnect_count(int reconnect_count) { - reconnect_cnt_ = reconnect_count; - } - - bool connect(size_t timeout = 3, bool is_ssl = false) { - if (has_connected_) - return true; - - assert(port_ != 0); - if (is_ssl) { - upgrade_to_ssl(); - } - async_connect(); - return wait_conn(timeout); - } - - bool connect(const std::string& host, unsigned short port, bool is_ssl = false, size_t timeout = 3) { - if (port_==0) { - host_ = host; - port_ = port; - } - - return connect(timeout, is_ssl); - } - - void async_connect(const std::string& host, unsigned short port) { - if (port_ == 0) { - host_ = host; - port_ = port; - } - - async_connect(); - } - - bool wait_conn(size_t timeout) { - if (has_connected_) { - return true; - } - - has_wait_ = true; - std::unique_lock lock(conn_mtx_); - bool result = conn_cond_.wait_for(lock, std::chrono::seconds(timeout), - [this] {return has_connected_.load(); }); - has_wait_ = false; - return has_connected_; - } - - void enable_auto_reconnect(bool enable = true) { - enable_reconnect_ = enable; - } - - void enable_auto_heartbeat(bool enable = true) { - if (enable) { - reset_deadline_timer(5); - } - else { - deadline_.cancel(); - } - } - - void update_addr(const std::string& host, unsigned short port) { - host_ = host; - port_ = port; - } - - void close(bool close_ssl = true) { - boost::system::error_code ec; - if (close_ssl) { -#ifdef CINATRA_ENABLE_SSL - if (ssl_stream_) { - ssl_stream_->shutdown(ec); - ssl_stream_ = nullptr; - } -#endif - } - - if (!has_connected_) - return; - - has_connected_ = false; - socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); - socket_.close(ec); - clear_cache(); - } - - void set_error_callback(std::function f) { - err_cb_ = std::move(f); - } - - uint64_t reqest_id() { - return temp_req_id_; - } - - bool has_connected() const { - return has_connected_; - } - - //sync call -#if __cplusplus > 201402L - template - auto call(const std::string& rpc_name, Args&& ... args) { - std::future future = async_call(rpc_name, std::forward(args)...); - auto status = future.wait_for(std::chrono::milliseconds(TIMEOUT)); - if (status == std::future_status::timeout || status == std::future_status::deferred) { - throw std::out_of_range("timeout or deferred"); - } - - if constexpr (std::is_void_v) { - future.get().as(); - } - else { - return future.get().as(); - } - } - - template - auto call(const std::string& rpc_name, Args&& ... args) { - return call(rpc_name, std::forward(args)...); - } -#else - template - typename std::enable_if::value>::type call(const std::string& rpc_name, Args&& ... args) { - std::future future = async_call(rpc_name, std::forward(args)...); - auto status = future.wait_for(std::chrono::milliseconds(TIMEOUT)); - if (status == std::future_status::timeout || status == std::future_status::deferred) { - throw std::out_of_range("timeout or deferred"); - } - - future.get().as(); - } - - template - typename std::enable_if::value>::type call(const std::string& rpc_name, Args&& ... args) { - call(rpc_name, std::forward(args)...); - } - - template - typename std::enable_if::value, T>::type call(const std::string& rpc_name, Args&& ... args) { - std::future future = async_call(rpc_name, std::forward(args)...); - auto status = future.wait_for(std::chrono::milliseconds(TIMEOUT)); - if (status == std::future_status::timeout || status == std::future_status::deferred) { - throw std::out_of_range("timeout or deferred"); - } - - return future.get().as(); - } - - template - typename std::enable_if::value, T>::type call(const std::string& rpc_name, Args&& ... args) { - return call(rpc_name, std::forward(args)...); - } -#endif - - template - std::future async_call(const std::string& rpc_name, Args&&... args) { - auto p = std::make_shared>(); - std::future future = p->get_future(); - - uint64_t fu_id = 0; - { - std::unique_lock lock(cb_mtx_); - fu_id_++; - fu_id = fu_id_; - future_map_.emplace(fu_id, std::move(p)); - } - - msgpack_codec codec; - auto ret = codec.pack_args(rpc_name, std::forward(args)...); - write(fu_id, request_type::req_res, std::move(ret)); - return future; - } - - template - void async_call(const std::string& rpc_name, std::function cb, Args&& ... args) { - if (!has_connected_) { - if(cb) - cb(boost::asio::error::make_error_code(boost::asio::error::not_connected), "not connected"); - return; - } - - uint64_t cb_id = 0; - { - std::unique_lock lock(cb_mtx_); - callback_id_++; - callback_id_ |= (uint64_t(1) << 63); - cb_id = callback_id_; - auto call = std::make_shared(ios_, std::move(cb), TIMEOUT); - call->start_timer(); - callback_map_.emplace(cb_id, call); - } - - msgpack_codec codec; - auto ret = codec.pack_args(rpc_name, std::forward(args)...); - write(cb_id, request_type::req_res, std::move(ret)); - } - - void stop() { - if (thd_ != nullptr) { - ios_.stop(); - thd_->join(); - thd_ = nullptr; - } - } - - template - void subscribe(std::string key, Func f) { - auto it = sub_map_.find(key); - if (it != sub_map_.end()) { - assert("duplicated subscribe"); - return; - } - - sub_map_.emplace(key, std::move(f)); - send_subscribe(key, ""); - key_token_set_.emplace(std::move(key), ""); - } - - template - void subscribe(std::string key, std::string token, Func f) { - auto composite_key = key + token; - auto it = sub_map_.find(composite_key); - if (it != sub_map_.end()) { - assert("duplicated subscribe"); - return; - } - - sub_map_.emplace(std::move(composite_key), std::move(f)); - send_subscribe(key, token); - key_token_set_.emplace(std::move(key), std::move(token)); - } - - template - void publish(std::string key, T&& t) { - msgpack_codec codec; - auto buf = codec.pack(std::move(t)); - call("publish", std::move(key), "", std::string(buf.data(), buf.size())); - } - - template - void publish_by_token(std::string key, std::string token, T&& t) { - msgpack_codec codec; - auto buf = codec.pack(std::move(t)); - call("publish_by_token", std::move(key), std::move(token), std::string(buf.data(), buf.size())); - } - -#ifdef CINATRA_ENABLE_SSL - void set_ssl_context_callback(std::function ssl_context_callback) { - ssl_context_callback_ = std::move(ssl_context_callback); - } -#endif - - private: - void async_connect() { - assert(port_ != 0); - auto addr = boost::asio::ip::address::from_string(host_); - socket_.async_connect({ addr, port_ }, [this](const boost::system::error_code& ec) { - if (has_connected_) { - return; - } - - if (ec) { - //std::cout << ec.message() << std::endl; - - has_connected_ = false; - - if (reconnect_cnt_ <= 0) { - return; - } - - if (reconnect_cnt_ > 0) { - reconnect_cnt_--; - } - - async_reconnect(); - } - else { - //std::cout<<"connected ok"< lock(write_mtx_); - outbox_.emplace_back(std::move(msg)); - if (outbox_.size() > 1) { - // outstanding async_write - return; - } - - write(); - } - - void write() { - auto& msg = outbox_[0]; - write_size_ = (uint32_t)msg.content.length(); - std::array write_buffers; - write_buffers[0] = boost::asio::buffer(&write_size_, sizeof(int32_t)); - write_buffers[1] = boost::asio::buffer(&msg.req_id, sizeof(uint64_t)); - write_buffers[2] = boost::asio::buffer(&msg.req_type, sizeof(request_type)); - write_buffers[3] = boost::asio::buffer((char*)msg.content.data(), write_size_); - - async_write(write_buffers, - [this](const boost::system::error_code& ec, const size_t length) { - if (ec) { - has_connected_ = false; - close(false); - error_callback(ec); - - return; - } - - std::unique_lock lock(write_mtx_); - if (outbox_.empty()) { - return; - } - - ::free((char*)outbox_.front().content.data()); - outbox_.pop_front(); - - if (!outbox_.empty()) { - // more messages to send - this->write(); - } - }); - } - - void do_read() { - async_read_head([this](const boost::system::error_code& ec, const size_t length) { - if (!socket_.is_open()) { - //LOG(INFO) << "socket already closed"; - has_connected_ = false; - return; - } - - if (!ec) { - //const uint32_t body_len = *((uint32_t*)(head_)); - //auto req_id = *((std::uint64_t*)(head_ + sizeof(int32_t))); - //auto req_type = *(request_type*)(head_ + sizeof(int32_t) + sizeof(int64_t)); - rpc_header* header = (rpc_header*)(head_); - const uint32_t body_len = header->body_len; - if (body_len > 0 && body_len < MAX_BUF_LEN) { - if (body_.size() < body_len) { body_.resize(body_len); } - read_body(header->req_id, header->req_type, body_len); - return; - } - - if (body_len == 0 || body_len > MAX_BUF_LEN) { - //LOG(INFO) << "invalid body len"; - close(); - error_callback(asio::error::make_error_code(asio::error::message_size)); - return; - } - } - else { - close(false); - error_callback(ec); - } - }); - } - - void read_body(std::uint64_t req_id, request_type req_type, size_t body_len) { - async_read(body_len, - [this, req_id, req_type, body_len](boost::system::error_code ec, std::size_t length) { - //cancel_timer(); - - if (!socket_.is_open()) { - //LOG(INFO) << "socket already closed"; - call_back(req_id, asio::error::make_error_code(asio::error::connection_aborted), {}); - return; - } - - if (!ec) { - //entier body - if (req_type == request_type::req_res) { - call_back(req_id, ec, { body_.data(), body_len }); - } - else if (req_type == request_type::sub_pub) { - callback_sub(ec, { body_.data(), body_len }); - } - else { - close(); - error_callback(asio::error::make_error_code(asio::error::invalid_argument)); - return; - } - - do_read(); - } - else { - //LOG(INFO) << ec.message(); - has_connected_ = false; - close(); - error_callback(ec); - } - }); - } - - void send_subscribe(const std::string& key, const std::string& token) { - msgpack_codec codec; - auto ret = codec.pack_args(key, token); - write(0, request_type::sub_pub, std::move(ret)); - } - - void resend_subscribe() { - if (key_token_set_.empty()) - return; - - for (auto& pair : key_token_set_) { - send_subscribe(pair.first, pair.second); - } - } - - void call_back(uint64_t req_id, const boost::system::error_code& ec, string_view data) { - temp_req_id_ = req_id; - auto cb_flag = req_id >> 63; - if (cb_flag) { - std::shared_ptr cl = nullptr; - { - std::unique_lock lock(cb_mtx_); - cl = std::move(callback_map_[req_id]); - } - - assert(cl); - if (!cl->has_timeout()) { - cl->cancel(); - cl->callback(ec, data); - } - else { - cl->callback(asio::error::make_error_code(asio::error::timed_out), {}); - } - - std::unique_lock lock(cb_mtx_); - callback_map_.erase(req_id); - } - else { - std::unique_lock lock(cb_mtx_); - auto& f = future_map_[req_id]; - if (ec) { - //LOG<set_value(req_result{ data }); - future_map_.erase(req_id); - } - } - - void callback_sub(const boost::system::error_code& ec, string_view result) { - rpc_service::msgpack_codec codec; - try { - auto tp = codec.unpack>(result.data(), result.size()); - auto code = std::get<0>(tp); - auto& key = std::get<1>(tp); - auto& data = std::get<2>(tp); - - auto it = sub_map_.find(key); - if (it == sub_map_.end()) { - return; - } - - it->second(data); - } - catch (const std::exception& /*ex*/) { - error_callback(asio::error::make_error_code(asio::error::invalid_argument)); - } - } - - void clear_cache() { - { - std::unique_lock lock(write_mtx_); - while (!outbox_.empty()) { - ::free((char*)outbox_.front().content.data()); - outbox_.pop_front(); - } - } - - { - std::unique_lock lock(cb_mtx_); - callback_map_.clear(); - future_map_.clear(); - } - } - - void reset_socket(){ - boost::system::error_code igored_ec; - socket_.close(igored_ec); - socket_ = decltype(socket_)(ios_); - if(!socket_.is_open()){ - socket_.open(boost::asio::ip::tcp::v4()); - } - } - - class call_t : asio::noncopyable, public std::enable_shared_from_this { - public: - call_t(asio::io_service& ios, std::function cb, size_t timeout) : timer_(ios), - cb_(std::move(cb)), timeout_(timeout){ - } - - void start_timer() { - if (timeout_ == 0) { - return; - } - - timer_.expires_from_now(std::chrono::milliseconds(timeout_)); - auto self = this->shared_from_this(); - timer_.async_wait([this, self](boost::system::error_code ec) { - if (ec) { - return; - } - - has_timeout_ = true; - }); - } - - void callback(boost::system::error_code ec, string_view data) { - cb_(ec, data); - } - - bool has_timeout() const { - return has_timeout_; - } - - void cancel() { - if (timeout_ == 0) { - return; - } - - boost::system::error_code ec; - timer_.cancel(ec); - } - - private: - boost::asio::steady_timer timer_; - std::function cb_; - size_t timeout_; - bool has_timeout_ = false; - }; - - void error_callback(const boost::system::error_code& ec) { - if (err_cb_) { - err_cb_(ec); - } - - if (enable_reconnect_) { - async_connect(); - } - } - - void set_default_error_cb() { - err_cb_ = [this](boost::system::error_code){ - async_connect(); - }; - } - - bool is_ssl() const { -#ifdef CINATRA_ENABLE_SSL - return ssl_stream_ != nullptr; -#else - return false; -#endif - } - - - void handshake() { -#ifdef CINATRA_ENABLE_SSL - ssl_stream_->async_handshake(boost::asio::ssl::stream_base::client, - [this](const boost::system::error_code& ec) { - if (!ec) { - has_connected_ = true; - do_read(); - resend_subscribe(); - if (has_wait_) - conn_cond_.notify_one(); - } - else { - error_callback(ec); - close(); - } - }); -#endif - } - - void upgrade_to_ssl() { -#ifdef CINATRA_ENABLE_SSL - if (ssl_stream_) - return; - - boost::asio::ssl::context ssl_context(boost::asio::ssl::context::sslv23); - ssl_context.set_default_verify_paths(); - boost::system::error_code ec; - ssl_context.set_options(boost::asio::ssl::context::default_workarounds, ec); - if (ssl_context_callback_) { - ssl_context_callback_(ssl_context); - } - ssl_stream_ = std::make_unique>(socket_, ssl_context); - //verify peer TODO -#else - assert(is_ssl());//please add definition CINATRA_ENABLE_SSL, not allowed coming in this branch -#endif - } - - template - void async_read_head(Handler handler) { - if (is_ssl()) { -#ifdef CINATRA_ENABLE_SSL - boost::asio::async_read(*ssl_stream_, boost::asio::buffer(head_, HEAD_LEN), std::move(handler)); -#endif - } - else { - boost::asio::async_read(socket_, boost::asio::buffer(head_, HEAD_LEN), std::move(handler)); - } - } - - template - void async_read(size_t size_to_read, Handler handler) { - if (is_ssl()) { -#ifdef CINATRA_ENABLE_SSL - boost::asio::async_read(*ssl_stream_, boost::asio::buffer(body_.data(), size_to_read), std::move(handler)); -#endif - } - else { - boost::asio::async_read(socket_, boost::asio::buffer(body_.data(), size_to_read), std::move(handler)); - } - } - - template - void async_write(const BufferType& buffers, Handler handler) { - if (is_ssl()) { -#ifdef CINATRA_ENABLE_SSL - boost::asio::async_write(*ssl_stream_, buffers, std::move(handler)); -#endif - } - else { - boost::asio::async_write(socket_, buffers, std::move(handler)); - } - } - - boost::asio::io_service ios_; - asio::ip::tcp::socket socket_; -#ifdef CINATRA_ENABLE_SSL - std::unique_ptr> ssl_stream_; - std::function ssl_context_callback_; -#endif - boost::asio::io_service::work work_; - std::shared_ptr thd_ = nullptr; - - std::string host_; - unsigned short port_ = 0; - size_t connect_timeout_ = 1000;//s - int reconnect_cnt_ = -1; - std::atomic_bool has_connected_ = { false }; - std::mutex conn_mtx_; - std::condition_variable conn_cond_; - bool has_wait_ = false; - - asio::steady_timer deadline_; - - struct client_message_type { - std::uint64_t req_id; - request_type req_type; - string_view content; - }; - std::deque outbox_; - uint32_t write_size_ = 0; - std::mutex write_mtx_; - uint64_t fu_id_ = 0; - std::function err_cb_; - bool enable_reconnect_ = false; - - std::unordered_map>> future_map_; - std::unordered_map> callback_map_; - std::mutex cb_mtx_; - uint64_t callback_id_ = 0; - - uint64_t temp_req_id_ = 0; - - char head_[HEAD_LEN] = {}; - std::vector body_; - - std::unordered_map> sub_map_; - std::set> key_token_set_; - }; -} diff --git a/include/rpc_server.h b/include/rpc_server.h deleted file mode 100644 index 9a9a1a2..0000000 --- a/include/rpc_server.h +++ /dev/null @@ -1,227 +0,0 @@ -#ifndef REST_RPC_RPC_SERVER_H_ -#define REST_RPC_RPC_SERVER_H_ - -#include -#include -#include -#include "connection.h" -#include "io_service_pool.h" -#include "router.h" - -using boost::asio::ip::tcp; - -namespace rest_rpc { - namespace rpc_service { - using rpc_conn = std::weak_ptr; - class rpc_server : private asio::noncopyable { - public: - rpc_server(short port, size_t size, size_t timeout_seconds = 15, size_t check_seconds = 10) - : io_service_pool_(size), - acceptor_(io_service_pool_.get_io_service(), tcp::endpoint(tcp::v4(), port)), - timeout_seconds_(timeout_seconds), - check_seconds_(check_seconds) { - do_accept(); - check_thread_ = std::make_shared([this] { clean(); }); - pub_sub_thread_ = std::make_shared([this] { clean_sub_pub(); }); - } - - rpc_server(short port, size_t size, ssl_configure ssl_conf, size_t timeout_seconds = 15, size_t check_seconds = 10) : - rpc_server(port, size, timeout_seconds, check_seconds) { -#ifdef CINATRA_ENABLE_SSL - ssl_conf_ = std::move(ssl_conf); -#else - assert(false);//please add definition CINATRA_ENABLE_SSL, not allowed coming in this branch -#endif - } - - ~rpc_server() { - { - std::unique_lock lock(mtx_); - stop_check_ = true; - cv_.notify_all(); - } - check_thread_->join(); - - { - std::unique_lock lock(sub_mtx_); - stop_check_pub_sub_ = true; - sub_cv_.notify_all(); - } - pub_sub_thread_->join(); - - io_service_pool_.stop(); - if(thd_){ - thd_->join(); - } - } - - void async_run() { - thd_ = std::make_shared([this] { io_service_pool_.run(); }); - } - - void run() { - io_service_pool_.run(); - } - - template - void register_handler(std::string const& name, const Function& f) { - router::get().register_handler(name, f); - } - - template - void register_handler(std::string const& name, const Function& f, Self* self) { - router::get().register_handler(name, f, self); - } - - void set_conn_timeout_callback(std::function callback) { - conn_timeout_callback_ = std::move(callback); - } - - template - void publish(const std::string& key, T data) { - publish(key, "", std::move(data)); - } - - template - void publish_by_token(const std::string& key, std::string token, T data) { - publish(key, std::move(token), std::move(data)); - } - - std::set get_token_list() { - std::unique_lock lock(sub_mtx_); - return token_list_; - } - - private: - void do_accept() { - conn_.reset(new connection(io_service_pool_.get_io_service(), timeout_seconds_)); - conn_->set_callback([this](std::string key, std::string token, std::weak_ptr conn) { - std::unique_lock lock(sub_mtx_); - sub_map_.emplace(std::move(key) + token, conn); - if (!token.empty()) { - token_list_.emplace(std::move(token)); - } - }); - - acceptor_.async_accept(conn_->socket(), [this](boost::system::error_code ec) { - if (ec) { - //LOG(INFO) << "acceptor error: " << ec.message(); - } - else { -#ifdef CINATRA_ENABLE_SSL - if (!ssl_conf_.cert_file.empty()) { - conn_->init_ssl_context(ssl_conf_); - } -#endif - conn_->start(); - std::unique_lock lock(mtx_); - conn_->set_conn_id(conn_id_); - connections_.emplace(conn_id_++, conn_); - } - - do_accept(); - }); - } - - void clean() { - while (!stop_check_) { - std::unique_lock lock(mtx_); - cv_.wait_for(lock, std::chrono::seconds(check_seconds_)); - - for (auto it = connections_.cbegin(); it != connections_.cend();) { - if (it->second->has_closed()) { - if (conn_timeout_callback_) { - conn_timeout_callback_(it->second->conn_id()); - } - it = connections_.erase(it); - } - else { - ++it; - } - } - } - } - - void clean_sub_pub() { - while (!stop_check_pub_sub_) { - std::unique_lock lock(sub_mtx_); - sub_cv_.wait_for(lock, std::chrono::seconds(10)); - - for (auto it = sub_map_.cbegin(); it != sub_map_.cend();) { - auto conn = it->second.lock(); - if (conn == nullptr || conn->has_closed()) { - it = sub_map_.erase(it); - } - else { - ++it; - } - } - } - } - - template - void publish(std::string key, std::string token, T data) { - decltype(sub_map_.equal_range(key)) range; - - { - std::unique_lock lock(sub_mtx_); - if (sub_map_.empty()) - return; - - range = sub_map_.equal_range(key + token); - } - - std::shared_ptr shared_data = get_shared_data(std::move(data)); - for (auto it = range.first; it != range.second; ++it) { - auto conn = it->second.lock(); - if (conn == nullptr || conn->has_closed()) { - continue; - } - - conn->publish(key + token, *shared_data); - } - } - - template - typename std::enable_if::value, std::shared_ptr>::type - get_shared_data(std::string data) { - return std::make_shared(std::move(data)); - } - - template - typename std::enable_if::value, std::shared_ptr>::type - get_shared_data(T data) { - msgpack_codec codec; - auto buf = codec.pack(std::move(data)); - return std::make_shared(buf.data(), buf.size()); - } - - io_service_pool io_service_pool_; - tcp::acceptor acceptor_; - std::shared_ptr conn_; - std::shared_ptr thd_; - std::size_t timeout_seconds_; - - std::unordered_map> connections_; - int64_t conn_id_ = 0; - std::mutex mtx_; - std::shared_ptr check_thread_; - size_t check_seconds_; - bool stop_check_ = false; - std::condition_variable cv_; - - std::function conn_timeout_callback_; - std::unordered_multimap> sub_map_; - std::set token_list_; - std::mutex sub_mtx_; - std::condition_variable sub_cv_; - - std::shared_ptr pub_sub_thread_; - bool stop_check_pub_sub_ = false; - - ssl_configure ssl_conf_; - }; - } // namespace rpc_service -} // namespace rest_rpc - -#endif // REST_RPC_RPC_SERVER_H_ \ No newline at end of file diff --git a/include/string_view.hpp b/include/string_view.hpp deleted file mode 100644 index 840599e..0000000 --- a/include/string_view.hpp +++ /dev/null @@ -1,1324 +0,0 @@ -#pragma once - -#ifndef NONSTD_SV_LITE_H_INCLUDED -#define NONSTD_SV_LITE_H_INCLUDED - -#define string_view_lite_MAJOR 1 -#define string_view_lite_MINOR 2 -#define string_view_lite_PATCH 0 - -#define string_view_lite_VERSION nssv_STRINGIFY(string_view_lite_MAJOR) "." nssv_STRINGIFY(string_view_lite_MINOR) "." nssv_STRINGIFY(string_view_lite_PATCH) - -#define nssv_STRINGIFY( x ) nssv_STRINGIFY_( x ) -#define nssv_STRINGIFY_( x ) #x - -// string-view lite configuration: - -#define nssv_STRING_VIEW_DEFAULT 0 -#define nssv_STRING_VIEW_NONSTD 1 -#define nssv_STRING_VIEW_STD 2 - -#if !defined( nssv_CONFIG_SELECT_STRING_VIEW ) -# define nssv_CONFIG_SELECT_STRING_VIEW ( nssv_HAVE_STD_STRING_VIEW ? nssv_STRING_VIEW_STD : nssv_STRING_VIEW_NONSTD ) -#endif - -#if defined( nssv_CONFIG_SELECT_STD_STRING_VIEW ) || defined( nssv_CONFIG_SELECT_NONSTD_STRING_VIEW ) -# error nssv_CONFIG_SELECT_STD_STRING_VIEW and nssv_CONFIG_SELECT_NONSTD_STRING_VIEW are deprecated and removed, please use nssv_CONFIG_SELECT_STRING_VIEW=nssv_STRING_VIEW_... -#endif - -#ifndef nssv_CONFIG_STD_SV_OPERATOR -# define nssv_CONFIG_STD_SV_OPERATOR 0 -#endif - -#ifndef nssv_CONFIG_USR_SV_OPERATOR -# define nssv_CONFIG_USR_SV_OPERATOR 1 -#endif - -#ifdef nssv_CONFIG_CONVERSION_STD_STRING -# define nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS nssv_CONFIG_CONVERSION_STD_STRING -# define nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS nssv_CONFIG_CONVERSION_STD_STRING -#endif - -#ifndef nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS -# define nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS 1 -#endif - -#ifndef nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS -# define nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS 1 -#endif - -// Control presence of exception handling (try and auto discover): - -#ifndef nssv_CONFIG_NO_EXCEPTIONS -# if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND) -# define nssv_CONFIG_NO_EXCEPTIONS 0 -# else -# define nssv_CONFIG_NO_EXCEPTIONS 1 -# endif -#endif - -// C++ language version detection (C++20 is speculative): -// Note: VC14.0/1900 (VS2015) lacks too much from C++14. - -#ifndef nssv_CPLUSPLUS -# if defined(_MSVC_LANG ) && !defined(__clang__) -# define nssv_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG ) -# else -# define nssv_CPLUSPLUS __cplusplus -# endif -#endif - -#define nssv_CPP98_OR_GREATER ( nssv_CPLUSPLUS >= 199711L ) -#define nssv_CPP11_OR_GREATER ( nssv_CPLUSPLUS >= 201103L ) -#define nssv_CPP11_OR_GREATER_ ( nssv_CPLUSPLUS >= 201103L ) -#define nssv_CPP14_OR_GREATER ( nssv_CPLUSPLUS >= 201402L ) -#define nssv_CPP17_OR_GREATER ( nssv_CPLUSPLUS >= 201703L ) -#define nssv_CPP20_OR_GREATER ( nssv_CPLUSPLUS >= 202000L ) - -// use C++17 std::string_view if available and requested: - -#if nssv_CPP17_OR_GREATER && defined(__has_include ) -# if __has_include( ) -# define nssv_HAVE_STD_STRING_VIEW 1 -# else -# define nssv_HAVE_STD_STRING_VIEW 0 -# endif -#else -# define nssv_HAVE_STD_STRING_VIEW 0 -#endif - -#define nssv_USES_STD_STRING_VIEW ( (nssv_CONFIG_SELECT_STRING_VIEW == nssv_STRING_VIEW_STD) || ((nssv_CONFIG_SELECT_STRING_VIEW == nssv_STRING_VIEW_DEFAULT) && nssv_HAVE_STD_STRING_VIEW) ) - -#define nssv_HAVE_STARTS_WITH ( nssv_CPP20_OR_GREATER || !nssv_USES_STD_STRING_VIEW ) -#define nssv_HAVE_ENDS_WITH nssv_HAVE_STARTS_WITH - -// -// Use C++17 std::string_view: -// - -#if nssv_USES_STD_STRING_VIEW - -#include - -// Extensions for std::string: - -#if nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS - -namespace nonstd { - - template< class CharT, class Traits, class Allocator = std::allocator > - std::basic_string - to_string(std::basic_string_view v, Allocator const& a = Allocator()) { - return std::basic_string(v.begin(), v.end(), a); - } - - template< class CharT, class Traits, class Allocator > - std::basic_string_view - to_string_view(std::basic_string const& s) { - return std::basic_string_view(s.data(), s.size()); - } - - // Literal operators sv and _sv: - -#if nssv_CONFIG_STD_SV_OPERATOR - - using namespace std::literals::string_view_literals; - -#endif - -#if nssv_CONFIG_USR_SV_OPERATOR - - inline namespace literals { - inline namespace string_view_literals { - - - constexpr std::string_view operator "" _sv(const char* str, size_t len) noexcept // (1) - { - return std::string_view{ str, len }; - } - - constexpr std::u16string_view operator "" _sv(const char16_t* str, size_t len) noexcept // (2) - { - return std::u16string_view{ str, len }; - } - - constexpr std::u32string_view operator "" _sv(const char32_t* str, size_t len) noexcept // (3) - { - return std::u32string_view{ str, len }; - } - - constexpr std::wstring_view operator "" _sv(const wchar_t* str, size_t len) noexcept // (4) - { - return std::wstring_view{ str, len }; - } - - } - } // namespace literals::string_view_literals - -#endif // nssv_CONFIG_USR_SV_OPERATOR - -} // namespace nonstd - -#endif // nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS - -namespace nonstd { - - using std::string_view; - using std::wstring_view; - using std::u16string_view; - using std::u32string_view; - using std::basic_string_view; - - // literal "sv" and "_sv", see above - - using std::operator==; - using std::operator!=; - using std::operator<; - using std::operator<=; - using std::operator>; - using std::operator>=; - - using std::operator<<; - -} // namespace nonstd - -#else // nssv_HAVE_STD_STRING_VIEW - -// -// Before C++17: use string_view lite: -// - -// Compiler versions: -// -// MSVC++ 6.0 _MSC_VER == 1200 (Visual Studio 6.0) -// MSVC++ 7.0 _MSC_VER == 1300 (Visual Studio .NET 2002) -// MSVC++ 7.1 _MSC_VER == 1310 (Visual Studio .NET 2003) -// MSVC++ 8.0 _MSC_VER == 1400 (Visual Studio 2005) -// MSVC++ 9.0 _MSC_VER == 1500 (Visual Studio 2008) -// MSVC++ 10.0 _MSC_VER == 1600 (Visual Studio 2010) -// MSVC++ 11.0 _MSC_VER == 1700 (Visual Studio 2012) -// MSVC++ 12.0 _MSC_VER == 1800 (Visual Studio 2013) -// MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015) -// MSVC++ 14.1 _MSC_VER >= 1910 (Visual Studio 2017) - -#if defined(_MSC_VER ) && !defined(__clang__) -# define nssv_COMPILER_MSVC_VER (_MSC_VER ) -# define nssv_COMPILER_MSVC_VERSION (_MSC_VER / 10 - 10 * ( 5 + (_MSC_VER < 1900 ) ) ) -#else -# define nssv_COMPILER_MSVC_VER 0 -# define nssv_COMPILER_MSVC_VERSION 0 -#endif - -#define nssv_COMPILER_VERSION( major, minor, patch ) ( 10 * ( 10 * (major) + (minor) ) + (patch) ) - -#if defined(__clang__) -# define nssv_COMPILER_CLANG_VERSION nssv_COMPILER_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__) -#else -# define nssv_COMPILER_CLANG_VERSION 0 -#endif - -#if defined(__GNUC__) && !defined(__clang__) -# define nssv_COMPILER_GNUC_VERSION nssv_COMPILER_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) -#else -# define nssv_COMPILER_GNUC_VERSION 0 -#endif - -// half-open range [lo..hi): -#define nssv_BETWEEN( v, lo, hi ) ( (lo) <= (v) && (v) < (hi) ) - -// Presence of language and library features: - -#ifdef _HAS_CPP0X -# define nssv_HAS_CPP0X _HAS_CPP0X -#else -# define nssv_HAS_CPP0X 0 -#endif - -// Unless defined otherwise below, consider VC14 as C++11 for variant-lite: - -#if nssv_COMPILER_MSVC_VER >= 1900 -# undef nssv_CPP11_OR_GREATER -# define nssv_CPP11_OR_GREATER 1 -#endif - -#define nssv_CPP11_90 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1500) -#define nssv_CPP11_100 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1600) -#define nssv_CPP11_110 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1700) -#define nssv_CPP11_120 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1800) -#define nssv_CPP11_140 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1900) -#define nssv_CPP11_141 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1910) - -#define nssv_CPP14_000 (nssv_CPP14_OR_GREATER) -#define nssv_CPP17_000 (nssv_CPP17_OR_GREATER) - -// Presence of C++11 language features: - -#define nssv_HAVE_CONSTEXPR_11 nssv_CPP11_140 -#define nssv_HAVE_EXPLICIT_CONVERSION nssv_CPP11_140 -#define nssv_HAVE_INLINE_NAMESPACE nssv_CPP11_140 -#define nssv_HAVE_NOEXCEPT nssv_CPP11_140 -#define nssv_HAVE_NULLPTR nssv_CPP11_100 -#define nssv_HAVE_REF_QUALIFIER nssv_CPP11_140 -#define nssv_HAVE_UNICODE_LITERALS nssv_CPP11_140 -#define nssv_HAVE_USER_DEFINED_LITERALS nssv_CPP11_140 -#define nssv_HAVE_WCHAR16_T nssv_CPP11_100 -#define nssv_HAVE_WCHAR32_T nssv_CPP11_100 - -#if ! ( ( nssv_CPP11_OR_GREATER && nssv_COMPILER_CLANG_VERSION ) || nssv_BETWEEN( nssv_COMPILER_CLANG_VERSION, 300, 400 ) ) -# define nssv_HAVE_STD_DEFINED_LITERALS nssv_CPP11_140 -#else -# define nssv_HAVE_STD_DEFINED_LITERALS 0 -#endif - -// Presence of C++14 language features: - -#define nssv_HAVE_CONSTEXPR_14 nssv_CPP14_000 - -// Presence of C++17 language features: - -#define nssv_HAVE_NODISCARD nssv_CPP17_000 - -// Presence of C++ library features: - -#define nssv_HAVE_STD_HASH nssv_CPP11_120 - -// C++ feature usage: - -#if nssv_HAVE_CONSTEXPR_11 -# define nssv_constexpr constexpr -#else -# define nssv_constexpr /*constexpr*/ -#endif - -#if nssv_HAVE_CONSTEXPR_14 -# define nssv_constexpr14 constexpr -#else -# define nssv_constexpr14 /*constexpr*/ -#endif - -#if nssv_HAVE_EXPLICIT_CONVERSION -# define nssv_explicit explicit -#else -# define nssv_explicit /*explicit*/ -#endif - -#if nssv_HAVE_INLINE_NAMESPACE -# define nssv_inline_ns inline -#else -# define nssv_inline_ns /*inline*/ -#endif - -#if nssv_HAVE_NOEXCEPT -# define nssv_noexcept noexcept -#else -# define nssv_noexcept /*noexcept*/ -#endif - -//#if nssv_HAVE_REF_QUALIFIER -//# define nssv_ref_qual & -//# define nssv_refref_qual && -//#else -//# define nssv_ref_qual /*&*/ -//# define nssv_refref_qual /*&&*/ -//#endif - -#if nssv_HAVE_NULLPTR -# define nssv_nullptr nullptr -#else -# define nssv_nullptr NULL -#endif - -#if nssv_HAVE_NODISCARD -# define nssv_nodiscard [[nodiscard]] -#else -# define nssv_nodiscard /*[[nodiscard]]*/ -#endif - -// Additional includes: - -#include -#include -#include -#include -#include -#include // std::char_traits<> - -#if ! nssv_CONFIG_NO_EXCEPTIONS -# include -#endif - -#if nssv_CPP11_OR_GREATER -# include -#endif - -// Clang, GNUC, MSVC warning suppression macros: - -#if defined(__clang__) -# pragma clang diagnostic ignored "-Wreserved-user-defined-literal" -# pragma clang diagnostic push -# pragma clang diagnostic ignored "-Wuser-defined-literals" -#elif defined(__GNUC__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wliteral-suffix" -#endif // __clang__ - -#if nssv_COMPILER_MSVC_VERSION >= 140 -# define nssv_SUPPRESS_MSGSL_WARNING(expr) [[gsl::suppress(expr)]] -# define nssv_SUPPRESS_MSVC_WARNING(code, descr) __pragma(warning(suppress: code) ) -# define nssv_DISABLE_MSVC_WARNINGS(codes) __pragma(warning(push)) __pragma(warning(disable: codes)) -#else -# define nssv_SUPPRESS_MSGSL_WARNING(expr) -# define nssv_SUPPRESS_MSVC_WARNING(code, descr) -# define nssv_DISABLE_MSVC_WARNINGS(codes) -#endif - -#if defined(__clang__) -# define nssv_RESTORE_WARNINGS() _Pragma("clang diagnostic pop") -#elif defined(__GNUC__) -# define nssv_RESTORE_WARNINGS() _Pragma("GCC diagnostic pop") -#elif nssv_COMPILER_MSVC_VERSION >= 140 -# define nssv_RESTORE_WARNINGS() __pragma(warning(pop )) -#else -# define nssv_RESTORE_WARNINGS() -#endif - -// Suppress the following MSVC (GSL) warnings: -// - C4455, non-gsl : 'operator ""sv': literal suffix identifiers that do not -// start with an underscore are reserved -// - C26472, gsl::t.1 : don't use a static_cast for arithmetic conversions; -// use brace initialization, gsl::narrow_cast or gsl::narow -// - C26481: gsl::b.1 : don't use pointer arithmetic. Use span instead - -nssv_DISABLE_MSVC_WARNINGS(4455 26481 26472) -//nssv_DISABLE_CLANG_WARNINGS( "-Wuser-defined-literals" ) -//nssv_DISABLE_GNUC_WARNINGS( -Wliteral-suffix ) - -namespace nonstd { - namespace sv_lite { - -#if nssv_CPP11_OR_GREATER - - namespace detail { - - // Expect tail call optimization to make length() non-recursive: - - template< typename CharT > - inline constexpr std::size_t length(CharT* s, std::size_t result = 0) { - return *s == '\0' ? result : length(s + 1, result + 1); - } - - } // namespace detail - -#endif // nssv_CPP11_OR_GREATER - - template - < - class CharT, - class Traits = std::char_traits - > - class basic_string_view; - - // - // basic_string_view: - // - - template - < - class CharT, - class Traits /* = std::char_traits */ - > - class basic_string_view { - public: - // Member types: - - typedef Traits traits_type; - typedef CharT value_type; - - typedef CharT* pointer; - typedef CharT const* const_pointer; - typedef CharT& reference; - typedef CharT const& const_reference; - - typedef const_pointer iterator; - typedef const_pointer const_iterator; - typedef std::reverse_iterator< const_iterator > reverse_iterator; - typedef std::reverse_iterator< const_iterator > const_reverse_iterator; - - typedef std::size_t size_type; - typedef std::ptrdiff_t difference_type; - - // 24.4.2.1 Construction and assignment: - - nssv_constexpr basic_string_view() nssv_noexcept - : data_(nssv_nullptr) - , size_(0) { - } - -#if nssv_CPP11_OR_GREATER - nssv_constexpr basic_string_view(basic_string_view const& other) nssv_noexcept = default; -#else - nssv_constexpr basic_string_view(basic_string_view const& other) nssv_noexcept - : data_(other.data_) - , size_(other.size_) { - } -#endif - - nssv_constexpr basic_string_view(CharT const* s, size_type count) nssv_noexcept // non-standard noexcept - : data_(s) - , size_(count) { - } - - nssv_constexpr basic_string_view(CharT const* s) nssv_noexcept // non-standard noexcept - : data_(s) -#if nssv_CPP17_OR_GREATER - , size_(Traits::length(s)) -#elif nssv_CPP11_OR_GREATER - , size_(detail::length(s)) -#else - , size_(Traits::length(s)) -#endif - { - } - - // Assignment: - -#if nssv_CPP11_OR_GREATER - nssv_constexpr14 basic_string_view& operator=(basic_string_view const& other) nssv_noexcept = default; -#else - nssv_constexpr14 basic_string_view& operator=(basic_string_view const& other) nssv_noexcept { - data_ = other.data_; - size_ = other.size_; - return *this; - } -#endif - - // 24.4.2.2 Iterator support: - - nssv_constexpr const_iterator begin() const nssv_noexcept { return data_; } - nssv_constexpr const_iterator end() const nssv_noexcept { return data_ + size_; } - - nssv_constexpr const_iterator cbegin() const nssv_noexcept { return begin(); } - nssv_constexpr const_iterator cend() const nssv_noexcept { return end(); } - - nssv_constexpr const_reverse_iterator rbegin() const nssv_noexcept { return const_reverse_iterator(end()); } - nssv_constexpr const_reverse_iterator rend() const nssv_noexcept { return const_reverse_iterator(begin()); } - - nssv_constexpr const_reverse_iterator crbegin() const nssv_noexcept { return rbegin(); } - nssv_constexpr const_reverse_iterator crend() const nssv_noexcept { return rend(); } - - // 24.4.2.3 Capacity: - - nssv_constexpr size_type size() const nssv_noexcept { return size_; } - nssv_constexpr size_type length() const nssv_noexcept { return size_; } - nssv_constexpr size_type max_size() const nssv_noexcept { return (std::numeric_limits< size_type >::max)(); } - - // since C++20 - nssv_nodiscard nssv_constexpr bool empty() const nssv_noexcept { - return 0 == size_; - } - - // 24.4.2.4 Element access: - - nssv_constexpr const_reference operator[](size_type pos) const { - return data_at(pos); - } - - nssv_constexpr14 const_reference at(size_type pos) const { -#if nssv_CONFIG_NO_EXCEPTIONS - assert(pos < size()); -#else - if (pos >= size()) { - throw std::out_of_range("nonstd::string_view::at()"); - } -#endif - return data_at(pos); - } - - nssv_constexpr const_reference front() const { return data_at(0); } - nssv_constexpr const_reference back() const { return data_at(size() - 1); } - - nssv_constexpr const_pointer data() const nssv_noexcept { return data_; } - - // 24.4.2.5 Modifiers: - - nssv_constexpr14 void remove_prefix(size_type n) { - assert(n <= size()); - data_ += n; - size_ -= n; - } - - nssv_constexpr14 void remove_suffix(size_type n) { - assert(n <= size()); - size_ -= n; - } - - nssv_constexpr14 void swap(basic_string_view & other) nssv_noexcept { - using std::swap; - swap(data_, other.data_); - swap(size_, other.size_); - } - - // 24.4.2.6 String operations: - - size_type copy(CharT * dest, size_type n, size_type pos = 0) const { -#if nssv_CONFIG_NO_EXCEPTIONS - assert(pos <= size()); -#else - if (pos > size()) { - throw std::out_of_range("nonstd::string_view::copy()"); - } -#endif - const size_type rlen = (std::min)(n, size() - pos); - - (void)Traits::copy(dest, data() + pos, rlen); - - return rlen; - } - - nssv_constexpr14 basic_string_view substr(size_type pos = 0, size_type n = npos) const { -#if nssv_CONFIG_NO_EXCEPTIONS - assert(pos <= size()); -#else - if (pos > size()) { - throw std::out_of_range("nonstd::string_view::substr()"); - } -#endif - return basic_string_view(data() + pos, (std::min)(n, size() - pos)); - } - - // compare(), 6x: - - nssv_constexpr14 int compare(basic_string_view other) const nssv_noexcept // (1) - { - if (const int result = Traits::compare(data(), other.data(), (std::min)(size(), other.size()))) { - return result; - } - - return size() == other.size() ? 0 : size() < other.size() ? -1 : 1; - } - - nssv_constexpr int compare(size_type pos1, size_type n1, basic_string_view other) const // (2) - { - return substr(pos1, n1).compare(other); - } - - nssv_constexpr int compare(size_type pos1, size_type n1, basic_string_view other, size_type pos2, size_type n2) const // (3) - { - return substr(pos1, n1).compare(other.substr(pos2, n2)); - } - - nssv_constexpr int compare(CharT const* s) const // (4) - { - return compare(basic_string_view(s)); - } - - nssv_constexpr int compare(size_type pos1, size_type n1, CharT const* s) const // (5) - { - return substr(pos1, n1).compare(basic_string_view(s)); - } - - nssv_constexpr int compare(size_type pos1, size_type n1, CharT const* s, size_type n2) const // (6) - { - return substr(pos1, n1).compare(basic_string_view(s, n2)); - } - - // 24.4.2.7 Searching: - - // starts_with(), 3x, since C++20: - - nssv_constexpr bool starts_with(basic_string_view v) const nssv_noexcept // (1) - { - return size() >= v.size() && compare(0, v.size(), v) == 0; - } - - nssv_constexpr bool starts_with(CharT c) const nssv_noexcept // (2) - { - return starts_with(basic_string_view(&c, 1)); - } - - nssv_constexpr bool starts_with(CharT const* s) const // (3) - { - return starts_with(basic_string_view(s)); - } - - // ends_with(), 3x, since C++20: - - nssv_constexpr bool ends_with(basic_string_view v) const nssv_noexcept // (1) - { - return size() >= v.size() && compare(size() - v.size(), npos, v) == 0; - } - - nssv_constexpr bool ends_with(CharT c) const nssv_noexcept // (2) - { - return ends_with(basic_string_view(&c, 1)); - } - - nssv_constexpr bool ends_with(CharT const* s) const // (3) - { - return ends_with(basic_string_view(s)); - } - - // find(), 4x: - - nssv_constexpr14 size_type find(basic_string_view v, size_type pos = 0) const nssv_noexcept // (1) - { - return assert(v.size() == 0 || v.data() != nssv_nullptr) - , pos >= size() - ? npos - : to_pos(std::search(cbegin() + pos, cend(), v.cbegin(), v.cend(), Traits::eq)); - } - - nssv_constexpr14 size_type find(CharT c, size_type pos = 0) const nssv_noexcept // (2) - { - return find(basic_string_view(&c, 1), pos); - } - - nssv_constexpr14 size_type find(CharT const* s, size_type pos, size_type n) const // (3) - { - return find(basic_string_view(s, n), pos); - } - - nssv_constexpr14 size_type find(CharT const* s, size_type pos = 0) const // (4) - { - return find(basic_string_view(s), pos); - } - - // rfind(), 4x: - - nssv_constexpr14 size_type rfind(basic_string_view v, size_type pos = npos) const nssv_noexcept // (1) - { - if (size() < v.size()) { - return npos; - } - - if (v.empty()) { - return (std::min)(size(), pos); - } - - const_iterator last = cbegin() + (std::min)(size() - v.size(), pos) + v.size(); - const_iterator result = std::find_end(cbegin(), last, v.cbegin(), v.cend(), Traits::eq); - - return result != last ? size_type(result - cbegin()) : npos; - } - - nssv_constexpr14 size_type rfind(CharT c, size_type pos = npos) const nssv_noexcept // (2) - { - return rfind(basic_string_view(&c, 1), pos); - } - - nssv_constexpr14 size_type rfind(CharT const* s, size_type pos, size_type n) const // (3) - { - return rfind(basic_string_view(s, n), pos); - } - - nssv_constexpr14 size_type rfind(CharT const* s, size_type pos = npos) const // (4) - { - return rfind(basic_string_view(s), pos); - } - - // find_first_of(), 4x: - - nssv_constexpr size_type find_first_of(basic_string_view v, size_type pos = 0) const nssv_noexcept // (1) - { - return pos >= size() - ? npos - : to_pos(std::find_first_of(cbegin() + pos, cend(), v.cbegin(), v.cend(), Traits::eq)); - } - - nssv_constexpr size_type find_first_of(CharT c, size_type pos = 0) const nssv_noexcept // (2) - { - return find_first_of(basic_string_view(&c, 1), pos); - } - - nssv_constexpr size_type find_first_of(CharT const* s, size_type pos, size_type n) const // (3) - { - return find_first_of(basic_string_view(s, n), pos); - } - - nssv_constexpr size_type find_first_of(CharT const* s, size_type pos = 0) const // (4) - { - return find_first_of(basic_string_view(s), pos); - } - - // find_last_of(), 4x: - - nssv_constexpr size_type find_last_of(basic_string_view v, size_type pos = npos) const nssv_noexcept // (1) - { - return empty() - ? npos - : pos >= size() - ? find_last_of(v, size() - 1) - : to_pos(std::find_first_of(const_reverse_iterator(cbegin() + pos + 1), crend(), v.cbegin(), v.cend(), Traits::eq)); - } - - nssv_constexpr size_type find_last_of(CharT c, size_type pos = npos) const nssv_noexcept // (2) - { - return find_last_of(basic_string_view(&c, 1), pos); - } - - nssv_constexpr size_type find_last_of(CharT const* s, size_type pos, size_type count) const // (3) - { - return find_last_of(basic_string_view(s, count), pos); - } - - nssv_constexpr size_type find_last_of(CharT const* s, size_type pos = npos) const // (4) - { - return find_last_of(basic_string_view(s), pos); - } - - // find_first_not_of(), 4x: - - nssv_constexpr size_type find_first_not_of(basic_string_view v, size_type pos = 0) const nssv_noexcept // (1) - { - return pos >= size() - ? npos - : to_pos(std::find_if(cbegin() + pos, cend(), not_in_view(v))); - } - - nssv_constexpr size_type find_first_not_of(CharT c, size_type pos = 0) const nssv_noexcept // (2) - { - return find_first_not_of(basic_string_view(&c, 1), pos); - } - - nssv_constexpr size_type find_first_not_of(CharT const* s, size_type pos, size_type count) const // (3) - { - return find_first_not_of(basic_string_view(s, count), pos); - } - - nssv_constexpr size_type find_first_not_of(CharT const* s, size_type pos = 0) const // (4) - { - return find_first_not_of(basic_string_view(s), pos); - } - - // find_last_not_of(), 4x: - - nssv_constexpr size_type find_last_not_of(basic_string_view v, size_type pos = npos) const nssv_noexcept // (1) - { - return empty() - ? npos - : pos >= size() - ? find_last_not_of(v, size() - 1) - : to_pos(std::find_if(const_reverse_iterator(cbegin() + pos + 1), crend(), not_in_view(v))); - } - - nssv_constexpr size_type find_last_not_of(CharT c, size_type pos = npos) const nssv_noexcept // (2) - { - return find_last_not_of(basic_string_view(&c, 1), pos); - } - - nssv_constexpr size_type find_last_not_of(CharT const* s, size_type pos, size_type count) const // (3) - { - return find_last_not_of(basic_string_view(s, count), pos); - } - - nssv_constexpr size_type find_last_not_of(CharT const* s, size_type pos = npos) const // (4) - { - return find_last_not_of(basic_string_view(s), pos); - } - - // Constants: - -#if nssv_CPP17_OR_GREATER - static nssv_constexpr size_type npos = size_type(-1); -#elif nssv_CPP11_OR_GREATER - enum : size_type { npos = size_type(-1) }; -#else - enum { npos = size_type(-1) }; -#endif - - private: - struct not_in_view { - const basic_string_view v; - - nssv_constexpr explicit not_in_view(basic_string_view v) : v(v) {} - - nssv_constexpr bool operator()(CharT c) const { - return npos == v.find_first_of(c); - } - }; - - nssv_constexpr size_type to_pos(const_iterator it) const { - return it == cend() ? npos : size_type(it - cbegin()); - } - - nssv_constexpr size_type to_pos(const_reverse_iterator it) const { - return it == crend() ? npos : size_type(crend() - it - 1); - } - - nssv_constexpr const_reference data_at(size_type pos) const { -#if nssv_BETWEEN( nssv_COMPILER_GNUC_VERSION, 1, 500 ) - return data_[pos]; -#else - return assert(pos < size()), data_[pos]; -#endif - } - - private: - const_pointer data_; - size_type size_; - - public: -#if nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS - - template< class Allocator > - basic_string_view(std::basic_string const& s) nssv_noexcept - : data_(s.data()) - , size_(s.size()) { - } - -#if nssv_HAVE_EXPLICIT_CONVERSION - - template< class Allocator > - explicit operator std::basic_string() const { - return to_string(Allocator()); - } - -#endif // nssv_HAVE_EXPLICIT_CONVERSION - -#if nssv_CPP11_OR_GREATER - - template< class Allocator = std::allocator > - std::basic_string - to_string(Allocator const& a = Allocator()) const { - return std::basic_string(begin(), end(), a); - } - -#else - - std::basic_string - to_string() const { - return std::basic_string(begin(), end()); - } - - template< class Allocator > - std::basic_string - to_string(Allocator const& a) const { - return std::basic_string(begin(), end(), a); - } - -#endif // nssv_CPP11_OR_GREATER - -#endif // nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS - }; - - // - // Non-member functions: - // - - // 24.4.3 Non-member comparison functions: - // lexicographically compare two string views (function template): - - template< class CharT, class Traits > - nssv_constexpr bool operator== ( - basic_string_view lhs, - basic_string_view rhs) nssv_noexcept { - return lhs.compare(rhs) == 0; - } - - template< class CharT, class Traits > - nssv_constexpr bool operator!= ( - basic_string_view lhs, - basic_string_view rhs) nssv_noexcept { - return lhs.compare(rhs) != 0; - } - - template< class CharT, class Traits > - nssv_constexpr bool operator< ( - basic_string_view lhs, - basic_string_view rhs) nssv_noexcept { - return lhs.compare(rhs) < 0; - } - - template< class CharT, class Traits > - nssv_constexpr bool operator<= ( - basic_string_view lhs, - basic_string_view rhs) nssv_noexcept { - return lhs.compare(rhs) <= 0; - } - - template< class CharT, class Traits > - nssv_constexpr bool operator> ( - basic_string_view lhs, - basic_string_view rhs) nssv_noexcept { - return lhs.compare(rhs) > 0; - } - - template< class CharT, class Traits > - nssv_constexpr bool operator>= ( - basic_string_view lhs, - basic_string_view rhs) nssv_noexcept { - return lhs.compare(rhs) >= 0; - } - - // Let S be basic_string_view, and sv be an instance of S. - // Implementations shall provide sufficient additional overloads marked - // constexpr and noexcept so that an object t with an implicit conversion - // to S can be compared according to Table 67. - -#if nssv_CPP11_OR_GREATER && ! nssv_BETWEEN( nssv_COMPILER_MSVC_VERSION, 100, 141 ) - -#define nssv_BASIC_STRING_VIEW_I(T,U) typename std::decay< basic_string_view >::type - -#if nssv_BETWEEN( nssv_COMPILER_MSVC_VERSION, 140, 150 ) -# define nssv_MSVC_ORDER(x) , int=x -#else -# define nssv_MSVC_ORDER(x) /*, int=x*/ -#endif - -// == - - template< class CharT, class Traits nssv_MSVC_ORDER(1) > - nssv_constexpr bool operator==( - basic_string_view lhs, - nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept { - return lhs.compare(rhs) == 0; - } - - template< class CharT, class Traits nssv_MSVC_ORDER(2) > - nssv_constexpr bool operator==( - nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs, - basic_string_view rhs) nssv_noexcept { - return lhs.size() == rhs.size() && lhs.compare(rhs) == 0; - } - - // != - - template< class CharT, class Traits nssv_MSVC_ORDER(1) > - nssv_constexpr bool operator!= ( - basic_string_view < CharT, Traits > lhs, - nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept { - return lhs.size() != rhs.size() || lhs.compare(rhs) != 0; - } - - template< class CharT, class Traits nssv_MSVC_ORDER(2) > - nssv_constexpr bool operator!= ( - nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs, - basic_string_view < CharT, Traits > rhs) nssv_noexcept { - return lhs.compare(rhs) != 0; - } - - // < - - template< class CharT, class Traits nssv_MSVC_ORDER(1) > - nssv_constexpr bool operator< ( - basic_string_view < CharT, Traits > lhs, - nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept { - return lhs.compare(rhs) < 0; - } - - template< class CharT, class Traits nssv_MSVC_ORDER(2) > - nssv_constexpr bool operator< ( - nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs, - basic_string_view < CharT, Traits > rhs) nssv_noexcept { - return lhs.compare(rhs) < 0; - } - - // <= - - template< class CharT, class Traits nssv_MSVC_ORDER(1) > - nssv_constexpr bool operator<= ( - basic_string_view < CharT, Traits > lhs, - nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept { - return lhs.compare(rhs) <= 0; - } - - template< class CharT, class Traits nssv_MSVC_ORDER(2) > - nssv_constexpr bool operator<= ( - nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs, - basic_string_view < CharT, Traits > rhs) nssv_noexcept { - return lhs.compare(rhs) <= 0; - } - - // > - - template< class CharT, class Traits nssv_MSVC_ORDER(1) > - nssv_constexpr bool operator> ( - basic_string_view < CharT, Traits > lhs, - nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept { - return lhs.compare(rhs) > 0; - } - - template< class CharT, class Traits nssv_MSVC_ORDER(2) > - nssv_constexpr bool operator> ( - nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs, - basic_string_view < CharT, Traits > rhs) nssv_noexcept { - return lhs.compare(rhs) > 0; - } - - // >= - - template< class CharT, class Traits nssv_MSVC_ORDER(1) > - nssv_constexpr bool operator>= ( - basic_string_view < CharT, Traits > lhs, - nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept { - return lhs.compare(rhs) >= 0; - } - - template< class CharT, class Traits nssv_MSVC_ORDER(2) > - nssv_constexpr bool operator>= ( - nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs, - basic_string_view < CharT, Traits > rhs) nssv_noexcept { - return lhs.compare(rhs) >= 0; - } - -#undef nssv_MSVC_ORDER -#undef nssv_BASIC_STRING_VIEW_I - -#endif // nssv_CPP11_OR_GREATER - - // 24.4.4 Inserters and extractors: - - namespace detail { - - template< class Stream > - void write_padding(Stream& os, std::streamsize n) { - for (std::streamsize i = 0; i < n; ++i) - os.rdbuf()->sputc(os.fill()); - } - - template< class Stream, class View > - Stream& write_to_stream(Stream & os, View const& sv) { - typename Stream::sentry sentry(os); - - if (!os) - return os; - - const std::streamsize length = static_cast(sv.length()); - - // Whether, and how, to pad: - const bool pad = (length < os.width()); - const bool left_pad = pad && (os.flags() & std::ios_base::adjustfield) == std::ios_base::right; - - if (left_pad) - write_padding(os, os.width() - length); - - // Write span characters: - os.rdbuf()->sputn(sv.begin(), length); - - if (pad && !left_pad) - write_padding(os, os.width() - length); - - // Reset output stream width: - os.width(0); - - return os; - } - - } // namespace detail - - template< class CharT, class Traits > - std::basic_ostream& - operator<<( - std::basic_ostream & os, - basic_string_view sv) { - return detail::write_to_stream(os, sv); - } - - // Several typedefs for common character types are provided: - - typedef basic_string_view string_view; - typedef basic_string_view wstring_view; -#if nssv_HAVE_WCHAR16_T - typedef basic_string_view u16string_view; - typedef basic_string_view u32string_view; -#endif - - } -} // namespace nonstd::sv_lite - -// -// 24.4.6 Suffix for basic_string_view literals: -// - -#if nssv_HAVE_USER_DEFINED_LITERALS - -namespace nonstd { - nssv_inline_ns namespace literals { - nssv_inline_ns namespace string_view_literals { - -#if nssv_CONFIG_STD_SV_OPERATOR && nssv_HAVE_STD_DEFINED_LITERALS - - nssv_constexpr nonstd::sv_lite::string_view operator "" sv(const char* str, size_t len) nssv_noexcept // (1) - { - return nonstd::sv_lite::string_view{ str, len }; - } - - nssv_constexpr nonstd::sv_lite::u16string_view operator "" sv(const char16_t* str, size_t len) nssv_noexcept // (2) - { - return nonstd::sv_lite::u16string_view{ str, len }; - } - - nssv_constexpr nonstd::sv_lite::u32string_view operator "" sv(const char32_t* str, size_t len) nssv_noexcept // (3) - { - return nonstd::sv_lite::u32string_view{ str, len }; - } - - nssv_constexpr nonstd::sv_lite::wstring_view operator "" sv(const wchar_t* str, size_t len) nssv_noexcept // (4) - { - return nonstd::sv_lite::wstring_view{ str, len }; - } - -#endif // nssv_CONFIG_STD_SV_OPERATOR && nssv_HAVE_STD_DEFINED_LITERALS - -#if nssv_CONFIG_USR_SV_OPERATOR - - nssv_constexpr nonstd::sv_lite::string_view operator "" _sv(const char* str, size_t len) nssv_noexcept // (1) - { - return nonstd::sv_lite::string_view{ str, len }; - } - - nssv_constexpr nonstd::sv_lite::u16string_view operator "" _sv(const char16_t* str, size_t len) nssv_noexcept // (2) - { - return nonstd::sv_lite::u16string_view{ str, len }; - } - - nssv_constexpr nonstd::sv_lite::u32string_view operator "" _sv(const char32_t* str, size_t len) nssv_noexcept // (3) - { - return nonstd::sv_lite::u32string_view{ str, len }; - } - - nssv_constexpr nonstd::sv_lite::wstring_view operator "" _sv(const wchar_t* str, size_t len) nssv_noexcept // (4) - { - return nonstd::sv_lite::wstring_view{ str, len }; - } - -#endif // nssv_CONFIG_USR_SV_OPERATOR - - } - } -} // namespace nonstd::literals::string_view_literals - -#endif - -// -// Extensions for std::string: -// - -#if nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS - -namespace nonstd { - namespace sv_lite { - - // Exclude MSVC 14 (19.00): it yields ambiguous to_string(): - -#if nssv_CPP11_OR_GREATER && nssv_COMPILER_MSVC_VERSION != 140 - - template< class CharT, class Traits, class Allocator = std::allocator > - std::basic_string - to_string(basic_string_view v, Allocator const& a = Allocator()) { - return std::basic_string(v.begin(), v.end(), a); - } - -#else - - template< class CharT, class Traits > - std::basic_string - to_string(basic_string_view v) { - return std::basic_string(v.begin(), v.end()); - } - - template< class CharT, class Traits, class Allocator > - std::basic_string - to_string(basic_string_view v, Allocator const& a) { - return std::basic_string(v.begin(), v.end(), a); - } - -#endif // nssv_CPP11_OR_GREATER - - template< class CharT, class Traits, class Allocator > - basic_string_view - to_string_view(std::basic_string const& s) { - return basic_string_view(s.data(), s.size()); - } - - } -} // namespace nonstd::sv_lite - -#endif // nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS - -// -// make types and algorithms available in namespace nonstd: -// - -namespace nonstd { - - using sv_lite::basic_string_view; - using sv_lite::string_view; - using sv_lite::wstring_view; - -#if nssv_HAVE_WCHAR16_T - using sv_lite::u16string_view; -#endif -#if nssv_HAVE_WCHAR32_T - using sv_lite::u32string_view; -#endif - - // literal "sv" - - using sv_lite::operator==; - using sv_lite::operator!=; - using sv_lite::operator<; - using sv_lite::operator<=; - using sv_lite::operator>; - using sv_lite::operator>=; - - using sv_lite::operator<<; - -#if nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS - using sv_lite::to_string; - using sv_lite::to_string_view; -#endif - -} // namespace nonstd - -// 24.4.5 Hash support (C++11): - -// Note: The hash value of a string view object is equal to the hash value of -// the corresponding string object. - -#if nssv_HAVE_STD_HASH - -#include - -namespace std { - - template<> - struct hash< nonstd::string_view > { - public: - std::size_t operator()(nonstd::string_view v) const nssv_noexcept { - return std::hash()(std::string(v.data(), v.size())); - } - }; - - template<> - struct hash< nonstd::wstring_view > { - public: - std::size_t operator()(nonstd::wstring_view v) const nssv_noexcept { - return std::hash()(std::wstring(v.data(), v.size())); - } - }; - - template<> - struct hash< nonstd::u16string_view > { - public: - std::size_t operator()(nonstd::u16string_view v) const nssv_noexcept { - return std::hash()(std::u16string(v.data(), v.size())); - } - }; - - template<> - struct hash< nonstd::u32string_view > { - public: - std::size_t operator()(nonstd::u32string_view v) const nssv_noexcept { - return std::hash()(std::u32string(v.data(), v.size())); - } - }; - -} // namespace std - -#endif // nssv_HAVE_STD_HASH - -nssv_RESTORE_WARNINGS() - -#endif // nssv_HAVE_STD_STRING_VIEW -#endif // NONSTD_SV_LITE_H_INCLUDED \ No newline at end of file diff --git a/include/use_asio.hpp b/include/use_asio.hpp deleted file mode 100644 index 3ca808e..0000000 --- a/include/use_asio.hpp +++ /dev/null @@ -1,66 +0,0 @@ -#pragma once - -#if defined(ASIO_STANDALONE) -//MSVC : define environment path 'ASIO_STANDALONE_INCLUDE', e.g. 'E:\bdlibs\asio-1.10.6\include' - -#include -#ifdef CINATRA_ENABLE_SSL -#include -#endif -#include -#include -namespace boost -{ - namespace asio - { - using namespace ::asio; - } - namespace system { - using ::std::error_code; - } -} -#else -#include -#ifdef CINATRA_ENABLE_SSL -#include -#endif -#include -using namespace boost; -using tcp_socket = boost::asio::ip::tcp::socket; -#ifdef CINATRA_ENABLE_SSL -using ssl_socket = boost::asio::ssl::stream; -#endif -#endif - -#if __cplusplus > 201402L -#include -using string_view = std::string_view; -#else -#ifdef ASIO_STANDALONE -#include "string_view.hpp" -using namespace nonstd; -#else -#include -using string_view = boost::string_view; -#endif -#endif - -#ifdef CINATRA_ENABLE_SSL -#if __cplusplus > 201402L -#if defined (__GNUC__) -#if __GNUC__ < 8 -#include -namespace rpcfs = std::experimental::filesystem; -#else -#include -namespace rpcfs = std::filesystem; -#endif -#else -#include -namespace rpcfs = boost::filesystem; -#endif -#else -#include -namespace rpcfs = boost::filesystem; -#endif -#endif