Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 64458de8ed71
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: f538f18ec88f
Choose a head ref
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on Jan 1, 2016

  1. TCPSocket.new fixes for rubysl-socket specs.

    * Try all addresses found for target hostname, as in MRI.
    * Only proceed to select and finishConnect if did not connect yet.
    headius committed Jan 1, 2016
    Copy the full SHA
    921ac08 View commit details
  2. Copy the full SHA
    f538f18 View commit details
Showing with 76 additions and 32 deletions.
  1. +30 −0 core/src/main/java/org/jruby/ext/socket/RubyTCPServer.java
  2. +46 −32 core/src/main/java/org/jruby/ext/socket/RubyTCPSocket.java
30 changes: 30 additions & 0 deletions core/src/main/java/org/jruby/ext/socket/RubyTCPServer.java
Original file line number Diff line number Diff line change
@@ -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;

@@ -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);
78 changes: 46 additions & 32 deletions core/src/main/java/org/jruby/ext/socket/RubyTCPSocket.java
Original file line number Diff line number Diff line change
@@ -99,38 +99,52 @@ public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
try {
// This is a bit convoluted because (1) SocketChannel.bind is only in jdk 7 and
// (2) Socket.getChannel() seems to return null in some cases
channel = SocketChannel.open();
Socket socket = channel.socket();

if (localHost != null) {
socket.setReuseAddress(true);
socket.bind( new InetSocketAddress(InetAddress.getByName(localHost), localPort) );
}

try {
// Do this nonblocking so we can be interrupted
channel.configureBlocking(false);
channel.connect( new InetSocketAddress(InetAddress.getByName(remoteHost), remotePort) );
context.getThread().select(channel, this, SelectionKey.OP_CONNECT);
channel.finishConnect();

// only try to set blocking back if we succeeded to finish connecting
channel.configureBlocking(true);

initSocket(newChannelFD(runtime, channel));
success = true;
}
catch (BindException e) {
throw runtime.newErrnoEADDRFromBindException(e, " to: " + remoteHost + ':' + remotePort);
}
catch (NoRouteToHostException e) {
throw runtime.newErrnoEHOSTUNREACHError("SocketChannel.connect");
}
catch (ConnectException e) {
throw runtime.newErrnoECONNREFUSEDError("connect(2) for " + host.inspect() + " port " + remotePort);
}
catch (UnknownHostException e) {
throw SocketUtils.sockerr(runtime, "initialize: name or service not known");
InetAddress[] allAddresses = InetAddress.getAllByName(remoteHost);

// try all addresses associated with the hostname until one works. See MRI: init_inetsock_internal
for (int i = 0; i < allAddresses.length; i++) {
InetAddress inet = allAddresses[i];

channel = SocketChannel.open();
Socket socket = channel.socket();

if (localHost != null) {
socket.setReuseAddress(true);
socket.bind(new InetSocketAddress(InetAddress.getByName(localHost), localPort));
}

try {
// Do this nonblocking so we can be interrupted
channel.configureBlocking(false);
boolean connected = channel.connect(new InetSocketAddress(inet, remotePort));
if (!connected) {
context.getThread().select(channel, this, SelectionKey.OP_CONNECT);
channel.finishConnect();
}

// only try to set blocking back if we succeeded to finish connecting
channel.configureBlocking(true);

initSocket(newChannelFD(runtime, channel));
success = true;
break;
} catch (BindException e) {
throw runtime.newErrnoEADDRFromBindException(e, " to: " + remoteHost + ':' + remotePort);
} catch (NoRouteToHostException e) {
if (i+1 < allAddresses.length) {
// try next address
continue;
}
throw runtime.newErrnoEHOSTUNREACHError("SocketChannel.connect");
} catch (ConnectException e) {
if (i+1 < allAddresses.length) {
// try next address
continue;
}
throw runtime.newErrnoECONNREFUSEDError("connect(2) for " + remoteHost + " port " + remotePort);
} catch (UnknownHostException e) {
throw SocketUtils.sockerr(runtime, "initialize: name or service not known");
}
}
}
catch (ClosedChannelException e) {