diff --git a/docs/integration-guide.md b/docs/integration-guide.md index c630d9f0..e62599d3 100644 --- a/docs/integration-guide.md +++ b/docs/integration-guide.md @@ -763,7 +763,7 @@ 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 @@ -771,7 +771,8 @@ if (!client.start()) { } // 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 @@ -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 | diff --git a/docs/internals.md b/docs/internals.md index 87bc7d5d..dee376e6 100644 --- a/docs/internals.md +++ b/docs/internals.md @@ -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: diff --git a/include/sendspin/client.h b/include/sendspin/client.h index c7185ca3..0534a212 100644 --- a/include/sendspin/client.h +++ b/include/sendspin/client.h @@ -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 diff --git a/include/sendspin/config.h b/include/sendspin/config.h index 225bb0e7..4f127165 100644 --- a/include/sendspin/config.h +++ b/include/sendspin/config.h @@ -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 diff --git a/src/connection_manager.cpp b/src/connection_manager.cpp index b0588010..82710f8b 100644 --- a/src/connection_manager.cpp +++ b/src/connection_manager.cpp @@ -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) { // 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(); diff --git a/tests/test_client_lifecycle.cpp b/tests/test_client_lifecycle.cpp index a567c359..d8dc1292 100644 --- a/tests/test_client_lifecycle.cpp +++ b/tests/test_client_lifecycle.cpp @@ -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 @@ -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