Example usage for io.netty.buffer ByteBuf writeLong

List of usage examples for io.netty.buffer ByteBuf writeLong

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf writeLong.

Prototype

public abstract ByteBuf writeLong(long value);

Source Link

Document

Sets the specified 64-bit long integer at the current writerIndex and increases the writerIndex by 8 in this buffer.

Usage

From source file:qunar.tc.qmq.protocol.consumer.AckRequestPayloadHolder.java

License:Apache License

@Override
public void writeBody(ByteBuf out) {
    PayloadHolderUtils.writeString(request.getSubject(), out);
    PayloadHolderUtils.writeString(request.getGroup(), out);
    PayloadHolderUtils.writeString(request.getConsumerId(), out);
    out.writeLong(request.getPullOffsetBegin());
    out.writeLong(request.getPullOffsetLast());
    out.writeByte(request.isBroadcast());
}

From source file:qunar.tc.qmq.protocol.consumer.PullRequestSerde.java

License:Apache License

public void write(final PullRequest request, final ByteBuf out) {
    PayloadHolderUtils.writeString(request.getSubject(), out);
    PayloadHolderUtils.writeString(request.getGroup(), out);
    PayloadHolderUtils.writeString(request.getConsumerId(), out);

    out.writeInt(request.getRequestNum());
    out.writeLong(request.getOffset());
    out.writeLong(request.getPullOffsetBegin());
    out.writeLong(request.getPullOffsetLast());
    out.writeLong(request.getTimeoutMillis());
    out.writeByte(request.isBroadcast() ? 1 : 0);

    writeFilters(request.getFilters(), out);
}

From source file:qunar.tc.qmq.protocol.MessagesPayloadHolder.java

License:Apache License

private void serializeMessage(BaseMessage message, ByteBuf out) {
    int crcIndex = out.writerIndex();
    // sizeof(bodyCrc<long>)
    out.ensureWritable(8);/*from  w  ww . j av  a2s . c o  m*/
    out.writerIndex(crcIndex + 8);

    final int messageStart = out.writerIndex();

    // flag
    byte flag = 0;
    //?(1)?(0)?(1)?(0)?Tag
    flag = Flags.setDelay(flag, DelayUtil.isDelayMessage(message));

    //in avoid add tag after sendMessage
    Set<String> tags = new HashSet<>(message.getTags());
    flag = Flags.setTags(flag, hasTags(tags));

    out.writeByte(flag);

    // created time
    out.writeLong(message.getCreatedTime().getTime());
    if (Flags.isDelay(flag)) {
        out.writeLong(message.getScheduleReceiveTime().getTime());
    } else {
        // expired time
        out.writeLong(System.currentTimeMillis());
    }
    // subject
    PayloadHolderUtils.writeString(message.getSubject(), out);
    // message id
    PayloadHolderUtils.writeString(message.getMessageId(), out);

    writeTags(tags, out);

    out.markWriterIndex();
    // writerIndex + sizeof(bodyLength<int>)
    final int bodyStart = out.writerIndex() + 4;
    out.ensureWritable(4);
    out.writerIndex(bodyStart);

    serializeMap(message.getAttrs(), out);
    final int bodyEnd = out.writerIndex();

    final int messageEnd = out.writerIndex();

    final int bodyLen = bodyEnd - bodyStart;
    final int messageLength = bodyEnd - messageStart;

    // write body length
    out.resetWriterIndex();
    out.writeInt(bodyLen);

    // write message crc
    out.writerIndex(crcIndex);
    out.writeLong(messageCrc(out, messageStart, messageLength));

    out.writerIndex(messageEnd);
}

From source file:qunar.tc.qmq.store.IndexLog.java

License:Apache License

