Browse Source

支持ASIO 1.12.2以上版本

tags/v2.9.7
tearshark 5 years ago
parent
commit
f4214e45e7

+ 2
- 2
benchmark/benchmark_asio_echo.cpp View File

@@ -1,4 +1,4 @@
#include <SDKDDKVer.h>
#include <SDKDDKVer.h>
/*
#include <chrono>
@@ -117,7 +117,7 @@ void RunOneBenchmark(bool bMain)
asio::io_service io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 3456));
uarray<tcp::socket, 16> socketes(acceptor.get_io_service());
uarray<tcp::socket, 16> socketes(io_service);
AcceptConnections(acceptor, socketes);
if (bMain) StartPrintEchoCount();

+ 5
- 149
librf/src/asio_task.h View File

@@ -2,152 +2,8 @@
#pragma once
#include <asio.hpp>
#include "future.h"
#include <memory>
#include "asio/detail/push_options.hpp"
/*
#include "asio/detail/config.hpp"
#include "asio/async_result.hpp"
#include "asio/error_code.hpp"
#include "asio/handler_type.hpp"
#include "asio/system_error.hpp"
*/
namespace asio {
template<typename Allocator = std::allocator<int> >
class rf_task_t
{
public:
typedef Allocator allocator_type;
constexpr rf_task_t() {}
explicit rf_task_t(const Allocator& allocator) : allocator_(allocator) {}
template<typename OtherAllocator>
rf_task_t<OtherAllocator> operator[](const OtherAllocator& allocator) const {
return rf_task_t<OtherAllocator>(allocator);
}
allocator_type get_allocator() const { return allocator_; }
private:
Allocator allocator_;
};
//constexpr rf_task_t<> rf_task;
#pragma warning(push)
#pragma warning(disable : 4592)
__declspec(selectany) rf_task_t<> rf_task;
#pragma warning(pop)
namespace detail {
template<typename T>
class promise_handler
{
public:
using result_type_t = T;
using state_type = resumef::state_t<result_type_t>;
template<typename Allocator>
promise_handler(const rf_task_t<Allocator> &)
: state_(resumef::make_counted<state_type>())
{
}
void operator()(T t) const
{
state_->set_value(std::move(t));
}
void operator()(const asio::error_code& ec, T t) const
{
if (!ec)
{
state_->set_value(std::move(t));
}
else
{
state_->set_exception(std::make_exception_ptr(asio::system_error(ec)));
}
}
resumef::counted_ptr<state_type> state_;
};
template<>
class promise_handler<void>
{
public:
using result_type_t = void;
using state_type = resumef::state_t<result_type_t>;
template<typename Allocator>
promise_handler(const rf_task_t<Allocator> &)
: state_(resumef::make_counted<state_type>())
{
}
void operator()() const
{
state_->set_value();
}
void operator()(const asio::error_code& ec) const
{
if (!ec)
{
state_->set_value();
}
else
{
state_->set_exception(std::make_exception_ptr(asio::system_error(ec)));
}
}
resumef::counted_ptr<state_type> state_;
};
} // namespace detail
template<typename T>
class async_result<detail::promise_handler<T> >
{
public:
typedef resumef::future_t<T> type;
explicit async_result(detail::promise_handler<T> & h)
: task_(std::move(h.state_))
{ }
resumef::future_t<T> get() { return std::move(task_); }
private:
resumef::future_t<T> task_;
};
// Handler type specialisation for zero arg.
template<typename Allocator, typename ReturnType>
struct handler_type<rf_task_t<Allocator>, ReturnType()> {
typedef detail::promise_handler<void> type;
};
// Handler type specialisation for one arg.
template<typename Allocator, typename ReturnType, typename Arg1>
struct handler_type<rf_task_t<Allocator>, ReturnType(Arg1)> {
typedef detail::promise_handler<Arg1> type;
};
// Handler type specialisation for two arg.
template<typename Allocator, typename ReturnType, typename Arg2>
struct handler_type<rf_task_t<Allocator>, ReturnType(asio::error_code, Arg2)> {
typedef detail::promise_handler<Arg2> type;
};
template<typename Allocator, typename ReturnType>
struct handler_type<rf_task_t<Allocator>, ReturnType(asio::error_code)> {
typedef detail::promise_handler<void> type;
};
} // namespace asio
#include "asio/detail/pop_options.hpp"
#if ASIO_VERSION >= 101200
#include "asio_task_1.12.0.inl"
#else
#include "asio_task_1.10.0.inl"
#endif

