Commit Graph

27 Commits

Author SHA1 Message Date
bestgopher 0f6d1b8854 style(examples): use would_block and remove set_nonblocking 2022-06-27 09:48:34 +00:00
Tricster 6f99bc9104 Fix typo in example.
Signed-off-by: Tricster <mediosrity@gmail.com>
2022-06-09 08:01:35 +00:00
Harald Hoyer ad45d500dd Add tcp_listenfd_server example with target_os = "wasi" support
Use the `LISTEN_FDS` mechanism to use pre-opened sockets.

Especially for `wasm32-wasi` there is no other way to get access to sockets,
than to use pre-opened sockets.

Because `wasm32-wasi` does not yet return `TcpListener::local_addr()`, an
unspecified IP address and port will be returned and displayed.

```
$ cargo +nightly build --target wasm32-wasi --release --example tcp_listenfd_server --features="os-poll net"
   Compiling cfg-if v1.0.0
   Compiling wasi v0.10.2+wasi-snapshot-preview1
   Compiling log v0.4.14
   Compiling libc v0.2.112
   Compiling ppv-lite86 v0.2.15
   Compiling wasi v0.11.0+wasi-snapshot-preview1
   Compiling getrandom v0.2.3
   Compiling rand_core v0.6.3
   Compiling env_logger v0.8.4
   Compiling rand_chacha v0.3.1
   Compiling mio v0.8.0 (/home/harald/git/mio)
   Compiling rand v0.8.4
    Finished release [optimized] target(s) in 2.92s

$ wasmtime run --tcplisten 127.0.0.1:9000 --env 'LISTEN_FDS=1' target/wasm32-wasi/release/examples/tcp_listenfd_server.wasm
Using preopened socket FD 3
You can connect to the server using `nc`:
 $ nc <IP> <PORT>
You'll see our welcome message and anything you type will be printed here.
```

Signed-off-by: Harald Hoyer <harald@profian.com>
2022-03-08 20:27:18 +01:00
Harald Hoyer e2a7548eb7 Stub non-working examples for target_os = "wasi"
Otherwise `cargo test` fails.

Signed-off-by: Harald Hoyer <harald@profian.com>
2022-03-08 20:27:18 +01:00
Thomas de Zeeuw c6fc16df34 Deregister connection before dropping it in TCP example 2021-11-07 10:47:51 +01:00
cdcode 2ae3b3d74c Small spelling correction in example 2021-06-09 15:45:10 +00:00
Persevere Von 054a80ac05 docs: correct comments in examples 2021-02-04 09:27:30 +00:00
Thomas de Zeeuw 6aa966fe7b Read directly into Vec in tcp_server example 2020-10-23 19:46:14 +00:00
Thomas de Zeeuw a8d95a06cc Fix TCP example
This now response to edge-trigger events properly.
2020-03-26 16:34:52 +00:00
Jingjing Duan 3e60230617 Add comments on how to run the examples 2020-02-22 20:16:37 +00:00
Kevin Leimkuhler 1262f2afaa Introduce default API shim and feature flags for net/OS resources (#1192)
## Motivation

Closes #1118

Mio should be as light as possible and forwards compatible to other non-blocking
I/O strategies such as user-land sockets.

Currently, depending on Mio means that all platform-specific networking
primitives and I/O source readiness polling functionality is present by default.

If a library is interested only in networking primitives but aims to provide an
other strategy for I/O source readiness polling, there is no way to disable the
provided implementation.

If a library is interested in the provided I/O source readiness polling
implementations, but only uses the TCP networking types, the UDP and UDS types
are also still provided.

This change proposes that Mio by default provides an API shim for its common
types such as `Poll`, `Events`, etc. Attempting to use them will result in a
panic. TCP, UDP, and UDS networking types are **not** present by default and
require feature flags to opt-in.

## Solution

### Overview

Below are the four feature flags that Mio now includes as opt-in features. By
default, no functionality is provided--only an API shim.

- `os-poll`: Provide the cross-platform implementations of `Poll`. This is
  currently epoll, IOCP, or kqueue depending on the platform.

- `os-util`: Provide the cross-platform adapters for the platform's underlying
  I/O source that implements `event::Source`. Currently, only Unix provides it's
  `SourceFd` type. There are proposed solutions for a Windows equivalent being
  worked on.

- `tcp`: Provide the TCP types and implementations

- `udp`: Provide the UDP types and implementations

- `uds`: Provide the UDS types and implementations. Currently, only Unix
  provides types and implementations for this feature.

The above feature flags can be used in any combination. If a library opts in to
`os-poll` and `tcp`, then the `Poll` and TCP implementations will be provided.
Later, the library could provide it's own `Poll` implementation and opt-out of
`os-poll`. It could alternatively opt-in to UDP at a later point without issue.

### Details

Mio now has the following cfg macros: `cfg_os_poll`, `cfg_not_os_poll`,
`cfg_net`, `cfg_tcp`, `cfg_udp`, `cfg_uds`, `cfg_any_os_util` that conditionally
provide the related types and implementations. *(If any of these names are not
obvious about what they provide, please let me know!)*.

The macros--again similar to Tokio--conditionally provide shell types that
**panic upon use**, or hide/expose methods on types an API shim should not
provide (e.g. `Poll::new`).

## Tips for Review

This change touches a lot of files, but there are no intended functional or
behavioral changes. I would recommend first looking at what types are provided
by the root-level `lib.rs` or changes in the `mod.rs` files. *(If any of the
`mod.rs` files are not clear about what they conditionally expose, please let me
know!)*.

Next, I would look at the methods that are now conditionally provided on the
types. These are mostly confined to the OS specific `Poll` implementations, but
there are a few exceptions.

Finally, there are some further changes that reorganize `impl`s or move `use`
statements due to being conditionally used. Because this change is ultimately a
"refactor", I found myself moving some blocks of code around to group with
similar `impl`s and such. I'd like to keep those as part of this change (instead
of a separate PR), but please me know if have questions/concerns about any of
those.