private AppendMessageResult doAppendData(final LogSegment segment, final ByteBuffer data) {
    int currentPos = segment.getWrotePosition();
    final int freeSize = segment.getFileSize() - currentPos;
    if (data.remaining() <= freeSize) {
        if (!segment.appendData(data))
            throw new RuntimeException("append index data failed.");
        return new AppendMessageResult<>(SUCCESS, segment.getBaseOffset() + segment.getWrotePosition());
    }/*from w  w w  .  j a va  2s  .  c  o  m*/

    ByteBuf to = ByteBufAllocator.DEFAULT.ioBuffer(freeSize);
    try {
        partialCopy(data, to);
        if (to.isWritable(Long.BYTES)) {
            to.writeLong(-1);
        }
        fillZero(to);
        if (!segment.appendData(to.nioBuffer()))
            throw new RuntimeException("append index data failed.");
    } finally {
        ReferenceCountUtil.release(to);
    }
    return new AppendMessageResult(END_OF_FILE, segment.getBaseOffset() + segment.getFileSize());
}

From source file:qunar.tc.qmq.store.IndexLog.java

License:Apache License

private void partialCopy(ByteBuffer src, ByteBuf to) {
    while (to.isWritable(Long.BYTES)) {
        src.mark();/*from  w  w  w  .  j  a va  2s  .  com*/
        to.markWriterIndex();
        if (!to.isWritable(Long.BYTES))
            break;
        to.writeLong(src.getLong());

        if (!to.isWritable(Long.BYTES)) {
            src.reset();
            to.resetWriterIndex();
            break;
        }
        to.writeLong(src.getLong());

        // subject
        if (!writeString(src, to))
            break;

        // msgId
        if (!writeString(src, to))
            break;
    }
}

From source file:qunar.tc.qmq.sync.master.AbstractLogSyncWorker.java

License:Apache License

private void processSyncLog(SyncRequestEntry entry, SegmentBuffer result) {
    if (!acquireSegmentLock(result)) {
        writeEmpty(entry, result.getStartOffset());
        return;//  w w w  .  j av  a 2 s  . co m
    }

    if (!result.retain()) {
        writeEmpty(entry, result.getStartOffset());
        return;
    }
    try {
        final int batchSize = config.getInt("sync.batch.size", 100000);
        final ByteBuffer buffer = result.getBuffer();
        int size = result.getSize();
        if (size > batchSize) {
            buffer.limit(batchSize);
            size = batchSize;
        }
        final RemotingHeader header = RemotingBuilder.buildResponseHeader(CommandCode.SUCCESS,
                entry.getRequestHeader());
        ByteBuf headerBuffer = HeaderSerializer.serialize(header, SYNC_HEADER_LEN + size, SYNC_HEADER_LEN);
        headerBuffer.writeInt(size);
        headerBuffer.writeLong(result.getStartOffset());
        entry.getCtx().writeAndFlush(new DataTransfer(headerBuffer, result, size));
    } catch (Exception e) {
        result.release();
    }
}

From source file:qunar.tc.qmq.sync.master.MessageIndexSyncWorker.java

License:Apache License

