Search in sources :

Example 1 with SctpMessage

use of io.netty.channel.sctp.SctpMessage in project netty by netty.

the class OioSctpChannel method doWrite.

@Override
protected void doWrite(ChannelOutboundBuffer in) throws Exception {
    if (!writeSelector.isOpen()) {
        return;
    }
    final int size = in.size();
    final int selectedKeys = writeSelector.select(SO_TIMEOUT);
    if (selectedKeys > 0) {
        final Set<SelectionKey> writableKeys = writeSelector.selectedKeys();
        if (writableKeys.isEmpty()) {
            return;
        }
        Iterator<SelectionKey> writableKeysIt = writableKeys.iterator();
        int written = 0;
        for (; ; ) {
            if (written == size) {
                // all written
                return;
            }
            writableKeysIt.next();
            writableKeysIt.remove();
            SctpMessage packet = (SctpMessage) in.current();
            if (packet == null) {
                return;
            }
            ByteBuf data = packet.content();
            int dataLen = data.readableBytes();
            ByteBuffer nioData;
            if (data.nioBufferCount() != -1) {
                nioData = data.nioBuffer();
            } else {
                nioData = ByteBuffer.allocate(dataLen);
                data.getBytes(data.readerIndex(), nioData);
                nioData.flip();
            }
            final MessageInfo mi = MessageInfo.createOutgoing(association(), null, packet.streamIdentifier());
            mi.payloadProtocolID(packet.protocolIdentifier());
            mi.streamNumber(packet.streamIdentifier());
            mi.unordered(packet.isUnordered());
            ch.send(nioData, mi);
            written++;
            in.remove();
            if (!writableKeysIt.hasNext()) {
                return;
            }
        }
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ByteBuf(io.netty.buffer.ByteBuf) SctpMessage(io.netty.channel.sctp.SctpMessage) ByteBuffer(java.nio.ByteBuffer) MessageInfo(com.sun.nio.sctp.MessageInfo)

Example 2 with SctpMessage

use of io.netty.channel.sctp.SctpMessage 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 3 with SctpMessage

use of io.netty.channel.sctp.SctpMessage in project netty by netty.

the class NioSctpChannel method filterOutboundMessage.

@Override
protected final Object filterOutboundMessage(Object msg) throws Exception {
    if (msg instanceof SctpMessage) {
        SctpMessage m = (SctpMessage) msg;
        ByteBuf buf = m.content();
        if (buf.isDirect() && buf.nioBufferCount() == 1) {
            return m;
        }
        return new SctpMessage(m.protocolIdentifier(), m.streamIdentifier(), m.isUnordered(), newDirectBuffer(m, buf));
    }
    throw new UnsupportedOperationException("unsupported message type: " + StringUtil.simpleClassName(msg) + " (expected: " + StringUtil.simpleClassName(SctpMessage.class));
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) SctpMessage(io.netty.channel.sctp.SctpMessage)

Example 4 with SctpMessage

use of io.netty.channel.sctp.SctpMessage 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 5 with SctpMessage

use of io.netty.channel.sctp.SctpMessage 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)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)6 SctpMessage (io.netty.channel.sctp.SctpMessage)6 MessageInfo (com.sun.nio.sctp.MessageInfo)4 ByteBuffer (java.nio.ByteBuffer)4 RecvByteBufAllocator (io.netty.channel.RecvByteBufAllocator)3 SctpChannel (com.sun.nio.sctp.SctpChannel)1 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 SelectionKey (java.nio.channels.SelectionKey)1