Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 7 additions & 21 deletions Common/Cpp/Sockets/AbstractServerSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,16 @@ class AbstractServerSocket{
// State::LISTENING on success or State::NOT_RUNNING on failure.
virtual void start(const std::string& address, uint16_t port) = 0;

// Closes the active connection, if any, and keeps listening. It does
// not wait for the worker threads, so it is safe to call from within
// a listener callback. "Listener::on_disconnect" is not called for
// the connection closed this way.
// Closes the active connection, if any, and keeps listening. Unlike
// the full teardown the destructor performs, this does not wait for
// the worker threads, so it is safe to call from within a listener
// callback. "Listener::on_disconnect" is not called for the
// connection closed this way.
//
// It cancels any in-flight "send()" the same way "close()" does: the
// send returns promptly with the number of bytes actually written so
// far (best effort).
// It cancels any in-flight "send()": the send returns promptly with
// the number of bytes actually written so far (best effort).
virtual void close_connection() = 0;

// Requests immediate teardown of the server, without waiting for the
// worker threads to exit. This is safe to call from within a listener
// callback (which runs on one of those threads; a blocking teardown
// there would deadlock). It marks the server DESTRUCTING (no new
// connections are accepted), closes the active connection, and
// cancels any in-flight "send()": its caller returns promptly with
// the number of bytes actually written so far (best effort). No
// "on_disconnect" callback is fired. The server is left in
// State::DESTRUCTING; its destructor completes the teardown (joins
// the threads and returns it to NOT_RUNNING), and so does a later
// "start()" on the same server, so it can be reused or destroyed. It
// is safe to call multiple times.
virtual void close() noexcept = 0;

// Sends data to the active connection. This blocks until every byte
// has been written, the connection is lost, "close()"/
// "close_connection()" is called, or "timeout" elapses.
Expand Down
31 changes: 2 additions & 29 deletions Common/Cpp/Sockets/ServerSocket_POSIX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ ServerSocket_POSIX::ServerSocket_POSIX(ThreadPool& thread_pool)
}

