public IRubyObject allocate(Ruby runtime, RubyClass klass) { return new RubyUDPSocket(runtime, klass); } };
@Deprecated public IRubyObject bind(IRubyObject host, IRubyObject port) { return bind(getRuntime().getCurrentContext(), host, port); }
@Deprecated public IRubyObject connect(IRubyObject host, IRubyObject port) { return connect(getRuntime().getCurrentContext(), host, port); }
private static ReceiveTuple doReceiveTuple(RubyBasicSocket socket, final Ruby runtime, final boolean non_block, int length) throws IOException { ReceiveTuple tuple = new ReceiveTuple(); final IRubyObject result; if (socket.multicastStateManager == null) { result = doReceive(socket, runtime, non_block, length, tuple); } else { result = doReceiveMulticast(socket, runtime, non_block, length, tuple); } return result == null ? null : tuple; // need to return null for non_block (if op would block) }
return send(context, _mesg, _flags); written = ((DatagramChannel) this.getChannel()).send(buf, addr); throw sockerr(runtime, e.getLocalizedMessage(), e);
private DatagramChannel getDatagramChannel() { return (DatagramChannel) getChannel(); }
@JRubyMethod public IRubyObject send(ThreadContext context, IRubyObject _mesg, IRubyObject _flags) { // TODO: implement flags final Ruby runtime = context.runtime; try { int written; RubyString data = _mesg.convertToString(); ByteList dataBL = data.getByteList(); ByteBuffer buf = ByteBuffer.wrap(dataBL.unsafeBytes(), dataBL.begin(), dataBL.realSize()); written = ((DatagramChannel) this.getChannel()).write(buf); return runtime.newFixnum(written); } catch (NotYetConnectedException e) { throw runtime.newErrnoEDESTADDRREQError("send(2)"); } catch (UnknownHostException e) { throw SocketUtils.sockerr(runtime, "send: name or service not known"); } catch (IOException e) { // SocketException throw runtime.newIOErrorFromException(e); } catch (RaiseException e) { throw e; } catch (Exception e) { throw sockerr(runtime, e.getLocalizedMessage(), e); } }
private ReceiveTuple doReceiveNonblockTuple(Ruby runtime, int length) throws IOException { DatagramChannel channel = (DatagramChannel)getChannel(); synchronized (channel.blockingLock()) { boolean oldBlocking = channel.isBlocking(); channel.configureBlocking(false); try { return doReceiveTuple(runtime, length); } finally { channel.configureBlocking(oldBlocking); } } }
return send(context, _mesg, _flags); written = ((DatagramChannel) this.getChannel()).send(buf, addr);
private static IRubyObject doReceive(RubyBasicSocket socket, final Ruby runtime, final boolean non_block, int length) throws IOException { return doReceive(socket, runtime, non_block, length, null); }
@JRubyMethod public IRubyObject recvfrom_nonblock(ThreadContext context, IRubyObject _length) { Ruby runtime = context.runtime; try { int length = RubyNumeric.fix2int(_length); ReceiveTuple tuple = doReceiveNonblockTuple(runtime, length); IRubyObject addressArray = addrFor(context, tuple.sender, false); return runtime.newArray(tuple.result, addressArray); } catch (UnknownHostException e) { throw SocketUtils.sockerr(runtime, "recvfrom: name or service not known"); } catch (PortUnreachableException e) { throw runtime.newErrnoECONNREFUSEDError(); } catch (IOException e) { throw SocketUtils.sockerr(runtime, "recvfrom: name or service not known"); } }
/** * Overrides IPSocket#recvfrom */ @Override public IRubyObject recvfrom(ThreadContext context, IRubyObject _length) { Ruby runtime = context.runtime; try { int length = RubyNumeric.fix2int(_length); ReceiveTuple tuple = doReceiveTuple(runtime, length); IRubyObject addressArray = addrFor(context, tuple.sender, false); return runtime.newArray(tuple.result, addressArray); } catch (UnknownHostException e) { throw SocketUtils.sockerr(runtime, "recvfrom: name or service not known"); } catch (PortUnreachableException e) { throw runtime.newErrnoECONNREFUSEDError(); } catch (IOException e) { throw SocketUtils.sockerr(runtime, "recvfrom: name or service not known"); } }
/** * Overrides BasicSocket#recv */ @Override public IRubyObject recv(ThreadContext context, IRubyObject length) { final Ruby runtime = context.runtime; try { return doReceive(this, runtime, false, RubyNumeric.fix2int(length), null); } catch (PortUnreachableException e) { throw runtime.newErrnoECONNREFUSEDError(); } catch (IOException e) { // SocketException throw runtime.newIOErrorFromException(e); } catch (RaiseException e) { throw e; } catch (Exception e) { throw sockerr(runtime, e.getLocalizedMessage(), e); } }
public static IRubyObject recvfrom(RubyBasicSocket socket, ThreadContext context, IRubyObject length) { final Ruby runtime = context.runtime; try { ReceiveTuple tuple = doReceiveTuple(socket, runtime, false, RubyNumeric.fix2int(length)); IRubyObject addressArray = socket.addrFor(context, tuple.sender, false); return runtime.newArray(tuple.result, addressArray); } catch (UnknownHostException e) { throw SocketUtils.sockerr(runtime, "recvfrom: name or service not known"); } catch (PortUnreachableException e) { throw runtime.newErrnoECONNREFUSEDError(); } catch (IOException e) { // SocketException throw runtime.newIOErrorFromException(e); } catch (RaiseException e) { throw e; } catch (Exception e) { throw sockerr(runtime, e.getLocalizedMessage(), e); } }
ReceiveTuple tuple = doReceiveNonblockTuple(socket, runtime, RubyNumeric.fix2int(length)); throw sockerr(runtime, e.getLocalizedMessage(), e);
@JRubyMethod(rest = true, meta = true) public static IRubyObject open(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) { RubyUDPSocket sock = (RubyUDPSocket) recv.callMethod(context, "new", args); if (!block.isGiven()) { return sock; } try { return block.yield(context, sock); } finally { if (sock.openFile.isOpen()) { sock.close(); } } }
public void load(final Ruby runtime, boolean wrap) throws IOException { runtime.defineClass("SocketError", runtime.getStandardError(), runtime.getStandardError().getAllocator()); RubyBasicSocket.createBasicSocket(runtime); RubySocket.createSocket(runtime); RubyServerSocket.createServerSocket(runtime); if (runtime.getInstanceConfig().isNativeEnabled() && !Platform.IS_WINDOWS) { RubyUNIXSocket.createUNIXSocket(runtime); RubyUNIXServer.createUNIXServer(runtime); } RubyIPSocket.createIPSocket(runtime); RubyTCPSocket.createTCPSocket(runtime); RubyTCPServer.createTCPServer(runtime); RubyUDPSocket.createUDPSocket(runtime); Addrinfo.createAddrinfo(runtime); Option.createOption(runtime); Ifaddr.createIfaddr(runtime); } }
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 ReceiveTuple doReceiveTuple(Ruby runtime, int length) throws IOException { ReceiveTuple tuple = new ReceiveTuple(); if (this.multicastStateManager == null) { doReceive(runtime, length, tuple); } else { doReceiveMulticast(runtime, length, tuple); } return tuple; }
return send(context, _mesg, _flags); written = ((DatagramChannel) this.getChannel()).send(buf, addr); throw sockerr(runtime, e.getLocalizedMessage(), e);