Search in sources :

Example 1 with LastErrorException

use of com.sun.jna.LastErrorException in project buck by facebook.

the class ReferenceCountedFileDescriptor method doClose.

private void doClose() throws IOException {
    try {
        UnixDomainSocketLibrary.close(fd);
        fd = -1;
    } catch (LastErrorException e) {
        throw new IOException(e);
    }
}
Also used : LastErrorException(com.sun.jna.LastErrorException) IOException(java.io.IOException)

Example 2 with LastErrorException

use of com.sun.jna.LastErrorException in project buck by facebook.

the class UnixDomainSocket method doShutdown.

private void doShutdown(int how) throws IOException {
    try {
        int socketFd = fd.acquire();
        if (socketFd != -1) {
            UnixDomainSocketLibrary.shutdown(socketFd, how);
        }
    } catch (LastErrorException e) {
        throw new IOException(e);
    } finally {
        fd.release();
        isConnected = false;
    }
}
Also used : LastErrorException(com.sun.jna.LastErrorException) IOException(java.io.IOException)

Example 3 with LastErrorException

use of com.sun.jna.LastErrorException in project jna by java-native-access.

the class Kernel32Util method formatMessage.

/**
     * Format a message from the value obtained from
     * {@link Kernel32#GetLastError()} or {@link Native#getLastError()}.
     *
     * @param code The error code
     * @return Formatted message.
     */
public static String formatMessage(int code) {
    PointerByReference buffer = new PointerByReference();
    int nLen = Kernel32.INSTANCE.FormatMessage(WinBase.FORMAT_MESSAGE_ALLOCATE_BUFFER | WinBase.FORMAT_MESSAGE_FROM_SYSTEM | WinBase.FORMAT_MESSAGE_IGNORE_INSERTS, null, code, // TODO: // MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT)
    0, buffer, 0, null);
    if (nLen == 0) {
        throw new LastErrorException(Native.getLastError());
    }
    Pointer ptr = buffer.getValue();
    try {
        String s = ptr.getWideString(0);
        return s.trim();
    } finally {
        freeLocalMemory(ptr);
    }
}
Also used : PointerByReference(com.sun.jna.ptr.PointerByReference) LastErrorException(com.sun.jna.LastErrorException) Pointer(com.sun.jna.Pointer)

Example 4 with LastErrorException

use of com.sun.jna.LastErrorException in project nailgun by facebook.

the class NGUnixDomainServerSocket method bind.

public synchronized void bind(SocketAddress endpoint) throws IOException {
    if (!(endpoint instanceof NGUnixDomainServerSocketAddress)) {
        throw new IllegalArgumentException("endpoint must be an instance of NGUnixDomainServerSocketAddress");
    }
    if (isBound) {
        throw new IllegalStateException("Socket is already bound");
    }
    if (isClosed) {
        throw new IllegalStateException("Socket is already closed");
    }
    NGUnixDomainServerSocketAddress unEndpoint = (NGUnixDomainServerSocketAddress) endpoint;
    NGUnixDomainSocketLibrary.SockaddrUn address = new NGUnixDomainSocketLibrary.SockaddrUn(unEndpoint.getPath());
    try {
        int socketFd = fd.get();
        NGUnixDomainSocketLibrary.bind(socketFd, address, address.size());
        NGUnixDomainSocketLibrary.listen(socketFd, backlog);
        isBound = true;
    } catch (LastErrorException e) {
        throw new IOException(e);
    }
}
Also used : LastErrorException(com.sun.jna.LastErrorException) IOException(java.io.IOException)

Example 5 with LastErrorException

use of com.sun.jna.LastErrorException in project nailgun by facebook.

the class NGUnixDomainServerSocket method close.

public synchronized void close() throws IOException {
    if (isClosed) {
        throw new IllegalStateException("Socket is already closed");
    }
    try {
        // Ensure any pending call to accept() fails.
        NGUnixDomainSocketLibrary.close(fd.getAndSet(-1));
        isClosed = true;
    } catch (LastErrorException e) {
        throw new IOException(e);
    }
}
Also used : LastErrorException(com.sun.jna.LastErrorException) IOException(java.io.IOException)

Aggregations

LastErrorException (com.sun.jna.LastErrorException)9 IOException (java.io.IOException)8 IntByReference (com.sun.jna.ptr.IntByReference)2 Pointer (com.sun.jna.Pointer)1 PointerByReference (com.sun.jna.ptr.PointerByReference)1