+ 145
- 0
librf/src/asio_task_1.10.0.inl View File

@@ -0,0 +1,145 @@

#pragma once

#include "future.h"
#include <memory>

#include "asio/detail/push_options.hpp"

namespace asio {

template<typename Allocator = std::allocator<int> >
class rf_task_t
{
public:
typedef Allocator allocator_type;
constexpr rf_task_t() {}
explicit rf_task_t(const Allocator& allocator) : allocator_(allocator) {}

template<typename OtherAllocator>
rf_task_t<OtherAllocator> operator[](const OtherAllocator& allocator) const {
return rf_task_t<OtherAllocator>(allocator);
}

allocator_type get_allocator() const { return allocator_; }
private:
Allocator allocator_;
};

//constexpr rf_task_t<> rf_task;
#pragma warning(push)
#pragma warning(disable : 4592)
__declspec(selectany) rf_task_t<> rf_task;
#pragma warning(pop)

namespace detail {

template<typename T>
class promise_handler
{
public:
using result_type_t = T;
using state_type = resumef::state_t<result_type_t>;

template<typename Allocator>
promise_handler(const rf_task_t<Allocator> &)
: state_(resumef::make_counted<state_type>())
{
}

void operator()(T t) const
{
state_->set_value(std::move(t));
}

void operator()(const asio::error_code& ec, T t) const
{
if (!ec)
{
state_->set_value(std::move(t));
}
else
{
state_->set_exception(std::make_exception_ptr(asio::system_error(ec)));
}
}

resumef::counted_ptr<state_type> state_;
};

template<>
class promise_handler<void>
{
public:
using result_type_t = void;
using state_type = resumef::state_t<result_type_t>;

template<typename Allocator>
promise_handler(const rf_task_t<Allocator> &)
: state_(resumef::make_counted<state_type>())
{
}

void operator()() const
{
state_->set_value();
}

void operator()(const asio::error_code& ec) const
{
if (!ec)
{
state_->set_value();
}
else
{
state_->set_exception(std::make_exception_ptr(asio::system_error(ec)));
}
}

resumef::counted_ptr<state_type> state_;
};

} // namespace detail

template<typename T>
class async_result<detail::promise_handler<T> >
{
public:
typedef resumef::future_t<T> type;

explicit async_result(detail::promise_handler<T> & h)
: task_(std::move(h.state_))
{ }

resumef::future_t<T> get() { return std::move(task_); }
private:
resumef::future_t<T> task_;
};

// Handler type specialisation for zero arg.
template<typename Allocator, typename ReturnType>
struct handler_type<rf_task_t<Allocator>, ReturnType()> {
typedef detail::promise_handler<void> type;
};

// Handler type specialisation for one arg.
template<typename Allocator, typename ReturnType, typename Arg1>
struct handler_type<rf_task_t<Allocator>, ReturnType(Arg1)> {
typedef detail::promise_handler<Arg1> type;
};

// Handler type specialisation for two arg.
template<typename Allocator, typename ReturnType, typename Arg2>
struct handler_type<rf_task_t<Allocator>, ReturnType(asio::error_code, Arg2)> {
typedef detail::promise_handler<Arg2> type;
};

template<typename Allocator, typename ReturnType>
struct handler_type<rf_task_t<Allocator>, ReturnType(asio::error_code)> {
typedef detail::promise_handler<void> type;
};

} // namespace asio

#include "asio/detail/pop_options.hpp"

+ 196
- 0
librf/src/asio_task_1.12.0.inl View File

@@ -0,0 +1,196 @@

#include "future.h"
#include <memory>

#include "asio/detail/push_options.hpp"

