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
18 changes: 17 additions & 1 deletion Common/Cpp/Sockets/AbstractServerSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define PokemonAutomation_Sockets_AbstractServerSocket_H

#include <chrono>
#include <cstdint>
#include <string>
#include "Common/Cpp/ListenerSet.h"

Expand Down Expand Up @@ -58,6 +59,18 @@ class AbstractServerSocket{
return m_state.load(std::memory_order_relaxed);
}

// A token identifying the connection the socket is currently
// serving. It changes every time a new client is accepted, and it
// is 0 when no connection is active. Capture it when a write

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review: Minor: none of the backends reset m_connection_id when a connection closes, so after the first connection this returns the last id while idle, not 0 (0 only holds before the very first accept). Harmless in practice — a stale id is still refused, and when no connection is active the state check in send() refuses first — but a caller can't use connection_id() == 0 as an "idle" test. Suggest either rewording the comment ("0 before the first connection; otherwise the id of the most recently accepted connection") or actually resetting to 0 on close if the idle⇒0 guarantee is wanted.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in c5c9e7f — with a twist: the accessor now reports 0 whenever the socket is not CONNECTED, while the stored counter stays monotonic (resetting the counter itself on close would let a queued write from the old connection match the next connection's id and slip through). send() compares against the counter, so its behavior is unchanged; Test_Sockets_StaleSend now also pins the idle⇒0 state (before the first connect and between connections). Sockets suite 30/30; the stacked #1879 (re-synced onto this commit) passes its full suite (20,588 passed, 0 failed, 5 skipped).

// starts and pass it to "send()", so that a write that outlives its
// connection (the peer disconnected and a new client was accepted
// in the meantime) refuses to reach the new client's socket.
// Implementations keep the underlying counter monotonic (never
// reset it when a connection closes, or a queued write for the old
// connection could match the next connection's id); 0 is only the
// "no connection is active" report, never a counter value.
virtual uint64_t connection_id() = 0;

// Binds to "address" and listens on "port".
// Use "0.0.0.0" to accept connections on all interfaces.
virtual void start(const std::string& address, uint16_t port) = 0;
Expand All @@ -69,7 +82,10 @@ class AbstractServerSocket{
virtual size_t send(
const void* data,
size_t bytes,
std::chrono::milliseconds timeout = std::chrono::milliseconds::max()
std::chrono::milliseconds timeout = std::chrono::milliseconds::max(),
// The connection_id() captured when this write started. 0 skips
// the check: the call applies to whatever connection is active.
uint64_t connection = 0
) = 0;


Expand Down
30 changes: 22 additions & 8 deletions Common/Cpp/Sockets/ServerSocket_POSIX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ void ServerSocket_POSIX::start(const std::string& address, uint16_t port){
}
}

uint64_t ServerSocket_POSIX::connection_id(){
std::lock_guard<Mutex> lg(m_lock);
return m_state.load(std::memory_order_relaxed) == State::CONNECTED
? m_connection_id
: 0;
}


