File: stream.rs

package info (click to toggle)
rust-interprocess 2.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,016 kB
  • sloc: makefile: 2
file content (55 lines) | stat: -rw-r--r-- 1,787 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//{
#[cfg(not(feature = "tokio"))]
fn main() {
    eprintln!("This example is not available when the Tokio feature is disabled.");
}
#[cfg(feature = "tokio")]
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    //}
    use {
        interprocess::local_socket::{
            tokio::{prelude::*, Stream},
            GenericFilePath, GenericNamespaced,
        },
        tokio::{
            io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
            try_join,
        },
    };

    // Pick a name.
    let name = if GenericNamespaced::is_supported() {
        "example.sock".to_ns_name::<GenericNamespaced>()?
    } else {
        "/tmp/example.sock".to_fs_name::<GenericFilePath>()?
    };

    // Await this here since we can't do a whole lot without a connection.
    let conn = Stream::connect(name).await?;

    // This consumes our connection and splits it into two halves, so that we can concurrently use
    // both.
    let (recver, mut sender) = conn.split();
    let mut recver = BufReader::new(recver);

    // Allocate a sizeable buffer for receiving. This size should be enough and should be easy to
    // find for the allocator.
    let mut buffer = String::with_capacity(128);

    // Describe the send operation as writing our whole string.
    let send = sender.write_all(b"Hello from client!\n");
    // Describe the receive operation as receiving until a newline into our buffer.
    let recv = recver.read_line(&mut buffer);

    // Concurrently perform both operations.
    try_join!(send, recv)?;

    // Close the connection a bit earlier than you'd think we would. Nice practice!
    drop((recver, sender));

    // Display the results when we're done!
    println!("Server answered: {}", buffer.trim());
    //{
    Ok(())
} //}