summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorevoskuil <eric@voskuil.org>2014-04-08 19:26:11 (GMT)
committer evoskuil <eric@voskuil.org>2014-04-08 19:26:11 (GMT)
commitd3e58705d11e773d27cc8edfff83544271d81852 (patch)
tree97576182b5b8e09b3e11551e3b2d60f5043ae224
parent8e843a3e40ac94c71ec4b8a72c95ce036c104193 (diff)
parent116f4ffe87dd0cfd9795873e81d3d3f0f8b95709 (diff)
Merge branch 'spesmilo/master'
-rw-r--r--include/bitcoin/Makefile.am1
-rw-r--r--include/bitcoin/define.hpp63
-rw-r--r--include/bitcoin/network/protocol.hpp3
-rw-r--r--src/network/protocol.cpp40
4 files changed, 99 insertions, 8 deletions
diff --git a/include/bitcoin/Makefile.am b/include/bitcoin/Makefile.am
index fb10876..85fb7f2 100644
--- a/include/bitcoin/Makefile.am
+++ b/include/bitcoin/Makefile.am
@@ -1,5 +1,6 @@
bitcoin_includedir = $(includedir)/bitcoin
bitcoin_include_HEADERS = \
+ define.hpp \
transaction_indexer.hpp \
satoshi_serialize.hpp \
getx_responder.hpp \
diff --git a/include/bitcoin/define.hpp b/include/bitcoin/define.hpp
new file mode 100644
index 0000000..1d506a7
--- /dev/null
+++ b/include/bitcoin/define.hpp
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2011-2013 libbitcoin developers (see AUTHORS)
+ *
+ * This file is part of libbitcoin.
+ *
+ * libbitcoin is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License with
+ * additional permissions to the one published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option)
+ * any later version. For more information see LICENSE.
+ *
+ * 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_DEFINE_HPP
+#define LIBBITCOIN_DEFINE_HPP
+
+// See http://gcc.gnu.org/wiki/Visibility
+
+// Generic helper definitions for shared library support
+#if defined _WIN32 || defined __CYGWIN__
+ #define BC_HELPER_DLL_IMPORT __declspec(dllimport)
+ #define BC_HELPER_DLL_EXPORT __declspec(dllexport)
+ #define BC_HELPER_DLL_LOCAL
+#else
+ #if __GNUC__ >= 4
+ #define BC_HELPER_DLL_IMPORT __attribute__ ((visibility ("default")))
+ #define BC_HELPER_DLL_EXPORT __attribute__ ((visibility ("default")))
+ #define BC_HELPER_DLL_LOCAL __attribute__ ((visibility ("internal")))
+ #else
+ #define BC_HELPER_DLL_IMPORT
+ #define BC_HELPER_DLL_EXPORT
+ #define BC_HELPER_DLL_LOCAL
+ #endif
+#endif
+
+// Now we use the generic helper definitions above to
+// define BC_API and BC_INTERNAL.
+// BC_API is used for the public API symbols. It either DLL imports or
+// DLL exports (or does nothing for static build)
+// BC_INTERNAL is used for non-api symbols.
+
+#ifdef BC_DLL
+ #ifdef BC_DLL_EXPORTS
+ // Being compiled as a DLL (instead of using it)
+ #define BC_API BC_HELPER_DLL_EXPORT
+ #else
+ #define BC_API BC_HELPER_DLL_IMPORT
+ #endif
+ #define BC_INTERNAL BC_HELPER_DLL_LOCAL
+#else
+ // BC_DLL is not defined: this means this is a static lib.
+ #define BC_API
+ #define BC_INTERNAL
+#endif
+
+#endif
+
diff --git a/include/bitcoin/network/protocol.hpp b/include/bitcoin/network/protocol.hpp
index edb87f5..dd9cad0 100644
--- a/include/bitcoin/network/protocol.hpp
+++ b/include/bitcoin/network/protocol.hpp
@@ -244,6 +244,9 @@ private:
std::shared_ptr<seeds> load_seeds_;
friend class seeds;
+ std::string state_to_string(connect_state state);
+ void modify_slot(slot_index slot, connect_state state);
+
// run loop
void start_connecting();
// Connect outwards
diff --git a/src/network/protocol.cpp b/src/network/protocol.cpp
index 6f4ddb6..8b2923f 100644
--- a/src/network/protocol.cpp
+++ b/src/network/protocol.cpp
@@ -263,6 +263,30 @@ void protocol::seeds::handle_store(const std::error_code& ec)
<< ec.message();
}
+std::string protocol::state_to_string(connect_state state)
+{
+ switch (state)
+ {
+ case connect_state::finding_peer:
+ return "Finding peer";
+ case connect_state::connecting:
+ return "Connecting to peer";
+ case connect_state::established:
+ return "Established connection";
+ case connect_state::stopped:
+ return "Stopped";
+ }
+ // Unhandled state!
+ BITCOIN_ASSERT(false);
+ return std::string();
+}
+void protocol::modify_slot(slot_index slot, connect_state state)
+{
+ connect_states_[slot] = state;
+ log_debug(LOG_PROTOCOL) << "Outbound connection " << slot
+ << ": " << state_to_string(state) << ".";
+}
+
void protocol::run()
{
strand_.queue(&protocol::start_connecting, this);
@@ -277,7 +301,7 @@ void protocol::start_connecting()
BITCOIN_ASSERT(connect_states_.empty());
connect_states_.resize(max_outbound_);
for (slot_index slot = 0; slot < max_outbound_; ++slot)
- connect_states_[slot] = connect_state::stopped;
+ modify_slot(slot, connect_state::stopped);
// Start the main outbound connect loop.
start_stopped_connects();
start_watermark_reset_timer();
@@ -297,7 +321,7 @@ void protocol::try_connect_once(slot_index slot)
BITCOIN_ASSERT(connect_states_[slot] == connect_state::stopped);
// Begin connection flow: finding_peer -> connecting -> established.
// Failures end with connect_state::stopped and loop back here again.
- connect_states_[slot] = connect_state::finding_peer;
+ modify_slot(slot, connect_state::finding_peer);
hosts_.fetch_address(strand_.wrap(
&protocol::attempt_connect, this, _1, _2, slot));
}
@@ -346,7 +370,7 @@ void protocol::attempt_connect(const std::error_code& ec,
const network_address_type& address, slot_index slot)
{
BITCOIN_ASSERT(connect_states_[slot] == connect_state::finding_peer);
- connect_states_[slot] = connect_state::connecting;
+ modify_slot(slot, connect_state::connecting);
if (ec)
{
log_error(LOG_PROTOCOL)
@@ -359,7 +383,7 @@ void protocol::attempt_connect(const std::error_code& ec,
<< "Already connected to " << encode_hex(address.ip);
// Retry another connection
// Still in same strand.
- connect_states_[slot] = connect_state::stopped;
+ modify_slot(slot, connect_state::stopped);
try_connect_once(slot);
return;
}
@@ -373,8 +397,6 @@ void protocol::handle_connect(
const network_address_type& address, slot_index slot)
{
BITCOIN_ASSERT(connect_states_[slot] == connect_state::connecting);
- connect_states_[slot] = connect_state::established;
- BITCOIN_ASSERT(connections_.size() <= max_outbound_);
if (ec)
{
log_warning(LOG_PROTOCOL) << "Unable to connect to "
@@ -382,10 +404,12 @@ void protocol::handle_connect(
<< " - " << ec.message();
// Retry another connection
// Still in same strand.
- connect_states_[slot] = connect_state::stopped;
+ modify_slot(slot, connect_state::stopped);
try_connect_once(slot);
return;
}
+ modify_slot(slot, connect_state::established);
+ BITCOIN_ASSERT(connections_.size() <= max_outbound_);
connections_.push_back({address, node});
log_info(LOG_PROTOCOL) << "Connected to "
<< pretty(address.ip) << ":" << address.port
@@ -501,7 +525,7 @@ void protocol::outbound_channel_stopped(
// And then attempt a reconnection.
remove_connection(connections_, which_node);
BITCOIN_ASSERT(connect_states_[slot] == connect_state::established);
- connect_states_[slot] = connect_state::stopped;
+ modify_slot(slot, connect_state::stopped);
// Attempt a reconnection.
// Recreate 1 new connection always.
// Still in same strand.