Code Monkey home page Code Monkey logo

Comments (5)

alexcrichton avatar alexcrichton commented on May 22, 2024

Perhaps tokio-core's example echo program can help? You can just replace TCP with Unix in this case

from tokio-uds.

joerg-krause avatar joerg-krause commented on May 22, 2024

Thanks @alexcrichton ! I am trying to use it similar to the example (just without an echo):

extern crate futures;
extern crate tokio_core;
extern crate tokio_uds;

use futures::Stream;
use tokio_core::reactor::Core;
use tokio_uds::{UnixListener, UnixStream};

use std::io::Read;

fn main() {
    let mut core = Core::new().unwrap();
    let handle = core.handle();

    let ctl = UnixListener::bind("/var/run/hello.sock", &handle).unwrap();
    let incoming = ctl.incoming();

    let done = incoming.for_each(|(mut socket, addr): (UnixStream, _)| {
        println!("New Connection: {:?}", addr);
        
        // Read the stream
        let mut hello = String::new();
        socket.read_to_string(&mut hello).unwrap();

        Ok(())
    });

    core.run(done).unwrap();
}

Unfortunately, this fails at runtime when I send some data to the socket with the error message: "Resource temporarily unavailable".

I guess I am doing it wrong, but the UnixListener is different than the TcpListener.

from tokio-uds.

alexcrichton avatar alexcrichton commented on May 22, 2024

@joerg-krause yes the problem is that socket is nonblocking, so read_to_string will fail because it needs to block waiting for data. You may wish to try out the various combinators in tokio_core::io for now.

In general though all TCP examples should suffice as UDS examples

from tokio-uds.

joerg-krause avatar joerg-krause commented on May 22, 2024

Finally, I got a working example 😄

extern crate futures;
extern crate tokio_core;
extern crate tokio_uds;

use std::fs;
use std::str;

use futures::{Future, Stream};
use tokio_core::io::read_to_end;
use tokio_core::reactor::Core;
use tokio_uds::UnixListener;

fn main() {
    let mut core = Core::new().unwrap();
    let handle = core.handle();

    static PATH: &'static str = "/var/run/hello.sock";

    let listener = match UnixListener::bind(PATH, &handle) {
        Ok(m) => m,
        Err(_) => {
            fs::remove_file(PATH).unwrap();
            UnixListener::bind(PATH, &handle).unwrap()
        }
    };

    let task = listener.incoming().for_each(|(socket, _)| {
        let buf = Vec::new();
        let reader = read_to_end(socket, buf).map(|(_, _buf)| {
            println!("incoming: {:?}", str::from_utf8(&_buf).unwrap());
        }).then(|_| Ok(()));
        handle.spawn(reader);
        Ok(())
    });

    core.run(task).unwrap()
}

from tokio-uds.

alexcrichton avatar alexcrichton commented on May 22, 2024

Great!

from tokio-uds.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.