@Override public IRubyObject setsockopt(ThreadContext context, IRubyObject _level, IRubyObject _opt, IRubyObject val) { SocketLevel level = SocketUtils.levelFromArg(_level); SocketOption opt = SocketUtils.optionFromArg(_opt); switch(level) { case SOL_SOCKET: switch(opt) { case SO_KEEPALIVE: { // TODO: socket options } break; default: throw context.runtime.newErrnoENOPROTOOPTError(); } break; default: throw context.runtime.newErrnoENOPROTOOPTError(); } return context.runtime.newFixnum(0); }
@Override public IRubyObject setsockopt(ThreadContext context, IRubyObject _level, IRubyObject _opt, IRubyObject val) { SocketLevel level = levelFromArg(_level); SocketOption opt = optionFromArg(_opt); switch(level) { case SOL_SOCKET: switch(opt) { case SO_KEEPALIVE: { // TODO: socket options } break; default: throw context.runtime.newErrnoENOPROTOOPTError(); } break; default: throw context.runtime.newErrnoENOPROTOOPTError(); } return context.runtime.newFixnum(0); }
@Override public IRubyObject setsockopt(ThreadContext context, IRubyObject _level, IRubyObject _opt, IRubyObject val) { SocketLevel level = levelFromArg(_level); SocketOption opt = optionFromArg(_opt); switch(level) { case SOL_SOCKET: switch(opt) { case SO_KEEPALIVE: { // TODO: socket options } break; default: throw context.runtime.newErrnoENOPROTOOPTError(); } break; default: throw context.runtime.newErrnoENOPROTOOPTError(); } return context.runtime.newFixnum(0); }
@Override public IRubyObject setsockopt(ThreadContext context, IRubyObject _level, IRubyObject _opt, IRubyObject val) { SocketLevel level = SocketUtils.levelFromArg(_level); SocketOption opt = SocketUtils.optionFromArg(_opt); switch(level) { case SOL_SOCKET: switch(opt) { case SO_KEEPALIVE: { // TODO: socket options } break; default: throw context.runtime.newErrnoENOPROTOOPTError(); } break; default: throw context.runtime.newErrnoENOPROTOOPTError(); } return context.runtime.newFixnum(0); }
private void doBind(ThreadContext context, Channel channel, InetSocketAddress iaddr, int backlog) { Ruby runtime = context.runtime; try { if (channel instanceof ServerSocketChannel) { ServerSocket socket = ((ServerSocketChannel)channel).socket(); socket.bind(iaddr, backlog); } else { throw runtime.newErrnoENOPROTOOPTError(); } } catch(UnknownHostException e) { throw SocketUtils.sockerr(runtime, "bind(2): unknown host"); } catch(SocketException e) { handleSocketException(runtime, "bind", e); } catch(IOException e) { throw SocketUtils.sockerr(runtime, "bind(2): name or service not known"); } catch (IllegalArgumentException iae) { throw SocketUtils.sockerr(runtime, iae.getMessage()); } } }// RubySocket
private void doBind(ThreadContext context, Channel channel, InetSocketAddress iaddr, int backlog) { Ruby runtime = context.runtime; try { if (channel instanceof ServerSocketChannel) { ServerSocket socket = ((ServerSocketChannel)channel).socket(); socket.bind(iaddr, backlog); } else { throw runtime.newErrnoENOPROTOOPTError(); } } catch(UnknownHostException e) { throw SocketUtils.sockerr(runtime, "bind(2): unknown host"); } catch(SocketException e) { handleSocketException(runtime, "bind", e); } catch(IOException e) { throw SocketUtils.sockerr(runtime, "bind(2): name or service not known"); } catch (IllegalArgumentException iae) { throw SocketUtils.sockerr(runtime, iae.getMessage()); } } }// RubySocket
private void doBind(ThreadContext context, Channel channel, InetSocketAddress iaddr, int backlog) { Ruby runtime = context.runtime; try { if (channel instanceof ServerSocketChannel) { ServerSocket socket = ((ServerSocketChannel)channel).socket(); socket.bind(iaddr, backlog); } else { throw runtime.newErrnoENOPROTOOPTError(); } } catch (UnknownHostException e) { throw SocketUtils.sockerr(runtime, "bind(2): unknown host"); } catch (SocketException e) { handleSocketException(runtime, e, "bind(2)", iaddr); } catch (IOException e) { throw sockerr(runtime, "bind(2): name or service not known", e); } catch (IllegalArgumentException e) { throw sockerr(runtime, e.getMessage(), e); } } }// RubySocket
private void doBind(ThreadContext context, Channel channel, InetSocketAddress iaddr, int backlog) { Ruby runtime = context.runtime; try { if (channel instanceof ServerSocketChannel) { ServerSocket socket = ((ServerSocketChannel)channel).socket(); socket.bind(iaddr, backlog); } else { throw runtime.newErrnoENOPROTOOPTError(); } } catch (UnknownHostException e) { throw SocketUtils.sockerr(runtime, "bind(2): unknown host"); } catch (SocketException e) { handleSocketException(runtime, e, "bind(2)", iaddr); } catch (IOException e) { throw sockerr(runtime, "bind(2): name or service not known", e); } catch (IllegalArgumentException e) { throw sockerr(runtime, e.getMessage(), e); } } }// RubySocket
protected void doBind(ThreadContext context, Channel channel, InetSocketAddress iaddr) { Ruby runtime = context.runtime; try { if (channel instanceof SocketChannel) { Socket socket = ((SocketChannel)channel).socket(); socket.bind(iaddr); } else if (channel instanceof UnixSocketChannel) { // do nothing } else if (channel instanceof DatagramChannel) { DatagramSocket socket = ((DatagramChannel)channel).socket(); socket.bind(iaddr); } else { throw runtime.newErrnoENOPROTOOPTError(); } } catch(UnknownHostException e) { throw SocketUtils.sockerr(runtime, "bind(2): unknown host"); } catch(SocketException e) { handleSocketException(runtime, "bind", e); } catch(IOException e) { throw SocketUtils.sockerr(runtime, "bind(2): name or service not known"); } catch (IllegalArgumentException iae) { throw SocketUtils.sockerr(runtime, iae.getMessage()); } }
protected void doBind(ThreadContext context, Channel channel, InetSocketAddress iaddr) { Ruby runtime = context.runtime; try { if (channel instanceof SocketChannel) { Socket socket = ((SocketChannel)channel).socket(); socket.bind(iaddr); } else if (channel instanceof UnixSocketChannel) { // do nothing } else if (channel instanceof DatagramChannel) { DatagramSocket socket = ((DatagramChannel)channel).socket(); socket.bind(iaddr); } else { throw runtime.newErrnoENOPROTOOPTError(); } } catch(UnknownHostException e) { throw SocketUtils.sockerr(runtime, "bind(2): unknown host"); } catch(SocketException e) { handleSocketException(runtime, "bind", e); } catch(IOException e) { throw SocketUtils.sockerr(runtime, "bind(2): name or service not known"); } catch (IllegalArgumentException iae) { throw SocketUtils.sockerr(runtime, iae.getMessage()); } }
private RubySocket doAccept(ThreadContext context, Channel channel) { Ruby runtime = context.runtime; try { if (channel instanceof ServerSocketChannel) { ServerSocketChannel serverChannel = (ServerSocketChannel)getChannel(); SocketChannel socket = serverChannel.accept(); if (socket == null) { // This appears to be undocumented in JDK; null as a sentinel value // for a nonblocking accept with nothing available. We raise for Ruby. // indicates that no connection is available in non-blocking mode throw runtime.newErrnoEAGAINReadableError("accept(2) would block"); } RubySocket rubySocket = new RubySocket(runtime, runtime.getClass("Socket")); rubySocket.initFromServer(runtime, this, socket); return rubySocket; } else { throw runtime.newErrnoENOPROTOOPTError(); } } catch (IllegalBlockingModeException ibme) { // indicates that no connection is available in non-blocking mode throw runtime.newErrnoEAGAINReadableError("accept(2) would block"); } catch(IOException e) { throw SocketUtils.sockerr(runtime, e.getLocalizedMessage()); } }
private RubySocket doAccept(ThreadContext context, Channel channel) { Ruby runtime = context.runtime; try { if (channel instanceof ServerSocketChannel) { ServerSocketChannel serverChannel = (ServerSocketChannel)getChannel(); SocketChannel socket = serverChannel.accept(); if (socket == null) { // This appears to be undocumented in JDK; null as a sentinel value // for a nonblocking accept with nothing available. We raise for Ruby. // indicates that no connection is available in non-blocking mode throw runtime.newErrnoEAGAINReadableError("accept(2) would block"); } RubySocket rubySocket = new RubySocket(runtime, runtime.getClass("Socket")); rubySocket.initFromServer(runtime, this, socket); return rubySocket; } else { throw runtime.newErrnoENOPROTOOPTError(); } } catch (IllegalBlockingModeException ibme) { // indicates that no connection is available in non-blocking mode throw runtime.newErrnoEAGAINReadableError("accept(2) would block"); } catch(IOException e) { throw SocketUtils.sockerr(runtime, e.getLocalizedMessage()); } }
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()); } }
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 static IRubyObject doAcceptNonblock(RubySocket sock, ThreadContext context, boolean ex) { try { Channel channel = sock.getChannel(); if (channel instanceof SelectableChannel) { SelectableChannel selectable = (SelectableChannel)channel; synchronized (selectable.blockingLock()) { boolean oldBlocking = selectable.isBlocking(); try { selectable.configureBlocking(false); IRubyObject socket = doAccept(sock, context, ex); if (!(socket instanceof RubySocket)) return socket; SocketChannel socketChannel = (SocketChannel)((RubySocket)socket).getChannel(); InetSocketAddress addr = (InetSocketAddress)socketChannel.socket().getRemoteSocketAddress(); return context.runtime.newArray( socket, Sockaddr.packSockaddrFromAddress(context, addr)); } finally { selectable.configureBlocking(oldBlocking); } } } else { throw context.runtime.newErrnoENOPROTOOPTError(); } } catch (IOException e) { throw sockerr(context.runtime, e.getLocalizedMessage(), e); } }
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"); } } }
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); } } }
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); } } }
public static IRubyObject doAccept(RubySocket sock, ThreadContext context, boolean ex) { Ruby runtime = context.runtime; Channel channel = sock.getChannel(); try { if (channel instanceof ServerSocketChannel) { ServerSocketChannel serverChannel = (ServerSocketChannel)sock.getChannel(); SocketChannel socket = serverChannel.accept(); if (socket == null) { // This appears to be undocumented in JDK; null as a sentinel value // for a nonblocking accept with nothing available. We raise for Ruby. // indicates that no connection is available in non-blocking mode if (!ex) return runtime.newSymbol("wait_readable"); throw runtime.newErrnoEAGAINReadableError("accept(2) would block"); } RubySocket rubySocket = new RubySocket(runtime, runtime.getClass("Socket")); rubySocket.initFromServer(runtime, sock, socket); return runtime.newArray(rubySocket, new Addrinfo(runtime, runtime.getClass("Addrinfo"), socket.getRemoteAddress())); } throw runtime.newErrnoENOPROTOOPTError(); } catch (IllegalBlockingModeException e) { // indicates that no connection is available in non-blocking mode if (!ex) return runtime.newSymbol("wait_readable"); throw runtime.newErrnoEAGAINReadableError("accept(2) would block"); } catch (IOException e) { throw sockerr(runtime, e.getLocalizedMessage(), e); } }
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"); } } }