Search in sources :

Example 1 with IllegalReceiveException

use of com.sun.nio.sctp.IllegalReceiveException in project jdk8u_jdk by JetBrains.

the class SctpMultiChannelImpl method receive.

@Override
public <T> MessageInfo receive(ByteBuffer buffer, T attachment, NotificationHandler<T> handler) throws IOException {
    if (buffer == null)
        throw new IllegalArgumentException("buffer cannot be null");
    if (buffer.isReadOnly())
        throw new IllegalArgumentException("Read-only buffer");
    if (receiveInvoked.get())
        throw new IllegalReceiveException("cannot invoke receive from handler");
    receiveInvoked.set(Boolean.TRUE);
    try {
        ResultContainer resultContainer = new ResultContainer();
        do {
            resultContainer.clear();
            synchronized (receiveLock) {
                ensureOpen();
                if (!isBound())
                    throw new NotYetBoundException();
                int n = 0;
                try {
                    begin();
                    synchronized (stateLock) {
                        if (!isOpen())
                            return null;
                        receiverThread = NativeThread.current();
                    }
                    do {
                        n = receive(fdVal, buffer, resultContainer);
                    } while ((n == IOStatus.INTERRUPTED) && isOpen());
                } finally {
                    receiverCleanup();
                    end((n > 0) || (n == IOStatus.UNAVAILABLE));
                    assert IOStatus.check(n);
                }
                if (!resultContainer.isNotification()) {
                    /* message or nothing */
                    if (resultContainer.hasSomething()) {
                        /* Set the association before returning */
                        MessageInfoImpl info = resultContainer.getMessageInfo();
                        info.setAssociation(lookupAssociation(info.associationID()));
                        SecurityManager sm = System.getSecurityManager();
                        if (sm != null) {
                            InetSocketAddress isa = (InetSocketAddress) info.address();
                            if (!addressMap.containsKey(isa)) {
                                /* must be a new association */
                                try {
                                    sm.checkAccept(isa.getAddress().getHostAddress(), isa.getPort());
                                } catch (SecurityException se) {
                                    buffer.clear();
                                    throw se;
                                }
                            }
                        }
                        assert info.association() != null;
                        return info;
                    } else {
                        /* Non-blocking may return null if nothing available*/
                        return null;
                    }
                } else {
                    /* notification */
                    synchronized (stateLock) {
                        handleNotificationInternal(resultContainer);
                    }
                }
            }
        /* receiveLock */
        } while (handler == null ? true : (invokeNotificationHandler(resultContainer, handler, attachment) == HandlerResult.CONTINUE));
    } finally {
        receiveInvoked.set(Boolean.FALSE);
    }
    return null;
}
Also used : NotYetBoundException(java.nio.channels.NotYetBoundException) IllegalReceiveException(com.sun.nio.sctp.IllegalReceiveException) InetSocketAddress(java.net.InetSocketAddress) ResultContainer(sun.nio.ch.sctp.ResultContainer)

Example 2 with IllegalReceiveException

use of com.sun.nio.sctp.IllegalReceiveException in project jdk8u_jdk by JetBrains.

the class SctpChannelImpl method receive.

private <T> MessageInfo receive(ByteBuffer buffer, T attachment, NotificationHandler<T> handler, boolean fromConnect) throws IOException {
    if (buffer == null)
        throw new IllegalArgumentException("buffer cannot be null");
    if (buffer.isReadOnly())
        throw new IllegalArgumentException("Read-only buffer");
    if (receiveInvoked.get())
        throw new IllegalReceiveException("cannot invoke receive from handler");
    receiveInvoked.set(Boolean.TRUE);
    try {
        ResultContainer resultContainer = new ResultContainer();
        do {
            resultContainer.clear();
            synchronized (receiveLock) {
                if (!ensureReceiveOpen())
                    return null;
                int n = 0;
                try {
                    begin();
                    synchronized (stateLock) {
                        if (!isOpen())
                            return null;
                        receiverThread = NativeThread.current();
                    }
                    do {
                        n = receive(fdVal, buffer, resultContainer, fromConnect);
                    } while ((n == IOStatus.INTERRUPTED) && isOpen());
                } finally {
                    receiverCleanup();
                    end((n > 0) || (n == IOStatus.UNAVAILABLE));
                    assert IOStatus.check(n);
                }
                if (!resultContainer.isNotification()) {
                    /* message or nothing */
                    if (resultContainer.hasSomething()) {
                        /* Set the association before returning */
                        MessageInfoImpl info = resultContainer.getMessageInfo();
                        synchronized (stateLock) {
                            assert association != null;
                            info.setAssociation(association);
                        }
                        return info;
                    } else
                        /* Non-blocking may return null if nothing available*/
                        return null;
                } else {
                    /* notification */
                    synchronized (stateLock) {
                        handleNotificationInternal(resultContainer);
                    }
                }
                if (fromConnect) {
                    /* If we reach here, then it was connect that invoked
                         * receive and received the COMM_UP. We have already
                         * handled the COMM_UP with the internal notification
                         * handler. Simply return. */
                    return null;
                }
            }
        /* receiveLock */
        } while (handler == null ? true : (invokeNotificationHandler(resultContainer, handler, attachment) == HandlerResult.CONTINUE));
        return null;
    } finally {
        receiveInvoked.set(Boolean.FALSE);
    }
}
Also used : IllegalReceiveException(com.sun.nio.sctp.IllegalReceiveException)

Aggregations

IllegalReceiveException (com.sun.nio.sctp.IllegalReceiveException)2 InetSocketAddress (java.net.InetSocketAddress)1 NotYetBoundException (java.nio.channels.NotYetBoundException)1 ResultContainer (sun.nio.ch.sctp.ResultContainer)1