diff --git a/contrib/epee/include/net/levin_client.h b/contrib/epee/include/net/levin_client.h index 335f6ba0..e2fb58b7 100644 --- a/contrib/epee/include/net/levin_client.h +++ b/contrib/epee/include/net/levin_client.h @@ -46,11 +46,12 @@ namespace levin /************************************************************************/ /* */ /************************************************************************/ - class levin_client_impl + template + class levin_client_impl_t { public: - levin_client_impl(); - virtual ~levin_client_impl(); + levin_client_impl_t(); + virtual ~levin_client_impl_t(); bool connect(u_long ip, int port, unsigned int timeout, const std::string& bind_ip = "0.0.0.0"); bool connect(const std::string& addr, int port, unsigned int timeout, const std::string& bind_ip = "0.0.0.0"); @@ -61,14 +62,16 @@ namespace levin virtual int notify(int command, const std::string& in_buff); protected: - net_utils::blocked_mode_client m_transport; + transport_t m_transport; + //net_utils::blocked_mode_client m_transport; }; /************************************************************************/ /* */ /************************************************************************/ - class levin_client_impl2: public levin_client_impl + template + class levin_client_impl2: public levin_client_impl_t { public: @@ -79,8 +82,8 @@ namespace levin } namespace net_utils { - typedef levin::levin_client_impl levin_client; - typedef levin::levin_client_impl2 levin_client2; + typedef levin::levin_client_impl_t levin_client; + typedef levin::levin_client_impl2 levin_client2; } } diff --git a/contrib/epee/include/net/levin_client.inl b/contrib/epee/include/net/levin_client.inl index 6f7c473e..8d69a939 100644 --- a/contrib/epee/include/net/levin_client.inl +++ b/contrib/epee/include/net/levin_client.inl @@ -32,175 +32,174 @@ #include "string_tools.h" namespace epee { -namespace levin -{ -inline -bool levin_client_impl::connect(u_long ip, int port, unsigned int timeout, const std::string& bind_ip) -{ - return m_transport.connect(string_tools::get_ip_string_from_int32(ip), port, timeout, timeout, bind_ip); -} -//------------------------------------------------------------------------------ -inline - bool levin_client_impl::connect(const std::string& addr, int port, unsigned int timeout, const std::string& bind_ip) -{ - return m_transport.connect(addr, port, timeout, timeout, bind_ip); -} -//------------------------------------------------------------------------------ -inline -bool levin_client_impl::is_connected() -{ - return m_transport.is_connected(); -} -//------------------------------------------------------------------------------ -inline -bool levin_client_impl::disconnect() -{ - return m_transport.disconnect(); -} -//------------------------------------------------------------------------------ -inline -levin_client_impl::levin_client_impl() -{ -} -//------------------------------------------------------------------------------ -inline -levin_client_impl::~levin_client_impl() -{ - disconnect(); -} -//------------------------------------------------------------------------------ -inline -int levin_client_impl::invoke(int command, const std::string& in_buff, std::string& buff_out) -{ - if(!is_connected()) - return -1; - - bucket_head head = {0}; - head.m_signature = LEVIN_SIGNATURE; - head.m_cb = in_buff.size(); - head.m_have_to_return_data = true; - head.m_command = command; - if(!m_transport.send(&head, sizeof(head))) - return -1; - - if(!m_transport.send(in_buff)) - return -1; - - std::string local_buff; - if(!m_transport.recv_n(local_buff, sizeof(bucket_head))) - return -1; - - head = *(bucket_head*)local_buff.data(); - - - if(head.m_signature!=LEVIN_SIGNATURE) - { - LOG_PRINT_L0("Signature missmatch in response"); - return -1; - } - - if(!m_transport.recv_n(buff_out, head.m_cb)) - return -1; - - return head.m_return_code; -} -//------------------------------------------------------------------------------ -inline -int levin_client_impl::notify(int command, const std::string& in_buff) -{ - if(!is_connected()) - return -1; - - bucket_head head = {0}; - head.m_signature = LEVIN_SIGNATURE; - head.m_cb = in_buff.size(); - head.m_have_to_return_data = false; - head.m_command = command; - - if(!m_transport.send((const char*)&head, sizeof(head))) - return -1; - - if(!m_transport.send(in_buff)) - return -1; - - return 1; -} - -//------------------------------------------------------------------------------ -//------------------------------------------------------------------------------ -inline - int levin_client_impl2::invoke(int command, const std::string& in_buff, std::string& buff_out) -{ - if(!is_connected()) - return -1; - - bucket_head2 head = {0}; - head.m_signature = LEVIN_SIGNATURE; - head.m_cb = in_buff.size(); - head.m_have_to_return_data = true; - head.m_command = static_cast(command); - head.m_protocol_version = LEVIN_PROTOCOL_VER_1; - head.m_flags = LEVIN_PACKET_REQUEST; - if(!m_transport.send(&head, sizeof(head))) - return -1; - - if(!m_transport.send(in_buff)) - return -1; - - //Since other side of connection could be running by async server, - //we can receive some unexpected notify(forwarded broadcast notifications for example). - //let's ignore every notify in the channel until we get invoke response - std::string local_buff; - - while (true) + namespace levin { - if (!m_transport.recv_n(local_buff, sizeof(bucket_head2))) - return LEVIN_ERROR_NET_ERROR; - head = *(bucket_head2*)local_buff.data(); - if (head.m_signature != LEVIN_SIGNATURE) + template + bool levin_client_impl_t::connect(u_long ip, int port, unsigned int timeout, const std::string& bind_ip) { - LOG_PRINT_L0("Signature missmatch in response"); - return LEVIN_ERROR_SIGNATURE_MISMATCH; + return m_transport.connect(string_tools::get_ip_string_from_int32(ip), port, timeout, timeout, bind_ip); } - if (!m_transport.recv_n(buff_out, head.m_cb)) - return LEVIN_ERROR_NET_ERROR; - - //now check if this is response to invoke (and extra validate if it's response to this(!) invoke) - if (head.m_flags&LEVIN_PACKET_RESPONSE) + //------------------------------------------------------------------------------ + template + bool levin_client_impl_t::connect(const std::string& addr, int port, unsigned int timeout, const std::string& bind_ip) { - //we got response, extra validate if its response to our request - CHECK_AND_ASSERT_MES(head.m_command == static_cast(command), LEVIN_ERROR_PROTOCOL_INCONSISTENT, "command id missmatch in response: " << head.m_command << ", expected: " << command); + return m_transport.connect(addr, port, timeout, timeout, bind_ip); + } + //------------------------------------------------------------------------------ + template + bool levin_client_impl_t::is_connected() + { + return m_transport.is_connected(); + } + //------------------------------------------------------------------------------ + template + bool levin_client_impl_t::disconnect() + { + return m_transport.disconnect(); + } + //------------------------------------------------------------------------------ + template + levin_client_impl_t::levin_client_impl_t() + { + } + //------------------------------------------------------------------------------ + template + levin_client_impl_t::~levin_client_impl_t() + { + disconnect(); + } + //------------------------------------------------------------------------------ + template + int levin_client_impl_t::invoke(int command, const std::string& in_buff, std::string& buff_out) + { + if (!is_connected()) + return -1; + + bucket_head head = { 0 }; + head.m_signature = LEVIN_SIGNATURE; + head.m_cb = in_buff.size(); + head.m_have_to_return_data = true; + head.m_command = command; + if (!m_transport.send(&head, sizeof(head))) + return -1; + + if (!m_transport.send(in_buff)) + return -1; + + std::string local_buff; + if (!m_transport.recv_n(local_buff, sizeof(bucket_head))) + return -1; + + head = *(bucket_head*)local_buff.data(); + + + if (head.m_signature != LEVIN_SIGNATURE) + { + LOG_PRINT_L0("Signature missmatch in response"); + return -1; + } + + if (!m_transport.recv_n(buff_out, head.m_cb)) + return -1; + return head.m_return_code; } + //------------------------------------------------------------------------------ + template + int levin_client_impl_t::notify(int command, const std::string& in_buff) + { + if (!is_connected()) + return -1; + + bucket_head head = { 0 }; + head.m_signature = LEVIN_SIGNATURE; + head.m_cb = in_buff.size(); + head.m_have_to_return_data = false; + head.m_command = command; + + if (!m_transport.send((const char*)&head, sizeof(head))) + return -1; + + if (!m_transport.send(in_buff)) + return -1; + + return 1; + } + //------------------------------------------------------------------------------ + template + int levin_client_impl2::invoke(int command, const std::string& in_buff, std::string& buff_out) + { + if (!is_connected()) + return -1; + + bucket_head2 head = { 0 }; + head.m_signature = LEVIN_SIGNATURE; + head.m_cb = in_buff.size(); + head.m_have_to_return_data = true; + head.m_command = static_cast(command); + head.m_protocol_version = LEVIN_PROTOCOL_VER_1; + head.m_flags = LEVIN_PACKET_REQUEST; + if (!m_transport.send(&head, sizeof(head))) + return -1; + + if (!m_transport.send(in_buff)) + return -1; + + //Since other side of connection could be running by async server, + //we can receive some unexpected notify(forwarded broadcast notifications for example). + //let's ignore every notify in the channel until we get invoke response + std::string local_buff; + + while (true) + { + if (!m_transport.recv_n(local_buff, sizeof(bucket_head2))) + return LEVIN_ERROR_NET_ERROR; + + head = *(bucket_head2*)local_buff.data(); + if (head.m_signature != LEVIN_SIGNATURE) + { + LOG_PRINT_L0("Signature missmatch in response"); + return LEVIN_ERROR_SIGNATURE_MISMATCH; + } + if (!m_transport.recv_n(buff_out, head.m_cb)) + return LEVIN_ERROR_NET_ERROR; + + //now check if this is response to invoke (and extra validate if it's response to this(!) invoke) + if (head.m_flags&LEVIN_PACKET_RESPONSE) + { + //we got response, extra validate if its response to our request + CHECK_AND_ASSERT_MES(head.m_command == static_cast(command), LEVIN_ERROR_PROTOCOL_INCONSISTENT, "command id missmatch in response: " << head.m_command << ", expected: " << command); + return head.m_return_code; + } + } + //never comes here + return LEVIN_ERROR_INTERNAL; + } + //------------------------------------------------------------------------------ + template + int levin_client_impl2::notify(int command, const std::string& in_buff) + { + if (!is_connected()) + return -1; + + bucket_head2 head = { 0 }; + head.m_signature = LEVIN_SIGNATURE; + head.m_cb = in_buff.size(); + head.m_have_to_return_data = false; + head.m_command = command; + head.m_protocol_version = LEVIN_PROTOCOL_VER_1; + head.m_flags = LEVIN_PACKET_REQUEST; + + if (!m_transport.send((const char*)&head, sizeof(head))) + return -1; + + if (!m_transport.send(in_buff)) + return -1; + + return 1; + } + } - //never comes here - return LEVIN_ERROR_INTERNAL; -} -//------------------------------------------------------------------------------ -inline - int levin_client_impl2::notify(int command, const std::string& in_buff) -{ - if(!is_connected()) - return -1; - - bucket_head2 head = {0}; - head.m_signature = LEVIN_SIGNATURE; - head.m_cb = in_buff.size(); - head.m_have_to_return_data = false; - head.m_command = command; - head.m_protocol_version = LEVIN_PROTOCOL_VER_1; - head.m_flags = LEVIN_PACKET_REQUEST; - - if(!m_transport.send((const char*)&head, sizeof(head))) - return -1; - - if(!m_transport.send(in_buff)) - return -1; - - return 1; -} - -} } //------------------------------------------------------------------------------ \ No newline at end of file diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 466a707e..b43f9608 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -4591,7 +4591,7 @@ void wallet2::send_transaction_to_network(const transaction& tx) #define ENABLE_TOR_RELAY #ifdef ENABLE_TOR_RELAY //TODO check that core synchronized - epee::levin::levin_client_impl p2p_client; + epee::net_utils::levin_client2 p2p_client; if (!p2p_client.connect("127.0.0.1", P2P_DEFAULT_PORT, 100000)) { THROW_IF_FALSE_WALLET_EX(false, error::no_connection_to_daemon, "Failed to connect to TOR node");