fix shutdown issue in web server unit test

This commit is contained in:
Arvid Norberg 2009-12-17 08:50:35 +00:00
parent da48f026b6
commit 310a4b8012
1 changed files with 64 additions and 11 deletions

View File

@ -35,6 +35,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/session.hpp" #include "libtorrent/session.hpp"
#include "libtorrent/hasher.hpp" #include "libtorrent/hasher.hpp"
#include "libtorrent/http_parser.hpp" #include "libtorrent/http_parser.hpp"
#include "libtorrent/thread.hpp"
#include "libtorrent/thread.hpp" #include "libtorrent/thread.hpp"
#include <boost/tuple/tuple.hpp> #include <boost/tuple/tuple.hpp>
@ -152,8 +153,9 @@ void start_proxy(int port, int proxy_type)
"SERVER=%s %s" "SERVER=%s %s"
, port, type, auth); , port, type, auth);
fprintf(stderr, "starting delegated proxy...\n");
system(buf); system(buf);
test_sleep(1000); fprintf(stderr, "launched\n");
} }
using namespace libtorrent; using namespace libtorrent;
@ -332,17 +334,19 @@ setup_transfer(session* ses1, session* ses2, session* ses3
} }
socket_acceptor* web_socket = 0; boost::asio::io_service* web_ios = 0;
boost::shared_ptr<libtorrent::thread> web_server; boost::shared_ptr<libtorrent::thread> web_server;
libtorrent::mutex web_lock;
libtorrent::condition web_initialized;
void stop_web_server(int port) void stop_web_server(int port)
{ {
if (web_server && web_socket) if (web_server && web_ios)
{ {
web_socket->close(); web_ios->stop();
web_server->join(); web_server->join();
web_server.reset(); web_server.reset();
web_socket = 0; web_ios = 0;
} }
} }
@ -352,8 +356,19 @@ void start_web_server(int port, bool ssl)
{ {
stop_web_server(port); stop_web_server(port);
{
mutex::scoped_lock l(web_lock);
web_initialized.clear(l);
}
fprintf(stderr, "starting web server on port %d\n", port);
web_server.reset(new libtorrent::thread(boost::bind(&web_server_thread, port, ssl))); web_server.reset(new libtorrent::thread(boost::bind(&web_server_thread, port, ssl)));
{
mutex::scoped_lock l(web_lock);
web_initialized.wait(l);
}
// create this directory so that the path // create this directory so that the path
// "relative/../test_file" can resolve // "relative/../test_file" can resolve
error_code ec; error_code ec;
@ -377,39 +392,71 @@ void send_response(stream_socket& s, error_code& ec
write(s, boost::asio::buffer(msg, pkt_len), boost::asio::transfer_all(), ec); write(s, boost::asio::buffer(msg, pkt_len), boost::asio::transfer_all(), ec);
} }
bool accept_done = false;
void on_accept(error_code const& ec)
{
if (ec)
{
fprintf(stderr, "Error accepting socket: %s\n", ec.message().c_str());
accept_done = false;
}
else
{
fprintf(stderr, "accepting connection\n");
accept_done = true;
}
}
void web_server_thread(int port, bool ssl) void web_server_thread(int port, bool ssl)
{ {
// TODO: support SSL // TODO: support SSL
io_service ios; io_service ios;
socket_acceptor acceptor(ios); socket_acceptor acceptor(ios);
web_socket = &acceptor;
error_code ec; error_code ec;
acceptor.open(tcp::v4(), ec); acceptor.open(tcp::v4(), ec);
if (ec) if (ec)
{ {
fprintf(stderr, "Error opening listen socket: %s\n", ec.message().c_str()); fprintf(stderr, "Error opening listen socket: %s\n", ec.message().c_str());
mutex::scoped_lock l(web_lock);
web_initialized.signal(l);
return; return;
} }
acceptor.set_option(socket_acceptor::reuse_address(true), ec); acceptor.set_option(socket_acceptor::reuse_address(true), ec);
if (ec) if (ec)
{ {
fprintf(stderr, "Error setting listen socket to reuse addr: %s\n", ec.message().c_str()); fprintf(stderr, "Error setting listen socket to reuse addr: %s\n", ec.message().c_str());
mutex::scoped_lock l(web_lock);
web_initialized.signal(l);
return; return;
} }
acceptor.bind(tcp::endpoint(address_v4::any(), port), ec); acceptor.bind(tcp::endpoint(address_v4::any(), port), ec);
if (ec) if (ec)
{ {
fprintf(stderr, "Error binding listen socket: %s\n", ec.message().c_str()); fprintf(stderr, "Error binding listen socket to port %d: %s\n", port, ec.message().c_str());
mutex::scoped_lock l(web_lock);
web_initialized.signal(l);
return; return;
} }
acceptor.listen(10, ec); acceptor.listen(10, ec);
if (ec) if (ec)
{ {
fprintf(stderr, "Error listening on socket: %s\n", ec.message().c_str()); fprintf(stderr, "Error listening on socket: %s\n", ec.message().c_str());
mutex::scoped_lock l(web_lock);
web_initialized.signal(l);
return; return;
} }
web_ios = &ios;
fprintf(stderr, "web server initialized on port %d\n", port);
{
mutex::scoped_lock l(web_lock);
web_initialized.signal(l);
}
char buf[10000]; char buf[10000];
int len = 0; int len = 0;
stream_socket s(ios); stream_socket s(ios);
@ -419,13 +466,18 @@ void web_server_thread(int port, bool ssl)
s.close(ec); s.close(ec);
len = 0; len = 0;
acceptor.accept(s, ec); accept_done = false;
if (ec) acceptor.async_accept(s, &on_accept);
ios.reset();
ios.run_one();
if (!accept_done)
{ {
fprintf(stderr, "accept failed: %s\n", ec.message().c_str()); fprintf(stderr, "accept failed\n");
return; return;
} }
if (!s.is_open()) continue;
http_parser p; http_parser p;
bool failed = false; bool failed = false;
@ -434,7 +486,7 @@ void web_server_thread(int port, bool ssl)
size_t received = s.read_some(boost::asio::buffer(&buf[len], sizeof(buf) - len), ec); size_t received = s.read_some(boost::asio::buffer(&buf[len], sizeof(buf) - len), ec);
if (ec || received <= 0) if (ec || received <= 0)
{ {
fprintf(stderr, "read failed: %s\n", ec.message().c_str()); fprintf(stderr, "read failed: %s received: %d\n", ec.message().c_str(), int(received));
failed = true; failed = true;
break; break;
} }
@ -513,6 +565,7 @@ void web_server_thread(int port, bool ssl)
write(s, boost::asio::buffer(&file_buf[0], file_buf.size()), boost::asio::transfer_all(), ec); write(s, boost::asio::buffer(&file_buf[0], file_buf.size()), boost::asio::transfer_all(), ec);
} }
} }
fprintf(stderr, "exiting web server thread\n");
} }