Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,12 @@ include_bitcoin_network_config_HEADERS = \
include_bitcoin_network_distributorsdir = ${includedir}/bitcoin/network/distributors
include_bitcoin_network_distributors_HEADERS = \
include/bitcoin/network/distributors/distributor.hpp \
include/bitcoin/network/distributors/distributor_bitcoind.hpp \
include/bitcoin/network/distributors/distributor_electrum.hpp \
include/bitcoin/network/distributors/distributor_http.hpp \
include/bitcoin/network/distributors/distributor_peer.hpp \
include/bitcoin/network/distributors/distributor_rpc.hpp \
include/bitcoin/network/distributors/distributor_stratum_v1.hpp \
include/bitcoin/network/distributors/distributors.hpp

include_bitcoin_network_impl_asyncdir = ${includedir}/bitcoin/network/impl/async
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,12 @@
<ClInclude Include="..\..\..\..\include\bitcoin\network\config\utilities.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\define.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\distributors\distributor.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\distributors\distributor_bitcoind.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\distributors\distributor_electrum.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\distributors\distributor_http.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\distributors\distributor_peer.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\distributors\distributor_rpc.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\distributors\distributor_stratum_v1.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\distributors\distributors.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\error.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\log\capture.hpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,12 @@
<ClInclude Include="..\..\..\..\include\bitcoin\network\distributors\distributor.hpp">
<Filter>include\bitcoin\network\distributors</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\network\distributors\distributor_bitcoind.hpp">
<Filter>include\bitcoin\network\distributors</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\network\distributors\distributor_electrum.hpp">
<Filter>include\bitcoin\network\distributors</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\network\distributors\distributor_http.hpp">
<Filter>include\bitcoin\network\distributors</Filter>
</ClInclude>
Expand All @@ -521,6 +527,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\network\distributors\distributor_rpc.hpp">
<Filter>include\bitcoin\network\distributors</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\network\distributors\distributor_stratum_v1.hpp">
<Filter>include\bitcoin\network\distributors</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\network\distributors\distributors.hpp">
<Filter>include\bitcoin\network\distributors</Filter>
</ClInclude>
Expand Down
3 changes: 3 additions & 0 deletions include/bitcoin/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@
#include <bitcoin/network/config/endpoint.hpp>
#include <bitcoin/network/config/utilities.hpp>
#include <bitcoin/network/distributors/distributor.hpp>
#include <bitcoin/network/distributors/distributor_bitcoind.hpp>
#include <bitcoin/network/distributors/distributor_electrum.hpp>
#include <bitcoin/network/distributors/distributor_http.hpp>
#include <bitcoin/network/distributors/distributor_peer.hpp>
#include <bitcoin/network/distributors/distributor_rpc.hpp>
#include <bitcoin/network/distributors/distributor_stratum_v1.hpp>
#include <bitcoin/network/distributors/distributors.hpp>
#include <bitcoin/network/log/capture.hpp>
#include <bitcoin/network/log/levels.hpp>
Expand Down
3 changes: 2 additions & 1 deletion include/bitcoin/network/async/desubscriber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ template <typename Key, typename... Args>
class desubscriber final
{
public:
DELETE_COPY_MOVE(desubscriber);
DELETE_COPY(desubscriber);
DEFAULT_MOVE(desubscriber);

using key = Key;
typedef std::function<bool(const code&, Args...)> handler;
Expand Down
3 changes: 2 additions & 1 deletion include/bitcoin/network/async/subscriber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ template <typename... Args>
class subscriber final
{
public:
DELETE_COPY_MOVE(subscriber);
DELETE_COPY(subscriber);
DEFAULT_MOVE(subscriber);

typedef std::function<void(const code&, Args...)> handler;

Expand Down
3 changes: 2 additions & 1 deletion include/bitcoin/network/async/unsubscriber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ template <typename... Args>
class unsubscriber final
{
public:
DELETE_COPY_MOVE(unsubscriber);
DELETE_COPY(unsubscriber);
DEFAULT_MOVE(unsubscriber);

typedef std::function<bool(const code&, Args...)> handler;

Expand Down
58 changes: 58 additions & 0 deletions include/bitcoin/network/distributors/distributor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,69 @@
#ifndef LIBBITCOIN_NETWORK_DISTRIBUTORS_DISTRIBUTOR_HPP
#define LIBBITCOIN_NETWORK_DISTRIBUTORS_DISTRIBUTOR_HPP

#include <algorithm>
#include <tuple>
#include <bitcoin/network/define.hpp>

// TODO: move this.

namespace libbitcoin {
namespace network {
namespace rpc {

BC_PUSH_WARNING(NO_UNSAFE_COPY_N)
BC_PUSH_WARNING(NO_ARRAY_TO_POINTER_DECAY)

enum class group { positional, named, either };

/// Non-type template parameter (NTTP) dynamically defines a name for type.
template <size_t Length>
struct method_name
{
constexpr method_name(const char (&text)[Length])
{
std::copy_n(text, Length, name);
}

char name[Length]{};
static constexpr auto length = sub1(Length);
};

template <method_name Unique, typename... Args>
struct method
{
static constexpr std::string_view name{ Unique.name, Unique.length };

using tag = method;
using args = std::tuple<Args...>;
using names_t = std::array<std::string_view, sizeof...(Args)>;

/// Required for construction of tag{}.
constexpr method() NOEXCEPT
: names_{}
{
}

template <typename ...ParameterNames,
if_equal<sizeof...(ParameterNames), sizeof...(Args)> = true>
constexpr method(ParameterNames&&... names) NOEXCEPT
: names_{ std::forward<ParameterNames>(names)... }
{
}

constexpr const names_t& names() const NOEXCEPT
{
return names_;
}

private:
const names_t names_;
};

BC_POP_WARNING()
BC_POP_WARNING()

} // namespace rpc
} // namespace network
} // namespace libbitcoin

Expand Down
44 changes: 44 additions & 0 deletions include/bitcoin/network/distributors/distributor_bitcoind.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_NETWORK_DISTRIBUTORS_DISTRIBUTOR_BITCOIND_HPP
#define LIBBITCOIN_NETWORK_DISTRIBUTORS_DISTRIBUTOR_BITCOIND_HPP

#include <bitcoin/network/define.hpp>
#include <bitcoin/network/distributors/distributor.hpp>

namespace libbitcoin {
namespace network {

struct bitcoind
{
static constexpr std::tuple methods
{
rpc::method<"get_version">{},
rpc::method<"add_element", int, int>{ "a", "b" },
};

using type = decltype(methods);
static constexpr auto size = std::tuple_size_v<type>;
static constexpr rpc::group mode = rpc::group::either;
};

} // namespace network
} // namespace libbitcoin

#endif
30 changes: 30 additions & 0 deletions include/bitcoin/network/distributors/distributor_electrum.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_NETWORK_DISTRIBUTORS_DISTRIBUTOR_ELECTRUM_HPP
#define LIBBITCOIN_NETWORK_DISTRIBUTORS_DISTRIBUTOR_ELECTRUM_HPP

#include <bitcoin/network/define.hpp>

namespace libbitcoin {
namespace network {

} // namespace network
} // namespace libbitcoin

#endif
54 changes: 14 additions & 40 deletions include/bitcoin/network/distributors/distributor_http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

#include <utility>
#include <bitcoin/network/async/async.hpp>
#include <bitcoin/network/distributors/distributor.hpp>
#include <bitcoin/network/define.hpp>
#include <bitcoin/network/messages/http/http.hpp>

Expand All @@ -30,13 +29,11 @@ namespace network {

#define SUBSCRIBER(name) name##_subscriber_
#define SUBSCRIBER_TYPE(name) name##_subscriber
#define DECLARE_SUBSCRIBER(name) SUBSCRIBER_TYPE(name) SUBSCRIBER(name)
#define HANDLER(name) distributor_http::handler<http::method::name>
#define DEFINE_SUBSCRIBER(name) \
using SUBSCRIBER_TYPE(name) = subscriber<const http::method::name&>
#define SUBSCRIBER_OVERLOAD(name) \
inline code do_subscribe(HANDLER(name)&& handler) NOEXCEPT \
{ return SUBSCRIBER(name).subscribe(std::forward<HANDLER(name)>(handler)); }
#define DECLARE_SUBSCRIBER(name) \
using SUBSCRIBER_TYPE(name) = subscriber<const http::method::name&>; \
SUBSCRIBER_TYPE(name) SUBSCRIBER(name); \
inline code do_subscribe(SUBSCRIBER_TYPE(name)::handler&& handler) NOEXCEPT \
{ return SUBSCRIBER(name).subscribe(std::move(handler)); }

/// Not thread safe.
class BCT_API distributor_http
Expand All @@ -48,16 +45,6 @@ class BCT_API distributor_http

DELETE_COPY_MOVE_DESTRUCT(distributor_http);

DEFINE_SUBSCRIBER(get);
DEFINE_SUBSCRIBER(head);
DEFINE_SUBSCRIBER(post);
DEFINE_SUBSCRIBER(put);
DEFINE_SUBSCRIBER(delete_);
DEFINE_SUBSCRIBER(trace);
DEFINE_SUBSCRIBER(options);
DEFINE_SUBSCRIBER(connect);
DEFINE_SUBSCRIBER(unknown);

/// Create an instance of this class.
distributor_http(asio::strand& strand) NOEXCEPT;

Expand Down Expand Up @@ -88,34 +75,21 @@ class BCT_API distributor_http
subscriber.notify(ec, method);
}

SUBSCRIBER_OVERLOAD(get);
SUBSCRIBER_OVERLOAD(head);
SUBSCRIBER_OVERLOAD(post);
SUBSCRIBER_OVERLOAD(put);
SUBSCRIBER_OVERLOAD(delete_);
SUBSCRIBER_OVERLOAD(trace);
SUBSCRIBER_OVERLOAD(options);
SUBSCRIBER_OVERLOAD(connect);
SUBSCRIBER_OVERLOAD(unknown);

// These are thread safe.
DECLARE_SUBSCRIBER(get);
DECLARE_SUBSCRIBER(head);
DECLARE_SUBSCRIBER(post);
DECLARE_SUBSCRIBER(put);
DECLARE_SUBSCRIBER(delete_);
DECLARE_SUBSCRIBER(trace);
DECLARE_SUBSCRIBER(options);
DECLARE_SUBSCRIBER(connect);
DECLARE_SUBSCRIBER(unknown);
DECLARE_SUBSCRIBER(get)
DECLARE_SUBSCRIBER(head)
DECLARE_SUBSCRIBER(post)
DECLARE_SUBSCRIBER(put)
DECLARE_SUBSCRIBER(delete_)
DECLARE_SUBSCRIBER(trace)
DECLARE_SUBSCRIBER(options)
DECLARE_SUBSCRIBER(connect)
DECLARE_SUBSCRIBER(unknown)
};

#undef SUBSCRIBER
#undef SUBSCRIBER_TYPE
#undef DECLARE_SUBSCRIBER
#undef HANDLER
#undef DEFINE_SUBSCRIBER
#undef SUBSCRIBER_OVERLOAD

} // namespace network
} // namespace libbitcoin
Expand Down
Loading
Loading