Signed-off-by: Kevin Leimkuhler <kleimkuhler@icloud.com>
2019-12-10 14:30:59 -08:00
Thomas de Zeeuw 1341896ef7 Change event::Source to take a mutable reference
Generally speaking event source shouldn't concurrently need to be
(re/de)-registered. Even though on most platforms this is possible it
makes it more complex on platforms that need user space bookkeeping,
such as Windows.
2019-12-04 21:26:42 +00:00
Thomas de Zeeuw dcc1e463dd Rename Interests to Interest 2019-12-04 16:02:19 +00:00
Thomas de Zeeuw f06439fccb Log unexpected event in UDP example 2019-11-27 11:31:34 +00:00
Thomas de Zeeuw 2b43bd8fd4 Change TCP server example port to 9000
Port 13265 was a bit odd, 9000 is more common in examples.
2019-11-27 11:31:34 +00:00
Thomas de Zeeuw a885d22d98 Cleanup UDP example
Changes the structure a bit to match the TCP server example. Enables
logging and prints how the server can be accessed, also matching the TCP
server example
2019-11-27 11:31:34 +00:00
Mario Lang 487f65070c Run rustfmt and put back unused env_logger::init call 2019-11-10 14:50:58 +00:00
Mario Lang 58092e9e35 handle_connection_event just needs access to the Registry 2019-11-10 14:50:58 +00:00
Dmitrii Goriunov 09b03cbe6b Use 4096 size, remove useless recursion, only extend valid bytes 2019-09-17 08:18:29 +00:00
Dmitrii Goriunov 2d9b0c1a8a Return ok true 2019-09-17 08:18:29 +00:00
Dmitrii Goriunov 4ff200a5f9 If connection closed we still may have some data available 2019-09-17 08:18:29 +00:00
Dmitrii Goriunov f62e3b8cb4 Read till would block 2019-09-17 08:18:29 +00:00
Thomas de Zeeuw 758f7c6921 Add TCP server example
It's a simple example but it shows how to do error handling and what to
do when reading 0 bytes.
2019-09-13 09:22:49 +00:00
Heinz N. Gies 3e10b9e3e5 Fix typo and format code 2019-09-08 11:47:52 +00:00
Heinz N. Gies b72bfeaf86 Return instead of panicing 2019-09-08 11:47:52 +00:00
Heinz N. Gies 08adf56a5f Replace unwrap with io::Result 2019-09-08 11:47:52 +00:00
Heinz N. Gies bf91812358 Add udp echo server example 2019-09-08 11:47:52 +00:00