Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions core/src/main/java/org/jruby/ext/socket/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.jruby.RubyClass;
import org.jruby.RubyObject;
import org.jruby.RubyString;
import org.jruby.RubyNumeric;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
Expand Down Expand Up @@ -47,25 +48,25 @@ public Option(Ruby runtime, ProtocolFamily family, SocketLevel level, SocketOpti

public Option(Ruby runtime, RubyClass klass, ProtocolFamily family, SocketLevel level, SocketOption option, int data) {
super(runtime, klass);

this.family = family;
this.level = level;
this.option = option;
this.intData = data;
ByteList result = new ByteList(4);
this.data = Pack.packInt_i(result, data);
}

@JRubyMethod(required = 4, visibility = Visibility.PRIVATE)
public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
family = ProtocolFamily.valueOf(args[0].convertToInteger().getLongValue());
level = SocketLevel.valueOf(args[1].convertToInteger().getLongValue());
option = SocketOption.valueOf(args[2].convertToInteger().getLongValue());
family = SocketUtils.protocolFamilyFromArg(args[0]);
level = SocketUtils.levelFromArg(args[1]);
option = SocketUtils.optionFromArg(args[2]);
data = args[3].convertToString().getByteList();
intData = Pack.unpackInt_i(ByteBuffer.wrap(data.bytes()));
return context.nil;
return this;
}

@JRubyMethod
public IRubyObject family(ThreadContext context) {
return context.runtime.newFixnum(family.longValue());
Expand Down Expand Up @@ -166,24 +167,34 @@ private String optionValue() {
return "";
}

@JRubyMethod(meta = true)
public IRubyObject rb_int(ThreadContext context, IRubyObject self) {
return context.nil;
@JRubyMethod(name = "int", required = 4, meta = true)
public static IRubyObject rb_int(ThreadContext context, IRubyObject self, IRubyObject[] args) {
ProtocolFamily family = SocketUtils.protocolFamilyFromArg(args[0]);
SocketLevel level = SocketUtils.levelFromArg(args[1]);
SocketOption option = SocketUtils.optionFromArg(args[2]);
int intData = RubyNumeric.fix2int(args[3]);

return new Option(context.getRuntime(), family, level, option, intData);
}

@JRubyMethod(name = "int")
public IRubyObject asInt(ThreadContext context) {
return context.getRuntime().newFixnum((int) intData);
}

@JRubyMethod(meta = true)
public IRubyObject bool(ThreadContext context, IRubyObject self) {
return context.nil;
@JRubyMethod(required = 4, meta = true)
public static IRubyObject bool(ThreadContext context, IRubyObject self, IRubyObject[] args) {
ProtocolFamily family = SocketUtils.protocolFamilyFromArg(args[0]);
SocketLevel level = SocketUtils.levelFromArg(args[1]);
SocketOption option = SocketUtils.optionFromArg(args[2]);
int intData = args[3].isTrue() ? 1 : 0;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be checked there that it actually a boolean arg and what to do in case if it's not?


return new Option(context.getRuntime(), family, level, option, intData);
}

@JRubyMethod
public IRubyObject bool(ThreadContext context) {
return context.nil;
return context.getRuntime().newBoolean(intData != 0);
}

@JRubyMethod(meta = true)
Expand Down
28 changes: 4 additions & 24 deletions core/src/main/java/org/jruby/ext/socket/RubyBasicSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ protected final IRubyObject recv_nonblock(ThreadContext context,
public IRubyObject getsockopt(ThreadContext context, IRubyObject _level, IRubyObject _opt) {
Ruby runtime = context.runtime;

SocketLevel level = levelFromArg(_level);
SocketOption opt = optionFromArg(_opt);
SocketLevel level = SocketUtils.levelFromArg(_level);
SocketOption opt = SocketUtils.optionFromArg(_opt);

try {
Channel channel = getOpenChannel();
Expand Down Expand Up @@ -282,8 +282,8 @@ public IRubyObject getsockopt(ThreadContext context, IRubyObject _level, IRubyOb
public IRubyObject setsockopt(ThreadContext context, IRubyObject _level, IRubyObject _opt, IRubyObject val) {
Ruby runtime = context.runtime;

SocketLevel level = levelFromArg(_level);
SocketOption opt = optionFromArg(_opt);
SocketLevel level = SocketUtils.levelFromArg(_level);
SocketOption opt = SocketUtils.optionFromArg(_opt);

try {
Channel channel = getOpenChannel();
Expand Down Expand Up @@ -708,26 +708,6 @@ protected boolean asBoolean(IRubyObject val) {
return val.isTrue();
}

protected static SocketOption optionFromArg(IRubyObject _opt) {
SocketOption opt;
if (_opt instanceof RubyString || _opt instanceof RubySymbol) {
opt = SocketOption.valueOf("SO_" + _opt.toString());
} else {
opt = SocketOption.valueOf(RubyNumeric.fix2int(_opt));
}
return opt;
}

protected static SocketLevel levelFromArg(IRubyObject _level) {
SocketLevel level;
if (_level instanceof RubyString || _level instanceof RubySymbol) {
level = SocketLevel.valueOf("SOL_" + _level.toString());
} else {
level = SocketLevel.valueOf(RubyNumeric.fix2int(_level));
}
return level;
}

protected IRubyObject addrFor(ThreadContext context, InetSocketAddress addr, boolean reverse) {
final Ruby runtime = context.runtime;
IRubyObject ret0, ret1, ret2, ret3;
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/ext/socket/RubyUNIXSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ public static IRubyObject socketpair(ThreadContext context, IRubyObject recv, IR

@Override
public IRubyObject setsockopt(ThreadContext context, IRubyObject _level, IRubyObject _opt, IRubyObject val) {
SocketLevel level = levelFromArg(_level);
SocketOption opt = optionFromArg(_opt);
SocketLevel level = SocketUtils.levelFromArg(_level);
SocketOption opt = SocketUtils.optionFromArg(_opt);

switch(level) {
case SOL_SOCKET:
Expand Down
25 changes: 24 additions & 1 deletion core/src/main/java/org/jruby/ext/socket/SocketUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import jnr.constants.platform.AddressFamily;
import jnr.constants.platform.ProtocolFamily;
import jnr.constants.platform.Sock;
import jnr.constants.platform.SocketLevel;
import jnr.constants.platform.SocketOption;
import jnr.netdb.Protocol;
import jnr.netdb.Service;
import org.jruby.Ruby;
Expand Down Expand Up @@ -586,7 +588,8 @@ static Sock sockFromArg(IRubyObject type) {

if(type instanceof RubyString || type instanceof RubySymbol) {
String typeString = type.toString();
sockType = Sock.valueOf("SOCK_" + typeString);
if (!typeString.startsWith("SOCK_")) typeString = "SOCK_" + typeString;
sockType = Sock.valueOf(typeString);
} else {
int typeInt = RubyNumeric.fix2int(type);
sockType = Sock.valueOf(typeInt);
Expand Down Expand Up @@ -624,6 +627,26 @@ static Protocol protocolFromArg(IRubyObject protocol) {
return proto;
}

static SocketLevel levelFromArg(IRubyObject _level) {
SocketLevel level;
if (_level instanceof RubyString || _level instanceof RubySymbol) {
level = SocketLevel.valueOf("SOL_" + _level.toString());
} else {
level = SocketLevel.valueOf(RubyNumeric.fix2int(_level));
}
return level;
}

static SocketOption optionFromArg(IRubyObject _opt) {
SocketOption opt;
if (_opt instanceof RubyString || _opt instanceof RubySymbol) {
opt = SocketOption.valueOf("SO_" + _opt.toString());
} else {
opt = SocketOption.valueOf(RubyNumeric.fix2int(_opt));
}
return opt;
}

public static int portToInt(IRubyObject port) {
return port.isNil() ? 0 : RubyNumeric.fix2int(port);
}
Expand Down
1 change: 0 additions & 1 deletion test/mri/excludes/TestSocket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
exclude :test_getaddrinfo, "needs investigation"
exclude :test_getaddrinfo_raises_no_errors_on_port_argument_of_0, "needs investigation"
exclude :test_getnameinfo, "needs investigation"
exclude :test_initialize, "needs investigation"
exclude :test_linger, "needs investigation"
exclude :test_listen_in_rescue, "needs investigation"
exclude :test_recvmsg_udp_no_arg, "needs investigation"
Expand Down
3 changes: 0 additions & 3 deletions test/mri/excludes/TestSocketOption.rb

This file was deleted.