From f5f7f04e1e3caeaf2e1b4ade982afe2997146eba Mon Sep 17 00:00:00 2001 From: Marcos Pinto Date: Sun, 18 Nov 2007 04:13:41 +0000 Subject: [PATCH] fix lsd --- .../include/libtorrent/http_connection.hpp | 2 + .../libtorrent/http_tracker_connection.hpp | 3 + libtorrent/src/http_connection.cpp | 62 +++++++------------ libtorrent/src/lsd.cpp | 2 + src/deluge_core.cpp | 5 +- 5 files changed, 33 insertions(+), 41 deletions(-) diff --git a/libtorrent/include/libtorrent/http_connection.hpp b/libtorrent/include/libtorrent/http_connection.hpp index 4a70a902c..9d32af2e9 100644 --- a/libtorrent/include/libtorrent/http_connection.hpp +++ b/libtorrent/include/libtorrent/http_connection.hpp @@ -115,6 +115,8 @@ private: , asio::error_code const& e); void on_assign_bandwidth(asio::error_code const& e); + void callback(asio::error_code const& e, char const* data = 0, int size = 0); + std::vector m_recvbuffer; tcp::socket m_sock; int m_read_pos; diff --git a/libtorrent/include/libtorrent/http_tracker_connection.hpp b/libtorrent/include/libtorrent/http_tracker_connection.hpp index c0057dfa1..41df4c953 100755 --- a/libtorrent/include/libtorrent/http_tracker_connection.hpp +++ b/libtorrent/include/libtorrent/http_tracker_connection.hpp @@ -91,6 +91,9 @@ namespace libtorrent int content_length() const { return m_content_length; } void reset(); + + std::map const& headers() const { return m_header; } + private: int m_recv_pos; int m_status_code; diff --git a/libtorrent/src/http_connection.cpp b/libtorrent/src/http_connection.cpp index c96bb05dd..a0d8e3fa3 100644 --- a/libtorrent/src/http_connection.cpp +++ b/libtorrent/src/http_connection.cpp @@ -96,9 +96,7 @@ void http_connection::on_connect_timeout() if (m_connection_ticket > -1) m_cc.done(m_connection_ticket); m_connection_ticket = -1; - if (m_bottled && m_called) return; - m_called = true; - m_handler(asio::error::timed_out, m_parser, 0, 0); + callback(asio::error::timed_out); close(); } @@ -112,12 +110,10 @@ void http_connection::on_timeout(boost::weak_ptr p if (e == asio::error::operation_aborted) return; - if (c->m_bottled && c->m_called) return; - if (c->m_last_receive + c->m_timeout < time_now()) { - c->m_called = true; - c->m_handler(asio::error::timed_out, c->m_parser, 0, 0); + c->callback(asio::error::timed_out); + c->close(); return; } @@ -138,9 +134,7 @@ void http_connection::close() if (m_connection_ticket > -1) m_cc.done(m_connection_ticket); m_connection_ticket = -1; - if (m_called) return; - m_called = true; - m_handler(asio::error::operation_aborted, m_parser, 0, 0); + m_handler.clear(); } void http_connection::on_resolve(asio::error_code const& e @@ -148,10 +142,8 @@ void http_connection::on_resolve(asio::error_code const& e { if (e) { + callback(e); close(); - if (m_bottled && m_called) return; - m_called = true; - m_handler(e, m_parser, 0, 0); return; } TORRENT_ASSERT(i != tcp::resolver::iterator()); @@ -187,10 +179,17 @@ void http_connection::on_connect(asio::error_code const& e } */ else { + callback(e); close(); - if (m_bottled && m_called) return; + } +} + +void http_connection::callback(asio::error_code const& e, char const* data, int size) +{ + if (!m_bottled || !m_called) + { m_called = true; - m_handler(e, m_parser, 0, 0); + if (m_handler) m_handler(e, m_parser, data, size); } } @@ -198,10 +197,8 @@ void http_connection::on_write(asio::error_code const& e) { if (e) { + callback(e); close(); - if (m_bottled && m_called) return; - m_called = true; - m_handler(e, m_parser, 0, 0); return; } @@ -236,9 +233,6 @@ void http_connection::on_read(asio::error_code const& e if (e == asio::error::eof) { - close(); - if (m_bottled && m_called) return; - m_called = true; char const* data = 0; std::size_t size = 0; if (m_bottled && m_parser.header_finished()) @@ -246,16 +240,15 @@ void http_connection::on_read(asio::error_code const& e data = m_parser.get_body().begin; size = m_parser.get_body().left(); } - m_handler(e, m_parser, data, size); + callback(e, data, size); + close(); return; } if (e) { + callback(e); close(); - if (m_bottled && m_called) return; - m_called = true; - m_handler(e, m_parser, 0, 0); return; } @@ -273,9 +266,7 @@ void http_connection::on_read(asio::error_code const& e if (url.empty()) { // missing location header - if (m_bottled && m_called) return; - m_called = true; - m_handler(e, m_parser, 0, 0); + callback(e); return; } @@ -297,7 +288,7 @@ void http_connection::on_read(asio::error_code const& e if (!m_bottled && m_parser.header_finished()) { if (m_read_pos > m_parser.body_start()) - m_handler(e, m_parser, &m_recvbuffer[0] + m_parser.body_start() + callback(e, &m_recvbuffer[0] + m_parser.body_start() , m_read_pos - m_parser.body_start()); m_read_pos = 0; m_last_receive = time_now(); @@ -305,15 +296,13 @@ void http_connection::on_read(asio::error_code const& e else if (m_bottled && m_parser.finished()) { m_timer.cancel(); - if (m_bottled && m_called) return; - m_called = true; - m_handler(e, m_parser, m_parser.get_body().begin, m_parser.get_body().left()); + callback(e, m_parser.get_body().begin, m_parser.get_body().left()); } } else { TORRENT_ASSERT(!m_bottled); - m_handler(e, m_parser, &m_recvbuffer[0], m_read_pos); + callback(e, &m_recvbuffer[0], m_read_pos); m_read_pos = 0; m_last_receive = time_now(); } @@ -322,10 +311,8 @@ void http_connection::on_read(asio::error_code const& e m_recvbuffer.resize((std::min)(m_read_pos + 2048, int(max_bottled_buffer))); if (m_read_pos == max_bottled_buffer) { + callback(asio::error::eof); close(); - if (m_bottled && m_called) return; - m_called = true; - m_handler(asio::error::eof, m_parser, 0, 0); return; } int amount_to_read = m_recvbuffer.size() - m_read_pos; @@ -351,8 +338,7 @@ void http_connection::on_assign_bandwidth(asio::error_code const& e) && m_limiter_timer_active) || !m_sock.is_open()) { - if (!m_bottled || !m_called) - m_handler(e, m_parser, 0, 0); + callback(asio::error::eof); return; } m_limiter_timer_active = false; diff --git a/libtorrent/src/lsd.cpp b/libtorrent/src/lsd.cpp index 6e1dcb7b3..06e570f3c 100644 --- a/libtorrent/src/lsd.cpp +++ b/libtorrent/src/lsd.cpp @@ -185,5 +185,7 @@ void lsd::close() { m_socket.close(); m_broadcast_timer.cancel(); + m_disabled = true; + m_callback.clear(); } diff --git a/src/deluge_core.cpp b/src/deluge_core.cpp index e8ee17c51..1be980715 100644 --- a/src/deluge_core.cpp +++ b/src/deluge_core.cpp @@ -1642,15 +1642,14 @@ static PyObject *torrent_use_lsd(PyObject *self, PyObject *args) python_long action; PyArg_ParseTuple(args, "i", &action); - /* if (action){ + if (action){ M_ses->start_lsd(); } else{ M_ses->stop_lsd(); } -*/ - Py_INCREF(Py_None); return Py_None; + Py_INCREF(Py_None); return Py_None; } static PyObject *torrent_use_natpmp(PyObject *self, PyObject *args)