diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/tokio-0.1.11/examples/hello_world.rs | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/tokio-0.1.11/examples/hello_world.rs')
-rw-r--r-- | third_party/rust/tokio-0.1.11/examples/hello_world.rs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/third_party/rust/tokio-0.1.11/examples/hello_world.rs b/third_party/rust/tokio-0.1.11/examples/hello_world.rs new file mode 100644 index 0000000000..398ec11aac --- /dev/null +++ b/third_party/rust/tokio-0.1.11/examples/hello_world.rs @@ -0,0 +1,70 @@ +//! Hello world server. +//! +//! A simple server that accepts connections, writes "hello world\n", and closes +//! the connection. +//! +//! You can test this out by running: +//! +//! cargo run --example hello_world +//! +//! And then in another terminal run: +//! +//! telnet localhost 6142 +//! + +#![deny(warnings)] + +extern crate tokio; + +use tokio::io; +use tokio::net::TcpListener; +use tokio::prelude::*; + +pub fn main() { + let addr = "127.0.0.1:6142".parse().unwrap(); + + // Bind a TCP listener to the socket address. + // + // Note that this is the Tokio TcpListener, which is fully async. + let listener = TcpListener::bind(&addr).unwrap(); + + // The server task asynchronously iterates over and processes each + // incoming connection. + let server = listener.incoming().for_each(|socket| { + println!("accepted socket; addr={:?}", socket.peer_addr().unwrap()); + + let connection = io::write_all(socket, "hello world\n") + .then(|res| { + println!("wrote message; success={:?}", res.is_ok()); + Ok(()) + }); + + // Spawn a new task that processes the socket: + tokio::spawn(connection); + + Ok(()) + }) + .map_err(|err| { + // All tasks must have an `Error` type of `()`. This forces error + // handling and helps avoid silencing failures. + // + // In our example, we are only going to log the error to STDOUT. + println!("accept error = {:?}", err); + }); + + println!("server running on localhost:6142"); + + // Start the Tokio runtime. + // + // The Tokio is a pre-configured "out of the box" runtime for building + // asynchronous applications. It includes both a reactor and a task + // scheduler. This means applications are multithreaded by default. + // + // This function blocks until the runtime reaches an idle state. Idle is + // defined as all spawned tasks have completed and all I/O resources (TCP + // sockets in our case) have been dropped. + // + // In our example, we have not defined a shutdown strategy, so this will + // block until `ctrl-c` is pressed at the terminal. + tokio::run(server); +} |