public boolean connect(UnixSocketAddress remote) throws IOException { remoteAddress = remote; if (!doConnect(remoteAddress.getStruct())) { state = State.CONNECTING; return false; } else { state = State.CONNECTED; return true; } }
public boolean connect(UnixSocketAddress remote) throws IOException { remoteAddress = remote; if (!doConnect(remoteAddress.getStruct())) { stateLock.writeLock().lock(); state = State.CONNECTING; stateLock.writeLock().unlock(); return false; } else { stateLock.writeLock().lock(); state = State.CONNECTED; stateLock.writeLock().unlock(); return true; } }
public boolean finishConnect() throws IOException { switch (state) { case CONNECTED: return true; case CONNECTING: if (!doConnect(remoteAddress.getStruct())) { return false; } state = State.CONNECTED; return true; default: throw new IllegalStateException("socket is not waiting for connect to complete"); } }
public boolean finishConnect() throws IOException { stateLock.writeLock().lock(); try { switch (state) { case CONNECTED: return true; case CONNECTING: if (!doConnect(remoteAddress.getStruct())) { return false; } state = State.CONNECTED; return true; default: throw new IllegalStateException( "socket is not waiting for connect to complete"); } } finally { stateLock.writeLock().unlock(); } }
public void bind(SocketAddress endpoint, int backlog) throws java.io.IOException { if (!(endpoint instanceof UnixSocketAddress)) { throw new IOException("Invalid address"); } UnixSocketAddress addr = (UnixSocketAddress) endpoint; if (Native.bind(fd, addr.getStruct(), addr.length()) < 0) { throw new IOException("bind failed: " + Native.getLastErrorString()); } if (Native.listen(fd, backlog) < 0) { throw new IOException("listen failed: " + Native.getLastErrorString()); } }
static UnixSocketAddress getsockname(int sockfd) { UnixSocketAddress remote = new UnixSocketAddress(); IntByReference len = new IntByReference(remote.getStruct().getMaximumLength()); if (Native.libc().getsockname(sockfd, remote.getStruct(), len) < 0) { throw new Error(Native.getLastErrorString()); } return remote; } }
static UnixSocketAddress getpeername(int sockfd) { UnixSocketAddress remote = new UnixSocketAddress(); IntByReference len = new IntByReference(remote.getStruct().getMaximumLength()); if (Native.libc().getpeername(sockfd, remote.getStruct(), len) < 0) { throw new Error(Native.getLastErrorString()); } return remote; }
@Override public UnixSocketAddress receive(ByteBuffer src) throws IOException { UnixSocketAddress remote = new UnixSocketAddress(); int n = Native.recvfrom(getFD(), src, remote.getStruct()); if (n < 0) { throw new IOException(Native.getLastErrorString()); } return remote; }
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); }
@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; }
public UnixSocketChannel accept() throws IOException { UnixSocketAddress remote = new UnixSocketAddress(); SockAddrUnix addr = remote.getStruct(); IntByReference len = new IntByReference(addr.getMaximumLength()); int clientfd = Native.accept(getFD(), addr, len); if (clientfd < 0) { throw new IOException("accept failed: " + Native.getLastErrorString()); } // Always force the socket back to blocking mode Native.setBlocking(clientfd, true); return new UnixSocketChannel(clientfd, remote); }
static UnixSocketAddress getsockname(int sockfd) { UnixSocketAddress local = new UnixSocketAddress(); SockAddrUnix addr = local.getStruct(); IntByReference len = new IntByReference(addr.getMaximumLength()); if (Native.libc().getsockname(sockfd, addr, len) < 0) { throw new Error(Native.getLastErrorString()); } addr.updatePath(len.getValue()); return local; }
static UnixSocketAddress getpeername(int sockfd) { UnixSocketAddress remote = new UnixSocketAddress(); SockAddrUnix addr = remote.getStruct(); IntByReference len = new IntByReference(addr.getMaximumLength()); if (Native.libc().getpeername(sockfd, addr, len) < 0) { throw new Error(Native.getLastErrorString()); } addr.updatePath(len.getValue()); return remote; }
public UnixSocketChannel accept() throws IOException { UnixSocketAddress remote = new UnixSocketAddress(); SockAddrUnix addr = remote.getStruct(); int maxLength = addr.getMaximumLength(); IntByReference len = new IntByReference(maxLength); int clientfd = Native.accept(getFD(), addr, len); if (clientfd < 0) { if (isBlocking()) { throw new IOException("accept failed: " + Native.getLastErrorString()); } return null; } // Handle unnamed sockets and sockets in Linux' abstract namespace addr.updatePath(len.getValue()); // Always force the socket back to blocking mode Native.setBlocking(clientfd, true); return new UnixSocketChannel(clientfd); }