namespace asio {

template <typename Executor = executor>
struct rf_task_t
{
ASIO_CONSTEXPR rf_task_t() {}
};

#if defined(ASIO_HAS_CONSTEXPR) || defined(GENERATING_DOCUMENTATION)
constexpr rf_task_t<> rf_task;
#elif defined(ASIO_MSVC)
__declspec(selectany) rf_task_t<> rf_task;
#endif

namespace detail {

template <typename Executor, typename T>
struct librf_awaitable_handler_base
{
public:
typedef T result_type;
typedef resumef::state_t<result_type> state_type;

librf_awaitable_handler_base()
: state_(resumef::make_counted<state_type>())
{
}

resumef::counted_ptr<state_type> state_;
librf_awaitable_handler_base(librf_awaitable_handler_base &&) = default;
librf_awaitable_handler_base(const librf_awaitable_handler_base &) = default;
librf_awaitable_handler_base & operator = (librf_awaitable_handler_base &&) = default;
librf_awaitable_handler_base & operator = (const librf_awaitable_handler_base &) = default;
};

template <typename, typename...>
struct librf_promise_handler;

template <typename Executor>
struct librf_promise_handler<Executor, void> : public librf_awaitable_handler_base<Executor, void>
{
using librf_awaitable_handler_base<Executor, void>::librf_awaitable_handler_base;

void operator()() const
{
state_->set_value();
}
};

template <typename Executor>
struct librf_promise_handler<Executor, asio::error_code> : public librf_awaitable_handler_base<Executor, void>
{
using librf_awaitable_handler_base<Executor, void>::librf_awaitable_handler_base;

void operator()(const asio::error_code& ec) const
{
if (!ec)
state_->set_value();
else
state_->set_exception(std::make_exception_ptr(asio::system_error(ec)));
}
};

template <typename Executor>
struct librf_promise_handler<Executor, std::exception_ptr> : public librf_awaitable_handler_base<Executor, void>
{
using librf_awaitable_handler_base<Executor, void>::librf_awaitable_handler_base;

void operator()(std::exception_ptr ex) const
{
if (!ex)
state_->set_value();
else
state_->set_exception(ex);
}
};



template <typename Executor, typename T>
struct librf_promise_handler<Executor, T> : public librf_awaitable_handler_base<Executor, T>
{
using librf_awaitable_handler_base<Executor, T>::librf_awaitable_handler_base;

template <typename Arg>
void operator()(Arg&& arg) const
{
state_->set_value(std::forward<Arg>(arg));
}
};

template <typename Executor, typename T>
struct librf_promise_handler<Executor, asio::error_code, T> : public librf_awaitable_handler_base<Executor, T>
{
using librf_awaitable_handler_base<Executor, T>::librf_awaitable_handler_base;

template <typename Arg>
void operator()(const asio::error_code& ec, Arg&& arg) const
{
if (!ec)
state_->set_value(std::forward<Arg>(arg));
else
state_->set_exception(std::make_exception_ptr(asio::system_error(ec)));
}
};

template <typename Executor, typename T>
struct librf_promise_handler<Executor, std::exception_ptr, T> : public librf_awaitable_handler_base<Executor, T>
{
using librf_awaitable_handler_base<Executor, T>::librf_awaitable_handler_base;

template <typename Arg>
void operator()(std::exception_ptr ex, Arg&& arg) const
{
if (!ex)
state_->set_value(std::forward<Arg>(arg));
else
state_->set_exception(ex);
}
};



template <typename Executor, typename... Ts>
struct librf_promise_handler : public librf_awaitable_handler_base<Executor, std::tuple<Ts...>>
{
using librf_awaitable_handler_base<Executor, std::tuple<Ts...>>::librf_awaitable_handler_base;

template <typename... Args>
void operator()(Args&&... args) const
{
state_->set_value(std::make_tuple(std::forward<Args>(args)...));
}
};

template <typename Executor, typename... Ts>
struct librf_promise_handler<Executor, asio::error_code, Ts...> : public librf_awaitable_handler_base<Executor, std::tuple<Ts...>>
{
using librf_awaitable_handler_base<Executor, std::tuple<Ts...>>::librf_awaitable_handler_base;

template <typename... Args>
void operator()(const asio::error_code& ec, Args&&... args) const
{
if (!ec)
state_->set_value(std::make_tuple(std::forward<Args>(args)...));
else
state_->set_exception(std::make_exception_ptr(asio::system_error(ec)));
}
};

template <typename Executor, typename... Ts>
struct librf_promise_handler<Executor, std::exception_ptr, Ts...> : public librf_awaitable_handler_base<Executor, std::tuple<Ts...>>
{
using librf_awaitable_handler_base<Executor, std::tuple<Ts...>>::librf_awaitable_handler_base;

template <typename... Args>
void operator()(std::exception_ptr ex, Args&&... args) const
{
if (!ex)
state_->set_value(std::make_tuple(std::forward<Args>(args)...));
else
state_->set_exception(ex);
}
};

} // namespace detail

template <typename Executor, typename R, typename... Args>
class async_result<rf_task_t<Executor>, R(Args...)>
{
public:
typedef detail::librf_promise_handler<Executor, Args...> handler_type;
typedef typename handler_type::result_type result_type;
typedef resumef::future_t<result_type> return_type;

template <typename Initiation, typename... InitArgs>
static return_type initiate(ASIO_MOVE_ARG(Initiation) initiation,
rf_task_t<Executor>, ASIO_MOVE_ARG(InitArgs)... args)
{
handler_type handler{};
return_type future{ handler.state_ };

std::move(initiation)(std::move(handler), std::move(args)...);
return std::move(future);
}
};

} // namespace asio

