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

Comments

Projects
None yet
3 participants
@s-ludwig
Member

s-ludwig commented Feb 20, 2014

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

This comment has been minimized.

Show comment
Hide comment
@zek

zek Apr 21, 2014

Is this done?

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

This comment has been minimized.

Show comment
Hide comment
@s-ludwig

s-ludwig Apr 21, 2014

Member

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.

Member

s-ludwig commented Apr 21, 2014

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

This comment has been minimized.

Show comment
Hide comment
@zek

zek 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

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

This comment has been minimized.

Show comment
Hide comment
@s-ludwig

s-ludwig Apr 21, 2014

Member

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)

Member

s-ludwig commented Apr 21, 2014

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

This comment has been minimized.

Show comment
Hide comment
@zek

zek Apr 22, 2014

It works well 👍

zek commented Apr 22, 2014

It works well 👍

@zek

This comment has been minimized.

Show comment
Hide comment
@zek

zek Apr 22, 2014

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

zek commented Apr 22, 2014

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

@s-ludwig s-ludwig closed this in aab803a May 7, 2014

@s-ludwig

This comment has been minimized.

Show comment
Hide comment
@s-ludwig

s-ludwig May 7, 2014

Member

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

Member

s-ludwig commented May 7, 2014

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

@zek

This comment has been minimized.

Show comment
Hide comment
@zek

zek May 7, 2014

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

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

This comment has been minimized.

Show comment
Hide comment
@s-ludwig

s-ludwig May 7, 2014

Member

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);
    });
}
Member

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

This comment has been minimized.

Show comment
Hide comment
@Downchuck

Downchuck Feb 19, 2015

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);

Downchuck commented Feb 19, 2015

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