Exemplo n.º 1
0
  private final SendBuffer acquire(SctpPayload message) {
    final ChannelBuffer src = message.getPayloadBuffer();
    final int streamNo = message.getStreamIdentifier();
    final int protocolId = message.getProtocolIdentifier();

    final int size = src.readableBytes();
    if (size == 0) {
      return EMPTY_BUFFER;
    }

    if (src.isDirect()) {
      return new UnpooledSendBuffer(streamNo, protocolId, src.toByteBuffer());
    }
    if (src.readableBytes() > DEFAULT_PREALLOCATION_SIZE) {
      return new UnpooledSendBuffer(streamNo, protocolId, src.toByteBuffer());
    }

    Preallocation current = this.current;
    ByteBuffer buffer = current.buffer;
    int remaining = buffer.remaining();
    PooledSendBuffer dst;

    if (size < remaining) {
      int nextPos = buffer.position() + size;
      ByteBuffer slice = buffer.duplicate();
      buffer.position(align(nextPos));
      slice.limit(nextPos);
      current.refCnt++;
      dst = new PooledSendBuffer(streamNo, protocolId, current, slice);
    } else if (size > remaining) {
      this.current = current = getPreallocation();
      buffer = current.buffer;
      ByteBuffer slice = buffer.duplicate();
      buffer.position(align(size));
      slice.limit(size);
      current.refCnt++;
      dst = new PooledSendBuffer(streamNo, protocolId, current, slice);
    } else { // size == remaining
      current.refCnt++;
      this.current = getPreallocation0();
      dst = new PooledSendBuffer(streamNo, protocolId, current, current.buffer);
    }

    ByteBuffer dstbuf = dst.buffer;
    dstbuf.mark();
    src.getBytes(src.readerIndex(), dstbuf);
    dstbuf.reset();
    return dst;
  }
    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
      if (server) {
        Channels.write(channel, e.getMessage(), e.getRemoteAddress());
      } else {
        ChannelBuffer m = (ChannelBuffer) e.getMessage();
        byte[] actual = new byte[m.readableBytes()];
        m.getBytes(0, actual);

        int lastIdx = counter;
        for (int i = 0; i < actual.length; i++) {
          assertEquals(frames.getByte(ignoredBytes + i + lastIdx), actual[i]);
        }

        counter += actual.length;
      }
    }