io.netty.channel.sctp.SctpMessage Java Examples

The following examples show how to use io.netty.channel.sctp.SctpMessage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NioSctpChannel.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@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(), newDirectBuffer(m, buf));
    }

    throw new UnsupportedOperationException(
            "unsupported message type: " + StringUtil.simpleClassName(msg) +
            " (expected: " + StringUtil.simpleClassName(SctpMessage.class));
}
 
Example #2
Source File: NioSctpChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #3
Source File: NettyAssociationImpl.java    From sctp with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void send(PayloadData payloadData) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Tx : Ass=%s %s", this.getName(), payloadData));
    }

    NettySctpChannelInboundHandlerAdapter handler = checkSocketIsOpen();

    final ByteBuf byteBuf = payloadData.getByteBuf();
    if (this.ipChannelType == IpChannelType.SCTP) {
        SctpMessage sctpMessage = new SctpMessage(payloadData.getPayloadProtocolId(), payloadData.getStreamNumber(),
                payloadData.isUnordered(), byteBuf);
        handler.writeAndFlush(sctpMessage);
    } else {
        handler.writeAndFlush(byteBuf);
    }
}
 
Example #4
Source File: SctpMessageCompletionHandler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, SctpMessage msg, List<Object> out) throws Exception {
    final ByteBuf byteBuf = msg.content();
    final int protocolIdentifier = msg.protocolIdentifier();
    final int streamIdentifier = msg.streamIdentifier();
    final boolean isComplete = msg.isComplete();
    final boolean isUnordered = msg.isUnordered();

    ByteBuf frag = fragments.remove(streamIdentifier);
    if (frag == null) {
        frag = Unpooled.EMPTY_BUFFER;
    }

    if (isComplete && !frag.isReadable()) {
        //data chunk is not fragmented
        out.add(msg);
    } else if (!isComplete && frag.isReadable()) {
        //more message to complete
        fragments.put(streamIdentifier, Unpooled.wrappedBuffer(frag, byteBuf));
    } else if (isComplete && frag.isReadable()) {
        //last message to complete
        SctpMessage assembledMsg = new SctpMessage(
                protocolIdentifier,
                streamIdentifier,
                isUnordered,
                Unpooled.wrappedBuffer(frag, byteBuf));
        out.add(assembledMsg);
    } else {
        //first incomplete message
        fragments.put(streamIdentifier, byteBuf);
    }
    byteBuf.retain();
}
 
Example #5
Source File: SctpInboundByteStreamHandler.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public final boolean acceptInboundMessage(Object msg) throws Exception {
    if (super.acceptInboundMessage(msg)) {
        return acceptInboundMessage((SctpMessage) msg);
    }
    return false;
}
 
Example #6
Source File: SctpInboundByteStreamHandler.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, SctpMessage msg, List<Object> out) throws Exception {
    if (!msg.isComplete()) {
        throw new CodecException(String.format("Received SctpMessage is not complete, please add %s in the " +
                "pipeline before this handler", SctpMessageCompletionHandler.class.getSimpleName()));
    }
    out.add(msg.content().retain());
}
 
Example #7
Source File: NioSctpChannel.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@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());

    final int writtenBytes = javaChannel().send(nioData, mi);
    return writtenBytes > 0;
}
 
Example #8
Source File: NioSctpChannel.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
protected int doReadMessages(List<Object> buf) throws Exception {
    SctpChannel ch = javaChannel();

    RecvByteBufAllocator.Handle allocHandle = this.allocHandle;
    if (allocHandle == null) {
        this.allocHandle = allocHandle = config().getRecvByteBufAllocator().newHandle();
    }
    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;
        }
        buf.add(new SctpMessage(messageInfo, buffer.writerIndex(buffer.writerIndex() + data.position() - pos)));
        free = false;
        return 1;
    } catch (Throwable cause) {
        PlatformDependent.throwException(cause);
        return -1;
    }  finally {
        int bytesRead = buffer.readableBytes();
        allocHandle.record(bytesRead);
        if (free) {
            buffer.release();
        }
    }
}
 
Example #9
Source File: OioSctpChannel.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
protected Object filterOutboundMessage(Object msg) throws Exception {
    if (msg instanceof SctpMessage) {
        return msg;
    }

    throw new UnsupportedOperationException(
            "unsupported message type: " + StringUtil.simpleClassName(msg) + EXPECTED_TYPE);
}
 
Example #10
Source File: SctpMessageCompletionHandler.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, SctpMessage msg, List<Object> out) throws Exception {
    final ByteBuf byteBuf = msg.content();
    final int protocolIdentifier = msg.protocolIdentifier();
    final int streamIdentifier = msg.streamIdentifier();
    final boolean isComplete = msg.isComplete();

    ByteBuf frag;
    if (fragments.containsKey(streamIdentifier)) {
        frag = fragments.remove(streamIdentifier);
    } else {
        frag = Unpooled.EMPTY_BUFFER;
    }

    if (isComplete && !frag.isReadable()) {
        //data chunk is not fragmented
        out.add(msg);
    } else if (!isComplete && frag.isReadable()) {
        //more message to complete
        fragments.put(streamIdentifier, Unpooled.wrappedBuffer(frag, byteBuf));
    } else if (isComplete && frag.isReadable()) {
        //last message to complete
        fragments.remove(streamIdentifier);
        SctpMessage assembledMsg = new SctpMessage(
                protocolIdentifier,
                streamIdentifier,
                Unpooled.wrappedBuffer(frag, byteBuf));
        out.add(assembledMsg);
    } else {
        //first incomplete message
        fragments.put(streamIdentifier, byteBuf);
    }
    byteBuf.retain();
}
 
Example #11
Source File: SctpMessageToMessageDecoder.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public boolean acceptInboundMessage(Object msg) throws Exception {
    if (msg instanceof SctpMessage) {
        SctpMessage sctpMsg = (SctpMessage) msg;
        if (sctpMsg.isComplete()) {
            return true;
        }

        throw new CodecException(String.format("Received SctpMessage is not complete, please add %s in " +
                "the pipeline before this handler", SctpMessageCompletionHandler.class.getSimpleName()));
    } else {
        return false;
    }
}
 
Example #12
Source File: SctpMessageToMessageDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public boolean acceptInboundMessage(Object msg) throws Exception {
    if (msg instanceof SctpMessage) {
        SctpMessage sctpMsg = (SctpMessage) msg;
        if (sctpMsg.isComplete()) {
            return true;
        }

        throw new CodecException(String.format("Received SctpMessage is not complete, please add %s in " +
                "the pipeline before this handler", SctpMessageCompletionHandler.class.getSimpleName()));
    } else {
        return false;
    }
}
 
Example #13
Source File: SctpInboundByteStreamHandler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, SctpMessage msg, List<Object> out) throws Exception {
    if (!msg.isComplete()) {
        throw new CodecException(String.format("Received SctpMessage is not complete, please add %s in the " +
                "pipeline before this handler", SctpMessageCompletionHandler.class.getSimpleName()));
    }
    out.add(msg.content().retain());
}
 
Example #14
Source File: SimpleSctpServerHandler.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
	System.out.println(msg);
	if(msg instanceof SctpMessage){ 
		SctpMessage sctpMsg = (SctpMessage) msg;
		System.out.println(sctpMsg.content().toString(CharsetUtil.UTF_8));
		ctx.write(sctpMsg); 
	}    	
}
 
Example #15
Source File: SctpInboundByteStreamHandler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public final boolean acceptInboundMessage(Object msg) throws Exception {
    if (super.acceptInboundMessage(msg)) {
        return acceptInboundMessage((SctpMessage) msg);
    }
    return false;
}
 
Example #16
Source File: NioSctpChannel.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #17
Source File: NioSctpChannel.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@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();
        }
    }
}
 
