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
72 changes: 46 additions & 26 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,43 @@ holds only what is specific to trystero.
alone in a call of their own, without it, so the lint analyses the timer,
the sealer and the WebSockets without exceptions too. trystero raises
nothing of its own; a raise would go through `boost::throw_exception`.
- **The external dependencies.** libsecp256k1 (every room), libdatachannel
and OpenSSL (the native backend) are not part of Boost. `build.jam` finds
them with webcpp's `webcpp.external` and `webcpp.external-found`
(`tools/webcpp.jam`): where `-sSECP256K1_ROOT=<dir>`,
`-sSECP256K1_EMSCRIPTEN_ROOT=<dir>`, `-sLIBDATACHANNEL_ROOT=<dir>` and
`-sOPENSSL_ROOT=<dir>` on b2's command line say, a relative one from b2's
working directory, never the environment,
else in the superproject's `.local/secp256k1-native`,
`.local/secp256k1-emscripten`, `.local/libdatachannel` and
`.local/openssl` when it holds the library's header, where webcpp's CI
actions install them, until webcpp builds them from `third_party/`; so the
reference and the lint need no `-s`. A library found in neither is looked
for, natively outside Windows, in `/usr/include` or `/usr/local/include`,
where a system install is legitimate. A `-s`
that names a directory without its library's header stops the build,
naming it. A library that no candidate holds leaves trystero out, its
targets skipped, with one message that names the library and every place
it looked, so that `b2 test` builds every other library; with
`webcpp-require-external=on`, as the CI builds, it stops the build
instead. With MSVC the names are CMake's and Shining
Light's: `libsecp256k1`, linked with `SECP256K1_STATIC` defined, which a
static libsecp256k1 needs on Windows, and `libssl` and `libcrypto`, also
searched in `lib/VC/x64/MD`. libdatachannel is a
C++ library: it must be built with the same standard library as the
program that links it (libc++ for clang on macOS, libstdc++ for GCC), or
the program does not link, though the native headers still compile alone.
- **The dependencies.** libsecp256k1 (every room) remains an external
installation, found through `webcpp.external` and `webcpp.external-found`
at `-sSECP256K1_ROOT=<dir>` or `-sSECP256K1_EMSCRIPTEN_ROOT=<dir>`, then
`.local/secp256k1-native` or `.local/secp256k1-emscripten`. Native system
fallback outside Windows is allowed only for this dependency. A missing
installation leaves trystero out unless `webcpp-require-external=on`.
MSVC links its static `libsecp256k1.lib` with `SECP256K1_STATIC`.
The native backend instead names `/webcpp/third_party//libdatachannel`,
which builds pinned libdatachannel 0.24.6, libjuice, usrsctp and libsrtp as
separate static archives against `/webcpp/third_party//openssl` and its
matching generated headers. These are the same OpenSSL target used by
Asio and Beast, so a combined application has one provider. No
`LIBDATACHANNEL_ROOT`, `OPENSSL_ROOT` or installed binary is consulted.
Initialize their sources with `git submodule update --init --recursive
third_party/libs/libdatachannel third_party/libs/openssl`. Native builds
need CMake 3.21 or newer in addition to OpenSSL's Perl and Make/nmake.
B2 forwards the configured compiler, C++ ABI, runtime and optimization
flags, and isolates artifacts by their effective configuration. The
upstream source trees stay unchanged; verified staged copies are built.
- **Native threading.** libdatachannel always uses worker threads, so every
consumer of `/webcpp/trystero//native` must declare `<threading>multi`,
including native headers-alone and reference targets. An explicit
single-threaded native backend fails with an actionable B2 diagnostic.
Asio and Beast still support their own single-threaded OpenSSL builds.
- **Native channel handoff.** Valid frames received before a message handler
is installed wait in an app-resource FIFO, at most 64 frames for 10 s from
the first. Overflow or expiry closes the channel; close clears the queue.
`native/timer.hpp` holds the existing `native::timer`, still included by
`native/backend.hpp`, whose generation invalidates queued expiries.
Installing a receiver posts a resource-allocated flush, never an inline
call. Each frame checks the current handler and open channel, and a message
already posted ahead of the flush drains older frames when a receiver
exists. A transport closed during adoption, before binding, forgets its
negotiation and restarts announcements. `native_peer_connection` tests
real queued callbacks, FIFO, replacement, close, limits and resource release;
`shared` tests closure during adoption, and interop controls native open
delivery while a JS peer sends both rooms' presence first.
- **embind.** Every program on emscripten links `-lembind`, which
`/webcpp/trystero//trystero` adds as a linker flag: `<embind>on`, b2's own
feature, is not free, and b2 ignores it among usage requirements.
Expand Down Expand Up @@ -278,6 +289,15 @@ holds only what is specific to trystero.
while another room pings; (9) a held handshake frame recovering in both
id orders with no false password report. Crossed adoption races allow
retries but fail by connection count above four constructions per side.
- Recovered-peer relay scenarios wait for fresh, currently joined peers on
both sides within the existing 30 s setup bound, then require a fresh
round-trip message before injecting a relay fault. A join reported by one
side alone does not prove the other's handshake completed. The controlled
`delayNextRecovery` fixture delays one outbound direction of the first
recovered channel for 12 s, beyond the C++ handshake deadline; a subsequent
connection must complete the setup. A departure after the relay fault
still fails. Console errors remain fatal, with RTCError fields recorded
alongside peer events so a failure is not reduced to `OperationError`.
- Every peer gathers its ICE candidates on the machine alone: the C++
peers get `stun:127.0.0.1:9`, where nothing answers, in place of
Trystero's public STUN servers, the native peer binds its connections to
Expand Down
48 changes: 21 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ send a wrong room proof, reported by the lower id only.
## Building inside the superproject

