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; }
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; }
@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(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"); } }
@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"); } }
@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"); } }
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); } } }
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); } } }
private static ReceiveTuple doReceiveNonblockTuple(RubyBasicSocket socket, Ruby runtime, int length) throws IOException { DatagramChannel channel = (DatagramChannel) socket.getChannel(); synchronized (channel.blockingLock()) { boolean oldBlocking = channel.isBlocking(); channel.configureBlocking(false); try { return doReceiveTuple(socket, runtime, true, length); } finally { channel.configureBlocking(oldBlocking); } } }
private static ReceiveTuple doReceiveNonblockTuple(RubyBasicSocket socket, Ruby runtime, int length) throws IOException { DatagramChannel channel = (DatagramChannel) socket.getChannel(); synchronized (channel.blockingLock()) { boolean oldBlocking = channel.isBlocking(); channel.configureBlocking(false); try { return doReceiveTuple(socket, runtime, true, length); } finally { channel.configureBlocking(oldBlocking); } } }
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); } } }
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); } } }
@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 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 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(); }
@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(); }