Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example for using tokio::net::UdpSocket in actix without UdpFramed #171

Open
PlaneB opened this issue Nov 18, 2018 · 3 comments
Open

Example for using tokio::net::UdpSocket in actix without UdpFramed #171

PlaneB opened this issue Nov 18, 2018 · 3 comments

Comments

@PlaneB
Copy link

@PlaneB PlaneB commented Nov 18, 2018

There is the example code https://gist.github.com/apatrushev/4bbddb65e9a35a72047e3b9993c6636e#gistcomment-2761661

for using UdPSockets in actix. However I would like to have an example more in the line of

https://github.com/tokio-rs/tokio/blob/master/examples/echo-udp.rs

using UdpSocket.poll_send_to and UdPsocket.poll_recv_from. Can someone write a simple example how to to this? It seems unnatural to implement tokio::Future for an actor and then use something like tokio::run(server.map_err(|e| println!("server error = {:?}", e))) in addition to to ator.start();

So maybe an explanation of using these UdPSocket functions "as intended" in actix would be great. I think it would fit the actix framework much better to run the loop of the second example in fn started(); However this seems not possible as the try_ready! macro seems to be different there... Maybe I miss the point.

@PlaneB

This comment has been minimized.

Copy link
Author

@PlaneB PlaneB commented Nov 18, 2018

Using the following code doesn't work, as it seems that its not polled on the appropriate network event:

fn started(&mut self, _ctx: &mut Self::Context) {
    let mut datagram = BytesMut::with_capacity(EVENT_SIZE);
    loop {
        println!("poll recv data...");
        match self.socket.poll_recv_from(&mut datagram) {
            Ok(Async::Ready((bytes, addr))) => {
                println!("Got {:?} from addr {:?}", bytes, addr);
            }
            Ok(Async::NotReady) => {
                println!("waiting....");
                return;
            }
            Err(e) => {
                println!("Error: {:?}; shutting down", e);
                return;
            }
        }
    }
}
@DoumanAsh

This comment has been minimized.

Copy link
Contributor

@DoumanAsh DoumanAsh commented Nov 18, 2018

The problem with UDP is that it is too low level to interact well with actix like that.
I would advise to wrap it into Stream that could produce datagrams and then handle it using StreamHandler in your actor

@PlaneB

This comment has been minimized.

Copy link
Author

@PlaneB PlaneB commented Nov 18, 2018

Ah ok. I will try

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.