trystero is developed inside the webcpp superproject, as a Boost library is
developed inside Boost. Besides Boost, it needs three libraries that are not
part of it, until webcpp builds them from `third_party/`:
developed inside Boost. Its TLS and WebRTC dependencies are built from
pinned sources; only libsecp256k1 needs an external installation:

| Library | Needed by | Found where |
| --- | --- | --- |
| libsecp256k1 0.8.0, with its `schnorrsig` and `extrakeys` modules | every room: it signs the Nostr events peers meet through | `-sSECP256K1_ROOT=<dir>` natively, `-sSECP256K1_EMSCRIPTEN_ROOT=<dir>` for its emscripten build, else `.local/secp256k1-native` and `.local/secp256k1-emscripten` |
| libdatachannel 0.24.6 | the native backend | `-sLIBDATACHANNEL_ROOT=<dir>`, else `.local/libdatachannel` |
| OpenSSL 3 | the native backend | `-sOPENSSL_ROOT=<dir>`, else `.local/openssl` |
| libdatachannel 0.24.6 | the native backend | `/webcpp/third_party//libdatachannel`, from pinned source submodules |
| OpenSSL 3.5.9 | the native backend | `/webcpp/third_party//openssl`, also used by Asio and Beast |

Each `-s`, on b2's command line, names a directory with the library's
The libsecp256k1 `-s` option, on b2's command line, names a directory with the library's
`include/` and `lib/`; the environment is never read. Without it, the library
is looked for in the superproject's `.local/` directory the table names,
where webcpp's CI installs it, and else, natively outside Windows, in
Expand All @@ -95,7 +95,7 @@ project uses the rooms and the browser backend through
libsecp256k1 and Boost.JSON's definitions, compiled once per variant, and on
emscripten embind and `BOOST_ASIO_DISABLE_THREADS`. A native program uses
`/webcpp/trystero//native`, which adds libdatachannel and OpenSSL, and
declares `<exception-handling>on` itself, as `example/Jamfile` does: b2 lets
declares `<threading>multi` and `<exception-handling>on` itself, as `example/Jamfile` does: b2 lets
no usage requirement override a build's `exception-handling=off`.

