22
33This module defines the [`Sender`] and [`Receiver`] traits, representing the
44endpoints of a one-way communications channel between a wgpu client and server.
5- A single `Sender` sends messages to a single `Receiver`. The two sides use
6- shared memory segments to hold message content and other common data. Remote
7- wgpu access requires two `Sender`/`Receiver` pairs, providing communication in
8- each direction between client and server. This documentation refers to an
9- interacting client and server as "counterparts".
5+ A single `Sender` sends messages to a single `Receiver`. A `Sender` can also
6+ create memory segments shared with the `Receiver` to hold message content and
7+ other common data.
8+
9+ Remote wgpu access requires two `Sender`/`Receiver` pairs, providing
10+ communication in each direction between client and server. This documentation
11+ refers to an interacting client and server as "counterparts".
12+
13+ `Sender` and `Receiver` are low-level traits. Using them entails working with
14+ raw pointers to shared memory segments, so applications will generally need to
15+ build higher-level abstractions around them them to provide well-typed
16+ interfaces, prevent data races, batch messages to reduce IPC overhead, and so
17+ on.
18+
19+ ## Senders
1020
11- `Sender` and `Receiver` are low-level traits, meant to be easy to implement in
12- terms of a wide range of operating system mechanisms:
21+ A [`Sender`] implementation is responsible for interacting with the operating
22+ system to create memory segments that are shared with the `Receiver`, and
23+ sending unbuffered messages to the `Receiver`. However, a `Sender` should be
24+ agnostic to the actual content of those memory segments and messages. The
25+ `Sender` trait is meant to be easy to implement in terms of a wide range of
26+ operating system mechanisms:
1327
1428- A Unix implementation might use [`mmap`] to create memory segments, and then
1529 send `SCM_RIGHTS` messages over [`AF_UNIX`] sockets to share them with their
@@ -18,9 +32,21 @@ terms of a wide range of operating system mechanisms:
1832- A Windows implementation might use [`CreateFileMappingW`] to create memory
1933 segments, and then use [`DuplicateHandle`] to share them.
2034
21- - For testing, the [`local]` module this crate provides a [`Sender`]
22- implementation in which both sides of the connection live in the same process.
23- "Shared" memory is simply an ordinary block of memory.
35+ - For testing, the [`transport::local`] module provides a [`Sender`]
36+ implementation in which both sides of the connection live in the
37+ same process. "Shared" memory is simply an ordinary block of memory.
38+
39+ ## Receivers
40+
41+ The [`Receiver`] trait is meant to be implemented by users of the transport,
42+ serving as the callback invoked when messages are received. Exactly how
43+ `Receiver`s get called is specific to the `Sender` implementation:
44+
45+ - A `Sender` implementation might spawn a thread to read messages from a socket
46+ and invoke the `Receiver` when a complete message has been received.
47+
48+ - A `Sender` implementation might register a listener with some sort of platform
49+ event loop, and have that listener call the `Receiver` when appropriate.
2450
2551The [`Sender`] and [`Receiver`] traits are meant to integrate smoothly with
2652existing interprocess communication mechanisms and event loops, like Firefox's
@@ -84,6 +110,7 @@ for the purposes of avoiding data races.
84110[`AF_UNIX`]: https://man7.org/linux/man-pages/man7/unix.7.html
85111[`CreateFileMappingW`]: https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-createfilemappingw
86112[`DuplicateHandle`]: https://learn.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-duplicatehandle
113+ [`transport::local`]: crate::transport::local
87114[`IPDL`]: https://firefox-source-docs.mozilla.org/ipc/ipdl.html
88115[`nsISerialEventTarget`]: https://searchfox.org/mozilla-central/rev/7f7e8f6e4b8e09b145d29a57a1b341c6b11f4225/xpcom/threads/nsISerialEventTarget.idl#25
89116[`flush_shared_memory_range`]: Sender::flush_shared_memory_range
@@ -179,8 +206,13 @@ pub trait Sender {
179206 /// The exact interpretation of `range` is entirely up to the user of the
180207 /// transport. In typical use, `range` refers to a byte range of `handle`
181208 /// that holds serialized messages of some sort, but the transport itself
182- /// doesn't assume that. Shared memory handles are meant to be conveyed
183- /// by serializing them the content of messages
209+ /// doesn't assume that.
210+ ///
211+ /// Since shared memory handles are ordinary integers and are automatically
212+ /// usable in both counterparts, handles other than `handle` itself can be
213+ /// conveyed to the counterpart simply by serializing them in the content of
214+ /// messages, or storing them in other shared memory segments where the
215+ /// counterpart can find them.
184216 ///
185217 /// [`receive_message`]: Receiver::receive_message
186218 fn send_message (
0 commit comments