Skip to content

Commit

Permalink
Merge pull request #4189 from etehtsea/tcpserver-sysaccept
Browse files Browse the repository at this point in the history
Implement TCPServer#sysaccept
  • Loading branch information
headius committed Sep 30, 2016
2 parents 7d22b20 + 8435a4a commit 0e48f7b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
30 changes: 30 additions & 0 deletions core/src/main/java/org/jruby/ext/socket/RubyTCPServer.java
Expand Up @@ -52,6 +52,7 @@
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;

import org.jruby.util.io.FilenoUtil;
import org.jruby.util.io.SelectorFactory;
import java.nio.channels.spi.SelectorProvider;

Expand Down Expand Up @@ -236,6 +237,35 @@ public IRubyObject accept_nonblock(ThreadContext context, Ruby runtime, boolean
}
}

@JRubyMethod(name = "sysaccept")
public IRubyObject sysaccept(ThreadContext context) {
Ruby runtime = context.runtime;

try {
RubyThread thread = context.getThread();

while (true) {
boolean ready = thread.select(this, SelectionKey.OP_ACCEPT);

if (!ready) {
// we were woken up without being selected...poll for thread events and go back to sleep
context.pollThreadEvents();

} else {
SocketChannel connected = getServerSocketChannel().accept();
if (connected == null) continue;

connected.finishConnect();

return runtime.newFixnum(FilenoUtil.filenoFrom(connected));
}
}

} catch(IOException e) {
throw runtime.newIOErrorFromException(e);
}
}

@JRubyMethod(name = "listen", required = 1)
public IRubyObject listen(ThreadContext context, IRubyObject backlog) {
return RubyFixnum.zero(context.runtime);
Expand Down
2 changes: 0 additions & 2 deletions spec/tags/ruby/library/socket/tcpserver/sysaccept_tags.txt

This file was deleted.

0 comments on commit 0e48f7b

Please sign in to comment.