Example #18
Source File: OioSctpChannel.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected Object filterOutboundMessage(Object msg) throws Exception {
    if (msg instanceof SctpMessage) {
        return msg;
    }

    throw new UnsupportedOperationException(
            "unsupported message type: " + StringUtil.simpleClassName(msg) + EXPECTED_TYPE);
}
 
Example #19
Source File: SimpleSctpServerHandler.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
	System.out.println(msg);
	if(msg instanceof SctpMessage){ 
		SctpMessage sctpMsg = (SctpMessage) msg;
		System.out.println(sctpMsg.content().toString(CharsetUtil.UTF_8));
		ctx.write(sctpMsg); 
	}    	
}
 
Example #20
Source File: OioSctpChannel.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@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());

            ch.send(nioData, mi);
            written ++;
            in.remove();

            if (!writableKeysIt.hasNext()) {
                return;
            }
        }
    }
}
 
Example #21
Source File: SimpleSctpClientHandler.java    From netty-cookbook with Apache License 2.0 4 votes vote down vote up
@Override
public void channelActive(ChannelHandlerContext ctx) {
    ctx.write(new SctpMessage(0, 0, firstMessage));
    ctx.write(new SctpMessage(0, 0, secondMessage));
    ctx.flush();
}
 
Example #22
Source File: SimpleSctpClientHandler.java    From netty-cookbook with Apache License 2.0 4 votes vote down vote up
@Override
public void channelActive(ChannelHandlerContext ctx) {
    ctx.write(new SctpMessage(0, 0, firstMessage));
    ctx.write(new SctpMessage(0, 0, secondMessage));
    ctx.flush();
}
 
Example #23
Source File: SctpInboundByteStreamHandler.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
protected boolean acceptInboundMessage(SctpMessage msg) {
    return msg.protocolIdentifier() == protocolIdentifier && msg.streamIdentifier() == streamIdentifier;
}
 
Example #24
Source File: SctpOutboundByteStreamHandler.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
    out.add(new SctpMessage(streamIdentifier, protocolIdentifier, msg.retain()));
}
 
Example #25
Source File: SctpEchoClientHandler.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public void channelActive(ChannelHandlerContext ctx) {
    ctx.writeAndFlush(new SctpMessage(0, 0, firstMessage));
}
 
Example #26
Source File: OioSctpChannel.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@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;
    }

    Set<SelectionKey> reableKeys = readSelector.selectedKeys();
    try {
        for (SelectionKey ignored : reableKeys) {
            RecvByteBufAllocator.Handle allocHandle = this.allocHandle;
            if (allocHandle == null) {
                this.allocHandle = allocHandle = config().getRecvByteBufAllocator().newHandle();
            }
            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();
                msgs.add(new SctpMessage(messageInfo, buffer.writerIndex(buffer.writerIndex() + data.remaining())));
                free = false;
                readMessages ++;
            } catch (Throwable cause) {
                PlatformDependent.throwException(cause);
            }  finally {
                int bytesRead = buffer.readableBytes();
                allocHandle.record(bytesRead);
                if (free) {
                    buffer.release();
                }
            }
        }
    } finally {
        reableKeys.clear();
    }
    return readMessages;
}
 
Example #27
Source File: SctpEchoClientHandler.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
public void channelActive(ChannelHandlerContext ctx) {
    ctx.writeAndFlush(new SctpMessage(0, 0, firstMessage));
}
 
Example #28
Source File: SctpInboundByteStreamHandler.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
protected boolean acceptInboundMessage(SctpMessage msg) {
    return msg.protocolIdentifier() == protocolIdentifier && msg.streamIdentifier() == streamIdentifier;
}
 
Example #29
Source File: SctpOutboundByteStreamHandler.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
    out.add(new SctpMessage(protocolIdentifier, streamIdentifier, unordered, msg.retain()));
}
 
Example #30
Source File: OioSctpChannel.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@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;
            }
        }
    }
}