List of usage examples for io.netty.buffer ByteBuf readerIndex
public abstract int readerIndex();
From source file:com.xx_dev.apn.socks.local.FakeHttpClientDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { int startReaderIndex = in.readerIndex(); long start = System.nanoTime(); this._decode(ctx, in, out); long end = System.nanoTime(); int endReaderIndex = in.readerIndex(); perfLogger.debug("local decode: " + (endReaderIndex - startReaderIndex) + ", " + (end - start)); }
From source file:com.xx_dev.apn.socks.local.FakeHttpClientDecoder.java
License:Apache License
protected void _decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { switch (this.state()) { case READ_FAKE_HTTP: { int fakeHttpHeadStartIndex = in.readerIndex(); int fakeHttpHeadEndIndex = in.forEachByte(new ByteBufProcessor() { int c = 0; @Override//from w ww .java 2 s . c om public boolean process(byte value) throws Exception { if (value == '\r' || value == '\n') { c++; } else { c = 0; } //logger.info("value=" + value + ", c=" + c); if (c >= 4) { return false; } else { return true; } } }); logger.debug("s: " + fakeHttpHeadStartIndex); logger.debug("e: " + fakeHttpHeadEndIndex); if (fakeHttpHeadEndIndex == -1) { logger.warn("w: " + fakeHttpHeadStartIndex); break; } byte[] buf = new byte[fakeHttpHeadEndIndex - fakeHttpHeadStartIndex + 1]; in.readBytes(buf, 0, fakeHttpHeadEndIndex - fakeHttpHeadStartIndex + 1); String s = TextUtil.fromUTF8Bytes(buf); //logger.info(s); String[] ss = StringUtils.split(s, "\r\n"); //System.out.println(s + "" + this + " " + Thread.currentThread().getName()); for (String line : ss) { if (StringUtils.startsWith(line, "X-C:")) { String lenStr = StringUtils.trim(StringUtils.split(line, ":")[1]); //System.out.println(lenStr + "" + this + " " + Thread.currentThread().getName()); //System.out.println("*****************************************"); try { length = Integer.parseInt(lenStr, 16); trafficLogger.info("D," + LocalConfig.ins().getUser() + "," + length); } catch (Throwable t) { logger.error("--------------------------------------"); logger.error(s + "" + this + " " + Thread.currentThread().getName()); logger.error("--------------------------------------"); } } } this.checkpoint(STATE.READ_CONTENT); } case READ_CONTENT: { if (length > 0) { byte[] buf = new byte[length]; in.readBytes(buf, 0, length); byte[] res = new byte[length]; for (int i = 0; i < length; i++) { res[i] = (byte) (buf[i] ^ (LocalConfig.ins().getEncryptKey() & 0xFF)); } ByteBuf outBuf = ctx.alloc().buffer(); outBuf.writeBytes(res); out.add(outBuf); } this.checkpoint(STATE.READ_FAKE_HTTP); break; } default: throw new Error("Shouldn't reach here."); } }
From source file:com.xx_dev.apn.socks.remote.FakeHttpServerDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { int startReaderIndex = in.readerIndex(); long start = System.nanoTime(); this._decode(ctx, in, out); long end = System.nanoTime(); int endReaderIndex = in.readerIndex(); perfLogger.debug("remote decode: " + (endReaderIndex - startReaderIndex) + ", " + (end - start)); }
From source file:com.xx_dev.apn.socks.remote.FakeHttpServerDecoder.java
License:Apache License
protected void _decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { switch (this.state()) { case READ_FAKE_HTTP: { int fakeHttpHeadStartIndex = in.readerIndex(); int fakeHttpHeadEndIndex = in.forEachByte(new ByteBufProcessor() { int c = 0; @Override//from w ww .j a v a2 s . c o m public boolean process(byte value) throws Exception { if (value == '\r' || value == '\n') { c++; } else { c = 0; } //logger.info("value=" + value + ", c=" + c); if (c >= 4) { return false; } else { return true; } } }); logger.debug("s: " + fakeHttpHeadStartIndex); logger.debug("e: " + fakeHttpHeadEndIndex); if (fakeHttpHeadEndIndex == -1) { logger.warn("w: " + fakeHttpHeadStartIndex); break; } byte[] buf = new byte[fakeHttpHeadEndIndex - fakeHttpHeadStartIndex + 1]; in.readBytes(buf, 0, fakeHttpHeadEndIndex - fakeHttpHeadStartIndex + 1); String s = TextUtil.fromUTF8Bytes(buf); //logger.info(s); String[] ss = StringUtils.split(s, "\r\n"); //System.out.println(s + "" + this + " " + Thread.currentThread().getName()); for (String line : ss) { if (StringUtils.startsWith(line, "X-C:")) { String lenStr = StringUtils.trim(StringUtils.split(line, ":")[1]); //System.out.println(lenStr + "" + this + " " + Thread.currentThread().getName()); //System.out.println("*****************************************"); try { length = Integer.parseInt(lenStr, 16); } catch (Throwable t) { logger.error("--------------------------------------"); logger.error(s + "" + this + " " + Thread.currentThread().getName()); logger.error("--------------------------------------"); } } if (StringUtils.startsWith(line, "X-U:")) { String user = StringUtils.trim(StringUtils.split(line, ":")[1]); ctx.channel().attr(NettyAttributeKey.LINK_USER).set(user); logger.info(user); } } this.checkpoint(STATE.READ_CONTENT); } case READ_CONTENT: { trafficLogger.info("U," + ctx.channel().attr(NettyAttributeKey.LINK_USER).get() + "," + length); if (length > 0) { byte[] buf = new byte[length]; in.readBytes(buf, 0, length); byte[] res = new byte[length]; for (int i = 0; i < length; i++) { res[i] = (byte) (buf[i] ^ (RemoteConfig.ins().getEncryptKey() & 0xFF)); } ByteBuf outBuf = ctx.alloc().buffer(); outBuf.writeBytes(res); out.add(outBuf); } this.checkpoint(STATE.READ_FAKE_HTTP); break; } default: throw new Error("Shouldn't reach here."); } }
From source file:com.yahoo.pulsar.broker.service.Producer.java
License:Apache License
private boolean verifyChecksum(ByteBuf headersAndPayload) { if (hasChecksum(headersAndPayload)) { int checksum = readChecksum(headersAndPayload).intValue(); int readerIndex = headersAndPayload.readerIndex(); try {//from www . j a v a2 s . c o m long computedChecksum = computeChecksum(headersAndPayload); if (checksum == computedChecksum) { return true; } else { log.error("[{}] [{}] Failed to verify checksum", topic, producerName); return false; } } finally { headersAndPayload.readerIndex(readerIndex); } } else { // ignore if checksum is not available if (log.isDebugEnabled()) { log.debug("[{}] [{}] Payload does not have checksum to verify", topic, producerName); } return true; } }
From source file:com.yahoo.pulsar.common.api.Commands.java
License:Apache License
public static boolean hasChecksum(ByteBuf buffer) { return buffer.getShort(buffer.readerIndex()) == magicCrc32c; }
From source file:com.yahoo.pulsar.common.api.Commands.java
License:Apache License
public static MessageMetadata parseMessageMetadata(ByteBuf buffer) { try {/*from w ww . jav a 2 s.co m*/ // initially reader-index may point to start_of_checksum : increment reader-index to start_of_metadata to parse // metadata readChecksum(buffer); int metadataSize = (int) buffer.readUnsignedInt(); int writerIndex = buffer.writerIndex(); buffer.writerIndex(buffer.readerIndex() + metadataSize); ByteBufCodedInputStream stream = ByteBufCodedInputStream.get(buffer); MessageMetadata.Builder messageMetadataBuilder = MessageMetadata.newBuilder(); MessageMetadata res = messageMetadataBuilder.mergeFrom(stream, null).build(); buffer.writerIndex(writerIndex); messageMetadataBuilder.recycle(); stream.recycle(); return res; } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.yahoo.pulsar.common.api.Commands.java
License:Apache License
public static ByteBuf deSerializeSingleMessageInBatch(ByteBuf uncompressedPayload, PulsarApi.SingleMessageMetadata.Builder singleMessageMetadataBuilder, int index, int batchSize) throws IOException { int singleMetaSize = (int) uncompressedPayload.readUnsignedInt(); int writerIndex = uncompressedPayload.writerIndex(); int beginIndex = uncompressedPayload.readerIndex() + singleMetaSize; uncompressedPayload.writerIndex(beginIndex); ByteBufCodedInputStream stream = ByteBufCodedInputStream.get(uncompressedPayload); PulsarApi.SingleMessageMetadata singleMessageMetadata = singleMessageMetadataBuilder.mergeFrom(stream, null) .build();/*from ww w . j a v a 2 s .c o m*/ int singleMessagePayloadSize = singleMessageMetadata.getPayloadSize(); uncompressedPayload.markReaderIndex(); ByteBuf singleMessagePayload = uncompressedPayload.slice(uncompressedPayload.readerIndex(), singleMessagePayloadSize); singleMessagePayload.retain(); uncompressedPayload.writerIndex(writerIndex); uncompressedPayload.resetReaderIndex(); // reader now points to beginning of payload read; so move it past message payload just read if (index < batchSize) { uncompressedPayload.readerIndex(uncompressedPayload.readerIndex() + singleMessagePayloadSize); } return singleMessagePayload; }
From source file:com.yahoo.pulsar.common.api.PulsarDecoder.java
License:Apache License
@Override final public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // Get a buffer that contains the full frame ByteBuf buffer = (ByteBuf) msg; BaseCommand cmd = null;// w w w . java 2s .c o m BaseCommand.Builder cmdBuilder = null; try { // De-serialize the command int cmdSize = (int) buffer.readUnsignedInt(); int writerIndex = buffer.writerIndex(); buffer.writerIndex(buffer.readerIndex() + cmdSize); ByteBufCodedInputStream cmdInputStream = ByteBufCodedInputStream.get(buffer); cmdBuilder = BaseCommand.newBuilder(); cmd = cmdBuilder.mergeFrom(cmdInputStream, null).build(); buffer.writerIndex(writerIndex); cmdInputStream.recycle(); if (log.isDebugEnabled()) { log.debug("[{}] Received cmd {}", ctx.channel().remoteAddress(), cmd.getType()); } messageReceived(); switch (cmd.getType()) { case PARTITIONED_METADATA: checkArgument(cmd.hasPartitionMetadata()); handlePartitionMetadataRequest(cmd.getPartitionMetadata()); cmd.getPartitionMetadata().recycle(); break; case PARTITIONED_METADATA_RESPONSE: checkArgument(cmd.hasPartitionMetadataResponse()); handlePartitionResponse(cmd.getPartitionMetadataResponse()); cmd.getPartitionMetadataResponse().recycle(); break; case LOOKUP: checkArgument(cmd.hasLookupTopic()); handleLookup(cmd.getLookupTopic()); cmd.getLookupTopic().recycle(); break; case LOOKUP_RESPONSE: checkArgument(cmd.hasLookupTopicResponse()); handleLookupResponse(cmd.getLookupTopicResponse()); cmd.getLookupTopicResponse().recycle(); break; case ACK: checkArgument(cmd.hasAck()); handleAck(cmd.getAck()); cmd.getAck().getMessageId().recycle(); cmd.getAck().recycle(); break; case CLOSE_CONSUMER: checkArgument(cmd.hasCloseConsumer()); handleCloseConsumer(cmd.getCloseConsumer()); cmd.getCloseConsumer().recycle(); break; case CLOSE_PRODUCER: checkArgument(cmd.hasCloseProducer()); handleCloseProducer(cmd.getCloseProducer()); cmd.getCloseProducer().recycle(); break; case CONNECT: checkArgument(cmd.hasConnect()); handleConnect(cmd.getConnect()); cmd.getConnect().recycle(); break; case CONNECTED: checkArgument(cmd.hasConnected()); handleConnected(cmd.getConnected()); cmd.getConnected().recycle(); break; case ERROR: checkArgument(cmd.hasError()); handleError(cmd.getError()); cmd.getError().recycle(); break; case FLOW: checkArgument(cmd.hasFlow()); handleFlow(cmd.getFlow()); cmd.getFlow().recycle(); break; case MESSAGE: { checkArgument(cmd.hasMessage()); handleMessage(cmd.getMessage(), buffer); cmd.getMessage().recycle(); break; } case PRODUCER: checkArgument(cmd.hasProducer()); handleProducer(cmd.getProducer()); cmd.getProducer().recycle(); break; case SEND: { checkArgument(cmd.hasSend()); // Store a buffer marking the content + headers ByteBuf headersAndPayload = buffer.markReaderIndex(); handleSend(cmd.getSend(), headersAndPayload); cmd.getSend().recycle(); break; } case SEND_ERROR: checkArgument(cmd.hasSendError()); handleSendError(cmd.getSendError()); cmd.getSendError().recycle(); break; case SEND_RECEIPT: checkArgument(cmd.hasSendReceipt()); handleSendReceipt(cmd.getSendReceipt()); cmd.getSendReceipt().recycle(); break; case SUBSCRIBE: checkArgument(cmd.hasSubscribe()); handleSubscribe(cmd.getSubscribe()); cmd.getSubscribe().recycle(); break; case SUCCESS: checkArgument(cmd.hasSuccess()); handleSuccess(cmd.getSuccess()); cmd.getSuccess().recycle(); break; case PRODUCER_SUCCESS: checkArgument(cmd.hasProducerSuccess()); handleProducerSuccess(cmd.getProducerSuccess()); cmd.getProducerSuccess().recycle(); break; case UNSUBSCRIBE: checkArgument(cmd.hasUnsubscribe()); handleUnsubscribe(cmd.getUnsubscribe()); cmd.getUnsubscribe().recycle(); break; case PING: checkArgument(cmd.hasPing()); handlePing(cmd.getPing()); cmd.getPing().recycle(); break; case PONG: checkArgument(cmd.hasPong()); handlePong(cmd.getPong()); cmd.getPong().recycle(); break; case REDELIVER_UNACKNOWLEDGED_MESSAGES: checkArgument(cmd.hasRedeliverUnacknowledgedMessages()); handleRedeliverUnacknowledged(cmd.getRedeliverUnacknowledgedMessages()); cmd.getRedeliverUnacknowledgedMessages().recycle(); break; } } finally { if (cmdBuilder != null) { cmdBuilder.recycle(); } if (cmd != null) { cmd.recycle(); } buffer.release(); } }
From source file:com.yahoo.pulsar.common.compression.CompressionCodecLZ4.java
License:Apache License
@Override public ByteBuf encode(ByteBuf source) { int uncompressedLength = source.readableBytes(); int maxLength = compressor.maxCompressedLength(uncompressedLength); ByteBuffer sourceNio = source.nioBuffer(source.readerIndex(), source.readableBytes()); ByteBuf target = PooledByteBufAllocator.DEFAULT.buffer(maxLength, maxLength); ByteBuffer targetNio = target.nioBuffer(0, maxLength); int compressedLength = compressor.compress(sourceNio, 0, uncompressedLength, targetNio, 0, maxLength); target.writerIndex(compressedLength); return target; }