@Override
protected SegmentBuffer getSyncLog(SyncRequest syncRequest) {
    final long originalOffset = syncRequest.getMessageLogOffset();
    long startSyncOffset = originalOffset;

    long minMessageOffset = storage.getMinMessageOffset();
    if (startSyncOffset < minMessageOffset) {
        startSyncOffset = minMessageOffset;
        LOG.info("reset message log sync offset from {} to {}", originalOffset, startSyncOffset);
    }/*www . j a  v  a2s  . c o m*/

    try (MessageLogRecordVisitor visitor = storage.newMessageLogVisitor(startSyncOffset)) {
        LogSegment logSegment = null;
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.ioBuffer(batchSize);
        long nextSyncOffset = startSyncOffset;
        try {
            for (int i = 0; i < MAX_SYNC_NUM; ++i) {
                LogVisitorRecord<MessageLogRecord> record = visitor.nextRecord();
                if (record.isNoMore()) {
                    nextSyncOffset = visitor.getStartOffset() + visitor.visitedBufferSize();
                    break;
                }

                if (!record.hasData()) {
                    nextSyncOffset = visitor.getStartOffset() + visitor.visitedBufferSize();
                    continue;
                }

                MessageLogRecord data = record.getData();
                logSegment = data.getLogSegment();

                if (!byteBuf.isWritable(Long.BYTES))
                    break;
                byteBuf.markWriterIndex();
                byteBuf.writeLong(data.getSequence());

                if (!byteBuf.isWritable(Long.BYTES)) {
                    byteBuf.resetWriterIndex();
                    break;
                }

                ByteBuffer body = data.getPayload();
                //skip flag
                body.get();
                long createTime = body.getLong();
                //skip expireTime
                body.getLong();

                //subject
                short len = body.getShort();
                byte[] subject = new byte[len];
                body.get(subject);

                //message id
                len = body.getShort();
                byte[] messageId = new byte[len];
                body.get(messageId);

                byteBuf.writeLong(createTime);

                if (!byteBuf.isWritable(Short.BYTES + subject.length)) {
                    byteBuf.resetWriterIndex();
                    break;
                }
                PayloadHolderUtils.writeString(subject, byteBuf);

                if (!byteBuf.isWritable(Short.BYTES + messageId.length)) {
                    byteBuf.resetWriterIndex();
                    break;
                }
                PayloadHolderUtils.writeString(messageId, byteBuf);

                nextSyncOffset = visitor.getStartOffset() + visitor.visitedBufferSize();
            }
        } finally {
            if (!byteBuf.isReadable()) {
                byteBuf.release();
            }
        }

        if (originalOffset == nextSyncOffset) {
            return null;
        }

        //FIXME: ????
        if (byteBuf.isReadable()) {
            return new ByteBufSegmentBuffer(nextSyncOffset, logSegment, byteBuf, byteBuf.readableBytes());
        } else {
            return new ByteBufSegmentBuffer(nextSyncOffset);
        }
    }
}

From source file:ratpack.session.clientside.internal.ClientSideSessionStore.java

License:Apache License

private void setLastAccessTime(CookieStorage cookieStorage) throws Exception {
    ByteBuf data = null;
    try {//from w w  w  .j a  va 2s  .c  o m
        data = Unpooled.buffer();
        data.writeLong(System.currentTimeMillis());
        int oldCookiesCount = cookieStorage.lastAccessToken.size();
        String[] partitions = serialize(data);
        for (int i = 0; i < partitions.length; i++) {
            addCookie(config.getLastAccessTimeCookieName() + "_" + i, partitions[i]);
        }
        for (int i = partitions.length; i < oldCookiesCount; i++) {
            invalidateCookie(config.getLastAccessTimeCookieName() + "_" + i);
        }
    } finally {
        if (data != null) {
            data.release();
        }
    }
}

From source file:ru.jts_dev.authserver.util.Encoder.java

License:Open Source License

public ByteBuf encWithXor(ByteBuf buf) {
    if (buf.readableBytes() % 4 != 0) {
        throw new IndexOutOfBoundsException("ByteBuf size must be multiply of 4");
    }// ww w .j  ava  2  s  .c  om
    int edx;
    int ecx = 0; // Initial xor key

    buf.writeLong(random.nextLong()); // 8 bytes padding

    for (int pos = 4; pos < buf.readableBytes(); pos += 4) {
        edx = buf.getInt(pos);

        ecx += edx;
        edx ^= ecx;

        buf.setInt(pos, edx);
    }
    buf.writeInt(ecx);

    buf.writeInt(random.nextInt()); // 4 bytes for blowfish block

    return buf;
}

From source file:se.sics.caracaldb.flow.FlowMessageSerializer.java

License:Open Source License

private void ctsToBinary(CTS cts, ByteBuf buf) {
    MessageSerializationUtil.msgToBinary(cts, buf, CTS.getValue0(), CTS.getValue1());
    SpecialSerializers.UUIDSerializer.INSTANCE.toBinary(cts.flowId, buf);
    buf.writeInt(cts.clearId);//from   w  w w  . j  a  va 2  s.co  m
    buf.writeInt(cts.allowance);
    buf.writeLong(cts.validThrough);
}