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

Support a receive timeout for UDP connections #540

Closed
s-ludwig opened this issue Feb 20, 2014 · 10 comments
Closed

Support a receive timeout for UDP connections #540

s-ludwig opened this issue Feb 20, 2014 · 10 comments
Milestone

Comments

@s-ludwig
Copy link
Member

Add either an overload of receive that takes a timeout parameter, or add a separate waitForData method analogous to the one in TCPConnection.

@s-ludwig s-ludwig added this to the 0.7.19 milestone Feb 20, 2014
@zek
Copy link

zek commented Apr 21, 2014

Is this done?

@s-ludwig s-ludwig modified the milestones: 0.7.20, 0.7.19 Apr 21, 2014
@s-ludwig
Copy link
Member Author

Seems like this slipped the 0.7.19 milestone. It's marked now for 0.7.20. I'm still very busy with other issues, so I can't work on it right now yet, though.

@zek
Copy link

zek commented Apr 21, 2014

I see.
Is there any temporary solution to solve this problem?
I am sending udp packet and trying to receive but sometimes it just goes infinite loop it's waiting data

@s-ludwig
Copy link
Member Author

A relatively crude workaround would go similar to this:

ubyte[] readTimeout(UDPConnection udp, ubyte[] buffer, Duration timeout)
{
    ubyte[] ret;
    Exception ex;
    auto read_task = runTask({
        try ret = udp.read(buffer);
        catch (Exception e) ex = e;
    }
    auto timer = setTimer(timeout, { read_task.interrupt(); });
    read_task.join();
    timer.stop();
    if (ex) throw ex;
    return ret;
}

(I didn't test actually this, so it may still contain some mistakes)

@zek
Copy link

zek commented Apr 22, 2014

It works well 👍

@zek
Copy link

zek commented Apr 22, 2014

I tried the same code for
connectTCP(ip, port);
but it doesn't work.

@s-ludwig
Copy link
Member Author

s-ludwig commented May 7, 2014

What happens for connectTCP? The code looks like it should work there, too.

@zek
Copy link

zek commented May 7, 2014

I tried but doesnt work. it tries to connect to peer but it just stuck. Need to a connection timeout

@s-ludwig
Copy link
Member Author

s-ludwig commented May 7, 2014

This works for me:

import vibe.d;

TCPConnection connectTCPTimeout(Duration timeout, string host, ushort port)
{
    TCPConnection conn;
    Exception ex;
    auto connect_task = runTask({
        try conn = connectTCP(host, port);
        catch (Exception e) { ex = e; }
    });
    auto tm = setTimer(10.seconds, { connect_task.interrupt(); });
    connect_task.join();
    tm.stop();
    if (ex) throw ex;
    return conn;
}

shared static this()
{
    runTask({
        // must run in a task so that join() works
        connectTCPTimeout(10.seconds, "1.1.1.1", 23);
    });
}

@Downchuck
Copy link

Was this tested in Windows? My thread is handing on a simple timeout on Win 8.1, vibe 0.7.22:
Duration packetTimeout = dur!"msecs"(100);
auto pack = udp_listener.recv(packetTimeout);

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

No branches or pull requests

3 participants