#include "asio/detail/pop_options.hpp"

+ 6
- 6
vs_proj/librf.cpp View File

@@ -1,4 +1,4 @@

#include "librf.h"
extern void resumable_main_yield_return();
@@ -25,12 +25,12 @@ extern void resumable_main_benchmark_asio_client(intptr_t nNum);
int main(int argc, const char * argv[])
{
resumable_main_modern_cb();
//resumable_main_modern_cb();
//resumable_main_benchmark_mem();
//if (argc > 1)
// resumable_main_benchmark_asio_client(atoi(argv[1]));
//else
// resumable_main_benchmark_asio_server();
if (argc > 1)
resumable_main_benchmark_asio_client(atoi(argv[1]));
else
resumable_main_benchmark_asio_server();
return 0;
resumable_main_when_all();

+ 10
- 6
vs_proj/librf.vcxproj View File

@@ -42,13 +42,13 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>NotSet</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>NotSet</CharacterSet>
</PropertyGroup>
@@ -104,9 +104,9 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;ASIO_STANDALONE;RESUMEF_DEBUG_COUNTER=0;RESUMEF_ENABLE_MULT_SCHEDULER=1;RESUMEF_USE_BOOST_ANY=0;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;ASIO_STANDALONE;RESUMEF_DEBUG_COUNTER=0;RESUMEF_ENABLE_MULT_SCHEDULER=1;RESUMEF_USE_BOOST_ANY=0;_SILENCE_CXX17_ALLOCATOR_VOID_DEPRECATION_WARNING=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>..\librf;..\..\asio-1.10.6\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>..\librf;..\..\asio\asio\include;..\..\asio-1.10.6\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/await</AdditionalOptions>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<LanguageStandard>stdcpplatest</LanguageStandard>
@@ -153,8 +153,8 @@
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;ASIO_STANDALONE;RESUMEF_ENABLE_MULT_SCHEDULER=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..\librf;..\..\asio-1.10.6\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;ASIO_STANDALONE;RESUMEF_ENABLE_MULT_SCHEDULER=1;_SILENCE_CXX17_ALLOCATOR_VOID_DEPRECATION_WARNING=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..\librf;..\..\asio\asio\include;..\..\asio-1.10.6\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/await</AdditionalOptions>
<ExceptionHandling>Sync</ExceptionHandling>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
@@ -232,6 +232,10 @@
<ClInclude Include="..\librf\src\when.h" />
<ClInclude Include="..\librf\src\_awaker.h" />
</ItemGroup>
<ItemGroup>
<None Include="..\librf\src\asio_task_1.10.0.inl" />
<None Include="..\librf\src\asio_task_1.12.0.inl" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>

+ 8
- 0
vs_proj/librf.vcxproj.filters View File

@@ -172,4 +172,12 @@
<Filter>librf\src</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\librf\src\asio_task_1.12.0.inl">
<Filter>librf\src</Filter>
</None>
<None Include="..\librf\src\asio_task_1.10.0.inl">
<Filter>librf\src</Filter>
</None>
</ItemGroup>
</Project>

Loading…
Cancel
Save