Skip to content

Initialize Winsock before the host transports open a socket - #213

Merged
kahrendt merged 1 commit into
mainfrom
chrisuthe/winsock-init
Oct 28, 2026
Merged

kahrendt merged 1 commit into
mainfrom
chrisuthe/winsock-init

Conversation

@chrisuthe

Copy link
Copy Markdown
Member

On Windows every socket call fails until WSAStartup() runs, and IXWebSocket leaves that to the application through ix::initNetSystem(). The client could neither dial a server ("Either the application has not called WSAStartup") nor listen.

Summary

  • SendspinWsServer::start() and SendspinClientConnection::start() call ix::initNetSystem() once, through a function-local static, before their first socket. It is never undone; Windows releases it at process exit.
  • The two handle a failure differently on purpose. The server's start() returns a bool, so it logs and returns false. The client's start() has no failure return, so it logs and lets the attempt go ahead; its first socket call then fails and IXWebSocket reports it as the connection's Error, which already reports the close.

Notes

Testing

  • Windows 11, clang-cl 22.1.3 (from Fix what running on Windows turns up #211): basic_client dials Music Assistant with -u and listens on its own port; before this change both failed with the WSAStartup error.
  • Linux, GCC 16: host build clean; sendspin_tests all pass. The ASan/UBSan and TSan runs are this PR's CI (no sanitizer runtime on the local machine).

@kahrendt kahrendt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks

@kahrendt
kahrendt merged commit b0fcf5b into main Oct 28, 2026
16 checks passed
@kahrendt
kahrendt deleted the chrisuthe/winsock-init branch October 28, 2026 11:20
kahrendt added a commit that referenced this pull request Nov 4, 2026
Depends on #213 (Winsock init): this branch is stacked on it, and
`basic_client` cannot dial or listen on Windows without it. Review only
the top commit; the base switches to `main` once #213 merges.

## Summary

- **PortAudio on Windows.** Windows has no pkg-config or system
PortAudio, so `examples/basic_client/CMakeLists.txt` fetches PortAudio
v19.7.0 on `WIN32` and builds it as a static library. POSIX keeps the
pkg-config lookup.
- **`getopt()` for Windows.** `examples/common/win_getopt.h` is a
minimal stand-in for the missing one. It covers grouped flags, `-x val`
/ `-xval`, and stops at the first non-option or `--`. Errors return `?`
without a message, since the example prints its usage on that.
- **The pairing-window gesture.** Console keys `p` / `c` stand in for
`SIGUSR1` / `SIGUSR2`. A single `#ifdef _WIN32` block defines the key
poll (a no-op on POSIX) and the hint strings, so the usage text and the
pairing-window prompt name the right gesture on each platform.
- **Persistence.** The provider writes through `_open` / `_write` /
`_commit` and replaces the file with `std::filesystem::rename()`,
because `std::rename()` won't replace an existing file on Windows. It
falls back to `%USERPROFILE%` when `HOME` is unset. Each `write_file()`
variant has its own doc comment.
- **README.** `examples/basic_client/README.md` gets a Windows section
covering the build, no mDNS (so dial with `-u`), the `p`/`c` keys, and
the persistence file's permissions.
- **The default build.** `tui_client` includes POSIX `<getopt.h>`, so
the top-level `CMakeLists.txt` skips it on `WIN32` with a status
message, the way `source_client` already skips itself without PortAudio.
A standalone Windows build with the examples on (the default) now
succeeds.
- **CRT warnings.** `basic_client` defines `_CRT_SECURE_NO_WARNINGS` on
`WIN32`, since the persistence provider's `getenv()` and `_open()`
otherwise draw deprecation warnings pointing at `_dupenv_s` /
`_sopen_s`.

## Notes

- The persistence file can't be created 0600 on Windows. It inherits its
directory's ACL, which under `%USERPROFILE%` grants only the user,
SYSTEM and Administrators.
- `tui_client`, `source_client` and the tests remain POSIX-only. The
top-level README's "macOS/Linux" wording is left alone, since #210
deferred the library's Windows support scope.
- Split out of #211.

## Testing

- Windows 11, clang-cl 22.1.3 (Visual Studio 2026, Ninja, Release), from
#211: `sendspin` and `basic_client` built with no warnings from the
library's own sources. `basic_client.exe -p 8929 -u
ws://<music-assistant>:8927/sendspin` completed the Noise handshake, was
admitted (unpaired) and listened locally. Pairing and playback were not
exercised.
- Windows 11, clang-cl (Visual Studio 2026, Ninja), on this branch: a
clean configure and build with the default `BUILD_EXAMPLES` succeeds,
skipping `tui_client` and `source_client`, with no warnings from the
repo's own sources. `basic_client.exe -h` names the `p`/`c` keys, an
unknown option prints the usage, and `basic_client.exe -p 8931` starts
and listens on 8931.
- The trimmed `win_getopt.h` was checked on Linux against
`basic_client`'s optstring: grouped flags, `-pVAL`, `-p VAL`, `--`, a
lone `-`, an unknown option, a missing argument and `-:`.
- Linux, GCC 16: host build including the examples is clean, and
`sendspin_tests` passes 705/705. The sanitizer runs are this PR's CI.

---------

Co-authored-by: Kevin Ahrendt <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants