@JRubyMethod(meta = true) public static IRubyObject for_fd(ThreadContext context, IRubyObject socketClass, IRubyObject fd) { Ruby runtime = context.runtime; if (fd instanceof RubyFixnum) { int intFD = (int)((RubyFixnum)fd).getLongValue(); ChannelDescriptor descriptor = ChannelDescriptor.getDescriptorByFileno(intFD); if (descriptor == null) { throw runtime.newErrnoEBADFError(); } RubySocket socket = (RubySocket)((RubyClass)socketClass).allocate(); socket.initFieldsFromDescriptor(runtime, descriptor); socket.initSocket(runtime, descriptor); return socket; } else { throw runtime.newTypeError(fd, context.runtime.getFixnum()); } }
@JRubyMethod(meta = true) public static IRubyObject for_fd(ThreadContext context, IRubyObject socketClass, IRubyObject fd) { Ruby runtime = context.runtime; if (fd instanceof RubyFixnum) { int intFD = (int)((RubyFixnum)fd).getLongValue(); ChannelDescriptor descriptor = ChannelDescriptor.getDescriptorByFileno(intFD); if (descriptor == null) { throw runtime.newErrnoEBADFError(); } RubySocket socket = (RubySocket)((RubyClass)socketClass).allocate(); socket.initFieldsFromDescriptor(runtime, descriptor); socket.initSocket(runtime, descriptor); return socket; } else { throw runtime.newTypeError(fd, context.runtime.getFixnum()); } }
@JRubyMethod(name = {"socketpair", "pair"}, meta = true) public static IRubyObject socketpair(ThreadContext context, IRubyObject recv, IRubyObject domain, IRubyObject type) { AddressFamily af = SocketUtils.addressFamilyFromArg(domain); if (af == null) af = AddressFamily.AF_UNIX; Sock s = SocketUtils.sockFromArg(type); if (s == null) s = Sock.SOCK_STREAM; if (af != AddressFamily.AF_UNIX || s != Sock.SOCK_STREAM) { throw context.runtime.newErrnoEOPNOTSUPPError("Socket.socketpair only supports streaming UNIX sockets"); } final Ruby runtime = context.runtime; // TODO: type and protocol UnixSocketChannel[] sp; try { sp = UnixSocketChannel.pair(); final RubyClass socketClass = runtime.getClass("Socket"); RubySocket sock0 = new RubySocket(runtime, socketClass); ChannelFD fd0 = newChannelFD(runtime, sp[0]); sock0.initFieldsFromDescriptor(runtime, fd0); sock0.initSocket(fd0); RubySocket sock1 = new RubySocket(runtime, socketClass); ChannelFD fd1 = newChannelFD(runtime, sp[1]); sock1.initFieldsFromDescriptor(runtime, fd1); sock1.initSocket(fd1); return runtime.newArray(sock0, sock1); } catch (IOException ioe) { throw runtime.newIOErrorFromException(ioe); } }
@JRubyMethod(meta = true) public static IRubyObject for_fd(ThreadContext context, IRubyObject socketClass, IRubyObject _fd) { Ruby runtime = context.runtime; if (_fd instanceof RubyFixnum) { int intFD = (int)((RubyFixnum)_fd).getLongValue(); ChannelFD fd = runtime.getFilenoUtil().getWrapperFromFileno(intFD); if (fd == null) { throw runtime.newErrnoEBADFError(); } RubySocket socket = (RubySocket)((RubyClass)socketClass).allocate(); socket.initFieldsFromDescriptor(runtime, fd); socket.initSocket(fd); return socket; } else { throw runtime.newTypeError(_fd, context.runtime.getFixnum()); } }
@JRubyMethod(name = {"socketpair", "pair"}, meta = true) public static IRubyObject socketpair(ThreadContext context, IRubyObject recv, IRubyObject domain, IRubyObject type) { AddressFamily af = SocketUtils.addressFamilyFromArg(domain); if (af == null) af = AddressFamily.AF_UNIX; Sock s = SocketUtils.sockFromArg(type); if (s == null) s = Sock.SOCK_STREAM; if (af != AddressFamily.AF_UNIX || s != Sock.SOCK_STREAM) { throw context.runtime.newErrnoEOPNOTSUPPError("Socket.socketpair only supports streaming UNIX sockets"); } final Ruby runtime = context.runtime; // TODO: type and protocol UnixSocketChannel[] sp; try { sp = UnixSocketChannel.pair(); final RubyClass socketClass = runtime.getClass("Socket"); RubySocket sock0 = new RubySocket(runtime, socketClass); ChannelFD fd0 = newChannelFD(runtime, sp[0]); sock0.initFieldsFromDescriptor(runtime, fd0); sock0.initSocket(fd0); RubySocket sock1 = new RubySocket(runtime, socketClass); ChannelFD fd1 = newChannelFD(runtime, sp[1]); sock1.initFieldsFromDescriptor(runtime, fd1); sock1.initSocket(fd1); return runtime.newArray(sock0, sock1); } catch (IOException ioe) { throw runtime.newIOErrorFromException(ioe); } }
@JRubyMethod(meta = true) public static IRubyObject for_fd(ThreadContext context, IRubyObject socketClass, IRubyObject _fd) { Ruby runtime = context.runtime; if (_fd instanceof RubyFixnum) { int intFD = (int)((RubyFixnum)_fd).getLongValue(); ChannelFD fd = runtime.getFilenoUtil().getWrapperFromFileno(intFD); if (fd == null) { throw runtime.newErrnoEBADFError(); } RubySocket socket = (RubySocket)((RubyClass)socketClass).allocate(); socket.initFieldsFromDescriptor(runtime, fd); socket.initSocket(fd); return socket; } else { throw runtime.newTypeError(_fd, context.runtime.getFixnum()); } }