void ServerSocket_POSIX::start(const std::string& address, uint16_t port){
// Complete a requested-but-unfinished close() before starting a new
// listener: close() is non-blocking (it leaves the server DESTRUCTING
// with the threads still exiting), so re-listening on the same server
// joins them here. No-op if the server is already NOT_RUNNING.
// start() is always user-initiated, never a callback, so blocking is
// safe.
close_and_wait();

uint64_t generation = 0;
{
std::lock_guard<Mutex> lg(m_lock);
Expand Down Expand Up @@ -67,7 +59,7 @@ void ServerSocket_POSIX::start(const std::string& address, uint16_t port){
// caller's wait timed out while the pool was starved). It
// notices its stale generation when it gets scheduled and
// exits without listening, so it only has to outlive this
// call; close_and_wait() joins the whole list.
// call; close() joins the whole list.
std::lock_guard<Mutex> lg(m_task_lock);
m_accept_threads.push_back(std::move(task));
}
Expand Down Expand Up @@ -110,25 +102,6 @@ void ServerSocket_POSIX::close_connection(){
}

void ServerSocket_POSIX::close() noexcept{
// Non-blocking teardown, safe to call from within a listener callback
// (which runs on one of the threads this would otherwise have to
// join). The accept loop and the connection loop observe
// State::DESTRUCTING on their next iteration (a few milliseconds) and
// exit on their own, closing their sockets; the server is left in
// State::DESTRUCTING. The blocking "close_and_wait()" - driven by the
// destructor and by a later "start()" on the same object - joins the
// threads and returns it to NOT_RUNNING.
{
std::lock_guard<Mutex> lg(m_lock);
if (m_state.load(std::memory_order_relaxed) != State::NOT_RUNNING){
m_close_requested = true;
m_state.store(State::DESTRUCTING, std::memory_order_release);
}
}
m_cv.notify_all();
}

void ServerSocket_POSIX::close_and_wait() noexcept{
{
std::unique_lock<Mutex> lg(m_lock);
// Wait for an in-progress start() to publish the listener task
Expand All @@ -148,7 +121,7 @@ void ServerSocket_POSIX::close_and_wait() noexcept{
}
if (!drain){
// Either nothing was running (m_closing == 0), or another
// close_and_wait() is already draining the threads (m_closing > 1).
// close() is already draining the threads (m_closing > 1).
// In the second case, wait for it to finish.
std::unique_lock<Mutex> lg(m_lock);
m_cv.wait(lg, [&]{
Expand Down
26 changes: 8 additions & 18 deletions Common/Cpp/Sockets/ServerSocket_POSIX.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ServerSocket_POSIX final : public AbstractServerSocket{
ServerSocket_POSIX(ThreadPool& thread_pool);

virtual ~ServerSocket_POSIX(){
close_and_wait();
close();
}

virtual void start(const std::string& address, uint16_t port) override;
Expand All @@ -39,16 +39,6 @@ class ServerSocket_POSIX final : public AbstractServerSocket{
// within a listener callback.
virtual void close_connection() override;

// Requests immediate teardown of the server, without waiting for the
// worker threads to exit. It marks the server DESTRUCTING and closes
// the active connection; the accept and connection threads observe
// the new state on their next iteration and exit on their own, closing
// their sockets. It cancels any in-flight "send()" the same way
// "close_connection()" does. Safe to call from within a listener
// callback. The blocking "close_and_wait()" (the destructor, and the
// "start()" prelude) joins the threads and returns NOT_RUNNING.
virtual void close() noexcept override;

virtual size_t send(
const void* data,
size_t bytes,
Expand All @@ -58,11 +48,11 @@ class ServerSocket_POSIX final : public AbstractServerSocket{

private:

// The blocking teardown: waits for an in-progress start() to publish
// its task, then joins the accept and connection threads and returns
// the server to NOT_RUNNING. Driven by the destructor and by start()
// (which re-listens on the same object after a close()).
void close_and_wait() noexcept;
// Stops the server: it stops accepting new connections, closes the
// active connection, and returns the server to State::NOT_RUNNING so
// the object is fully torn down. It blocks until the worker threads
// have exited, so it is private and only called by the destructor.
void close() noexcept;

void accept_thread_loop(std::string address, uint16_t port, uint64_t generation);
void connection_thread_loop();
Expand Down Expand Up @@ -100,8 +90,8 @@ class ServerSocket_POSIX final : public AbstractServerSocket{
// One task per start() call. start() must never destroy the previous
// task's AsyncTask here: its destructor blocks until the task has
// finished, and a task from an earlier attempt may still be running
// (the caller's wait timed out while the pool was starved).
// close_and_wait() joins the whole list.
// (the caller's wait timed out while the pool was starved). close()
// joins the whole list.
std::vector<AsyncTask> m_accept_threads;
AsyncTask m_client_thread;
};
Expand Down
67 changes: 11 additions & 56 deletions Common/Cpp/Sockets/ServerSocket_Qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@
namespace PokemonAutomation{

void ServerSocket_Qt::start(const std::string& address, uint16_t port){
// Complete a requested-but-unfinished close() before starting a new
// listener: close() is non-blocking (it leaves the server DESTRUCTING
// with the QTcpServer still alive), so re-listening on the same server
// destroys the old one here. No-op if the server is already
// NOT_RUNNING. start() is always user-initiated, never a callback, so
// blocking is safe.
close_and_wait();

bool starting;
{
std::lock_guard<Mutex> lg(m_lock);
Expand All @@ -33,7 +25,7 @@ void ServerSocket_Qt::start(const std::string& address, uint16_t port){
&& !m_starting;
// Only claim the "starting" flag when we actually start: a
// rejected call (server already running or already started) must
// not leave "m_starting" true - "close_and_wait()" waits on
// not leave "m_starting" true - "close()" waits on
// "!m_starting", so a stale flag would deadlock the destructor.
// (This happens when a test's start-retry loop calls start() on
// a server whose first listen failed: "m_server" is set even on
Expand Down Expand Up @@ -74,8 +66,7 @@ void ServerSocket_Qt::start(const std::string& address, uint16_t port){
(m_state.load(std::memory_order_relaxed) == State::DESTRUCTING);
m_server = server;
if (destructing){
// "close_and_wait()" is already in progress (from the
// destructor or a concurrent start()): it will destroy the
// "close()" is already in progress: it will destroy the
// server we just created. Leave the state for it.
}else{
listening = m_listen_error.empty();
Expand All @@ -92,9 +83,9 @@ void ServerSocket_Qt::start(const std::string& address, uint16_t port){
// exactly as it was before this start() (no "m_server", state
// NOT_RUNNING), so the caller's start-retry loop can immediately
// try another port. The other backends behave the same: a failed
// listen leaves no listener behind. If "close_and_wait()" raced
// us, it has already claimed "m_server", and the pool's lookup
// makes the second destruction a no-op.
// listen leaves no listener behind. If "close()" raced us, it has
// already claimed "m_server", and the pool's lookup makes the
// second destruction a no-op.
GlobalThreadPools::qt_event_threadpool().remove_object(server);
std::lock_guard<Mutex> lg(m_lock);
m_server = nullptr;
Expand Down Expand Up @@ -140,41 +131,6 @@ void ServerSocket_Qt::close_connection(){
}
}
void ServerSocket_Qt::close() noexcept{
// Non-blocking teardown, safe to call from within a listener callback
// (which runs on the event thread; the blocking teardown there would
// deadlock). It marks the server DESTRUCTING (on_new_connection()
// stops accepting, and connection_lost() no longer reports), cancels
// the in-flight "send()" calls so their callers return promptly, and
// aborts the active connection on the event thread the same way
// "close_connection()" does - queued when called from another
// thread, so it runs as soon as the event loop next turns. It does
// not wait for any of that. The QTcpServer itself (the connection
// socket is its child) is destroyed by the blocking
// "close_and_wait()" - the destructor, and the "start()" prelude -
// which also returns the state to NOT_RUNNING.
{
std::lock_guard<Mutex> lg(m_lock);
if (m_server == nullptr && !m_starting){
// Never started, or already closed.
return;
}
m_close_requested = true;
m_state.store(State::DESTRUCTING, std::memory_order_release);
}

// Cancel the in-flight "send()" calls so their callers return
// promptly with the best-effort byte count.
cancel_inflight_sends();

// Abort the active connection, same event as "close_connection()".
// Deliberately no wait: close() must never block. If we are not on
// the event thread, the abort runs when the event loop next turns
// (or is discarded with the server, which is harmless: it only ever
// touches state under m_lock).
emit internal_close_connection(QSharedPointer<CloseCompletion>::create());
}

void ServerSocket_Qt::close_and_wait() noexcept{
QTcpServer* server;
{
std::lock_guard<Mutex> lg(m_lock);
Expand Down Expand Up @@ -333,8 +289,8 @@ void ServerSocket_Qt::on_new_connection(){
if (sock == nullptr){
break;
}
// The connection socket lives with the server, so
// "close_and_wait()" destroys it on the event thread.
// The connection socket lives with the server, so "close()"
// destroys it on the event thread.
sock->setParent(server);

bool accepted;
Expand Down Expand Up @@ -436,9 +392,9 @@ void ServerSocket_Qt::on_close_connection(QSharedPointer<CloseCompletion> comple
if (m_socket != nullptr){
m_close_requested = true;
if (m_state.load(std::memory_order_relaxed) != State::DESTRUCTING){
// A plain close_connection(): the server keeps running,
// so it goes back to LISTENING. A close() in progress
// already set DESTRUCTING: leave the state for it.
// The server keeps running, so it goes back to
// LISTENING. The destructor's close() may have set
// DESTRUCTING: leave the state for it.
m_state.store(State::LISTENING, std::memory_order_release);
}
sock = m_socket;
Expand Down Expand Up @@ -479,8 +435,7 @@ void ServerSocket_Qt::on_send(QSharedPointer<SendData> send_data){
const char* ptr = send_data->packet.data();
size_t remaining = send_data->packet.size();
// The deadline keeps a client that never reads from pinning this
// event thread forever (which would also block
// "close_and_wait()").
// event thread forever (which would also block "close()").
while (
remaining > 0
&& sock->state() == QAbstractSocket::ConnectedState
Expand Down
36 changes: 10 additions & 26 deletions Common/Cpp/Sockets/ServerSocket_Qt.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ class ServerSocket_Qt final : public QObject, public AbstractServerSocket{
std::string packet;
// The event-thread write loop stops at this deadline, so a send
// that can never complete (a client that never reads) cannot pin
// the server's event thread forever and block
// "close_and_wait()". "max()" means no deadline (the infinite
// default).
// the server's event thread forever and block "close()". "max()"
// means no deadline (the infinite default).
std::chrono::steady_clock::time_point deadline = std::chrono::steady_clock::time_point::max();
// The running byte count: the write loop updates it as it goes,
// so "close()"/"close_connection()" cancelling the send mid-write
Expand Down Expand Up @@ -70,7 +69,7 @@ class ServerSocket_Qt final : public QObject, public AbstractServerSocket{
ServerSocket_Qt(ThreadPool&){}

virtual ~ServerSocket_Qt(){
close_and_wait();
close();
}

// Binds to "address" and listens on "port" on the Qt event thread. This
Expand All @@ -83,21 +82,6 @@ class ServerSocket_Qt final : public QObject, public AbstractServerSocket{
// effort). It is safe to call from within a listener callback.
virtual void close_connection() override;

// Requests immediate teardown of the server, without waiting for the
// event thread. It marks the server DESTRUCTING (no new connections
// are accepted), cancels the in-flight "send()" calls so their
// callers return promptly with the bytes written so far (best
// effort), and aborts the active connection on the event thread the
// same way "close_connection()" does. It does not wait for any of
// that, so it is safe to call from within a listener callback (which
// runs on the event thread). No "on_disconnect" callback is fired.
// The QTcpServer itself (and the connection socket, its child) is
// destroyed by the blocking "close_and_wait()" - the destructor, and
// the "start()" prelude - which also returns the state to
// NOT_RUNNING. Calling "close()" when the server is not running is a
// no-op. It is safe to call multiple times.
virtual void close() noexcept override;

virtual size_t send(
const void* data,
size_t bytes,
Expand Down Expand Up @@ -136,13 +120,13 @@ class ServerSocket_Qt final : public QObject, public AbstractServerSocket{
// runs (or is discarded with the server).
void cancel_inflight_sends();

// The blocking teardown: cancels the in-flight "send()" calls the
// same way close() does, waits for an in-progress start() to publish
// the server, destroys the QTcpServer (and its connection socket) on
// the event thread, and returns the state to NOT_RUNNING. Driven by
// the destructor and by start() (which re-listens on the same object
// after a close()).
void close_and_wait() noexcept;
// Stops the server: it destroys the QTcpServer (and its connection
// socket) on the event thread and returns the state to
// State::NOT_RUNNING, so the object is fully torn down. It blocks
// until the QTcpServer has been destroyed, so any in-flight listener
// callback is finished by the time it returns; it is private and
// only called by the destructor.
void close() noexcept;

std::unique_ptr<QTcpServer> make_server(const std::string& address, uint16_t port);

Expand Down
Loading
Loading