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

Duration optional? Forever? #3

Closed
kevinelliott opened this issue Dec 3, 2019 · 5 comments
Closed

Duration optional? Forever? #3

kevinelliott opened this issue Dec 3, 2019 · 5 comments
Labels
enhancement New feature or request

Comments

@kevinelliott
Copy link

What if we want the UDP listen to be forever or until interrupted in some way?

@xenoken xenoken added the enhancement New feature or request label Dec 6, 2019
@xenoken
Copy link
Owner

xenoken commented Dec 6, 2019

Hi @kevinelliott, thanks for using udp!
Version 3.0.0 introduces an optional duration parameter for udp.listen(). So now it's possible to listen indefinitely.
To stop listening, please call udp.close() on the udp instance.

@xenoken xenoken closed this as completed Dec 6, 2019
@kevinelliott
Copy link
Author

Hi @xenoken ... thank you for the library!

Upon removal of the duration, my application exits immediately and does not receive any UDP packets. I'm assuming now I need some kind of main state loop with sleeps in order to prevent the application from exiting?

@xenoken
Copy link
Owner

xenoken commented Dec 8, 2019

That's correct @kevinelliott.
When timeout is specified, udp.listen() returns a future that completes only after the time has run out. When timeout is not specified, udp.listen() returns a future that is already completed (with value true). In the latter case listen() behaves like a synchronous function call that returns immediately.

@kevinelliott
Copy link
Author

kevinelliott commented Dec 8, 2019

Ahh, I see... what I was looking for was a never ending listen... I need to process millions of UDP packets over time.

@xenoken
Copy link
Owner

xenoken commented Dec 9, 2019

Yes, udp.listen() is indeed never ending. It will keep listening for packets even though the function returns immediately. for example:

var receiver = await UDP.bind(Endpoint.loopback(port: Port(54321));

var success = await receiver.listen((datagram){
// ...your callback code here...
});

// even though the execution reaches this point, receiver is still listening to udp packets.
// what is required now is a way to prevent the application from exiting.

// ...end of program here...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants