Skip to content

Commit

Permalink
Showing 3 changed files with 42 additions and 26 deletions.
16 changes: 10 additions & 6 deletions core/src/main/java/org/jruby/ext/socket/Addrinfo.java
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
import jnr.constants.platform.AddressFamily;
import jnr.constants.platform.ProtocolFamily;
import jnr.constants.platform.Sock;
import jnr.netdb.Protocol;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyClass;
@@ -153,6 +154,8 @@ private void initializeCommon(Ruby runtime, IRubyObject sockaddr, IRubyObject fa
} else {
this.port = (int)port.convertToInteger().getLongValue();
}

protocol = Protocol.getProtocolByName("tcp");
} catch (IOException ioe) {
throw runtime.newIOErrorFromException(ioe);
}
@@ -209,22 +212,22 @@ public static IRubyObject unix(ThreadContext context, IRubyObject recv, IRubyObj

@JRubyMethod
public IRubyObject afamily(ThreadContext context) {
return context.runtime.newFixnum(pfamily.intValue());
return context.runtime.newFixnum(afamily.intValue());
}

@JRubyMethod(notImplemented = true)
@JRubyMethod
public IRubyObject pfamily(ThreadContext context) {
return context.runtime.newFixnum(afamily.intValue());
return context.runtime.newFixnum(pfamily.intValue());
}

@JRubyMethod(notImplemented = true)
@JRubyMethod
public IRubyObject socktype(ThreadContext context) {
return context.runtime.newFixnum(sock.intValue());
}

@JRubyMethod(notImplemented = true)
@JRubyMethod
public IRubyObject protocol(ThreadContext context) {
return context.runtime.newFixnum(port);
return context.runtime.newFixnum(protocol.getProto());
}

@JRubyMethod
@@ -391,4 +394,5 @@ public IRubyObject marshal_load(ThreadContext context, IRubyObject arg) {
private AddressFamily afamily;
private Sock sock;
private SocketType socketType;
private Protocol protocol = Protocol.getProtocolByNumber(0);
}
37 changes: 17 additions & 20 deletions core/src/main/java/org/jruby/ext/socket/RubySocket.java
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@
import jnr.constants.platform.SocketLevel;
import jnr.constants.platform.SocketOption;
import jnr.constants.platform.TCP;
import jnr.netdb.Protocol;
import jnr.unixsocket.UnixSocketAddress;
import jnr.unixsocket.UnixSocketChannel;
import org.jruby.CompatVersion;
@@ -315,19 +316,20 @@ private void initFieldsFromDescriptor(Ruby runtime, ChannelDescriptor descriptor
// just using AF_INET since we can't tell from SocketChannel...
soDomain = AddressFamily.AF_INET;
soType = Sock.SOCK_STREAM;
soProtocol = ProtocolFamily.PF_INET;
soProtocolFamily = ProtocolFamily.PF_INET;
soProtocol = Protocol.getProtocolByName("tcp");

} else if (mainChannel instanceof UnixSocketChannel) {
soDomain = AddressFamily.AF_UNIX;
soType = Sock.SOCK_STREAM;
soProtocol = ProtocolFamily.PF_UNIX;
soProtocolFamily = ProtocolFamily.PF_UNIX;

} else if (mainChannel instanceof DatagramChannel) {
// datagram, set accordingly
// again, AF_INET
soDomain = AddressFamily.AF_INET;
soType = Sock.SOCK_DGRAM;
soProtocol = ProtocolFamily.PF_INET;
soProtocolFamily = ProtocolFamily.PF_INET;

} else {
throw runtime.newErrnoENOTSOCKError("can't Socket.new/for_fd against a non-socket");
@@ -362,15 +364,15 @@ protected ChannelDescriptor initChannel(Ruby runtime) {
try {
if(soType == Sock.SOCK_STREAM) {

if (soProtocol == ProtocolFamily.PF_UNIX ||
soProtocol == ProtocolFamily.PF_LOCAL) {
if (soProtocolFamily == ProtocolFamily.PF_UNIX ||
soProtocolFamily == ProtocolFamily.PF_LOCAL) {
channel = UnixSocketChannel.open();
} else if (soProtocol == ProtocolFamily.PF_INET ||
soProtocol == ProtocolFamily.PF_INET6 ||
soProtocol == ProtocolFamily.PF_UNSPEC) {
} else if (soProtocolFamily == ProtocolFamily.PF_INET ||
soProtocolFamily == ProtocolFamily.PF_INET6 ||
soProtocolFamily == ProtocolFamily.PF_UNSPEC) {
channel = SocketChannel.open();
} else {
throw runtime.newArgumentError("unsupported protocol family `" + soProtocol + "'");
throw runtime.newArgumentError("unsupported protocol family `" + soProtocolFamily + "'");
}

} else if(soType == Sock.SOCK_DGRAM) {
@@ -396,13 +398,7 @@ protected static ChannelDescriptor newChannelDescriptor(Ruby runtime, Channel ch
}

private void initProtocol(Ruby runtime, IRubyObject protocol) {
ProtocolFamily protocolFamily = SocketUtils.protocolFamilyFromArg(protocol);

if (protocolFamily == null) {
return; // no protocol specified, ignore it
}

soProtocol = protocolFamily;
soProtocol = SocketUtils.protocolFromArg(protocol);
}

private void initType(Ruby runtime, IRubyObject type) {
@@ -423,7 +419,7 @@ private void initDomain(Ruby runtime, IRubyObject domain) {
}

soDomain = family;
soProtocol = ProtocolFamily.valueOf("PF" + soDomain.name().substring(2));
soProtocolFamily = ProtocolFamily.valueOf("PF" + soDomain.name().substring(2));
}

private void doConnectNonblock(ThreadContext context, Channel channel, SocketAddress addr) {
@@ -576,7 +572,7 @@ private static String formatMessage(Throwable e, String defaultMsg) {
private SocketAddress addressForChannel(ThreadContext context, IRubyObject arg) {
if (arg instanceof Addrinfo) return Sockaddr.addressFromArg(context, arg);

switch (soProtocol) {
switch (soProtocolFamily) {
case PF_UNIX:
case PF_LOCAL:
return Sockaddr.addressFromSockaddr_un(context, arg);
@@ -587,7 +583,7 @@ private SocketAddress addressForChannel(ThreadContext context, IRubyObject arg)
return Sockaddr.addressFromSockaddr_in(context, arg);

default:
throw context.runtime.newArgumentError("unsupported protocol family `" + soProtocol + "'");
throw context.runtime.newArgumentError("unsupported protocol family `" + soProtocolFamily + "'");
}
}

@@ -606,8 +602,9 @@ public static RuntimeException sockerr(Ruby runtime, String msg) {
public static final int MSG_WAITALL = 0x100;

protected AddressFamily soDomain;
protected ProtocolFamily soProtocolFamily;
protected Sock soType;
protected ProtocolFamily soProtocol;
protected Protocol soProtocol = Protocol.getProtocolByNumber(0);

private static final String JRUBY_SERVER_SOCKET_ERROR =
"use ServerSocket for servers (http://wiki.jruby.org/ServerSocket)";
15 changes: 15 additions & 0 deletions core/src/main/java/org/jruby/ext/socket/SocketUtils.java
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@
import jnr.constants.platform.AddressFamily;
import jnr.constants.platform.ProtocolFamily;
import jnr.constants.platform.Sock;
import jnr.netdb.Protocol;
import jnr.netdb.Service;
import org.jruby.Ruby;
import org.jruby.RubyArray;
@@ -608,6 +609,20 @@ static ProtocolFamily protocolFamilyFromArg(IRubyObject protocol) {

return protocolFamily;
}

static Protocol protocolFromArg(IRubyObject protocol) {
Protocol proto;

if(protocol instanceof RubyString || protocol instanceof RubySymbol) {
String protocolString = protocol.toString();
proto = Protocol.getProtocolByName(protocolString);
} else {
int protocolInt = RubyNumeric.fix2int(protocol);
proto = Protocol.getProtocolByNumber(protocolInt);
}

return proto;
}

public static int portToInt(IRubyObject port) {
return port.isNil() ? 0 : RubyNumeric.fix2int(port);

0 comments on commit 4b0fc45

Please sign in to comment.