public static final UnixDatagramChannel open() throws IOException { return new UnixDatagramChannel(); }
UnixDatagramChannel(int fd, UnixSocketAddress remote) throws IOException { this(fd); connect(remote); }
@Override public <T> T getOption(SocketOption<T> name) throws IOException { if (!supportedOptions().contains(name)) { throw new UnsupportedOperationException("'" + name + "' not supported"); } return Common.getSocketOption(getFD(), name); }
public final UnixSocketAddress getRemoteSocketAddress() { if (!isConnected()) { return null; } return remoteAddress != null ? remoteAddress : (remoteAddress = Common.getpeername(getFD())); }
/** * Retrieves the credentials for this UNIX socket. Clients calling this * method will receive the server's credentials, and servers will receive * the client's credentials. User ID, group ID, and PID are supplied. * * See man unix 7; SCM_CREDENTIALS * * @throws UnsupportedOperationException if the underlying socket library * doesn't support the SO_PEERCRED option * @throws SocketException if fetching the socket option failed. * * @return the credentials of the remote; null if not connected */ public final Credentials getCredentials() throws SocketException { if (!chan.isConnected()) { return null; } try { return chan.getOption(UnixSocketOptions.SO_PEERCRED); } catch (IOException e) { throw (SocketException)new SocketException().initCause(e); } }
@Override public UnixDatagramChannel bind(SocketAddress local) throws IOException { localAddress = bindHandler.bind(getFD(), local); return this; }
@Override public int getSendBufferSize() throws SocketException { try { return chan.getOption(UnixSocketOptions.SO_SNDBUF).intValue(); } catch (IOException e) { throw (SocketException)new SocketException().initCause(e); } }
@Override public boolean isBound() { if (null == chan) { return false; } return chan.isBound(); }
@Override public synchronized void close() { if (null != chan && closed.compareAndSet(false, true)) { try { chan.close(); } catch (IOException e) { ignore(); } } }
/** * Returns the address of the endpoint this socket is connected to, or * {@code null} if it is unconnected. * * @return a {@code SocketAddress} representing the remote * endpoint of this socket, or {@code null} if it is * not connected. * A non-null return value is always of type {@link UnixSocketAddress} */ @Override public SocketAddress getRemoteSocketAddress() { if (!isConnected()) { return null; } return chan.getRemoteSocketAddress(); }
/** * Returns the address of the endpoint this socket is bound to. * * @return a {@code SocketAddress} representing the local endpoint of this * socket, or {@code null} if it is closed or not bound. * A non-null return value is always of type {@link UnixSocketAddress} * @see #bind(SocketAddress) */ @Override public SocketAddress getLocalSocketAddress() { if (isClosed()) { return null; } if (null == chan) { return null; } return chan.getLocalSocketAddress(); }
@Override public synchronized void disconnect() { if (isClosed()) { return; } if (null != chan) { try { chan.disconnect(); } catch (IOException e) { ignore(); } } }
/** * Binds this UnixDatagramSocket to a specific AF_UNIX address. * <p> * If the address is {@code null}, then on Linux, an autobind will be performed, * which will bind this socket in Linux' abstract namespace on a unique path, chosen by * the system. On all other platforms, A temporary path in the regular filesystem will be chosen. *<p> * @param local The {@link UnixSocketAddress} to bind to. * @throws SocketException if any error happens during the bind, or if the * socket is already bound. * @throws UnsupportedAddressTypeException if addr is a SocketAddress subclass * not supported by this socket. */ @Override public void bind(final SocketAddress local) throws SocketException { if (null != chan) { if (isClosed()) { throw new SocketException("Socket is closed"); } if (isBound()) { throw new SocketException("already bound"); } try { chan.bind(local); } catch (IOException e) { throw (SocketException)new SocketException().initCause(e); } } }
@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 final UnixSocketAddress getLocalSocketAddress() { return localAddress != null ? localAddress : (localAddress = Common.getsockname(getFD())); }
@Override public int getReceiveBufferSize() throws SocketException { try { return chan.getOption(UnixSocketOptions.SO_RCVBUF).intValue(); } catch (IOException e) { throw (SocketException)new SocketException().initCause(e); } }
@Override public <T> DatagramChannel setOption(SocketOption<T> name, T value) throws IOException { if (name == null) { throw new IllegalArgumentException("name may not be null"); } if (!supportedOptions().contains(name)) { throw new UnsupportedOperationException("'" + name + "' not supported"); } Common.setSocketOption(getFD(), name, value); return this; }
@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; }
@Override public void connect(SocketAddress addr) throws SocketException { try { chan.connect(addr); } catch (IOException e) { throw (SocketException)new SocketException().initCause(e); } }
@Override public int getSoTimeout() throws SocketException { try { return chan.getOption(UnixSocketOptions.SO_RCVTIMEO).intValue(); } catch (IOException e) { throw (SocketException)new SocketException().initCause(e); } }