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
6 changes: 3 additions & 3 deletions include/sendspin/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -967,13 +967,13 @@ class SendspinClient {
// listener callback for delivery from the main loop's drain_inbox().

/// @brief Queue an on_pairing_started notification
void note_pairing_started(const std::string& server_id);
void note_pairing_started(std::string_view server_id);

/// @brief Queue an on_pairing_succeeded notification
void note_pairing_succeeded(const std::string& server_id);
void note_pairing_succeeded(std::string_view server_id);

/// @brief Queue an on_pairing_failed notification
void note_pairing_failed(const std::string& server_id, SendspinPairAbortReason reason);
void note_pairing_failed(std::string_view server_id, SendspinPairAbortReason reason);

/// @brief Queue an on_display_pairing_code notification
void note_display_pairing_code(const std::string& code, SendspinPairingCodeFormat format);
Expand Down
2 changes: 2 additions & 0 deletions include/sendspin/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ enum class SendspinGoodbyeReason : uint8_t {
/// @brief Server identity fields received in server/hello messages
struct ServerInformationObject {
std::string server_id{};
/// At most 64 bytes in UTF-8, cut at a character boundary
std::string name{};
};

Expand All @@ -55,6 +56,7 @@ enum class SendspinPlaybackState : uint8_t {
struct GroupUpdateObject {
std::optional<SendspinPlaybackState> playback_state{};
std::optional<std::string> group_id{};
/// At most 64 bytes in UTF-8, cut at a character boundary
Comment thread
kahrendt marked this conversation as resolved.
Resolved
std::optional<std::string> group_name{};
};

Expand Down
4 changes: 2 additions & 2 deletions src/admission.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ inline int activity_rank(ActivitySet activities) {
/// shielding a pairing that has finished.
/// @return true if the incoming connection should become the admitted one.
inline bool should_admit_connection(ActivitySet incoming_activities,
const std::string& incoming_server_id,
std::string_view incoming_server_id,
ActivitySet admitted_activities,
const std::string& admitted_server_id, bool has_admitted,
std::string_view admitted_server_id, bool has_admitted,
std::string_view last_playback_server_id,
bool admitted_pairing_in_flight) {
if (!has_admitted) {
Expand Down
15 changes: 8 additions & 7 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1740,20 +1740,21 @@ void SendspinClient::on_activation_applied([[maybe_unused]] SendspinConnection*
#endif
}

void SendspinClient::note_pairing_started(const std::string& server_id) {
void SendspinClient::note_pairing_started(std::string_view server_id) {
this->event_state_->push_pairing_note(
{.type = PairingNoteType::PAIRING_STARTED, .text = server_id});
{.type = PairingNoteType::PAIRING_STARTED, .text = std::string(server_id)});
}

void SendspinClient::note_pairing_succeeded(const std::string& server_id) {
void SendspinClient::note_pairing_succeeded(std::string_view server_id) {
this->event_state_->push_pairing_note(
{.type = PairingNoteType::PAIRING_SUCCEEDED, .text = server_id});
{.type = PairingNoteType::PAIRING_SUCCEEDED, .text = std::string(server_id)});
}

void SendspinClient::note_pairing_failed(const std::string& server_id,
void SendspinClient::note_pairing_failed(std::string_view server_id,
SendspinPairAbortReason reason) {
this->event_state_->push_pairing_note(
{.type = PairingNoteType::PAIRING_FAILED, .text = server_id, .reason = reason});
this->event_state_->push_pairing_note({.type = PairingNoteType::PAIRING_FAILED,
.text = std::string(server_id),
.reason = reason});
}

void SendspinClient::note_display_pairing_code(const std::string& code,
Expand Down
6 changes: 3 additions & 3 deletions src/client_dispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,15 +510,15 @@ void SendspinClient::process_json_message(SendspinConnection& connection, const
ServerHelloMessage hello_msg;
if (parsed.extract<process_server_hello_message>(&hello_msg)) {
// server_id comes from the Noise handshake result (already set on the
// connection); server/hello supplies the display name.
conn->set_server_name(std::move(hello_msg.name));
// connection); server/hello supplies the friendly name.
conn->set_server_name(hello_msg.name);
conn->set_server_source_codecs(hello_msg.source_codecs);
// The nursery scan later in this tick sends client/hello in reply; nothing
// needs to be scheduled here.
conn->set_server_hello_received(true);

SS_LOGD(TAG, "Connected to server '%s' (server_id=%s)",
conn->get_server_information().name.c_str(), conn->get_server_id().c_str());
conn->get_server_name().c_str(), conn->get_server_id().c_str());
}
break;
}
Expand Down
15 changes: 7 additions & 8 deletions src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,7 @@ void SendspinConnection::handle_noise_handshake_text(std::string_view text) {
this->noise_handshake_.reset();
this->noise_handshake_complete_ = true;
SS_LOGI(TAG, "Noise transport active (server_id=%s, psk_category=%d)",
this->server_information_.server_id.c_str(),
static_cast<int>(this->get_psk_category()));
this->server_id_.c_str(), static_cast<int>(this->get_psk_category()));
}

// NEED_MORE, or COMPLETE handled above: nothing else to do until the next frame.
Expand Down Expand Up @@ -334,9 +333,9 @@ bool SendspinConnection::handle_noise_rehandshake(std::span<uint8_t> msg1_bytes)
SS_LOGE(TAG, "handle_noise_rehandshake: no handshake hash available");
return false;
}
auto result = run_rehandshake_msg1(
msg1_bytes, this->server_information_.server_id, *this->noise_identity_,
*this->noise_record_store_, this->noise_suite_name_, prior_h.value(), *this->json_arena_);
auto result = run_rehandshake_msg1(msg1_bytes, this->server_id_.view(), *this->noise_identity_,
*this->noise_record_store_, this->noise_suite_name_,
prior_h.value(), *this->json_arena_);
if (!result.has_value()) {
SS_LOGW(TAG, "handle_noise_rehandshake: re-handshake failed; closing connection");
return false;
Expand Down Expand Up @@ -370,9 +369,9 @@ bool SendspinConnection::handle_noise_rehandshake(std::span<uint8_t> msg1_bytes)
// connection.md "Re-handshake": neither hello is re-sent, so the hello state carries over
// untouched; the server's first message under the new keys is server/activate, which
// first_activate_received_ (cleared above) now waits on.
SS_LOGI(
TAG, "Noise re-handshake complete: server_id=%s psk_category=%d; awaiting server/activate",
this->server_information_.server_id.c_str(), static_cast<int>(this->get_psk_category()));
SS_LOGI(TAG,
"Noise re-handshake complete: server_id=%s psk_category=%d; awaiting server/activate",
this->server_id_.c_str(), static_cast<int>(this->get_psk_category()));
return true;
}

Expand Down
28 changes: 15 additions & 13 deletions src/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,14 @@ class SendspinConnection : public std::enable_shared_from_this<SendspinConnectio

/// @brief Gets the server ID from the Noise handshake result (set at COMPLETE; empty until
/// then when a Noise handshake is installed on this connection).
const std::string& get_server_id() const {
return this->server_information_.server_id;
const IdText& get_server_id() const {
return this->server_id_;
}

/// @brief Gets the server information from the server/hello message (empty until received).
const ServerInformationObject& get_server_information() const {
return this->server_information_;
/// @brief Gets the server's friendly name from the server/hello message (empty until
/// received).
const ServerName& get_server_name() const {
return this->server_name_;
}

/// @brief Whether server/hello's source@v1 support object lists `codec` among the codecs the
Expand Down Expand Up @@ -683,7 +684,7 @@ class SendspinConnection : public std::enable_shared_from_this<SendspinConnectio
/// Called at COMPLETE in connection.cpp.
void set_noise_handshake_result(std::string_view server_id, PskCategory psk_category,
std::string_view psk_id) {
this->server_information_.server_id = server_id;
this->server_id_.assign(server_id);
this->psk_category_ = psk_category;
this->psk_id_.assign(psk_id);
}
Expand All @@ -707,10 +708,10 @@ class SendspinConnection : public std::enable_shared_from_this<SendspinConnectio
this->server_source_codecs_ = codecs.value_or(0);
}

/// @brief Sets the server's display name from server/hello; the server_id the handshake
/// @brief Sets the server's friendly name from server/hello; the server_id the handshake
/// recorded stays.
void set_server_name(std::string name) {
this->server_information_.name = std::move(name);
void set_server_name(const ServerName& name) {
this->server_name_ = name;
}

// ========================================
Expand Down Expand Up @@ -1009,10 +1010,11 @@ class SendspinConnection : public std::enable_shared_from_this<SendspinConnectio
MessageTracer message_tracer_;
#endif

/// Server identity: name from server/hello, server_id from the Noise handshake result
/// (or re-handshake; unchanged across a re-handshake since it is the same server). Protocol
/// task only; ConnectionManager publishes a copy to other threads at admission.
ServerInformationObject server_information_{};
/// Server identity: server_id from the Noise handshake result (or re-handshake; unchanged
/// across a re-handshake since it is the same server), name from server/hello. Protocol task
/// only; ConnectionManager publishes a copy to other threads at admission.
IdText server_id_{};
ServerName server_name_{};

/// The pairing record staged by the attempt in flight, committed by the
/// server/pair-finalize handler (nullopt = nothing to store). Protocol task only.
Expand Down
40 changes: 23 additions & 17 deletions src/connection_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,16 @@ std::optional<TimeTransform> ConnectionManager::clock() const {
}

std::optional<ServerInformationObject> ConnectionManager::server_information() const {
std::lock_guard<std::mutex> lock(this->published_mutex_);
return this->published_.server_information;
std::optional<PublishedServer> server;
{
std::lock_guard<std::mutex> lock(this->published_mutex_);
server = this->published_.server;
}
if (!server.has_value()) {
return std::nullopt;
}
return ServerInformationObject{.server_id = std::string(server->server_id.view()),
.name = std::string(server->name.view())};
}

// ============================================================================
Expand Down Expand Up @@ -892,7 +900,7 @@ void ConnectionManager::on_pairing_succeeded(SendspinConnection* conn) {
if (this->pairing_window_conn_ != nullptr && this->pairing_window_conn_ == conn) {
this->close_pairing_window();
}
this->client_->note_pairing_succeeded(conn->get_server_id());
this->client_->note_pairing_succeeded(conn->get_server_id().view());
}

void ConnectionManager::on_connection_lost(SendspinConnection* conn) {
Expand Down Expand Up @@ -1041,13 +1049,7 @@ void ConnectionManager::refresh_published_state() {
// The server information changes only with the primary connection: it is complete before
// admission (server/hello precedes it).
const bool primary_changed = primary_id != this->published_primary_id_;
std::optional<ServerInformationObject> info;
if (primary_changed) {
this->published_primary_id_ = primary_id;
if (primary_conn != nullptr) {
info = primary_conn->get_server_information();
}
}
this->published_primary_id_ = primary_id;
// Each admitted slot's clock, which a measurement, the slot's connection or the primary moves.
// The protocol task is published_'s only writer, so it compares without the lock, and a tick
// that changed nothing takes none.
Expand All @@ -1061,10 +1063,14 @@ void ConnectionManager::refresh_published_state() {
primary != nullptr ? static_cast<size_t>(primary - this->admitted_.data()) : MAX_ADMITTED;
if (primary_changed || clocks != this->published_.clocks ||
primary_slot != this->published_.primary_slot) {
// Swapped under the leaf lock; the server information it replaces is destroyed outside it.
std::lock_guard<std::mutex> lock(this->published_mutex_);
if (primary_changed) {
this->published_.server_information.swap(info);
if (primary_changed && primary_conn == nullptr) {
this->published_.server.reset();
} else if (primary_changed) {
// Written in place: the slot is the only copy.
PublishedServer& server = this->published_.server.emplace();
server.server_id = primary_conn->get_server_id();
server.name = primary_conn->get_server_name();
Comment thread
kahrendt marked this conversation as resolved.
Resolved
}
this->published_.clocks = clocks;
this->published_.primary_slot = primary_slot;
Expand Down Expand Up @@ -1668,10 +1674,10 @@ bool ConnectionManager::should_switch_to_new_server(const SendspinConnection* ad
const bool pairing_in_flight = !has_admitted || !admitted->is_pairing_finalized();
return should_admit_connection(
/*incoming_activities=*/new_conn->get_activities(),
/*incoming_server_id=*/new_conn->get_server_id(),
/*incoming_server_id=*/new_conn->get_server_id().view(),
/*admitted_activities=*/
has_admitted ? admitted->get_activities() : ActivitySet{},
/*admitted_server_id=*/has_admitted ? admitted->get_server_id() : std::string{},
/*admitted_server_id=*/has_admitted ? admitted->get_server_id().view() : std::string_view{},
/*has_admitted=*/has_admitted,
/*last_playback_server_id=*/this->last_played_server_id_.view(),
/*admitted_pairing_in_flight=*/pairing_in_flight);
Expand All @@ -1686,14 +1692,14 @@ void ConnectionManager::note_playback_activity(const SendspinConnection* conn) {
if (!conn->has_activity(SendspinActivity::PLAYBACK)) {
return;
}
const std::string& server_id = conn->get_server_id();
const IdText& server_id = conn->get_server_id();
if (server_id.empty()) {
return;
}
// The RAM half updates here, because a later arbitration reads last_played_server_id_; the
// provider writes for this and for the recency move below wait for
// SendspinClient::flush_pending_persistence() on the main loop.
this->client_->note_last_played_server(server_id);
this->client_->note_last_played_server(server_id.view());
// A pair-finalize at capacity evicts against the recency order and spares only the records
// of open connections, so a connection that closes later must not leave its record looking
// least recent until a deferred move lands. RecordStore::mutex_ is a leaf (docs/conventions.md,
Expand Down
25 changes: 16 additions & 9 deletions src/connection_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,10 @@ class ConnectionManager {
/// connection or its filter.
std::optional<TimeTransform> clock() const;

/// @brief Returns a copy of the primary admitted connection's server information, or nullopt
/// when none is admitted. Any thread: reads a slot written at admission and cleared on the
/// drop, behind the leaf published_mutex_.
/// @brief Returns the primary admitted connection's server information, or nullopt when none
/// is admitted. Any thread: copies the inline id and name out of a slot written at admission
/// and cleared on the drop, behind the leaf published_mutex_, and builds the strings on the
/// caller's thread.
std::optional<ServerInformationObject> server_information() const;

// ========================================
Expand Down Expand Up @@ -789,8 +790,7 @@ class ConnectionManager {
/// @param server_id conn->get_server_id(), captured by handle_enter_pairing().
/// @param selected_method The selected method; DYNAMIC_PAIRING_CODE or STATIC_PAIRING_CODE.
void handle_enter_pairing_code(SendspinConnection* conn, uint32_t pairing_index,
const std::string& server_id,
SendspinPairMethod selected_method);
const IdText& server_id, SendspinPairMethod selected_method);

/// @brief Runs the Pairing-PSK branch of handle_enter_pairing(): resolves the pairing
/// outcome and sends client/pair-init followed by client/pair-finalize with the long-term
Expand All @@ -799,7 +799,7 @@ class ConnectionManager {
/// @param pairing_index Current pairing_index counter, captured by handle_enter_pairing().
/// @param server_id conn->get_server_id(), captured by handle_enter_pairing().
void handle_enter_pairing_psk(SendspinConnection* conn, uint32_t pairing_index,
const std::string& server_id);
const IdText& server_id);

/// @brief Handles a pair/abort on an admitted connection.
/// Cleans up pairing state. Per pairing.md "pair/abort", only closes the connection for reason
Expand Down Expand Up @@ -995,15 +995,22 @@ class ConnectionManager {
/// @brief Each admitted slot's clock (admitted_[slot]), nullopt for a free slot
using ClockSlots = std::array<std::optional<TimeTransform>, MAX_ADMITTED>;

/// @brief The primary connection's server identity, held inline so the publish copies it
/// without a heap allocation
struct PublishedServer {
IdText server_id{};
ServerName name{};
};

/// @brief What other threads read of the admitted connections
struct PublishedState {
ClockSlots clocks{};
/// The slot in clocks of the primary connection (primary()), MAX_ADMITTED for none.
size_t primary_slot{MAX_ADMITTED};
/// The primary connection's server information.
std::optional<ServerInformationObject> server_information;
/// The primary connection's server identity.
std::optional<PublishedServer> server;
};
/// Guards published_. A leaf (docs/conventions.md): held only to copy or swap a member, so it
/// Guards published_. A leaf (docs/conventions.md): held only to copy a member, so it
/// also serialises the role threads' clock() reads behind server_information() copies.
mutable std::mutex published_mutex_;
/// Written by the protocol task (refresh_published_state()), which, as its only writer, also
Expand Down
Loading
Loading