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
33 changes: 20 additions & 13 deletions Common/Cpp/Sockets/AbstractClientSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ class AbstractClientSocket{
};

struct Listener{
// A listener must outlive the socket that calls it. Callbacks can
// still be delivered after a non-blocking "close()" returns - the
// socket's threads only stop when the socket itself is destroyed -
// so a listener must not be destroyed before the socket that added
// it. Declare the listener before the socket, so that it is
// destroyed after the socket.
// Called at the start of the receiver thread. This can be used to set
// the thread priority of the thread.
virtual void on_thread_start(){}
Expand Down Expand Up @@ -61,20 +67,21 @@ class AbstractClientSocket{
return m_state.load(std::memory_order_relaxed);
}

// Stops the connection and returns the socket to State::NOT_RUNNING
// so "connect()" can be called again. This is safe to call multiple
// times.
//
// It cancels any in-flight operations: no further
// "Listener::on_receive_data" callbacks are started (one already
// running is allowed to finish), and an in-flight "send()" returns
// promptly with the number of bytes actually written so far (best
// effort). A "send()" that is already racing "close()" may still
// complete.
//
// This blocks until the receive thread has exited, so it must not be
// called from within a listener callback.
// Requests immediate teardown of the active connection, without waiting
// for the receive thread to exit. This is safe to call from within a
// listener callback (which runs on that thread; the blocking teardown
// there would deadlock). It marks the socket DESTRUCTING and releases
// the underlying socket; the receive thread observes this on its next
// iteration and exits. It 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 socket is
// left in State::DESTRUCTING; the socket's destructor completes the
// teardown (joins the thread and returns it to NOT_RUNNING), so the
// socket cannot be "connect()"ed again - destroy it and use a new one.
// Calling "close()" when there is no active connection is a no-op. It
// is safe to call multiple times.
virtual void close() noexcept = 0;

virtual void connect(const std::string& address, uint16_t port) = 0;

// Sends data on the active connection. This blocks until every byte
Expand Down
6 changes: 6 additions & 0 deletions Common/Cpp/Sockets/AbstractServerSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class AbstractServerSocket{
};

struct Listener{
// A listener must outlive the socket that calls it. Callbacks can
// still be delivered after "close_connection()" returns - the
// connection's threads only stop when the connection (or the
// socket) is destroyed - so a listener must not be destroyed
// before the socket that added it. Declare the listener before
// the socket, so that it is destroyed after the socket.
// Called at the start of the thread that will handle a connection.
// This can be used to set the thread priority of the thread.
virtual void on_thread_start(){}
Expand Down
29 changes: 27 additions & 2 deletions Common/Cpp/Sockets/ClientSocket_POSIX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ClientSocket_POSIX::ClientSocket_POSIX(ThreadPool& thread_pool)
// Ignore SIGPIPE. Handle errors via errno instead
signal(SIGPIPE, SIG_IGN);
}
void ClientSocket_POSIX::close() noexcept{
void ClientSocket_POSIX::close_and_wait() noexcept{
AsyncTask thread;
{
std::lock_guard<Mutex> lg(m_lock);
Expand All @@ -50,8 +50,33 @@ void ClientSocket_POSIX::close() noexcept{
m_cv.notify_all();
}
}
void ClientSocket_POSIX::close() noexcept{
// Non-blocking teardown, safe to call from the receive thread. The
// blocking "close_and_wait()" waits for the receive thread to exit and so
// must not run from a listener callback; this instead just flips the
// state and releases the socket. The receive thread's next iteration sees
// DESTRUCTING (or the missing socket) and exits on its own. The socket is
// left DESTRUCTING; the destructor's close_and_wait() returns it to
// NOT_RUNNING.
{
std::lock_guard<Mutex> lg(m_lock);
if (m_state.load(std::memory_order_relaxed) == State::NOT_RUNNING && !m_thread){
return;
}
m_state.store(State::DESTRUCTING, std::memory_order_release);
close_socket_locked();
}
m_cv.notify_all();
}

void ClientSocket_POSIX::connect(const std::string& address, uint16_t port){
// Complete any in-progress connection or a requested-but-unfinished
// close() before starting a new one. close() is non-blocking (it leaves
// the socket DESTRUCTING), so reconnecting on the same socket joins the
// old receive thread here to get back to NOT_RUNNING. No-op if the socket
// is already NOT_RUNNING.
close_and_wait();

std::string error;
{
std::lock_guard<Mutex> lg(m_lock);
Expand Down Expand Up @@ -91,7 +116,7 @@ void ClientSocket_POSIX::connect(const std::string& address, uint16_t port){
// connect() may be called from a thread that is itself running on
// this pool, and blocking here until a pool thread is free could
// deadlock a capped pool. The returned AsyncTask still lets
// close() join the receive thread.
// close_and_wait() join the receive thread.
try{
m_thread = m_thread_pool.dispatch([=, this]{
thread_loop(address, port);
Expand Down
37 changes: 22 additions & 15 deletions Common/Cpp/Sockets/ClientSocket_POSIX.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ class ClientSocket_POSIX final : public AbstractClientSocket{
public:
ClientSocket_POSIX(ThreadPool& thread_pool);

virtual ~ClientSocket_POSIX(){
close();
~ClientSocket_POSIX() override{
close_and_wait();
}

// Stops the receive thread, closes the socket, and returns the socket to
// State::NOT_RUNNING so "connect()" can be called again. This is safe to
// call multiple times.
//
// It cancels an in-flight "send()": the send loop re-checks the state
// every iteration, and "close()" notifies it, so the send returns within
// a millisecond with the bytes written so far.
//
// This blocks until the receive thread has exited, so it must not be
// called from within a listener callback (which runs on that thread).
virtual void close() noexcept override;

virtual void connect(const std::string& address, uint16_t port) override;
// Requests immediate teardown of the active connection, without waiting
// for the receive thread to exit. Safe to call from within a listener
// callback (which runs on that thread). It marks the socket DESTRUCTING
// and releases the underlying socket; the receive thread exits on its
// next iteration. It cancels an in-flight "send()" (the send loop
// re-checks the state and returns within a millisecond with the bytes
// written so far). The socket is left in State::DESTRUCTING; the
// destructor's close_and_wait() joins the thread and returns it to
// NOT_RUNNING. No "on_disconnect" callback is fired. Safe to call
// multiple times.
void close() noexcept override;

void connect(const std::string& address, uint16_t port) override;

virtual size_t send(
const void* data,
Expand All @@ -51,6 +51,13 @@ class ClientSocket_POSIX final : public AbstractClientSocket{


private:
// Stops the receive thread, closes the socket, and returns the socket to
// State::NOT_RUNNING. It blocks until the receive thread has exited, so
// it must not be called from within a listener callback. The public
// "close()" only requests the teardown; this actually joins the thread,
// and is called by the destructor.
void close_and_wait() noexcept;

// Closes the underlying socket, if any. Requires "m_lock" to be held.
void close_socket_locked();

Expand Down
39 changes: 38 additions & 1 deletion Common/Cpp/Sockets/ClientSocket_Qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <QSharedPointer>
#include <QTimer>
#include <QTcpSocket>
#include <QMetaObject>
#include "Common/Qt/QtThreadPool.h"
#include "Common/Qt/GlobalThreadPoolsQt.h"
#include "ClientSocket_Qt.h"
Expand All @@ -21,7 +22,7 @@ namespace PokemonAutomation{



void ClientSocket_Qt::close() noexcept{
void ClientSocket_Qt::close_and_wait() noexcept{
QTcpSocket* socket;
{
std::lock_guard<Mutex> lg(m_lock);
Expand Down Expand Up @@ -49,7 +50,43 @@ void ClientSocket_Qt::close() noexcept{
std::lock_guard<Mutex> lg(m_lock);
m_state.store(State::NOT_RUNNING, std::memory_order_release);
}
void ClientSocket_Qt::close() noexcept{
QTcpSocket* socket;
{
std::lock_guard<Mutex> lg(m_lock);
if (m_socket == nullptr){
return;
}
m_state.store(State::DESTRUCTING, std::memory_order_release);
m_success_pending = false;
socket = m_socket;
}

// Immediately break the connection. QTcpSocket::abort() must run on the
// socket's event thread, but "close()" may be called from any thread
// (including a listener callback, which runs on that event thread), so
// post the abort to the socket's event thread instead of calling it here
// - a cross-thread call would trip Qt's QSocketNotifier thread check.
// This stays non-blocking: close() returns immediately, and the socket is
// aborted as soon as the event thread reaches the queued call. The
// resulting "disconnected"/"errorOccurred" signals see State::DESTRUCTING
// and are no-ops. The destructor's close_and_wait() destroys the socket
// and returns it to NOT_RUNNING.
cancel_inflight_sends();
QMetaObject::invokeMethod(
socket,
[socket]{ socket->abort(); },
Qt::QueuedConnection
);
}
void ClientSocket_Qt::connect(const std::string& address, uint16_t port){
// Complete any in-progress connection or a requested-but-unfinished
// close() before starting a new one. close() is non-blocking (it leaves
// the socket DESTRUCTING), so reconnecting on the same socket destroys the
// old QTcpSocket here (on its event thread) to get back to NOT_RUNNING.
// No-op if the socket is already NOT_RUNNING.
close_and_wait();

QTcpSocket* old_socket;
{
std::lock_guard<Mutex> lg(m_lock);
Expand Down
43 changes: 23 additions & 20 deletions Common/Cpp/Sockets/ClientSocket_Qt.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class ClientSocket_Qt final : public QObject, public AbstractClientSocket{
std::string packet;
// The event-thread write loop stops at this deadline, so a send
// that can never complete (a peer that never reads) cannot pin
// the socket's event thread forever and block "close()". "max()"
// means no deadline (the infinite default).
// the socket's event thread forever and block "close_and_wait()".
// "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()" cancelling the send mid-write can report the
Expand All @@ -53,26 +53,22 @@ class ClientSocket_Qt final : public QObject, public AbstractClientSocket{
public:
ClientSocket_Qt(ThreadPool&){}

virtual ~ClientSocket_Qt(){
close();
~ClientSocket_Qt() override{
close_and_wait();
}

// Stops the connection, destroys the QTcpSocket, and returns the socket
// to State::NOT_RUNNING so "connect()" can be called again. This blocks
// until the QTcpSocket has been destroyed, so any in-flight listener
// callback is finished by the time it returns. It is safe to call
// multiple times.
//
// It cancels the in-flight "send()" calls: their callers return
// promptly with the number of bytes actually written so far (best
// effort), instead of waiting for the event thread to finish them.
//
// This must not be called from within a listener callback, since the
// callbacks run on the QTcpSocket's event thread and this destroys the
// socket that is running them.
virtual void close() noexcept override;

virtual void connect(const std::string& address, uint16_t port) override;
// Requests immediate teardown of the active connection, without waiting
// for the QTcpSocket's event thread to destroy it. Safe to call from
// within a listener callback (which runs on that event thread). It marks
// the socket DESTRUCTING and aborts the underlying QTcpSocket; the
// resulting disconnect signals are no-ops. It cancels the in-flight
// "send()" calls (their callers return promptly with the bytes written
// so far). The socket is left in State::DESTRUCTING; the destructor's
// close_and_wait() destroys the QTcpSocket and returns it to NOT_RUNNING.
// No "on_disconnect" callback is fired. Safe to call multiple times.
void close() noexcept override;

void connect(const std::string& address, uint16_t port) override;

virtual size_t send(
const void* data,
Expand All @@ -87,6 +83,13 @@ class ClientSocket_Qt final : public QObject, public AbstractClientSocket{


private:
// Stops the connection, destroys the QTcpSocket on its event thread, and
// returns the socket to State::NOT_RUNNING. It blocks until the
// QTcpSocket has been destroyed, so it must not be called from within a
// listener callback. The public "close()" only requests the teardown;
// this actually destroys the socket, and is called by the destructor.
void close_and_wait() noexcept;

// The connection attempt or the connection itself ended in a failure.
// This fires the appropriate callback and returns the socket to
// State::NOT_RUNNING so "connect()" can be called again. If the
Expand Down
29 changes: 27 additions & 2 deletions Common/Cpp/Sockets/ClientSocket_WinSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
namespace PokemonAutomation{


void ClientSocket_WinSocket::close() noexcept{
void ClientSocket_WinSocket::close_and_wait() noexcept{
AsyncTask thread;
{
std::lock_guard<Mutex> lg(m_lock);
Expand All @@ -40,7 +40,32 @@ void ClientSocket_WinSocket::close() noexcept{
m_cv.notify_all();
}
}
void ClientSocket_WinSocket::close() noexcept{
// Non-blocking teardown, safe to call from the receive thread. The
// blocking "close_and_wait()" waits for the receive thread to exit and so
// must not run from a listener callback; this instead just flips the
// state and releases the socket. The receive thread's next iteration sees
// DESTRUCTING (or the missing socket) and exits on its own. The socket is
// left DESTRUCTING; the destructor's close_and_wait() returns it to
// NOT_RUNNING.
{
std::lock_guard<Mutex> lg(m_lock);
if (m_state.load(std::memory_order_relaxed) == State::NOT_RUNNING && !m_thread){
return;
}
m_state.store(State::DESTRUCTING, std::memory_order_release);
close_socket_locked();
}
m_cv.notify_all();
}
void ClientSocket_WinSocket::connect(const std::string& address, uint16_t port){
// Complete any in-progress connection or a requested-but-unfinished
// close() before starting a new one. close() is non-blocking (it leaves
// the socket DESTRUCTING), so reconnecting on the same socket joins the
// old receive thread here to get back to NOT_RUNNING. No-op if the socket
// is already NOT_RUNNING.
close_and_wait();

std::string error;
{
std::lock_guard<Mutex> lg(m_lock);
Expand Down Expand Up @@ -82,7 +107,7 @@ void ClientSocket_WinSocket::connect(const std::string& address, uint16_t port){
// connect() may be called from a thread that is itself running on
// this pool, and blocking here until a pool thread is free could
// deadlock a capped pool. The returned AsyncTask still lets
// close() join the receive thread.
// close_and_wait() join the receive thread.
try{
m_thread = m_thread_pool.dispatch([=, this]{
thread_loop(address, port);
Expand Down
37 changes: 22 additions & 15 deletions Common/Cpp/Sockets/ClientSocket_WinSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ class ClientSocket_WinSocket final : public AbstractClientSocket{
: m_thread_pool(thread_pool)
{}

virtual ~ClientSocket_WinSocket(){
close();
~ClientSocket_WinSocket() override{
close_and_wait();
}

// Stops the receive thread, closes the socket, and returns the socket to
// State::NOT_RUNNING so "connect()" can be called again. This is safe to
// call multiple times.
//
// It cancels an in-flight "send()": the send loop re-checks the state
// every iteration, and "close()" notifies it, so the send returns within
// a millisecond with the bytes written so far.
//
// This blocks until the receive thread has exited, so it must not be
// called from within a listener callback (which runs on that thread).
virtual void close() noexcept override;

virtual void connect(const std::string& address, uint16_t port) override;
// Requests immediate teardown of the active connection, without waiting
// for the receive thread to exit. Safe to call from within a listener
// callback (which runs on that thread). It marks the socket DESTRUCTING
// and releases the underlying socket; the receive thread exits on its
// next iteration. It cancels an in-flight "send()" (the send loop
// re-checks the state and returns within a millisecond with the bytes
// written so far). The socket is left in State::DESTRUCTING; the
// destructor's close_and_wait() joins the thread and returns it to
// NOT_RUNNING. No "on_disconnect" callback is fired. Safe to call
// multiple times.
void close() noexcept override;

void connect(const std::string& address, uint16_t port) override;

virtual size_t send(
const void* data,
Expand All @@ -55,6 +55,13 @@ class ClientSocket_WinSocket final : public AbstractClientSocket{


private:
// Stops the receive thread, closes the socket, and returns the socket to
// State::NOT_RUNNING. It blocks until the receive thread has exited, so
// it must not be called from within a listener callback. The public
// "close()" only requests the teardown; this actually joins the thread,
// and is called by the destructor.
void close_and_wait() noexcept;

// Winsock has to be started before any of its functions can be used, and
// nothing else in the program calls WSAStartup, so call it exactly once
// before the first socket is created. We deliberately never call
Expand Down
Loading
Loading