123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959 |
- /*
- * Copyright (c) 2017-2023 zhllxt
- *
- * author : zhllxt
- * email : 37792738@qq.com
- *
- * Distributed under the Boost Software License, Version 1.0. (See accompanying
- * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- */
- #ifndef __ASIO2_UDP_SEND_COMPONENT_HPP__
- #define __ASIO2_UDP_SEND_COMPONENT_HPP__
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- #pragma once
- #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
- #include <cstdint>
- #include <memory>
- #include <functional>
- #include <string>
- #include <future>
- #include <queue>
- #include <tuple>
- #include <utility>
- #include <string_view>
- #include <asio2/base/iopool.hpp>
- #include <asio2/base/define.hpp>
- #include <asio2/base/detail/util.hpp>
- #include <asio2/base/detail/function_traits.hpp>
- #include <asio2/base/detail/buffer_wrap.hpp>
- #include <asio2/base/impl/data_persistence_cp.hpp>
- namespace asio2::detail
- {
- ASIO2_CLASS_FORWARD_DECLARE_BASE;
- template<class derived_t, class args_t>
- class udp_send_cp : public data_persistence_cp<derived_t, args_t>
- {
- ASIO2_CLASS_FRIEND_DECLARE_BASE;
- public:
- /**
- * @brief constructor
- */
- udp_send_cp() noexcept {}
- /**
- * @brief destructor
- */
- ~udp_send_cp() = default;
- public:
- /**
- * @brief Asynchronous send data,supporting multi data formats,
- * see asio::buffer(...) in /asio/buffer.hpp
- * You can call this function on the communication thread and anywhere,it's multi thread safed.
- * use like this : std::string m; async_send(std::move(m)); can reducing memory allocation.
- * PodType * : async_send("abc");
- * PodType (&data)[N] : double m[10]; async_send(m);
- * std::array<PodType, N> : std::array<int,10> m; async_send(m);
- * std::vector<PodType, Allocator> : std::vector<float> m; async_send(m);
- * std::basic_string<Elem, Traits, Allocator> : std::string m; async_send(m);
- */
- template<typename String, typename StrOrInt, class DataT>
- inline typename std::enable_if_t<!std::is_same_v<detail::remove_cvref_t<String>,
- asio::ip::udp::endpoint>, void>
- async_send(String&& host, StrOrInt&& port, DataT&& data) noexcept
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- detail::integer_add_sub_guard asg(derive.io_->pending());
- // We must ensure that there is only one operation to send data
- // at the same time,otherwise may be cause crash.
- derive._do_resolve(std::forward<String>(host), std::forward<StrOrInt>(port),
- derive._data_persistence(std::forward<DataT>(data)),
- [](const error_code&, std::size_t) mutable {});
- }
- /**
- * @brief Asynchronous send data
- * You can call this function on the communication thread and anywhere,it's multi thread safed.
- * PodType * : async_send("abc");
- */
- template<typename String, typename StrOrInt, class CharT, class Traits = std::char_traits<CharT>>
- inline typename std::enable_if_t<!std::is_same_v<detail::remove_cvref_t<String>,
- asio::ip::udp::endpoint> && detail::is_char_v<CharT>, void>
- async_send(String&& host, StrOrInt&& port, CharT* s) noexcept
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- derive.async_send(std::forward<String>(host), std::forward<StrOrInt>(port),
- s, s ? Traits::length(s) : 0);
- }
- /**
- * @brief Asynchronous send data
- * You can call this function on the communication thread and anywhere,it's multi thread safed.
- * PodType (&data)[N] : double m[10]; async_send(m,5);
- */
- template<typename String, typename StrOrInt, class CharT, class SizeT>
- inline typename std::enable_if_t<std::is_integral_v<detail::remove_cvref_t<SizeT>> &&
- !std::is_same_v<detail::remove_cvref_t<String>, asio::ip::udp::endpoint>, void>
- async_send(String&& host, StrOrInt&& port, CharT * s, SizeT count) noexcept
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- if (!s)
- {
- set_last_error(asio::error::invalid_argument);
- return;
- }
- detail::integer_add_sub_guard asg(derive.io_->pending());
- // We must ensure that there is only one operation to send data
- // at the same time,otherwise may be cause crash.
- derive._do_resolve(std::forward<String>(host), std::forward<StrOrInt>(port),
- derive._data_persistence(s, count), [](const error_code&, std::size_t) mutable {});
- }
- /**
- * @brief Asynchronous send data,supporting multi data formats,
- * see asio::buffer(...) in /asio/buffer.hpp
- * use like this : std::string m; async_send(std::move(m)); can reducing memory allocation.
- * the pair.first save the send result error_code,the pair.second save the sent_bytes.
- * note : Do not call this function in any listener callback function like this:
- * auto future = async_send(msg,asio::use_future); future.get(); it will cause deadlock and
- * the future.get() will never return.
- * PodType * : async_send("abc");
- * PodType (&data)[N] : double m[10]; async_send(m);
- * std::array<PodType, N> : std::array<int,10> m; async_send(m);
- * std::vector<PodType, Allocator> : std::vector<float> m; async_send(m);
- * std::basic_string<Elem, Traits, Allocator> : std::string m; async_send(m);
- */
- template<typename String, typename StrOrInt, class DataT>
- inline typename std::enable_if_t<
- !std::is_same_v<detail::remove_cvref_t<String>, asio::ip::udp::endpoint>,
- std::future<std::pair<error_code, std::size_t>>>
- async_send(String&& host, StrOrInt&& port, DataT&& data, asio::use_future_t<>)
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- detail::integer_add_sub_guard asg(derive.io_->pending());
- std::shared_ptr<std::promise<std::pair<error_code, std::size_t>>> promise =
- std::make_shared<std::promise<std::pair<error_code, std::size_t>>>();
- std::future<std::pair<error_code, std::size_t>> future = promise->get_future();
- derive._do_resolve(std::forward<String>(host), std::forward<StrOrInt>(port),
- derive._data_persistence(std::forward<DataT>(data)),
- [promise = std::move(promise)](const error_code& ec, std::size_t bytes_sent) mutable
- {
- // if multiple addresses is resolved for the host and port, then the promise
- // will set_value many times, then will cause exception
- try
- {
- promise->set_value(std::pair<error_code, std::size_t>(ec, bytes_sent));
- }
- catch (std::future_errc const& e)
- {
- set_last_error(e);
- }
- });
- return future;
- }
- /**
- * @brief Asynchronous send data
- * the pair.first save the send result error_code,the pair.second save the sent_bytes.
- * note : Do not call this function in any listener callback function like this:
- * auto future = async_send(msg,asio::use_future); future.get(); it will cause deadlock and
- * the future.get() will never return.
- * PodType * : async_send("abc");
- */
- template<typename String, typename StrOrInt, class CharT, class Traits = std::char_traits<CharT>>
- inline typename std::enable_if_t<
- !std::is_same_v<detail::remove_cvref_t<String>, asio::ip::udp::endpoint> && detail::is_char_v<CharT>,
- std::future<std::pair<error_code, std::size_t>>>
- async_send(String&& host, StrOrInt&& port, CharT * s, asio::use_future_t<> flag)
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- return derive.async_send(std::forward<String>(host), std::forward<StrOrInt>(port), s,
- s ? Traits::length(s) : 0, std::move(flag));
- }
- /**
- * @brief Asynchronous send data
- * the pair.first save the send result error_code,the pair.second save the sent_bytes.
- * note : Do not call this function in any listener callback function like this:
- * auto future = async_send(msg,asio::use_future); future.get(); it will cause deadlock and
- * the future.get() will never return.
- * PodType (&data)[N] : double m[10]; async_send(m,5);
- */
- template<typename String, typename StrOrInt, class CharT, class SizeT>
- inline typename std::enable_if_t<std::is_integral_v<detail::remove_cvref_t<SizeT>> &&
- !std::is_same_v<detail::remove_cvref_t<String>, asio::ip::udp::endpoint>,
- std::future<std::pair<error_code, std::size_t>>>
- async_send(String&& host, StrOrInt&& port, CharT * s, SizeT count, asio::use_future_t<>)
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- detail::integer_add_sub_guard asg(derive.io_->pending());
- std::shared_ptr<std::promise<std::pair<error_code, std::size_t>>> promise =
- std::make_shared<std::promise<std::pair<error_code, std::size_t>>>();
- std::future<std::pair<error_code, std::size_t>> future = promise->get_future();
- if (!s)
- {
- set_last_error(asio::error::invalid_argument);
- promise->set_value(std::pair<error_code, std::size_t>(asio::error::invalid_argument, 0));
- return future;
- }
- derive._do_resolve(std::forward<String>(host), std::forward<StrOrInt>(port),
- derive._data_persistence(s, count),
- [promise = std::move(promise)](const error_code& ec, std::size_t bytes_sent) mutable
- {
- // if multiple addresses is resolved for the host and port, then the promise
- // will set_value many times, then will cause exception
- try
- {
- promise->set_value(std::pair<error_code, std::size_t>(ec, bytes_sent));
- }
- catch (std::future_errc const& e)
- {
- set_last_error(e);
- }
- });
- return future;
- }
- /**
- * @brief Asynchronous send data,supporting multi data formats,
- * see asio::buffer(...) in /asio/buffer.hpp
- * You can call this function on the communication thread and anywhere,it's multi thread safed.
- * use like this : std::string m; async_send(std::move(m)); can reducing memory allocation.
- * PodType * : async_send("abc");
- * PodType (&data)[N] : double m[10]; async_send(m);
- * std::array<PodType, N> : std::array<int,10> m; async_send(m);
- * std::vector<PodType, Allocator> : std::vector<float> m; async_send(m);
- * std::basic_string<Elem, Traits, Allocator> : std::string m; async_send(m);
- * Callback signature : void() or void(std::size_t bytes_sent)
- */
- template<typename String, typename StrOrInt, class DataT, class Callback>
- inline typename std::enable_if_t<is_callable_v<Callback> &&
- !std::is_same_v<detail::remove_cvref_t<String>, asio::ip::udp::endpoint>, void>
- async_send(String&& host, StrOrInt&& port, DataT&& data, Callback&& fn)
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- detail::integer_add_sub_guard asg(derive.io_->pending());
- // We must ensure that there is only one operation to send data
- // at the same time,otherwise may be cause crash.
- derive._do_resolve(std::forward<String>(host), std::forward<StrOrInt>(port),
- derive._data_persistence(std::forward<DataT>(data)),
- [fn = std::forward<Callback>(fn)](const error_code&, std::size_t bytes_sent) mutable
- {
- callback_helper::call(fn, bytes_sent);
- });
- }
- /**
- * @brief Asynchronous send data
- * You can call this function on the communication thread and anywhere,it's multi thread safed.
- * PodType * : async_send("abc");
- * Callback signature : void() or void(std::size_t bytes_sent)
- */
- template<typename String, typename StrOrInt, class Callback, class CharT,
- class Traits = std::char_traits<CharT>>
- inline typename std::enable_if_t<is_callable_v<Callback> &&
- !std::is_same_v<detail::remove_cvref_t<String>, asio::ip::udp::endpoint> &&
- detail::is_char_v<CharT>, void>
- async_send(String&& host, StrOrInt&& port, CharT * s, Callback&& fn)
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- derive.async_send(std::forward<String>(host), std::forward<StrOrInt>(port),
- s, s ? Traits::length(s) : 0, std::forward<Callback>(fn));
- }
- /**
- * @brief Asynchronous send data
- * You can call this function on the communication thread and anywhere,it's multi thread safed.
- * PodType (&data)[N] : double m[10]; async_send(m,5);
- * Callback signature : void() or void(std::size_t bytes_sent)
- */
- template<typename String, typename StrOrInt, class Callback, class CharT, class SizeT>
- inline typename std::enable_if_t<is_callable_v<Callback> &&
- !std::is_same_v<detail::remove_cvref_t<String>, asio::ip::udp::endpoint> &&
- std::is_integral_v<detail::remove_cvref_t<SizeT>>, void>
- async_send(String&& host, StrOrInt&& port, CharT * s, SizeT count, Callback&& fn)
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- detail::integer_add_sub_guard asg(derive.io_->pending());
- // We must ensure that there is only one operation to send data
- // at the same time,otherwise may be cause crash.
- //// don't need do this
- //if (!s)
- //{
- // // ...
- //}
- derive._do_resolve(std::forward<String>(host), std::forward<StrOrInt>(port),
- derive._data_persistence(s, count),
- [fn = std::forward<Callback>(fn)](const error_code&, std::size_t bytes_sent) mutable
- {
- callback_helper::call(fn, bytes_sent);
- });
- }
- public:
- /**
- * @brief Asynchronous send data,supporting multi data formats,
- * see asio::buffer(...) in /asio/buffer.hpp
- * You can call this function on the communication thread and anywhere,it's multi thread safed.
- * use like this : std::string m; async_send(std::move(m)); can reducing memory allocation.
- * PodType * : async_send("abc");
- * PodType (&data)[N] : double m[10]; async_send(m);
- * std::array<PodType, N> : std::array<int,10> m; async_send(m);
- * std::vector<PodType, Allocator> : std::vector<float> m; async_send(m);
- * std::basic_string<Elem, Traits, Allocator> : std::string m; async_send(m);
- */
- template<class Endpoint, class DataT>
- inline typename std::enable_if_t<
- std::is_same_v<detail::remove_cvref_t<Endpoint>, asio::ip::udp::endpoint>, void>
- async_send(Endpoint&& endpoint, DataT&& data) noexcept
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- detail::integer_add_sub_guard asg(derive.io_->pending());
- // We must ensure that there is only one operation to send data
- // at the same time,otherwise may be cause crash.
- derive.push_event(
- [&derive, p = derive.selfptr(), id = derive.life_id(), endpoint = std::forward<Endpoint>(endpoint),
- data = derive._data_persistence(std::forward<DataT>(data))]
- (event_queue_guard<derived_t> g) mutable
- {
- if (!derive.is_started())
- {
- set_last_error(asio::error::not_connected);
- return;
- }
- if (id != derive.life_id())
- {
- set_last_error(asio::error::operation_aborted);
- return;
- }
- clear_last_error();
- derive._do_send(endpoint, data, [g = std::move(g)](const error_code&, std::size_t) mutable {});
- });
- }
- /**
- * @brief Asynchronous send data
- * You can call this function on the communication thread and anywhere,it's multi thread safed.
- * PodType * : async_send("abc");
- */
- template<class Endpoint, class CharT, class Traits = std::char_traits<CharT>>
- inline typename std::enable_if_t<
- std::is_same_v<detail::remove_cvref_t<Endpoint>, asio::ip::udp::endpoint> &&
- detail::is_char_v<CharT>, void>
- async_send(Endpoint&& endpoint, CharT * s) noexcept
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- derive.async_send(std::forward<Endpoint>(endpoint), s, s ? Traits::length(s) : 0);
- }
- /**
- * @brief Asynchronous send data
- * You can call this function on the communication thread and anywhere,it's multi thread safed.
- * PodType (&data)[N] : double m[10]; async_send(m,5);
- */
- template<class Endpoint, class CharT, class SizeT>
- inline typename std::enable_if_t<std::is_integral_v<detail::remove_cvref_t<SizeT>> &&
- std::is_same_v<detail::remove_cvref_t<Endpoint>, asio::ip::udp::endpoint>, void>
- async_send(Endpoint&& endpoint, CharT * s, SizeT count) noexcept
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- if (!s)
- {
- set_last_error(asio::error::invalid_argument);
- return;
- }
- detail::integer_add_sub_guard asg(derive.io_->pending());
- // We must ensure that there is only one operation to send data
- // at the same time,otherwise may be cause crash.
- derive.push_event(
- [&derive, p = derive.selfptr(), id = derive.life_id(), endpoint = std::forward<Endpoint>(endpoint),
- data = derive._data_persistence(s, count)]
- (event_queue_guard<derived_t> g) mutable
- {
- if (!derive.is_started())
- {
- set_last_error(asio::error::not_connected);
- return;
- }
- if (id != derive.life_id())
- {
- set_last_error(asio::error::operation_aborted);
- return;
- }
- clear_last_error();
- derive._do_send(endpoint, data, [g = std::move(g)](const error_code&, std::size_t) mutable {});
- });
- }
- /**
- * @brief Asynchronous send data,supporting multi data formats,
- * see asio::buffer(...) in /asio/buffer.hpp
- * use like this : std::string m; async_send(std::move(m)); can reducing memory allocation.
- * the pair.first save the send result error_code,the pair.second save the sent_bytes.
- * note : Do not call this function in any listener callback function like this:
- * auto future = async_send(msg,asio::use_future); future.get(); it will cause deadlock and
- * the future.get() will never return.
- * PodType * : async_send("abc");
- * PodType (&data)[N] : double m[10]; async_send(m);
- * std::array<PodType, N> : std::array<int,10> m; async_send(m);
- * std::vector<PodType, Allocator> : std::vector<float> m; async_send(m);
- * std::basic_string<Elem, Traits, Allocator> : std::string m; async_send(m);
- */
- template<class Endpoint, class DataT>
- inline typename std::enable_if_t<
- std::is_same_v<detail::remove_cvref_t<Endpoint>, asio::ip::udp::endpoint>,
- std::future<std::pair<error_code, std::size_t>>>
- async_send(Endpoint&& endpoint, DataT&& data, asio::use_future_t<>)
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- detail::integer_add_sub_guard asg(derive.io_->pending());
- std::shared_ptr<std::promise<std::pair<error_code, std::size_t>>> promise =
- std::make_shared<std::promise<std::pair<error_code, std::size_t>>>();
- std::future<std::pair<error_code, std::size_t>> future = promise->get_future();
- derive.push_event(
- [&derive, p = derive.selfptr(), id = derive.life_id(), endpoint = std::forward<Endpoint>(endpoint),
- data = derive._data_persistence(std::forward<DataT>(data)), promise = std::move(promise)]
- (event_queue_guard<derived_t> g) mutable
- {
- if (!derive.is_started())
- {
- set_last_error(asio::error::not_connected);
- promise->set_value(std::pair<error_code, std::size_t>(asio::error::not_connected, 0));
- return;
- }
- if (id != derive.life_id())
- {
- set_last_error(asio::error::operation_aborted);
- promise->set_value(std::pair<error_code, std::size_t>(asio::error::operation_aborted, 0));
- return;
- }
- clear_last_error();
- derive._do_send(endpoint, data, [&promise, g = std::move(g)]
- (const error_code& ec, std::size_t bytes_sent) mutable
- {
- promise->set_value(std::pair<error_code, std::size_t>(ec, bytes_sent));
- });
- });
- return future;
- }
- /**
- * @brief Asynchronous send data
- * the pair.first save the send result error_code,the pair.second save the sent_bytes.
- * note : Do not call this function in any listener callback function like this:
- * auto future = async_send(msg,asio::use_future); future.get(); it will cause deadlock and
- * the future.get() will never return.
- * PodType * : async_send("abc");
- */
- template<class Endpoint, class CharT, class Traits = std::char_traits<CharT>>
- inline typename std::enable_if_t<
- std::is_same_v<detail::remove_cvref_t<Endpoint>, asio::ip::udp::endpoint> && detail::is_char_v<CharT>,
- std::future<std::pair<error_code, std::size_t>>>
- async_send(Endpoint&& endpoint, CharT * s, asio::use_future_t<> flag)
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- return derive.async_send(std::forward<Endpoint>(endpoint), s,
- s ? Traits::length(s) : 0, std::move(flag));
- }
- /**
- * @brief Asynchronous send data
- * the pair.first save the send result error_code,the pair.second save the sent_bytes.
- * note : Do not call this function in any listener callback function like this:
- * auto future = async_send(msg,asio::use_future); future.get(); it will cause deadlock and
- * the future.get() will never return.
- * PodType (&data)[N] : double m[10]; async_send(m,5);
- */
- template<class Endpoint, class CharT, class SizeT>
- inline typename std::enable_if_t<std::is_integral_v<detail::remove_cvref_t<SizeT>> &&
- std::is_same_v<detail::remove_cvref_t<Endpoint>, asio::ip::udp::endpoint>,
- std::future<std::pair<error_code, std::size_t>>>
- async_send(Endpoint&& endpoint, CharT * s, SizeT count, asio::use_future_t<>)
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- detail::integer_add_sub_guard asg(derive.io_->pending());
- std::shared_ptr<std::promise<std::pair<error_code, std::size_t>>> promise =
- std::make_shared<std::promise<std::pair<error_code, std::size_t>>>();
- std::future<std::pair<error_code, std::size_t>> future = promise->get_future();
- if (!s)
- {
- set_last_error(asio::error::invalid_argument);
- promise->set_value(std::pair<error_code, std::size_t>(asio::error::invalid_argument, 0));
- return future;
- }
- derive.push_event(
- [&derive, p = derive.selfptr(), id = derive.life_id(), endpoint = std::forward<Endpoint>(endpoint),
- data = derive._data_persistence(s, count), promise = std::move(promise)]
- (event_queue_guard<derived_t> g) mutable
- {
- if (!derive.is_started())
- {
- set_last_error(asio::error::not_connected);
- promise->set_value(std::pair<error_code, std::size_t>(asio::error::not_connected, 0));
- return;
- }
- if (id != derive.life_id())
- {
- set_last_error(asio::error::operation_aborted);
- promise->set_value(std::pair<error_code, std::size_t>(asio::error::operation_aborted, 0));
- return;
- }
- clear_last_error();
- derive._do_send(endpoint, data, [&promise, g = std::move(g)]
- (const error_code& ec, std::size_t bytes_sent) mutable
- {
- promise->set_value(std::pair<error_code, std::size_t>(ec, bytes_sent));
- });
- });
- return future;
- }
- /**
- * @brief Asynchronous send data,supporting multi data formats,
- * see asio::buffer(...) in /asio/buffer.hpp
- * You can call this function on the communication thread and anywhere,it's multi thread safed.
- * use like this : std::string m; async_send(std::move(m)); can reducing memory allocation.
- * PodType * : async_send("abc");
- * PodType (&data)[N] : double m[10]; async_send(m);
- * std::array<PodType, N> : std::array<int,10> m; async_send(m);
- * std::vector<PodType, Allocator> : std::vector<float> m; async_send(m);
- * std::basic_string<Elem, Traits, Allocator> : std::string m; async_send(m);
- * Callback signature : void() or void(std::size_t bytes_sent)
- */
- template<class Endpoint, class DataT, class Callback>
- inline typename std::enable_if_t<is_callable_v<Callback> &&
- std::is_same_v<detail::remove_cvref_t<Endpoint>, asio::ip::udp::endpoint>, void>
- async_send(Endpoint&& endpoint, DataT&& data, Callback&& fn)
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- detail::integer_add_sub_guard asg(derive.io_->pending());
- // We must ensure that there is only one operation to send data
- // at the same time,otherwise may be cause crash.
- derive.push_event(
- [&derive, p = derive.selfptr(), id = derive.life_id(), endpoint = std::forward<Endpoint>(endpoint),
- data = derive._data_persistence(std::forward<DataT>(data)), fn = std::forward<Callback>(fn)]
- (event_queue_guard<derived_t> g) mutable
- {
- if (!derive.is_started())
- {
- set_last_error(asio::error::not_connected);
- callback_helper::call(fn, 0);
- return;
- }
- if (id != derive.life_id())
- {
- set_last_error(asio::error::operation_aborted);
- callback_helper::call(fn, 0);
- return;
- }
- clear_last_error();
- derive._do_send(endpoint, data, [&fn, g = std::move(g)]
- (const error_code&, std::size_t bytes_sent) mutable
- {
- callback_helper::call(fn, bytes_sent);
- });
- });
- }
- /**
- * @brief Asynchronous send data
- * You can call this function on the communication thread and anywhere,it's multi thread safed.
- * PodType * : async_send("abc");
- * Callback signature : void() or void(std::size_t bytes_sent)
- */
- template<class Endpoint, class Callback, class CharT, class Traits = std::char_traits<CharT>>
- inline typename std::enable_if_t<is_callable_v<Callback> &&
- std::is_same_v<detail::remove_cvref_t<Endpoint>, asio::ip::udp::endpoint> &&
- detail::is_char_v<CharT>, void>
- async_send(Endpoint&& endpoint, CharT * s, Callback&& fn)
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- derive.async_send(std::forward<Endpoint>(endpoint),
- s, s ? Traits::length(s) : 0, std::forward<Callback>(fn));
- }
- /**
- * @brief Asynchronous send data
- * You can call this function on the communication thread and anywhere,it's multi thread safed.
- * PodType (&data)[N] : double m[10]; async_send(m,5);
- * Callback signature : void() or void(std::size_t bytes_sent)
- */
- template<class Endpoint, class Callback, class CharT, class SizeT>
- inline typename std::enable_if_t<is_callable_v<Callback> &&
- std::is_same_v<detail::remove_cvref_t<Endpoint>, asio::ip::udp::endpoint> &&
- std::is_integral_v<detail::remove_cvref_t<SizeT>>, void>
- async_send(Endpoint&& endpoint, CharT * s, SizeT count, Callback&& fn)
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- detail::integer_add_sub_guard asg(derive.io_->pending());
- // We must ensure that there is only one operation to send data
- // at the same time,otherwise may be cause crash.
- derive.push_event(
- [&derive, p = derive.selfptr(), id = derive.life_id(), endpoint = std::forward<Endpoint>(endpoint),
- s, data = derive._data_persistence(s, count), fn = std::forward<Callback>(fn)]
- (event_queue_guard<derived_t> g) mutable
- {
- if (!s)
- {
- set_last_error(asio::error::invalid_argument);
- callback_helper::call(fn, 0);
- return;
- }
- if (!derive.is_started())
- {
- set_last_error(asio::error::not_connected);
- callback_helper::call(fn, 0);
- return;
- }
- if (id != derive.life_id())
- {
- set_last_error(asio::error::operation_aborted);
- callback_helper::call(fn, 0);
- return;
- }
- clear_last_error();
- derive._do_send(endpoint, data, [&fn, g = std::move(g)]
- (const error_code&, std::size_t bytes_sent) mutable
- {
- callback_helper::call(fn, bytes_sent);
- });
- });
- }
- public:
- /**
- * @brief Synchronous send data,supporting multi data formats,
- * see asio::buffer(...) in /asio/buffer.hpp
- * You can call this function on the communication thread and anywhere,it's multi thread safed.
- * Note : If this function is called in communication thread, it will degenerates into async_send
- * and the return value is 0, you can use asio2::get_last_error() to check whether the
- * send is success, if asio2::get_last_error() is equal to asio::error::in_progress, it
- * means success, otherwise failed.
- * use like this : std::string m; send(std::move(m)); can reducing memory allocation.
- * PodType * : send("abc");
- * PodType (&data)[N] : double m[10]; send(m);
- * std::array<PodType, N> : std::array<int,10> m; send(m);
- * std::vector<PodType, Allocator> : std::vector<float> m; send(m);
- * std::basic_string<Elem, Traits, Allocator> : std::string m; send(m);
- */
- template<typename String, typename StrOrInt, class DataT>
- inline typename std::enable_if_t<
- !std::is_same_v<detail::remove_cvref_t<String>, asio::ip::udp::endpoint>, std::size_t>
- send(String&& host, StrOrInt&& port, DataT&& data)
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- std::future<std::pair<error_code, std::size_t>> future = derive.async_send(
- std::forward<String>(host), std::forward<StrOrInt>(port),
- std::forward<DataT>(data), asio::use_future);
- // Whether we run on the io_context thread
- if (derive.io_->running_in_this_thread())
- {
- std::future_status status = future.wait_for(std::chrono::nanoseconds(0));
- // async_send failed.
- if (status == std::future_status::ready)
- {
- set_last_error(future.get().first);
- return std::size_t(0);
- }
- // async_send success.
- else
- {
- set_last_error(asio::error::in_progress);
- return std::size_t(0);
- }
- }
- std::pair<error_code, std::size_t> pair = future.get();
- set_last_error(pair.first);
- return pair.second;
- }
- /**
- * @brief Synchronous send data
- * You can call this function on the communication thread and anywhere,it's multi thread safed.
- * Note : If this function is called in communication thread, it will degenerates into async_send
- * and the return value is 0, you can use asio2::get_last_error() to check whether the
- * send is success, if asio2::get_last_error() is equal to asio::error::in_progress, it
- * means success, otherwise failed.
- * PodType * : send("abc");
- */
- template<typename String, typename StrOrInt, class CharT, class Traits = std::char_traits<CharT>>
- inline typename std::enable_if_t<!std::is_same_v<detail::remove_cvref_t<String>,
- asio::ip::udp::endpoint> && detail::is_char_v<CharT>, std::size_t>
- send(String&& host, StrOrInt&& port, CharT* s)
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- return derive.send(std::forward<String>(host), std::forward<StrOrInt>(port),
- s, s ? Traits::length(s) : 0);
- }
- /**
- * @brief Synchronous send data
- * You can call this function on the communication thread and anywhere,it's multi thread safed.
- * Note : If this function is called in communication thread, it will degenerates into async_send
- * and the return value is 0, you can use asio2::get_last_error() to check whether the
- * send is success, if asio2::get_last_error() is equal to asio::error::in_progress, it
- * means success, otherwise failed.
- * PodType (&data)[N] : double m[10]; send(m,5);
- */
- template<typename String, typename StrOrInt, class CharT, class SizeT>
- inline typename std::enable_if_t<std::is_integral_v<detail::remove_cvref_t<SizeT>> &&
- !std::is_same_v<detail::remove_cvref_t<String>, asio::ip::udp::endpoint>, std::size_t>
- send(String&& host, StrOrInt&& port, CharT * s, SizeT count)
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- return derive.send(std::forward<String>(host), std::forward<StrOrInt>(port),
- derive._data_persistence(s, count));
- }
- public:
- /**
- * @brief Synchronous send data,supporting multi data formats,
- * see asio::buffer(...) in /asio/buffer.hpp
- * You can call this function on the communication thread and anywhere,it's multi thread safed.
- * Note : If this function is called in communication thread, it will degenerates into async_send
- * and the return value is 0, you can use asio2::get_last_error() to check whether the
- * send is success, if asio2::get_last_error() is equal to asio::error::in_progress, it
- * means success, otherwise failed.
- * use like this : std::string m; send(std::move(m)); can reducing memory allocation.
- * PodType * : send("abc");
- * PodType (&data)[N] : double m[10]; send(m);
- * std::array<PodType, N> : std::array<int,10> m; send(m);
- * std::vector<PodType, Allocator> : std::vector<float> m; send(m);
- * std::basic_string<Elem, Traits, Allocator> : std::string m; send(m);
- */
- template<class Endpoint, class DataT>
- inline typename std::enable_if_t<
- std::is_same_v<detail::remove_cvref_t<Endpoint>, asio::ip::udp::endpoint>, std::size_t>
- send(Endpoint&& endpoint, DataT&& data)
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- std::future<std::pair<error_code, std::size_t>> future = derive.async_send(
- std::forward<Endpoint>(endpoint), std::forward<DataT>(data), asio::use_future);
- // Whether we run on the io_context thread
- if (derive.io_->running_in_this_thread())
- {
- std::future_status status = future.wait_for(std::chrono::nanoseconds(0));
- // async_send failed.
- if (status == std::future_status::ready)
- {
- set_last_error(future.get().first);
- return std::size_t(0);
- }
- // async_send success.
- else
- {
- set_last_error(asio::error::in_progress);
- return std::size_t(0);
- }
- }
- std::pair<error_code, std::size_t> pair = future.get();
- set_last_error(pair.first);
- return pair.second;
- }
- /**
- * @brief Synchronous send data
- * You can call this function on the communication thread and anywhere,it's multi thread safed.
- * Note : If this function is called in communication thread, it will degenerates into async_send
- * and the return value is 0, you can use asio2::get_last_error() to check whether the
- * send is success, if asio2::get_last_error() is equal to asio::error::in_progress, it
- * means success, otherwise failed.
- * PodType * : send("abc");
- */
- template<class Endpoint, class CharT, class Traits = std::char_traits<CharT>>
- inline typename std::enable_if_t<
- std::is_same_v<detail::remove_cvref_t<Endpoint>, asio::ip::udp::endpoint> &&
- detail::is_char_v<CharT>, std::size_t>
- send(Endpoint&& endpoint, CharT * s)
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- return derive.send(std::forward<Endpoint>(endpoint), s, s ? Traits::length(s) : 0);
- }
- /**
- * @brief Synchronous send data
- * You can call this function on the communication thread and anywhere,it's multi thread safed.
- * Note : If this function is called in communication thread, it will degenerates into async_send
- * and the return value is 0, you can use asio2::get_last_error() to check whether the
- * send is success, if asio2::get_last_error() is equal to asio::error::in_progress, it
- * means success, otherwise failed.
- * PodType (&data)[N] : double m[10]; send(m,5);
- */
- template<class Endpoint, class CharT, class SizeT>
- inline typename std::enable_if_t<std::is_integral_v<detail::remove_cvref_t<SizeT>> &&
- std::is_same_v<detail::remove_cvref_t<Endpoint>, asio::ip::udp::endpoint>, std::size_t>
- send(Endpoint&& endpoint, CharT * s, SizeT count)
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- return derive.send(std::forward<Endpoint>(endpoint), derive._data_persistence(s, count));
- }
- protected:
- template<typename String, typename StrOrInt, typename Data, typename Callback>
- inline void _do_resolve(String&& host, StrOrInt&& port, Data&& data, Callback&& callback)
- {
- derived_t& derive = static_cast<derived_t&>(*this);
- using resolver_type = asio::ip::udp::resolver;
- using endpoints_type = typename resolver_type::results_type;
- //using endpoints_iterator = typename endpoints_type::iterator;
- std::unique_ptr<resolver_type> resolver_ptr = std::make_unique<resolver_type>(
- derive.io_->context());
- // Before async_resolve execution is complete, we must hold the resolver object.
- // so we captured the resolver_ptr into the lambda callback function.
- resolver_type * rp = resolver_ptr.get();
- rp->async_resolve(std::forward<String>(host), detail::to_string(std::forward<StrOrInt>(port)),
- [&derive, p = derive.selfptr(), resolver_ptr = std::move(resolver_ptr),
- data = std::forward<Data>(data), callback = std::forward<Callback>(callback)]
- (const error_code& ec, const endpoints_type& endpoints) mutable
- {
- set_last_error(ec);
- if (ec)
- {
- callback(ec, 0);
- return;
- }
- decltype(endpoints.size()) i = 1;
- for (auto iter = endpoints.begin(); iter != endpoints.end(); ++iter, ++i)
- {
- derive.push_event(
- [&derive, id = derive.life_id(), endpoint = iter->endpoint(),
- p = (endpoints.size() == i ? std::move(p) : p),
- data = (endpoints.size() == i ? std::move(data) : data),
- callback = (endpoints.size() == i ? std::move(callback) : callback)]
- (event_queue_guard<derived_t> g) mutable
- {
- if (!derive.is_started())
- {
- set_last_error(asio::error::not_connected);
- callback(asio::error::not_connected, 0);
- return;
- }
- if (id != derive.life_id())
- {
- set_last_error(asio::error::operation_aborted);
- callback(asio::error::operation_aborted, 0);
- return;
- }
- clear_last_error();
- derive._do_send(endpoint, data, [g = std::move(g), f = std::move(callback)]
- (const error_code& ec, std::size_t bytes_sent) mutable
- {
- f(ec, bytes_sent);
- });
- });
- }
- });
- }
- };
- }
- #endif // !__ASIO2_UDP_SEND_COMPONENT_HPP__
|