/** * Essential data that is being carried within SCTP Data Chunk * * @param msgInfo the {@link MessageInfo} * @param payloadBuffer channel buffer */ public SctpMessage(MessageInfo msgInfo, ByteBuf payloadBuffer) { super(payloadBuffer); if (msgInfo == null) { throw new NullPointerException("msgInfo"); } this.msgInfo = msgInfo; streamIdentifier = msgInfo.streamNumber(); protocolIdentifier = msgInfo.payloadProtocolID(); unordered = msgInfo.isUnordered(); }
private boolean read(SelectionKey k) { final SctpChannelImpl channel = (SctpChannelImpl) k.attachment(); final ReceiveBufferSizePredictor predictor = channel.getConfig().getReceiveBufferSizePredictor(); final int predictedRecvBufSize = predictor.nextReceiveBufferSize(); boolean messageReceived = false; boolean failure = true; MessageInfo messageInfo = null; ByteBuffer bb = recvBufferPool.acquire(predictedRecvBufSize); try { messageInfo = channel.channel.receive(bb, null, notificationHandler); if (messageInfo != null) { messageReceived = true; if (!messageInfo.isUnordered()) { failure = false; } else { if (logger.isErrorEnabled()) { logger.error("Received unordered SCTP Packet"); } failure = true; } } else { messageReceived = false; failure = false; } } catch (ClosedChannelException e) { // Can happen, and does not need a user attention. } catch (Throwable t) { fireExceptionCaught(channel, t); } if (messageReceived) { bb.flip(); final ChannelBufferFactory bufferFactory = channel.getConfig().getBufferFactory(); final int receivedBytes = bb.remaining(); final ChannelBuffer buffer = bufferFactory.getBuffer(receivedBytes); buffer.setBytes(0, bb); buffer.writerIndex(receivedBytes); recvBufferPool.release(bb); // Update the predictor. predictor.previousReceiveBufferSize(receivedBytes); // Fire the event. fireMessageReceived(channel, new SctpFrame(messageInfo, buffer), messageInfo.address()); } else { recvBufferPool.release(bb); } if (channel.channel.isBlocking() && !messageReceived || failure) { k.cancel(); // Some JDK implementations run into an infinite loop without this. close(channel, succeededFuture(channel)); return false; } return true; }