mirror of
https://github.com/rakshasa/rtorrent.git
synced 2026-10-14 21:26:17 +00:00
Fixed various SCGI issues.
This commit is contained in:
+29
-8
@@ -1,5 +1,6 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
@@ -20,6 +21,12 @@
|
|||||||
|
|
||||||
namespace rpc {
|
namespace rpc {
|
||||||
|
|
||||||
|
SCgi::SCgi() {
|
||||||
|
std::generate(m_tasks.begin(), m_tasks.end(), []() { return std::make_unique<SCgiTask>(); });
|
||||||
|
|
||||||
|
m_current = m_tasks.begin();
|
||||||
|
}
|
||||||
|
|
||||||
SCgi::~SCgi() {
|
SCgi::~SCgi() {
|
||||||
assert(!is_open() && "SCgi::~SCgi() called while open");
|
assert(!is_open() && "SCgi::~SCgi() called while open");
|
||||||
}
|
}
|
||||||
@@ -106,9 +113,10 @@ SCgi::stop() {
|
|||||||
if (!is_open())
|
if (!is_open())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (SCgiTask* itr = m_task, *last = m_task + max_tasks; itr != last; ++itr)
|
for (auto& itr : m_tasks) {
|
||||||
if (itr->is_open())
|
if (itr->is_open())
|
||||||
itr->close();
|
itr->close();
|
||||||
|
}
|
||||||
|
|
||||||
torrent::runtime::socket_manager()->close_event_or_throw(this, [this]() {
|
torrent::runtime::socket_manager()->close_event_or_throw(this, [this]() {
|
||||||
torrent::this_thread::poll()->remove_and_close(this);
|
torrent::this_thread::poll()->remove_and_close(this);
|
||||||
@@ -125,10 +133,19 @@ SCgi::stop() {
|
|||||||
|
|
||||||
void
|
void
|
||||||
SCgi::event_read() {
|
SCgi::event_read() {
|
||||||
while (true) {
|
if (m_current < m_tasks.begin() || m_current >= m_tasks.end())
|
||||||
auto* task = std::find_if(m_task, m_task + max_tasks, std::mem_fn(&SCgiTask::is_available));
|
throw torrent::internal_error("SCgi::event_read() m_current is out of bounds");
|
||||||
|
|
||||||
if (task == m_task + max_tasks) {
|
while (true) {
|
||||||
|
// TODO: Optimize this by keeping track of count.
|
||||||
|
auto prev = m_current;
|
||||||
|
|
||||||
|
m_current = std::find_if(m_current + 1, m_tasks.end(), [](const auto& task) { return !task->is_open(); });
|
||||||
|
|
||||||
|
if (m_current == m_tasks.end())
|
||||||
|
m_current = std::find_if(m_tasks.begin(), prev, [](const auto& task) { return !task->is_open(); });
|
||||||
|
|
||||||
|
if (m_current == prev) {
|
||||||
// TODO: Currently just close, although we should remove ourselves from read.
|
// TODO: Currently just close, although we should remove ourselves from read.
|
||||||
int fd = torrent::fd_accept(file_descriptor());
|
int fd = torrent::fd_accept(file_descriptor());
|
||||||
|
|
||||||
@@ -138,11 +155,15 @@ SCgi::event_read() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto open_func = [this, task]() {
|
auto open_func = [this, task = m_current->get()]() {
|
||||||
int fd = torrent::fd_accept(file_descriptor());
|
int fd = torrent::fd_accept(file_descriptor());
|
||||||
|
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Force a new event_read() call just to be sure we don't enter an infinite loop.
|
||||||
|
if (errno == ECONNABORTED)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
throw torrent::resource_error("Listener port accept() failed: " + std::string(std::strerror(errno)));
|
throw torrent::resource_error("Listener port accept() failed: " + std::string(std::strerror(errno)));
|
||||||
@@ -151,11 +172,11 @@ SCgi::event_read() {
|
|||||||
task->open(this, fd);
|
task->open(this, fd);
|
||||||
};
|
};
|
||||||
|
|
||||||
auto cleanup_func = [task]() {
|
auto cleanup_func = [task = m_current->get()]() {
|
||||||
task->cancel_open();
|
task->cancel_open();
|
||||||
};
|
};
|
||||||
|
|
||||||
bool result = torrent::runtime::socket_manager()->open_event_or_cleanup(task, open_func, cleanup_func);
|
bool result = torrent::runtime::socket_manager()->open_event_or_cleanup(m_current->get(), open_func, cleanup_func);
|
||||||
|
|
||||||
if (!result)
|
if (!result)
|
||||||
break;
|
break;
|
||||||
|
|||||||
+8
-1
@@ -1,7 +1,9 @@
|
|||||||
#ifndef RTORRENT_RPC_SCGI_H
|
#ifndef RTORRENT_RPC_SCGI_H
|
||||||
#define RTORRENT_RPC_SCGI_H
|
#define RTORRENT_RPC_SCGI_H
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
#include <torrent/event.h>
|
#include <torrent/event.h>
|
||||||
|
|
||||||
#include "rpc/scgi_task.h"
|
#include "rpc/scgi_task.h"
|
||||||
@@ -12,6 +14,7 @@ class SCgi : public torrent::Event {
|
|||||||
public:
|
public:
|
||||||
static const int max_tasks = 100;
|
static const int max_tasks = 100;
|
||||||
|
|
||||||
|
SCgi();
|
||||||
~SCgi() override;
|
~SCgi() override;
|
||||||
|
|
||||||
const char* type_name() const override { return "scgi"; }
|
const char* type_name() const override { return "scgi"; }
|
||||||
@@ -33,11 +36,15 @@ public:
|
|||||||
void event_error() override;
|
void event_error() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
using task_list = std::array<std::unique_ptr<SCgiTask>, max_tasks>;
|
||||||
|
|
||||||
void open(sockaddr* sa, unsigned int length);
|
void open(sockaddr* sa, unsigned int length);
|
||||||
|
|
||||||
std::string m_path;
|
std::string m_path;
|
||||||
int m_logFd{-1};
|
int m_logFd{-1};
|
||||||
SCgiTask m_task[max_tasks];
|
|
||||||
|
task_list m_tasks;
|
||||||
|
task_list::iterator m_current;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,12 +43,10 @@ SCgiTask::cancel_open() {
|
|||||||
if (!is_open())
|
if (!is_open())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
torrent::runtime::socket_manager()->close_event_or_throw(this, [this]() {
|
torrent::this_thread::poll()->remove_and_close(this);
|
||||||
torrent::this_thread::poll()->remove_and_close(this);
|
|
||||||
|
|
||||||
torrent::fd_close(file_descriptor());
|
torrent::fd_close(file_descriptor());
|
||||||
set_file_descriptor(-1);
|
set_file_descriptor(-1);
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|||||||
Reference in New Issue
Block a user