public void setBlocking(boolean block) throws IOException { if (!(descriptor.getChannel() instanceof SelectableChannel)) { return; } synchronized (((SelectableChannel) descriptor.getChannel()).blockingLock()) { blocking = block; try { ((SelectableChannel) descriptor.getChannel()).configureBlocking(block); } catch (IllegalBlockingModeException e) { // ignore this; select() will set the correct mode when it is finished } } }
public void setBlocking(boolean block) throws IOException { if (!(descriptor.getChannel() instanceof SelectableChannel)) { return; } synchronized (((SelectableChannel) descriptor.getChannel()).blockingLock()) { blocking = block; try { ((SelectableChannel) descriptor.getChannel()).configureBlocking(block); } catch (IllegalBlockingModeException e) { // ignore this; select() will set the correct mode when it is finished } } }
public void setBlocking(boolean block) throws IOException { if (!(descriptor.getChannel() instanceof SelectableChannel)) { return; } synchronized (((SelectableChannel) descriptor.getChannel()).blockingLock()) { blocking = block; try { ((SelectableChannel) descriptor.getChannel()).configureBlocking(block); } catch (IllegalBlockingModeException e) { // ignore this; select() will set the correct mode when it is finished } } }
public void setBlocking(boolean block) throws IOException { if (!(descriptor.getChannel() instanceof SelectableChannel)) { return; } synchronized (((SelectableChannel) descriptor.getChannel()).blockingLock()) { blocking = block; try { ((SelectableChannel) descriptor.getChannel()).configureBlocking(block); } catch (IllegalBlockingModeException e) { // ignore this; select() will set the correct mode when it is finished } } }
public void setBlocking(boolean block) throws IOException { if (!(descriptor.getChannel() instanceof SelectableChannel)) { return; } synchronized (((SelectableChannel) descriptor.getChannel()).blockingLock()) { blocking = block; try { ((SelectableChannel) descriptor.getChannel()).configureBlocking(block); } catch (IllegalBlockingModeException e) { // ignore this; select() will set the correct mode when it is finished } } }
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); } } }
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); } } }
/** * Returns an output stream based on a given writable byte channel. * * @param writableChannel * The writable byte channel. * @return An output stream based on a given writable byte channel. */ public static OutputStream getStream(WritableByteChannel writableChannel) { OutputStream result = null; if (writableChannel instanceof SelectableChannel) { final SelectableChannel selectableChannel = (SelectableChannel) writableChannel; synchronized (selectableChannel.blockingLock()) { if (selectableChannel.isBlocking()) { result = Channels.newOutputStream(writableChannel); } else { result = new NbChannelOutputStream(writableChannel); } } } else { result = new NbChannelOutputStream(writableChannel); } return result; }
private RubyArray doAcceptNonblock(ThreadContext context, Channel channel) { try { if (channel instanceof SelectableChannel) { SelectableChannel selectable = (SelectableChannel)channel; synchronized (selectable.blockingLock()) { boolean oldBlocking = selectable.isBlocking(); try { selectable.configureBlocking(false); RubySocket socket = doAccept(context, channel); SocketChannel socketChannel = (SocketChannel)socket.getChannel(); InetSocketAddress addr = (InetSocketAddress)socketChannel.socket().getLocalSocketAddress(); return context.runtime.newArray( socket, Sockaddr.packSockaddrFromAddress(context, addr)); } finally { selectable.configureBlocking(oldBlocking); } } } else { throw getRuntime().newErrnoENOPROTOOPTError(); } } catch(IOException e) { throw SocketUtils.sockerr(context.runtime, e.getLocalizedMessage()); } }
public synchronized ByteList readnonblock(int number) throws IOException, BadDescriptorException, EOFException { assert number >= 0; if (number == 0) { return null; } if (descriptor.getChannel() instanceof SelectableChannel) { SelectableChannel selectableChannel = (SelectableChannel)descriptor.getChannel(); synchronized (selectableChannel.blockingLock()) { boolean oldBlocking = selectableChannel.isBlocking(); try { selectableChannel.configureBlocking(false); return readpartial(number); } finally { selectableChannel.configureBlocking(oldBlocking); } } } else { return null; } }
public IRubyObject accept_nonblock(ThreadContext context, Ruby runtime, boolean ex) { SelectableChannel selectable = (SelectableChannel)getChannel(); synchronized (selectable.blockingLock()) { boolean oldBlocking = selectable.isBlocking(); try { selectable.configureBlocking(false); try { UnixSocketChannel socketChannel = ((UnixServerSocketChannel) selectable).accept(); if (socketChannel == null) { if (!ex) return runtime.newSymbol("wait_readable"); throw runtime.newErrnoEAGAINReadableError("accept(2) would block"); } RubyUNIXSocket sock = (RubyUNIXSocket)(Helpers.invoke(context, runtime.getClass("UNIXSocket"), "allocate")); sock.init_sock(context.runtime, socketChannel, ""); return sock; } finally { selectable.configureBlocking(oldBlocking); } } catch (IOException ioe) { if (ioe.getMessage().equals("accept failed: Resource temporarily unavailable")) { if (!ex) return runtime.newSymbol("wait_readable"); throw runtime.newErrnoEAGAINReadableError("accept"); } throw context.runtime.newIOErrorFromException(ioe); } } }
public synchronized ByteList readnonblock(int number) throws IOException, BadDescriptorException, EOFException { assert number >= 0; if (number == 0) { return null; } if (descriptor.getChannel() instanceof SelectableChannel) { SelectableChannel selectableChannel = (SelectableChannel)descriptor.getChannel(); synchronized (selectableChannel.blockingLock()) { boolean oldBlocking = selectableChannel.isBlocking(); try { selectableChannel.configureBlocking(false); return readpartial(number); } finally { selectableChannel.configureBlocking(oldBlocking); } } } else if (descriptor.getChannel() instanceof SeekableByteChannel) { return fread(number); } else { return null; } }
private void doConnectNonblock(ThreadContext context, Channel channel, SocketAddress addr) { if (!(channel instanceof SelectableChannel)) { throw getRuntime().newErrnoENOPROTOOPTError(); } SelectableChannel selectable = (SelectableChannel)channel; synchronized (selectable.blockingLock()) { boolean oldBlocking = selectable.isBlocking(); try { selectable.configureBlocking(false); try { doConnect(context, channel, addr); } finally { selectable.configureBlocking(oldBlocking); } } catch(ClosedChannelException e) { throw context.runtime.newErrnoECONNREFUSEDError(); } catch(IOException e) { throw SocketUtils.sockerr(context.runtime, "connect(2): name or service not known"); } } }
public synchronized int writenonblock(ByteList buf) throws IOException, BadDescriptorException { checkWritable(); ensureWrite(); // Ruby ignores empty syswrites if (buf == null || buf.length() == 0) return 0; if (buffer.position() != 0 && !flushWrite(false)) return 0; if (descriptor.getChannel() instanceof SelectableChannel) { SelectableChannel selectableChannel = (SelectableChannel)descriptor.getChannel(); synchronized (selectableChannel.blockingLock()) { boolean oldBlocking = selectableChannel.isBlocking(); try { if (oldBlocking) { selectableChannel.configureBlocking(false); } return descriptor.write(ByteBuffer.wrap(buf.getUnsafeBytes(), buf.begin(), buf.length())); } finally { if (oldBlocking) { selectableChannel.configureBlocking(oldBlocking); } } } } else { // can't set nonblocking, so go ahead with it...not much else we can do return descriptor.write(ByteBuffer.wrap(buf.getUnsafeBytes(), buf.begin(), buf.length())); } }
private void doConnectNonblock(ThreadContext context, Channel channel, SocketAddress addr) { if (!(channel instanceof SelectableChannel)) { throw getRuntime().newErrnoENOPROTOOPTError(); } SelectableChannel selectable = (SelectableChannel)channel; synchronized (selectable.blockingLock()) { boolean oldBlocking = selectable.isBlocking(); try { selectable.configureBlocking(false); try { doConnect(context, channel, addr); } finally { selectable.configureBlocking(oldBlocking); } } catch(ClosedChannelException e) { throw context.runtime.newErrnoECONNREFUSEDError(); } catch(IOException e) { throw SocketUtils.sockerr(context.runtime, "connect(2): name or service not known"); } } }
public synchronized ByteList readnonblock(int number) throws IOException, BadDescriptorException, EOFException { assert number >= 0; if (number == 0) { return null; } if (descriptor.getChannel() instanceof SelectableChannel) { SelectableChannel selectableChannel = (SelectableChannel)descriptor.getChannel(); synchronized (selectableChannel.blockingLock()) { boolean oldBlocking = selectableChannel.isBlocking(); try { selectableChannel.configureBlocking(false); return readpartial(number); } finally { selectableChannel.configureBlocking(oldBlocking); } } } else if (descriptor.getChannel() instanceof SeekableByteChannel) { return fread(number); } else { return null; } }
public synchronized ByteList readnonblock(int number) throws IOException, BadDescriptorException, EOFException { assert number >= 0; if (number == 0) { return null; } if (descriptor.getChannel() instanceof SelectableChannel) { SelectableChannel selectableChannel = (SelectableChannel)descriptor.getChannel(); synchronized (selectableChannel.blockingLock()) { boolean oldBlocking = selectableChannel.isBlocking(); try { selectableChannel.configureBlocking(false); return readpartial(number); } finally { selectableChannel.configureBlocking(oldBlocking); } } } else if (descriptor.getChannel() instanceof FileChannel) { return fread(number); } else { return null; } }
public synchronized ByteList readnonblock(int number) throws IOException, BadDescriptorException, EOFException { assert number >= 0; if (number == 0) { return null; } if (descriptor.getChannel() instanceof SelectableChannel) { SelectableChannel selectableChannel = (SelectableChannel)descriptor.getChannel(); synchronized (selectableChannel.blockingLock()) { boolean oldBlocking = selectableChannel.isBlocking(); try { selectableChannel.configureBlocking(false); return readpartial(number); } finally { selectableChannel.configureBlocking(oldBlocking); } } } else if (descriptor.getChannel() instanceof FileChannel) { return fread(number); } else { return null; } }
private IRubyObject doConnectNonblock(ThreadContext context, SocketAddress addr, boolean ex) { Channel channel = getChannel(); if ( ! (channel instanceof SelectableChannel) ) { throw context.runtime.newErrnoENOPROTOOPTError(); } SelectableChannel selectable = (SelectableChannel) channel; synchronized (selectable.blockingLock()) { boolean oldBlocking = selectable.isBlocking(); try { selectable.configureBlocking(false); try { return doConnect(context, addr, ex); } finally { selectable.configureBlocking(oldBlocking); } } catch (ClosedChannelException e) { throw context.runtime.newErrnoECONNREFUSEDError(); } catch (IOException e) { throw sockerr(context.runtime, "connect(2): name or service not known", e); } } }