public final String toString() { return description(); } public static SocketOption valueOf(long value) {
@JRubyMethod public IRubyObject optname(ThreadContext context) { return context.runtime.newFixnum(option.longValue()); }
public static int setsockopt(int s, SocketLevel level, SocketOption optname, boolean optval) { ByteBuffer buf = ByteBuffer.allocate(4); buf.order(ByteOrder.BIG_ENDIAN); buf.putInt(optval ? 1 : 0).flip(); return libsocket().setsockopt(s, level.intValue(), optname.intValue(), buf, buf.remaining()); } }
@JRubyMethod public IRubyObject optname(ThreadContext context) { return context.runtime.newFixnum(option.longValue()); }
static <T> T getSocketOption(int fd, SocketOption<?> name) throws IOException { jnr.constants.platform.SocketOption optname = rMap.get(name); if (null == optname) { throw new AssertionError("Option not found"); } Class<?> type = name.type(); if (type == Credentials.class) { return (T) Credentials.getCredentials(fd); } if (type == Integer.class) { return (T) Integer.valueOf(Native.getsockopt(fd, SocketLevel.SOL_SOCKET, optname.intValue())); } return (T) Boolean.valueOf(Native.getboolsockopt(fd, SocketLevel.SOL_SOCKET, optname.intValue())); }
public final String toString() { return description(); } public static SocketOption valueOf(long value) {
@JRubyMethod public IRubyObject optname(ThreadContext context) { return context.runtime.newFixnum(option.longValue()); }
public static int setsockopt(int s, SocketLevel level, SocketOption optname, int optval) { if (optname == SocketOption.SO_RCVTIMEO || optname == SocketOption.SO_SNDTIMEO) { DefaultNativeTimeval t = new DefaultNativeTimeval(Runtime.getSystemRuntime()); t.setTime(new long [] {optval / 1000, ((long)optval % 1000) * 1000}); return libsocket().setsockopt(s, level.intValue(), optname.intValue(), t, DefaultNativeTimeval.size(t)); } else { ByteBuffer buf = ByteBuffer.allocate(4); buf.order(ByteOrder.nativeOrder()); buf.putInt(optval).flip(); return libsocket().setsockopt(s, level.intValue(), optname.intValue(), buf, buf.remaining()); } }
public final String toString() { return description(); } public static SocketOption valueOf(long value) {
static SocketOption optionFromArg(IRubyObject opt) { IRubyObject maybeString = TypeConverter.checkStringType(opt.getRuntime(), opt); if (!maybeString.isNil()) { opt = maybeString; } try { if (opt instanceof RubyString || opt instanceof RubySymbol) { String optString = opt.toString(); if (optString.startsWith("SO_")) return SocketOption.valueOf(optString); return SocketOption.valueOf("SO_" + optString); } return SocketOption.valueOf(RubyNumeric.fix2int(opt)); } catch (IllegalArgumentException iae) { throw SocketUtils.sockerr(opt.getRuntime(), "invalid socket option: " + opt); } }
@JRubyMethod public IRubyObject optname(ThreadContext context) { return context.runtime.newFixnum(option.longValue()); }
public final boolean getKeepAlive() { ByteBuffer buf = ByteBuffer.allocate(4); buf.order(ByteOrder.BIG_ENDIAN); IntByReference ref = new IntByReference(4); Native.libsocket().getsockopt(channel.getFD(), SocketLevel.SOL_SOCKET.intValue(), SocketOption.SO_KEEPALIVE.intValue(), buf, ref); return buf.getInt(0) != 0; } }
private String noPrefix(SocketOption option) { return option.description().substring("SO_".length()); }
static SocketOption optionFromArg(IRubyObject opt) { IRubyObject maybeString = TypeConverter.checkStringType(opt.getRuntime(), opt); if (!maybeString.isNil()) { opt = maybeString; } try { if (opt instanceof RubyString || opt instanceof RubySymbol) { String optString = opt.toString(); if (optString.startsWith("SO_")) return SocketOption.valueOf(optString); return SocketOption.valueOf("SO_" + optString); } return SocketOption.valueOf(RubyNumeric.fix2int(opt)); } catch (IllegalArgumentException iae) { throw SocketUtils.sockerr(opt.getRuntime(), "invalid socket option: " + opt); } }
public static int getsockopt (int s, SocketLevel level, int optname) { IntByReference ref; if (optname == SocketOption.SO_RCVTIMEO.intValue() || optname == SocketOption.SO_SNDTIMEO.intValue()) { DefaultNativeTimeval t = new DefaultNativeTimeval(Runtime.getSystemRuntime()); ref = new IntByReference(DefaultNativeTimeval.size(t)); Native.libsocket().getsockopt(s, level.intValue(), optname, t, ref); return (t.tv_sec.intValue() * 1000 + t.tv_usec.intValue() / 1000); } else { ByteBuffer buf = ByteBuffer.allocate(4); buf.order(ByteOrder.nativeOrder()); ref = new IntByReference(4); Native.libsocket().getsockopt(s, level.intValue(), optname, buf, ref); return buf.getInt(); } }
private String noPrefix(SocketOption option) { return option.description().substring("SO_".length()); }
@JRubyMethod(required = 4) 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()); data = args[3].convertToString().getByteList(); intData = Pack.unpackInt_i(ByteBuffer.wrap(data.bytes())); return context.nil; }
public static int getsockopt(int s, SocketLevel level, SocketOption optname, Struct data) { Pointer struct_ptr = Struct.getMemory(data); IntByReference ref = new IntByReference(Struct.size(data)); ByteBuffer buf = ByteBuffer.wrap((byte[])struct_ptr.array()); return Native.libsocket().getsockopt(s, level.intValue(), optname.intValue(), buf, ref); }