public IRubyObject allocate(Ruby runtime, RubyClass klass) { return new RubyBasicSocket(runtime, klass); } };
protected void initSocket(Ruby runtime, ChannelDescriptor descriptor) { // continue with normal initialization MakeOpenFile(); try { openFile.setMainStream(ChannelStream.fdopen(runtime, descriptor, newModeFlags(runtime, ModeFlags.RDONLY))); openFile.setPipeStream(ChannelStream.fdopen(runtime, descriptor, newModeFlags(runtime, ModeFlags.WRONLY))); openFile.getPipeStream().setSync(true); } catch (org.jruby.util.io.InvalidValueException ex) { throw runtime.newErrnoEINVALError(); } openFile.setMode(OpenFile.READWRITE | OpenFile.SYNC); // see rsock_init_sock in MRI; sockets are initialized to binary setAscii8bitBinmode(); }
@Deprecated public static IRubyObject do_not_reverse_lookup(IRubyObject recv) { return do_not_reverse_lookup(recv.getRuntime().getCurrentContext(), recv); }
protected void initSocket(ChannelFD fd) { // continue with normal initialization MakeOpenFile(); openFile.setFD(fd); openFile.setMode(OpenFile.READWRITE | OpenFile.SYNC); // see rsock_init_sock in MRI; sockets are initialized to binary setAscii8bitBinmode(); }
protected SocketAddress getRemoteSocket() { Channel channel = getOpenChannel(); SocketAddress address = SocketType.forChannel(channel).getRemoteSocketAddress(channel); if (address == null) throw getRuntime().newErrnoENOTCONNError(); return address; }
@JRubyMethod(compat = CompatVersion.RUBY1_9) public IRubyObject remote_address(ThreadContext context) { try { InetSocketAddress address = getRemoteSocket(); if (address == null) { return context.nil; } else { return new Addrinfo(context.runtime, context.runtime.getClass("Addrinfo"), address.getAddress(), address.getPort(), SocketType.forChannel(getChannel())); } } catch (BadDescriptorException bde) { throw context.runtime.newErrnoEBADFError("address unavailable"); } }
Ruby runtime = context.runtime; SocketLevel level = levelFromArg(_level); SocketOption opt = optionFromArg(_opt); Channel channel = getOpenChannel(); SocketType socketType = SocketType.forChannel(channel); socketType.setSoLinger(channel, false, 0); } else { int num = asNumber(val); if(num == -1) { socketType.setSoLinger(channel, false, 0); socketType.setSocketOption(channel, opt, asNumber(val)); int intOpt = (int)_opt.convertToInteger().getLongValue(); if (IPPROTO_TCP.intValue() == intLevel && TCP_NODELAY.intValue() == intOpt) { socketType.setTcpNoDelay(channel, asBoolean(val)); joinMulticastGroup(val);
Channel channel = getOpenChannel(); SocketType socketType = SocketType.forChannel(channel); socketType.setSocketOption(channel, opt, asNumber(val)); int intOpt = (int)_opt.convertToInteger().getLongValue(); if (IPPROTO_TCP.intValue() == intLevel && TCP_NODELAY.intValue() == intOpt) { socketType.setTcpNoDelay(channel, asBoolean(val)); joinMulticastGroup(val);
Ruby runtime = context.runtime; SocketLevel level = levelFromArg(_level); SocketOption opt = optionFromArg(_opt); Channel channel = getOpenChannel(); return new Option(runtime, ProtocolFamily.PF_INET, level, opt, value); } else { return number(runtime, SocketType.forChannel(channel).getSocketOption(channel, opt));
protected InetSocketAddress getRemoteSocket() throws BadDescriptorException { Channel channel = getOpenChannel(); return (InetSocketAddress)SocketType.forChannel(channel).getRemoteSocketAddress(channel); }
@JRubyMethod public IRubyObject local_address(ThreadContext context) { Ruby runtime = context.runtime; InetSocketAddress address = getInetSocketAddress(); if (address != null) { SocketType socketType = SocketType.forChannel(getChannel()); return new Addrinfo(runtime, runtime.getClass("Addrinfo"), address, socketType.getSocketType(), socketType); } UnixSocketAddress unix = getUnixSocketAddress(); return Addrinfo.unix(context, runtime.getClass("Addrinfo"), runtime.newString(unix.path())); }
@JRubyMethod(compat = CompatVersion.RUBY1_9) public IRubyObject local_address(ThreadContext context) { try { InetSocketAddress address = getSocketAddress(); if (address == null) { return context.nil; } else { return new Addrinfo(context.runtime, context.runtime.getClass("Addrinfo"), address.getAddress(), address.getPort(), SocketType.forChannel(getChannel())); } } catch (BadDescriptorException bde) { throw context.runtime.newErrnoEBADFError("address unavailable"); } }
@JRubyMethod public IRubyObject remote_address(ThreadContext context) { Ruby runtime = context.runtime; InetSocketAddress address = getInetRemoteSocket(); if (address != null) { SocketType socketType = SocketType.forChannel(getChannel()); return new Addrinfo(runtime, runtime.getClass("Addrinfo"), address, socketType.getSocketType(), socketType); } UnixSocketAddress unix = getUnixRemoteSocket(); if (unix != null) { return Addrinfo.unix(context, runtime.getClass("Addrinfo"), runtime.newString(unix.path())); } throw runtime.newErrnoENOTCONNError(); }
channel = getOpenChannel(); try { SocketType.forChannel(channel).shutdownInput(channel); channel = getOpenChannel(); try { SocketType.forChannel(channel).shutdownOutput(channel); shutdownInternal(context, 0); shutdownInternal(context, 1);
protected final ByteList doReadNonblock(ThreadContext context, final ByteBuffer buffer) { Channel channel = getChannel(); if ( ! (channel instanceof SelectableChannel) ) { throw context.runtime.newErrnoEAGAINReadableError(channel.getClass().getName() + " does not support nonblocking"); } SelectableChannel selectable = (SelectableChannel) channel; synchronized (selectable.blockingLock()) { boolean oldBlocking = selectable.isBlocking(); try { selectable.configureBlocking(false); try { return doRead(context, buffer); } finally { selectable.configureBlocking(oldBlocking); } } catch (IOException e) { throw context.runtime.newIOErrorFromException(e); } } }
public ByteList doReceiveNonblock(ThreadContext context, int length) { Ruby runtime = context.runtime; Channel channel = getChannel(); if (!(channel instanceof SelectableChannel)) { if (runtime.is1_9()) { throw runtime.newErrnoEAGAINReadableError(channel.getClass().getName() + " does not support nonblocking"); } else { throw runtime.newErrnoEAGAINError(channel.getClass().getName() + " does not support nonblocking"); } } SelectableChannel selectable = (SelectableChannel)channel; synchronized (selectable.blockingLock()) { boolean oldBlocking = selectable.isBlocking(); try { selectable.configureBlocking(false); try { return doReceive(context, length); } finally { selectable.configureBlocking(oldBlocking); } } catch(IOException e) { throw runtime.newIOErrorFromException(e); } } }
@JRubyMethod(name = "do_not_reverse_lookup=") public IRubyObject set_do_not_reverse_lookup19(ThreadContext context, IRubyObject flag) { doNotReverseLookup = flag.isTrue(); return do_not_reverse_lookup19(context); }
protected static IRubyObject doReceive(RubyBasicSocket socket, final Ruby runtime, final boolean non_block, int length, ReceiveTuple tuple) throws IOException { DatagramChannel channel = (DatagramChannel) socket.getChannel(); ByteBuffer buf = ByteBuffer.allocate(length); InetSocketAddress sender = (InetSocketAddress) channel.receive(buf); if (sender == null) { if ( non_block ) { // non-blocking receive return null; // :wait_readable or "recvfrom(2) would block" } else { // see JRUBY-4678 throw runtime.newErrnoECONNRESETError(); } } RubyString result = runtime.newString(new ByteList(buf.array(), 0, buf.position(), false)); if (tuple != null) { tuple.result = result; tuple.sender = sender; } return result; }
public void load(final Ruby runtime, boolean wrap) throws IOException { runtime.defineClass("SocketError", runtime.getStandardError(), runtime.getStandardError().getAllocator()); RubyBasicSocket.createBasicSocket(runtime); RubySocket.createSocket(runtime); RubyServerSocket.createServerSocket(runtime); if (runtime.getInstanceConfig().isNativeEnabled() && !Platform.IS_WINDOWS) { RubyUNIXSocket.createUNIXSocket(runtime); RubyUNIXServer.createUNIXServer(runtime); } RubyIPSocket.createIPSocket(runtime); RubyTCPSocket.createTCPSocket(runtime); RubyTCPServer.createTCPServer(runtime); RubyUDPSocket.createUDPSocket(runtime); Addrinfo.createAddrinfo(runtime); Option.createOption(runtime); Ifaddr.createIfaddr(runtime); } }
protected IRubyObject addrFor(ThreadContext context, InetSocketAddress addr, boolean reverse) { final Ruby runtime = context.runtime; IRubyObject ret0, ret1, ret2, ret3; if (addr.getAddress() instanceof Inet6Address) { ret0 = runtime.newString("AF_INET6"); } else { ret0 = runtime.newString("AF_INET"); } ret1 = runtime.newFixnum(addr.getPort()); String hostAddress = addr.getAddress().getHostAddress(); if (!reverse || doNotReverseLookup(context)) { ret2 = runtime.newString(hostAddress); } else { ret2 = runtime.newString(addr.getHostName()); } ret3 = runtime.newString(hostAddress); return RubyArray.newArray(runtime, ret0, ret1, ret2, ret3); }