## The examples
Expand Down Expand Up @@ -189,7 +189,7 @@ struct application {
<!-- include::example/Jamfile[tag=native] -->
```
webcpp.link native_quick_start : native_quick_start.cpp
: <library>/webcpp/trystero//native <exception-handling>on : native ;
: <library>/webcpp/trystero//native <threading>multi <exception-handling>on : native ;
```

<!-- include::example/Jamfile[tag=browser] -->
Expand All @@ -212,11 +212,11 @@ says otherwise.

### 1. Install the tools

git, CMake, clang (the Xcode command line tools), Python 3, Node, OpenSSL 3,
Boost 1.92 and b2:
git, CMake 3.21 or newer, clang (the Xcode command line tools), Python 3,
Node, Perl, Make, Boost 1.92 and b2:

```sh
brew install cmake openssl@3 boost boost-build node
brew install cmake boost boost-build node
```

### 2. Create a workspace
Expand Down Expand Up @@ -262,28 +262,22 @@ cmake --build build-secp256k1-native && cmake --install build-secp256k1-native
cmake --build build-secp256k1-emscripten && cmake --install build-secp256k1-emscripten
```

### 6. Build libdatachannel
### 6. Initialize the native dependencies

libdatachannel's CMake finds OpenSSL where `OPENSSL_ROOT_DIR` says, which
Homebrew's OpenSSL needs, and the build where `webcpp/.local/openssl` links. It is built with the compiler that builds the
programs, clang: a program built with GCC and libstdc++ does not link against
a libdatachannel built with libc++.
The recursive checkout already includes pinned OpenSSL, libdatachannel,
libjuice, usrsctp, plog and libsrtp sources. For an existing checkout:

```sh
openssl=$(brew --prefix openssl@3)
ln -s "$openssl" webcpp/.local/openssl

git clone --recurse-submodules --branch v0.24.6 \
https://github.com/paullouisageneau/libdatachannel.git
cmake -S libdatachannel -B build-libdatachannel -DCMAKE_BUILD_TYPE=Release \
-DNO_WEBSOCKET=ON -DNO_EXAMPLES=ON -DNO_TESTS=ON -DOPENSSL_ROOT_DIR=$openssl \
-DCMAKE_INSTALL_PREFIX=$PWD/webcpp/.local/libdatachannel
cmake --build build-libdatachannel && cmake --install build-libdatachannel
git -C webcpp submodule update --init --recursive third_party/libs/openssl third_party/libs/libdatachannel
```

The three libraries are now where the build looks for them, in
`webcpp/.local/`, which git ignores: no variable is needed, in this terminal
or another.
B2 builds them with the selected compiler, runtime and C++ ABI when the
native backend is first needed. Each upstream checkout remains unchanged.
The generated headers and static archives are kept together by build
configuration. Asio, Beast and Trystero share one OpenSSL provider in a
combined application; no system OpenSSL or installed libdatachannel is used.
Native Trystero targets declare `<threading>multi` because WebRTC runs
worker threads. The supplied examples and tests declare it already.

### 7. Tell b2 its compilers and Boost

Expand Down
51 changes: 10 additions & 41 deletions build.jam
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,16 @@

project /webcpp/trystero ;

# libsecp256k1, libdatachannel and OpenSSL, which webcpp does not build
# until it builds them from third_party/, are found where
# -sSECP256K1_ROOT=<dir>, -sSECP256K1_EMSCRIPTEN_ROOT=<dir>,
# -sLIBDATACHANNEL_ROOT=<dir> and -sOPENSSL_ROOT=<dir> on b2's command line
# say, a relative one from b2's working directory, else in the
# superproject's .local/, where webcpp's CI installs them, else, natively
# outside Windows, in /usr/include or /usr/local/include, where a system
# install is legitimate (webcpp.external and webcpp.external-found). The
# environment is never read. When none holds a library, trystero is left
# out, with one message that names it, unless the build gives
# webcpp-require-external=on, as the CI does, which stops it instead.
# Only libsecp256k1 is externally installed. Native WebRTC and OpenSSL are
# pinned source dependencies, built together for the consumer's B2 variant.
import errors ;
import webcpp ;

# MSVC's names are CMake's there: libsecp256k1.lib, and OpenSSL's libssl.lib
# and libcrypto.lib, which an installation of Shining Light Productions, as
# GitHub's Windows images have, keeps in lib/VC/x64/MD. A static
# libsecp256k1 on Windows needs SECP256K1_STATIC, or its header declares
# every function imported from a DLL.
# tag::dependencies[]
local secp256k1-root = [ webcpp.external trystero : libsecp256k1
: SECP256K1_ROOT : secp256k1-native : secp256k1.h : native ] ;
local secp256k1-emscripten-root = [ webcpp.external trystero : libsecp256k1
: SECP256K1_EMSCRIPTEN_ROOT : secp256k1-emscripten : secp256k1.h : emscripten ] ;
local libdatachannel-root = [ webcpp.external trystero : libdatachannel
: LIBDATACHANNEL_ROOT : libdatachannel : rtc/rtc.hpp : native ] ;
local openssl-root = [ webcpp.external trystero : OpenSSL : OPENSSL_ROOT : openssl
: openssl/ssl.h : native ] ;
lib secp256k1 : : <name>secp256k1 <toolset>emscripten
<search>$(secp256k1-emscripten-root)/lib
: : <include>$(secp256k1-emscripten-root)/include ;
Expand All @@ -45,24 +28,8 @@ lib secp256k1 : : <name>libsecp256k1 <toolset>msvc
: : <include>$(secp256k1-root)/include <define>SECP256K1_STATIC ;
lib secp256k1 : : <name>secp256k1 <search>$(secp256k1-root)/lib
: : <include>$(secp256k1-root)/include ;
lib datachannel : : <name>datachannel <search>$(libdatachannel-root)/lib
: : <include>$(libdatachannel-root)/include
<dll-path>$(libdatachannel-root)/lib ;
lib ssl : : <name>libssl <toolset>msvc <search>$(openssl-root)/lib
<search>$(openssl-root)/lib/VC/x64/MD
: : <include>$(openssl-root)/include ;
lib ssl : : <name>ssl <search>$(openssl-root)/lib
: : <include>$(openssl-root)/include ;
lib crypto : : <name>libcrypto <toolset>msvc <search>$(openssl-root)/lib
<search>$(openssl-root)/lib/VC/x64/MD
: : <include>$(openssl-root)/include ;
lib crypto : : <name>crypto <search>$(openssl-root)/lib
: : <include>$(openssl-root)/include ;
# Searched libraries, used only through <library>: built as dependencies, with
# the properties of what uses them. Requested alone, by b2 at the
# superproject's root or b2 libs/trystero, their alternatives would be matched
# against a request without the <cxxstd>20 each inherits, and none chosen.
explicit secp256k1 datachannel ssl crypto ;
# Searched libraries are used only through usage requirements.
explicit secp256k1 ;
# end::dependencies[]

# The dependencies of the rooms: libsecp256k1, the emscripten build of it on
Expand All @@ -76,8 +43,10 @@ rule trystero-dependencies ( properties * )
# uses it is built.
rule native-dependencies ( properties * )
{
return [ webcpp.external-found trystero : libdatachannel OpenSSL
: $(properties) ] ;
if <threading>single in $(properties)
{
errors.user-error "Trystero's native backend uses worker threads. Add <threading>multi to the consuming target or pass threading=multi." ;
}
}

# On emscripten, the browser has no threads (the event loop drives Asio), and
Expand All @@ -100,5 +69,5 @@ alias trystero : : <conditional>@trystero-dependencies
# a build's exception-handling=off.
# tag::native[]
alias native : : <conditional>@native-dependencies
: : <library>trystero <library>datachannel <library>ssl <library>crypto ;
: : <library>trystero <library>/webcpp/third_party//libdatachannel ;
# end::native[]
2 changes: 1 addition & 1 deletion doc/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ webcpp.doc trystero : trystero.adoc ;
# brings libdatachannel and OpenSSL, and the browser build's define of
# build.jam, which a requirement conditioned on the emscripten toolset does
# not give it.
webcpp.reference trystero : <library>/webcpp/trystero//native
webcpp.reference trystero : <library>/webcpp/trystero//native <threading>multi
<library>/webcpp//emscripten-reference <define>BOOST_ASIO_DISABLE_THREADS ;
Loading
Loading