Remove obsolete SocketFd class.

This commit is contained in:
Jari Sundell
2026-01-26 11:13:03 +01:00
committed by GitHub
parent f4b718f685
commit 2fa7568165
8 changed files with 97 additions and 319 deletions
+51 -43
View File
@@ -1,6 +1,7 @@
#include "config.h"
#include <cassert>
#include <unistd.h>
#include <sys/un.h>
#include <torrent/connection_manager.h>
#include <torrent/torrent.h>
@@ -12,7 +13,6 @@
#include "control.h"
#include "globals.h"
#include "rpc/scgi_task.h"
#include "utils/socket_fd.h"
// TODO: Figure out why moving this to the top causes a build error.
#include "rpc/scgi.h"
@@ -20,30 +20,26 @@
namespace rpc {
SCgi::~SCgi() {
if (!get_fd().is_valid())
return;
for (SCgiTask* itr = m_task, *last = m_task + max_tasks; itr != last; ++itr)
if (itr->is_open())
itr->close();
deactivate();
torrent::connection_manager()->dec_socket_count();
get_fd().close();
get_fd().clear();
if (!m_path.empty())
::unlink(m_path.c_str());
assert(!is_open() && "SCgi::~SCgi() called while open");
}
void
SCgi::open_port(void* sa, unsigned int length, bool dontRoute) {
if (!get_fd().open_stream() ||
(dontRoute && !get_fd().set_dont_route(true)))
SCgi::open_port(sockaddr* sa, unsigned int length, bool dont_route) {
int fd = torrent::fd_open_family(torrent::fd_flag_stream | torrent::fd_flag_nonblock | torrent::fd_flag_reuse_address,
reinterpret_cast<sockaddr*>(sa)->sa_family);
if (fd == -1)
throw torrent::resource_error("Could not open socket for listening: " + std::string(std::strerror(errno)));
open(sa, length);
if (dont_route && !torrent::fd_set_dont_route(fd, true)) {
torrent::fd_close(fd);
throw torrent::resource_error("Could not set socket option IP_DONTROUTE: " + std::string(std::strerror(errno)));
}
set_file_descriptor(fd);
open(reinterpret_cast<sockaddr*>(sa), length);
torrent::connection_manager()->inc_socket_count();
}
void
@@ -51,45 +47,42 @@ SCgi::open_named(const std::string& filename) {
if (filename.empty() || filename.size() > 4096)
throw torrent::resource_error("Invalid filename length.");
auto buffer = std::make_unique<char[]>(sizeof(sockaddr_un) + filename.size());
auto buffer = std::make_unique<char[]>(sizeof(sockaddr_un) + filename.size() + 1);
sockaddr_un* sa = reinterpret_cast<sockaddr_un*>(buffer.get());
#ifdef __sun__
sa->sun_family = AF_UNIX;
#else
sa->sun_family = AF_LOCAL;
#endif
std::memcpy(sa->sun_path, filename.c_str(), filename.size() + 1);
if (!get_fd().open_local())
throw torrent::resource_error("Could not open socket for listening.");
int fd = torrent::fd_open_local(torrent::fd_flag_stream | torrent::fd_flag_nonblock | torrent::fd_flag_reuse_address);
if (fd == -1)
throw torrent::resource_error("Could not open socket for listening: " + std::string(std::strerror(errno)));
set_file_descriptor(fd);
open(reinterpret_cast<sockaddr*>(sa), offsetof(struct sockaddr_un, sun_path) + filename.size() + 1);
torrent::connection_manager()->inc_socket_count();
open(sa, offsetof(struct sockaddr_un, sun_path) + filename.size() + 1);
m_path = filename;
}
void
SCgi::open(void* sa, unsigned int length) {
SCgi::open(sockaddr* sa, unsigned int length) {
try {
if (!get_fd().set_nonblock() ||
!get_fd().set_reuse_address(true) ||
!get_fd().bind_sa(reinterpret_cast<sockaddr*>(sa), length) ||
!get_fd().listen(max_tasks))
if (::bind(file_descriptor(), sa, length) == -1)
throw torrent::resource_error("Could not bind socket for listening: " + std::string(std::strerror(errno)));
if (!torrent::fd_listen(file_descriptor(), max_tasks))
throw torrent::resource_error("Could not prepare socket for listening: " + std::string(std::strerror(errno)));
torrent::connection_manager()->inc_socket_count();
} catch (torrent::resource_error& e) {
get_fd().close();
get_fd().clear();
torrent::fd_close(file_descriptor());
set_file_descriptor(-1);
throw e;
}
}
// TODO: Verify this is run in correct thread, also only ever call poll methods from thread_self.
void
SCgi::activate() {
assert(torrent::this_thread::thread() == scgi_thread::thread());
@@ -100,16 +93,31 @@ SCgi::activate() {
}
void
SCgi::deactivate() {
SCgi::stop() {
assert(torrent::this_thread::thread() == scgi_thread::thread());
if (!is_open())
return;
for (SCgiTask* itr = m_task, *last = m_task + max_tasks; itr != last; ++itr)
if (itr->is_open())
itr->close();
torrent::this_thread::poll()->remove_and_close(this);
torrent::fd_close(file_descriptor());
set_file_descriptor(-1);
torrent::connection_manager()->dec_socket_count();
if (!m_path.empty())
::unlink(m_path.c_str());
}
void
SCgi::event_read() {
while (true) {
int fd = torrent::fd_accept(get_fd().get_fd());
int fd = torrent::fd_accept(file_descriptor());
if (fd == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK)