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: 4 additions & 2 deletions docs/integration-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -763,15 +763,16 @@ client.set_persistence_provider(&persistence_provider); // Optional
```cpp
// Start the role threads and the protocol task, and arm the WebSocket server (it listens before
// start() returns when the network provider already reports ready, otherwise once loop() finds it
// ready).
// ready; with accept_inbound_connections off, no server starts).
// Task priorities and PSRAM settings are taken from SendspinClientConfig.
if (!client.start()) {
// Handle failure
return 1;
}

// Optionally initiate a client-side connection to a known server URL.
// Without this, the client waits for incoming server connections.
// Without this, the client waits for incoming server connections. With
// accept_inbound_connections off, this is the only way a server reaches it.
client.connect_to("ws://192.168.1.10:8928/sendspin");

// Main loop: call loop() periodically to deliver callbacks. Connection work runs on the library's
Expand Down Expand Up @@ -1362,6 +1363,7 @@ X25519 keypair and read back via `client.client_id()` after `start()`.
| `protocol_task_priority` | `unsigned` | `5` | FreeRTOS priority for the protocol task (ESP-IDF only). Defaults to the httpd task's priority, below the player's sync task. |
| `protocol_task_stack_size` | `size_t` | `7168` | Protocol task stack size in bytes (ESP-IDF only). Every Noise handshake (including the in-band re-handshake after pairing, with its X25519 crypto), the pairing exchange (CPace and SHA-512), the JSON parse, the role handlers and every send run on this task. Derived as for `httpd_stack_size`. Values below the default are clamped up to it with a warning. |
| `server_port` | `uint16_t` | `8928` | WebSocket server port |
| `accept_inbound_connections` | `bool` | `true` | Whether to run the WebSocket server that servers connect to. `false` leaves `connect_to()` as the only way a server reaches the client, for a client that dials one configured server and must not be reachable by any other; `server_port` is then unused. |
| `server_max_connections` | `uint8_t` | `4` | Maximum simultaneous WebSocket connections (one established, two unproven, and one spare so a surplus peer can be rejected with a goodbye) |
| `httpd_ctrl_port` | `uint16_t` | `0` | ESP-IDF httpd control port; `0` uses `ESP_HTTPD_DEF_CTRL_PORT + 1` to avoid conflict with the web_server component |
| `time_burst_size` | `uint8_t` | `8` | Number of messages per time sync burst |
Expand Down
2 changes: 1 addition & 1 deletion docs/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ Each getter reads the slot once, so a caller that checks `is_time_synced()` and

### Client Start and Stop

`SendspinClient::start()` validates the pairing config, creates the `RecordStore` and identity, loads persisted state, creates the inbound ring whatever roles are enabled, since every admitted connection receives into it, the time replies included (see [The Inbound Ring](#the-inbound-ring)), starts the threaded roles with it (the source task, which needs no inbound ring, with the protocol task to wake), publishes the first `client/state` snapshot, opens `ConnectionManager` for admission (starting the WebSocket server at once when the network provider, polled here on the main loop, already reports ready; otherwise the protocol task starts it once a `loop()` finds the network ready and wakes it), and starts the protocol task. An accept the server delivers before the task starts waits in the command queue.
`SendspinClient::start()` validates the pairing config, creates the `RecordStore` and identity, loads persisted state, creates the inbound ring whatever roles are enabled, since every admitted connection receives into it, the time replies included (see [The Inbound Ring](#the-inbound-ring)), starts the threaded roles with it (the source task, which needs no inbound ring, with the protocol task to wake), publishes the first `client/state` snapshot, opens `ConnectionManager` for admission (starting the WebSocket server at once when the network provider, polled here on the main loop, already reports ready; otherwise the protocol task starts it once a `loop()` finds the network ready and wakes it; with `accept_inbound_connections` off, no server starts), and starts the protocol task. An accept the server delivers before the task starts waits in the command queue.

`SendspinClient::stop()` is synchronous and ordered so that every producer is gone before any state is reset:

Expand Down
3 changes: 2 additions & 1 deletion include/sendspin/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ class SendspinClient {
/// @brief Starts the role threads and the protocol task, and arms the WebSocket server
///
/// The server starts listening before this returns when the network provider already reports
/// ready, and otherwise on the protocol task once a loop() finds it ready. If a role fails to
/// ready, and otherwise on the protocol task once a loop() finds it ready. With
/// SendspinClientConfig::accept_inbound_connections off, no server starts. If a role fails to
/// start, the roles that did start are stopped again so a corrected retry begins from the
/// stopped state. A rejected configured pairing secret (SendspinClientConfig::pairing_psk or
/// static_pairing_code) fails every start() of this instance, since the config is fixed at
Expand Down
5 changes: 5 additions & 0 deletions include/sendspin/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ struct SendspinClientConfig {
///< + 1 (avoids conflict with web_server component)
uint16_t server_port{DEFAULT_SERVER_PORT}; ///< WebSocket server port

/// @brief Whether to run the WebSocket server that servers connect to. Off leaves connect_to()
/// as the only way a server reaches the client, for a client that dials one configured server
/// and must not be reachable by any other; `server_port` is then unused.
bool accept_inbound_connections{true};

/// @brief Default maximum simultaneous inbound connections: one established connection, two
/// unproven connections awaiting the hello handshake (the manager's nursery capacity), and
/// one spare so a surplus peer can still be accepted long enough to receive a graceful
Expand Down
2 changes: 1 addition & 1 deletion src/connection_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ void ConnectionManager::start() {
this->shutdown_ui_ = {false, false};
this->ws_server_start_retry_time_us_ = 0;

if (this->ws_server_ == nullptr) {
if (this->ws_server_ == nullptr && this->client_->config_.accept_inbound_connections) {
Comment thread
kahrendt marked this conversation as resolved.
Resolved
// First start: create the server object and configure it once. The config is immutable
// for the client's lifetime, so a restart reuses these values along with the object.
this->ws_server_ = std::make_unique<SendspinWsServer>();
Expand Down
38 changes: 38 additions & 0 deletions tests/test_client_lifecycle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ constexpr uint16_t VISUALIZER_HELD_TEARDOWN_TEST_PORT = 19104;
constexpr uint16_t HELLO_TEST_PORT = 19090;
constexpr uint16_t OUTBOUND_GOODBYE_TEST_PORT = 19069;
constexpr uint16_t NETWORK_PROVIDER_TEST_PORT = 19086;
constexpr uint16_t INBOUND_OFF_TEST_PORT = 19105;
#ifndef SENDSPIN_ENABLE_OPUS
constexpr uint16_t OPUS_STREAM_TEST_PORT = 19089;
#endif
Expand Down Expand Up @@ -2063,6 +2064,43 @@ TEST(ClientLifecycle, TheNetworkProviderIsCalledOnTheMainLoopOnly) {
}
}

// With accept_inbound_connections off the client never listens, yet connect_to() still dials.
// Control: the default listens on the configured port.
TEST(ClientLifecycle, TurningOffInboundConnectionsLeavesOnlyConnectTo) {
struct Row {
const char* name;
bool accept_inbound;
};
const Row rows[] = {
{"off: nothing listens", false},
{"Control: on: the server listens", true},
};
for (const Row& row : rows) {
SCOPED_TRACE(row.name);
TestNetworkProvider network;
SendspinClientConfig config = make_config(INBOUND_OFF_TEST_PORT);
config.accept_inbound_connections = row.accept_inbound;
SendspinClient client(std::move(config));
client.set_network_provider(&network);
ASSERT_TRUE(client.start());
pump_for(client, 100);
EXPECT_EQ(port_accepts(INBOUND_OFF_TEST_PORT), row.accept_inbound);

SilentListener silent;
ASSERT_NE(silent.port(), 0);
client.connect_to(loopback_url(silent.port()));
const int fd = silent.accept_connection();
EXPECT_GE(fd, 0) << "connect_to() did not dial";
// Ends the handshake at once, so the destructor does not wait out its timeout.
if (fd >= 0) {
::close(fd);
}
silent.close();
pump_for(client, 100);
client.stop();
}
}

// ConnectionManager::tick() returns the milliseconds until the earliest of its timers, one row per
// timer, so the protocol task sleeps exactly until the next one is due; with none armed it
// returns NO_DEADLINE and the task waits for a wake alone. The Control rows hold a connection, or
Expand Down
Loading