Search in sources :

Example 1 with ResultContainer

use of sun.nio.ch.sctp.ResultContainer 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)

Aggregations

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