int length() { return address.length(); }
int length() { return address.length(); }
static UnixSocketAddress bind(int fd, UnixSocketAddress local) throws IOException { SockAddrUnix sa; if (null == local) { // Support autobind sa = SockAddrUnix.create(); sa.setFamily(ProtocolFamily.PF_UNIX); if (currentOS == OS.LINUX) { // On Linux, we simply set an empty path sa.setPath(""); } else { // Emulate something similar (bind to some random unique address), // but use regular namespace File f = Files.createTempFile("jnr-unixsocket-tmp", ".sock").toFile(); f.deleteOnExit(); f.delete(); sa.setPath(f.getPath()); } } else { sa = local.getStruct(); } if (Native.bind(fd, sa, sa.length()) < 0) { throw new IOException(Native.getLastErrorString()); } return getsockname(fd); }
private final boolean doConnect(SockAddrUnix remote) throws IOException { if (Native.connect(getFD(), remote, remote.length()) != 0) { Errno error = Errno.valueOf(LastError.getLastError(jnr.ffi.Runtime.getSystemRuntime())); switch (error) { case EAGAIN: case EWOULDBLOCK: return false; default: throw new IOException(error.toString()); } } return true; }
private boolean doConnect(SockAddrUnix remote) throws IOException { if (Native.connect(getFD(), remote, remote.length()) != 0) { Errno error = Errno.valueOf(LastError.getLastError(jnr.ffi.Runtime .getSystemRuntime())); switch (error) { case EAGAIN: case EWOULDBLOCK: return false; default: throw new IOException(error.toString()); } } return true; }
@Override public int send(ByteBuffer src, SocketAddress target) throws IOException { UnixSocketAddress remote = null; if (null == target) { if (isConnected()) { remote = remoteAddress; } else { throw new IllegalArgumentException("Destination address cannot be null on unconnected datagram sockets"); } } else { if (!(target instanceof UnixSocketAddress)) { throw new UnsupportedAddressTypeException(); } remote = (UnixSocketAddress)target; } SockAddrUnix sa = (null == remote) ? null : remote.getStruct(); int addrlen = (null == sa) ? 0 : sa.length(); int n = Native.sendto(getFD(), src, sa, addrlen); if (n < 0) { throw new IOException(Native.getLastErrorString()); } return n; }