protected void initFromServer(Ruby runtime, RubySocket serverSocket, SocketChannel socketChannel) { soDomain = serverSocket.soDomain; soType = serverSocket.soType; soProtocol = serverSocket.soProtocol; initSocket(newChannelFD(runtime, socketChannel)); }
protected void initFromServer(Ruby runtime, RubySocket serverSocket, SocketChannel socketChannel) { soDomain = serverSocket.soDomain; soType = serverSocket.soType; soProtocol = serverSocket.soProtocol; initSocket(newChannelFD(runtime, socketChannel)); }
protected ChannelFD initChannelFD(Ruby runtime) { try { Channel channel; switch (soType) { case SOCK_STREAM: if ( soProtocolFamily == ProtocolFamily.PF_UNIX || soProtocolFamily == ProtocolFamily.PF_LOCAL ) { channel = UnixSocketChannel.open(); } else if ( soProtocolFamily == ProtocolFamily.PF_INET || soProtocolFamily == ProtocolFamily.PF_INET6 || soProtocolFamily == ProtocolFamily.PF_UNSPEC ) { channel = SocketChannel.open(); } else { throw runtime.newArgumentError("unsupported protocol family `" + soProtocolFamily + "'"); } break; case SOCK_DGRAM: channel = DatagramChannel.open(); break; default: throw runtime.newArgumentError("unsupported socket type `" + soType + "'"); } return newChannelFD(runtime, channel); } catch (IOException e) { throw sockerr(runtime, "initialize: " + e.toString(), e); } }
protected ChannelFD initChannelFD(Ruby runtime) { try { Channel channel; switch (soType) { case SOCK_STREAM: if ( soProtocolFamily == ProtocolFamily.PF_UNIX || soProtocolFamily == ProtocolFamily.PF_LOCAL ) { channel = UnixSocketChannel.open(); } else if ( soProtocolFamily == ProtocolFamily.PF_INET || soProtocolFamily == ProtocolFamily.PF_INET6 || soProtocolFamily == ProtocolFamily.PF_UNSPEC ) { channel = SocketChannel.open(); } else { throw runtime.newArgumentError("unsupported protocol family `" + soProtocolFamily + "'"); } break; case SOCK_DGRAM: channel = DatagramChannel.open(); break; default: throw runtime.newArgumentError("unsupported socket type `" + soType + "'"); } return newChannelFD(runtime, channel); } catch (IOException e) { throw sockerr(runtime, "initialize: " + e.toString(), e); } }
@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(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); } }