Search in sources :

Example 6 with MessageInfo

use of com.sun.nio.sctp.MessageInfo in project netty by netty.

the class NioSctpChannel method doWriteMessage.

@Override
protected boolean doWriteMessage(Object msg, ChannelOutboundBuffer in) throws Exception {
    SctpMessage packet = (SctpMessage) msg;
    ByteBuf data = packet.content();
    int dataLen = data.readableBytes();
    if (dataLen == 0) {
        return true;
    }
    ByteBufAllocator alloc = alloc();
    boolean needsCopy = data.nioBufferCount() != 1;
    if (!needsCopy) {
        if (!data.isDirect() && alloc.isDirectBufferPooled()) {
            needsCopy = true;
        }
    }
    ByteBuffer nioData;
    if (!needsCopy) {
        nioData = data.nioBuffer();
    } else {
        data = alloc.directBuffer(dataLen).writeBytes(data);
        nioData = data.nioBuffer();
    }
    final MessageInfo mi = MessageInfo.createOutgoing(association(), null, packet.streamIdentifier());
    mi.payloadProtocolID(packet.protocolIdentifier());
    mi.streamNumber(packet.streamIdentifier());
    mi.unordered(packet.isUnordered());
    final int writtenBytes = javaChannel().send(nioData, mi);
    return writtenBytes > 0;
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) RecvByteBufAllocator(io.netty.channel.RecvByteBufAllocator) ByteBuf(io.netty.buffer.ByteBuf) SctpMessage(io.netty.channel.sctp.SctpMessage) ByteBuffer(java.nio.ByteBuffer) MessageInfo(com.sun.nio.sctp.MessageInfo)

Example 7 with MessageInfo

use of com.sun.nio.sctp.MessageInfo in project netty by netty.

the class NioSctpChannel method doReadMessages.

@Override
protected int doReadMessages(List<Object> buf) throws Exception {
    SctpChannel ch = javaChannel();
    RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
    ByteBuf buffer = allocHandle.allocate(config().getAllocator());
    boolean free = true;
    try {
        ByteBuffer data = buffer.internalNioBuffer(buffer.writerIndex(), buffer.writableBytes());
        int pos = data.position();
        MessageInfo messageInfo = ch.receive(data, null, notificationHandler);
        if (messageInfo == null) {
            return 0;
        }
        allocHandle.lastBytesRead(data.position() - pos);
        buf.add(new SctpMessage(messageInfo, buffer.writerIndex(buffer.writerIndex() + allocHandle.lastBytesRead())));
        free = false;
        return 1;
    } catch (Throwable cause) {
        PlatformDependent.throwException(cause);
        return -1;
    } finally {
        if (free) {
            buffer.release();
        }
    }
}
Also used : SctpChannel(com.sun.nio.sctp.SctpChannel) RecvByteBufAllocator(io.netty.channel.RecvByteBufAllocator) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) SctpMessage(io.netty.channel.sctp.SctpMessage) MessageInfo(com.sun.nio.sctp.MessageInfo)

Example 8 with MessageInfo

use of com.sun.nio.sctp.MessageInfo in project netty by netty.

the class OioSctpChannel method doReadMessages.

@Override
protected int doReadMessages(List<Object> msgs) throws Exception {
    if (!readSelector.isOpen()) {
        return 0;
    }
    int readMessages = 0;
    final int selectedKeys = readSelector.select(SO_TIMEOUT);
    final boolean keysSelected = selectedKeys > 0;
    if (!keysSelected) {
        return readMessages;
    }
    // We must clear the selectedKeys because the Selector will never do it. If we do not clear it, the selectionKey
    // will always be returned even if there is no data can be read which causes performance issue. And in some
    // implementation of Selector, the select method may return 0 if the selectionKey which is ready for process has
    // already been in the selectedKeys and cause the keysSelected above to be false even if we actually have
    // something to read.
    readSelector.selectedKeys().clear();
    final RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
    ByteBuf buffer = allocHandle.allocate(config().getAllocator());
    boolean free = true;
    try {
        ByteBuffer data = buffer.nioBuffer(buffer.writerIndex(), buffer.writableBytes());
        MessageInfo messageInfo = ch.receive(data, null, notificationHandler);
        if (messageInfo == null) {
            return readMessages;
        }
        data.flip();
        allocHandle.lastBytesRead(data.remaining());
        msgs.add(new SctpMessage(messageInfo, buffer.writerIndex(buffer.writerIndex() + allocHandle.lastBytesRead())));
        free = false;
        ++readMessages;
    } catch (Throwable cause) {
        PlatformDependent.throwException(cause);
    } finally {
        if (free) {
            buffer.release();
        }
    }
    return readMessages;
}
Also used : RecvByteBufAllocator(io.netty.channel.RecvByteBufAllocator) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) SctpMessage(io.netty.channel.sctp.SctpMessage) MessageInfo(com.sun.nio.sctp.MessageInfo)