void ServerSocket_POSIX::close_connection(){
int socket;
{
Expand Down Expand Up @@ -159,16 +167,16 @@ void ServerSocket_POSIX::close_and_wait() noexcept{
}
}

size_t ServerSocket_POSIX::send(const void* data, size_t bytes, std::chrono::milliseconds timeout){
size_t ServerSocket_POSIX::send(const void* data, size_t bytes, std::chrono::milliseconds timeout, uint64_t connection){
int socket;
{
std::lock_guard<Mutex> lg(m_lock);
socket = m_state.load(std::memory_order_relaxed) == State::CONNECTED
? m_client
: -1;
}
if (socket == -1){
return 0;
if (m_state.load(std::memory_order_relaxed) != State::CONNECTED
|| (connection != 0 && m_connection_id != connection)
){
return 0;
}
socket = m_client;
}

const char* ptr = (const char*)data;
Expand All @@ -184,7 +192,12 @@ size_t ServerSocket_POSIX::send(const void* data, size_t bytes, std::chrono::mil
while (bytes > 0){
{
std::lock_guard<Mutex> lg(m_lock);
if (m_state.load(std::memory_order_relaxed) != State::CONNECTED){
// The connection may have been replaced (or closed) since the
// last iteration: in that case this write must stop rather
// than reach the new client's socket.
if (m_state.load(std::memory_order_relaxed) != State::CONNECTED
|| (connection != 0 && m_connection_id != connection)
){
return sent;
}
}
Expand Down Expand Up @@ -359,6 +372,7 @@ void ServerSocket_POSIX::accept_thread_loop(std::string address, uint16_t port,
fcntl(client_socket, F_SETFL, O_NONBLOCK);
m_client = client_socket;
m_close_requested = false;
m_connection_id++;
m_state.store(State::CONNECTED, std::memory_order_release);
accepted = true;
}
Expand Down
14 changes: 13 additions & 1 deletion Common/Cpp/Sockets/ServerSocket_POSIX.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,15 @@ class ServerSocket_POSIX final : public AbstractServerSocket{
virtual size_t send(
const void* data,
size_t bytes,
std::chrono::milliseconds timeout = std::chrono::milliseconds::max()
std::chrono::milliseconds timeout = std::chrono::milliseconds::max(),
uint64_t connection = 0
) override;

// A token identifying the connection the socket is currently
// serving. It changes every time a new client is accepted, and it
// is 0 when no connection is active.
virtual uint64_t connection_id() override;


private:

Expand Down Expand Up @@ -87,6 +93,12 @@ class ServerSocket_POSIX final : public AbstractServerSocket{
int m_client = -1;
bool m_close_requested = false;

// A connection token, incremented (under m_lock) on every accept.
// send() compares it against the token the caller captured, so a
// write that outlived its connection cannot reach the client that
// replaced it. 0 means no connection is active.
uint64_t m_connection_id = 0;

// 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
Expand Down
35 changes: 29 additions & 6 deletions Common/Cpp/Sockets/ServerSocket_Qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,24 @@ void ServerSocket_Qt::close_and_wait() noexcept{
m_close_requested = false;
}

size_t ServerSocket_Qt::send(const void* data, size_t bytes, std::chrono::milliseconds timeout){
size_t ServerSocket_Qt::send(const void* data, size_t bytes, std::chrono::milliseconds timeout, uint64_t connection){
QTcpSocket* socket;
{
std::lock_guard<Mutex> lg(m_lock);
socket = m_state.load(std::memory_order_relaxed) == State::CONNECTED
? m_socket
: nullptr;
if (m_state.load(std::memory_order_relaxed) != State::CONNECTED
|| (connection != 0 && m_connection_id != connection)
){
return 0;
}
socket = m_socket;
}
if (socket == nullptr){
return 0;
}

QSharedPointer<SendData> send_data = QSharedPointer<SendData>::create();
send_data->packet.assign((const char*)data, bytes);
send_data->connection = connection;
// "time_point::max()" (the infinite default) is never reached; using
// it also avoids overflowing "now() + timeout".
send_data->deadline =
Expand All @@ -213,7 +217,8 @@ size_t ServerSocket_Qt::send(const void* data, size_t bytes, std::chrono::millis
std::lock_guard<Mutex> lg(m_lock);
registered =
m_state.load(std::memory_order_relaxed) == State::CONNECTED
&& m_socket != nullptr;
&& m_socket != nullptr
&& (connection == 0 || m_connection_id == connection);
if (registered){
m_inflight_sends.push_back(send_data);
}
Expand Down Expand Up @@ -310,6 +315,7 @@ void ServerSocket_Qt::on_new_connection(){
}else{
m_socket = sock;
m_close_requested = false;
m_connection_id++;
m_state.store(State::CONNECTED, std::memory_order_release);
accepted = true;
rejected = false;
Expand Down Expand Up @@ -434,11 +440,28 @@ void ServerSocket_Qt::on_close_connection(QSharedPointer<CloseCompletion> comple
completion->cv.notify_all();
}

uint64_t ServerSocket_Qt::connection_id(){
std::lock_guard<Mutex> lg(m_lock);
return m_state.load(std::memory_order_relaxed) == State::CONNECTED
? m_connection_id
: 0;
}


void ServerSocket_Qt::on_send(QSharedPointer<SendData> send_data){
QTcpSocket* sock;
{
std::lock_guard<Mutex> lg(m_lock);
sock = m_socket;
if (send_data->connection != 0
&& m_connection_id != send_data->connection
){
// The connection this send was queued for was replaced (or
// torn down) while it was waiting in the queue: the write
// loop must not reach into the new connection's socket.
sock = nullptr;
}else{
sock = m_socket;
}
}

{
Expand Down
20 changes: 19 additions & 1 deletion Common/Cpp/Sockets/ServerSocket_Qt.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class ServerSocket_Qt final : public QObject, public AbstractServerSocket{
// "close_and_wait()"/"close_connection()" cancel the send.
bool done = false;

// The connection_id() the send was queued for; 0 skips the
// check. "on_send" refuses to write if the connection was
// replaced while the send was queued, so it cannot reach the
// new client's socket.
uint64_t connection = 0;

Mutex lock;
ConditionVariable cv;
};
Expand Down Expand Up @@ -86,9 +92,15 @@ class ServerSocket_Qt final : public QObject, public AbstractServerSocket{
virtual size_t send(
const void* data,
size_t bytes,
std::chrono::milliseconds timeout = std::chrono::milliseconds::max()
std::chrono::milliseconds timeout = std::chrono::milliseconds::max(),
uint64_t connection = 0
) override;

// A token identifying the connection the socket is currently
// serving. It changes every time a new client is accepted, and it
// is 0 when no connection is active.
virtual uint64_t connection_id() override;


signals:
void internal_send(QSharedPointer<SendData> send_data);
Expand Down Expand Up @@ -142,6 +154,12 @@ class ServerSocket_Qt final : public QObject, public AbstractServerSocket{
bool m_close_requested = false;
bool m_starting = false;

// A connection token, incremented (under m_lock) on every accept.
// "send()" compares it against the token the caller captured, so a
// write that outlived its connection cannot reach the client that
// replaced it. 0 means no connection is active.
uint64_t m_connection_id = 0;

// The "send()" calls that are queued on the event thread but not
// finished yet, so that "close_and_wait()"/"close_connection()" can
// cancel
Expand Down
27 changes: 22 additions & 5 deletions Common/Cpp/Sockets/ServerSocket_WinSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ void ServerSocket_WinSocket::start(const std::string& address, uint16_t port){
}
}

uint64_t ServerSocket_WinSocket::connection_id(){
std::lock_guard<Mutex> lg(m_lock);
return m_state.load(std::memory_order_relaxed) == State::CONNECTED
? m_connection_id
: 0;
}


void ServerSocket_WinSocket::close_connection(){
SOCKET socket;
{
Expand Down Expand Up @@ -170,13 +178,16 @@ void ServerSocket_WinSocket::close_and_wait() noexcept{
}
}

size_t ServerSocket_WinSocket::send(const void* data, size_t bytes, std::chrono::milliseconds timeout){
size_t ServerSocket_WinSocket::send(const void* data, size_t bytes, std::chrono::milliseconds timeout, uint64_t connection){
SOCKET socket;
{
std::lock_guard<Mutex> lg(m_lock);
socket = m_state.load(std::memory_order_relaxed) == State::CONNECTED
? m_client
: INVALID_SOCKET;
if (m_state.load(std::memory_order_relaxed) != State::CONNECTED
|| (connection != 0 && m_connection_id != connection)
){
return 0;
}
socket = m_client;
}
if (socket == INVALID_SOCKET){
return 0;
Expand All @@ -195,7 +206,12 @@ size_t ServerSocket_WinSocket::send(const void* data, size_t bytes, std::chrono:
while (bytes > 0){
{
std::lock_guard<Mutex> lg(m_lock);
if (m_state.load(std::memory_order_relaxed) != State::CONNECTED){
// The connection may have been replaced (or closed) since the
// last iteration: in that case this write must stop rather
// than reach the new client's socket.
if (m_state.load(std::memory_order_relaxed) != State::CONNECTED
|| (connection != 0 && m_connection_id != connection)
){
return sent;
}
}
Expand Down Expand Up @@ -379,6 +395,7 @@ void ServerSocket_WinSocket::accept_thread_loop(std::string address, uint16_t po
ioctlsocket(client_socket, FIONBIO, &client_non_blocking);
m_client = client_socket;
m_close_requested = false;
m_connection_id++;
m_state.store(State::CONNECTED, std::memory_order_release);
accepted = true;
}
Expand Down
14 changes: 13 additions & 1 deletion Common/Cpp/Sockets/ServerSocket_WinSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ class ServerSocket_WinSocket final : public AbstractServerSocket{
virtual size_t send(
const void* data,
size_t bytes,
std::chrono::milliseconds timeout = std::chrono::milliseconds::max()
std::chrono::milliseconds timeout = std::chrono::milliseconds::max(),
uint64_t connection = 0
) override;

// A token identifying the connection the socket is currently
// serving. It changes every time a new client is accepted, and it
// is 0 when no connection is active.
virtual uint64_t connection_id() override;


private:
// Winsock has to be started before any of its functions can be used, and
Expand Down Expand Up @@ -96,6 +102,12 @@ class ServerSocket_WinSocket final : public AbstractServerSocket{
SOCKET m_client = INVALID_SOCKET;
bool m_close_requested = false;

// A connection token, incremented (under m_lock) on every accept.
// send() compares it against the token the caller captured, so a
// write that outlived its connection cannot reach the client that
// replaced it. 0 means no connection is active.
uint64_t m_connection_id = 0;

// 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
Expand Down
Loading
Loading