Example 9 with MessageInfo

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

the class Bind method testBindUnbind.

void testBindUnbind(boolean connected) {
    SctpChannel channel = null;
    SctpChannel peerChannel = null;
    debug("testBindUnbind, connected: " + connected);
    try {
        channel = SctpChannel.open();
        List<InetAddress> addresses = Util.getAddresses(true, false);
        Iterator iterator = addresses.iterator();
        InetSocketAddress a = new InetSocketAddress((InetAddress) iterator.next(), 0);
        debug("channel.bind( " + a + ")");
        channel.bind(a);
        while (iterator.hasNext()) {
            InetAddress ia = (InetAddress) iterator.next();
            debug("channel.bindAddress(" + ia + ")");
            channel.bindAddress(ia);
        }
        if (debug) {
            Util.dumpAddresses(channel, out);
        }
        if (connected) {
            /* Test with connected channel */
            peerChannel = connectChannel(channel);
        }
        /* TEST 1: bind/unbindAddresses on the system addresses */
        debug("bind/unbindAddresses on the system addresses");
        List<InetAddress> addrs = Util.getAddresses(true, false);
        for (InetAddress addr : addrs) {
            try {
                debug("unbindAddress: " + addr);
                check(boundAddress(channel, addr), "trying to remove address that is not bound");
                channel.unbindAddress(addr);
                if (debug) {
                    Util.dumpAddresses(channel, out);
                }
                check(!boundAddress(channel, addr), "address was not removed");
                debug("bindAddress: " + addr);
                channel.bindAddress(addr);
                if (debug) {
                    Util.dumpAddresses(channel, out);
                }
                check(boundAddress(channel, addr), "address is not bound");
            } catch (IOException ioe) {
                unexpected(ioe);
            }
        }
        /* TEST 2: bindAddress - already bound address. */
        InetAddress againAddress = addrs.get(0);
        try {
            debug("bind already bound address " + againAddress);
            channel.bindAddress(againAddress);
        } catch (AlreadyBoundException unused) {
            debug("Caught AlreadyBoundException - OK");
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 3: bind non local address */
        try {
            InetAddress nla = InetAddress.getByName("123.123.123.123");
            debug("bind non local address " + nla);
            channel.bindAddress(nla);
        } catch (IOException ioe) {
            debug("Informative only " + ioe);
        }
        /* TEST 4: unbind address that is not bound */
        try {
            debug("unbind address that is not bound " + againAddress);
            /* remove address first then again */
            channel.unbindAddress(againAddress);
            channel.unbindAddress(againAddress);
        } catch (IllegalUnbindException unused) {
            debug("Caught IllegalUnbindException - OK");
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 5: unbind address that is not bound */
        try {
            InetAddress nla = InetAddress.getByName("123.123.123.123");
            debug("unbind address that is not bound " + nla);
            channel.unbindAddress(nla);
        } catch (IllegalUnbindException unused) {
            debug("Caught IllegalUnbindException - OK");
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        if (connected) {
            channel.shutdown();
            BindNotificationHandler handler = new BindNotificationHandler();
            ByteBuffer buffer = ByteBuffer.allocate(10);
            MessageInfo info;
            while ((info = peerChannel.receive(buffer, null, handler)) != null) {
                if (info != null) {
                    if (info.bytes() == -1) {
                        debug("peerChannel Reached EOF");
                        break;
                    }
                }
            }
            while ((info = channel.receive(buffer, null, handler)) != null) {
                if (info != null) {
                    if (info.bytes() == -1) {
                        debug("channel Reached EOF");
                        break;
                    }
                }
            }
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        try {
            if (channel != null)
                channel.close();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
    }
}
Also used : SctpChannel(com.sun.nio.sctp.SctpChannel) AlreadyBoundException(java.nio.channels.AlreadyBoundException) IllegalUnbindException(com.sun.nio.sctp.IllegalUnbindException) Iterator(java.util.Iterator) ByteBuffer(java.nio.ByteBuffer) MessageInfo(com.sun.nio.sctp.MessageInfo)

Example 10 with MessageInfo

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

the class Shutdown method doTest.

void doTest(SocketAddress peerAddress) {
    SctpChannel channel = null;
    ByteBuffer buffer = ByteBuffer.allocate(Util.SMALL_BUFFER);
    MessageInfo info;
    try {
        channel = SctpChannel.open();
        /* TEST 1: Verify NotYetConnectedException thrown */
        debug("Test 1: NotYetConnectedException");
        try {
            channel.shutdown();
            fail("shutdown not throwing expected NotYetConnectedException");
        } catch (NotYetConnectedException unused) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        channel.connect(peerAddress);
        sentLatch.await();
        channel.shutdown();
        /* TEST 2: receive data sent before shutdown */
        do {
            debug("Test 2: invoking receive");
            info = channel.receive(buffer, null, null);
            if (info == null) {
                fail("unexpected null from receive");
                return;
            }
        } while (!info.isComplete());
        buffer.flip();
        check(info != null, "info is null");
        check(info.bytes() == Util.SMALL_MESSAGE.getBytes("ISO-8859-1").length, "bytes received not equal to message length");
        check(info.bytes() == buffer.remaining(), "bytes != remaining");
        check(Util.compare(buffer, Util.SMALL_MESSAGE), "received message not the same as sent message");
        buffer.clear();
        /* TEST 3: receive notifications on the SCTP stack */
        debug("Test 3: receive notifications");
        while ((info = channel.receive(buffer, null, null)) != null && info.bytes() != -1) ;
        /* TEST 4: If the channel is already shutdown then invoking this
             * method has no effect. */
        debug("Test 4: no-op");
        try {
            channel.shutdown();
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 5: Further sends will throw ClosedChannelException */
        debug("Test 5: ClosedChannelException");
        info = MessageInfo.createOutgoing(null, 1);
        try {
            channel.send(buffer, info);
            fail("shutdown not throwing expected ClosedChannelException");
        } catch (ClosedChannelException unused) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 6: getRemoteAddresses */
        debug("Test 6: getRemoteAddresses");
        try {
            java.util.Set<SocketAddress> remoteAddrs = channel.getRemoteAddresses();
            check(remoteAddrs.isEmpty(), "A shutdown channel should not have remote addresses");
        } catch (IOException ioe) {
            unexpected(ioe);
        }
    } catch (IOException ioe) {
        unexpected(ioe);
    } catch (InterruptedException ie) {
        unexpected(ie);
    } finally {
        finishedLatch.countDown();
        try {
            if (channel != null)
                channel.close();
        } catch (IOException e) {
            unexpected(e);
        }
    }
}
Also used : SctpChannel(com.sun.nio.sctp.SctpChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) NotYetConnectedException(java.nio.channels.NotYetConnectedException) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ByteBuffer(java.nio.ByteBuffer) MessageInfo(com.sun.nio.sctp.MessageInfo)

Aggregations

MessageInfo (com.sun.nio.sctp.MessageInfo)12 ByteBuffer (java.nio.ByteBuffer)11 SctpChannel (com.sun.nio.sctp.SctpChannel)7 ByteBuf (io.netty.buffer.ByteBuf)4 SctpMessage (io.netty.channel.sctp.SctpMessage)4 IOException (java.io.IOException)4 Association (com.sun.nio.sctp.Association)3 SctpMultiChannel (com.sun.nio.sctp.SctpMultiChannel)3 RecvByteBufAllocator (io.netty.channel.RecvByteBufAllocator)3 InetSocketAddress (java.net.InetSocketAddress)2 SocketAddress (java.net.SocketAddress)2 ClosedChannelException (java.nio.channels.ClosedChannelException)2 NotYetConnectedException (java.nio.channels.NotYetConnectedException)2 IllegalUnbindException (com.sun.nio.sctp.IllegalUnbindException)1 InvalidStreamException (com.sun.nio.sctp.InvalidStreamException)1 SctpServerChannel (com.sun.nio.sctp.SctpServerChannel)1 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 AlreadyBoundException (java.nio.channels.AlreadyBoundException)1 SelectionKey (java.nio.channels.SelectionKey)1 Iterator (java.util.Iterator)1