List of usage examples for io.netty.buffer ByteBuf writerIndex
public abstract int writerIndex();
From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java
License:Apache License
/** * fastjson??javaBean </br>/*from w w w . j a va 2 s. co m*/ * ??javaBean </br> * * @param buffer buffer * @param clazz class sub-class * @return Object javaBean */ public static Object parseObject(ByteBuf buffer, Class clazz) { Object o = JSON.parseObject(buffer.toString(buffer.readerIndex(), buffer.writerIndex() - buffer.readerIndex(), Charset.defaultCharset()), clazz); buffer.release();//buf byteBuffer +buf? buffer = null; return o; }
From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java
License:Apache License
/** * 4/*from w ww .ja v a 2 s .c o m*/ * * @param ID ?? * @param messageLite * @return */ public static ByteBuf wrappedBufferInt(int ID, MessageLite messageLite) { byte[] src = messageLite.toByteArray(); int length = 8 + src.length; ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length); buffer.setIndex(0, 0x4);//writeIndex? //4 buffer.writeByte(ID); // 4 buffer.writeBytes(messageLite.toByteArray()); buffer.setInt(0, buffer.writerIndex() - 0x4); messageLite = null; return buffer; }
From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java
License:Apache License
/** * ?? </br>// w ww .j ava2 s .c o m * Encoder buffer </br> * ?4 </br> * proto buffer </br> * </br> * * @param arg1 * @param arg2 * @param messageLite * @return + ID+protoBufferData */ public static ByteBuf wrappedBufferInt(int arg1, int arg2, MessageLite messageLite) { byte[] src = messageLite.toByteArray(); int length = 12 + src.length; ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length); buffer.setIndex(0, 0x4);//writeIndex? //4 buffer.writeByte(arg1); // 4 buffer.writeByte(arg2); // 4 buffer.writeBytes(messageLite.toByteArray()); buffer.setInt(0, buffer.writerIndex() - 0x4); messageLite = null; return buffer; }
From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java
License:Apache License
/** * ?? </br>/*from www .j av a 2s. c o m*/ * Encoder buffer </br> * ?2 </br> * proto buffer </br> * </br> * * @param arg1 * @param arg2 * @param messageLite * @return + ID+protoBufferData */ public static ByteBuf wrappedBufferShort(int arg1, int arg2, MessageLite messageLite) { byte[] src = messageLite.toByteArray(); int length = 10 + src.length; ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length); buffer.setIndex(0, 0x2);//writeIndex? buffer.writeByte(arg1); buffer.writeByte(arg2); buffer.writeBytes(messageLite.toByteArray()); /**?2*/ buffer.setShort(0, buffer.writerIndex() - 0x2); messageLite = null; return buffer; }
From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java
License:Apache License
/** * Encoder buffer </br> * 2 </br>/*from ww w .j a va2s . co m*/ * * @param ID ?? * @param messageLite byte[] * @return */ public static ByteBuf wrappedBufferShort(int ID, MessageLite messageLite) { byte[] src = messageLite.toByteArray(); int length = 6 + src.length; ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length); buffer.setIndex(0, 0x2);//writeIndex? 2 buffer.writeInt(ID); //? 4 buffer.writeBytes(src); buffer.setShort(0, buffer.writerIndex() - 0x2); // short messageLite = null; //set null ,collection by GC return buffer; }
From source file:com.digitalpetri.modbus.codec.ModbusTcpCodec.java
License:Apache License
public void encode(ModbusTcpPayload payload, ByteBuf buffer) throws Exception { int headerStartIndex = buffer.writerIndex(); buffer.writeZero(MbapHeader.LENGTH); int pduStartIndex = buffer.writerIndex(); encoder.encode(payload.getModbusPdu(), buffer); int pduLength = buffer.writerIndex() - pduStartIndex; MbapHeader header = new MbapHeader(payload.getTransactionId(), pduLength + 1, payload.getUnitId()); int currentWriterIndex = buffer.writerIndex(); buffer.writerIndex(headerStartIndex); MbapHeader.encode(header, buffer);//w w w. j a va 2 s . c om buffer.writerIndex(currentWriterIndex); }
From source file:com.digitalpetri.opcua.stack.core.channel.ChunkDecoder.java
License:Apache License
private ByteBuf decode(Delegate delegate, SecureChannel channel, List<ByteBuf> chunkBuffers) throws UaException { CompositeByteBuf composite = BufferUtil.compositeBuffer(); int signatureSize = delegate.getSignatureSize(channel); int cipherTextBlockSize = delegate.getCipherTextBlockSize(channel); boolean encrypted = delegate.isEncryptionEnabled(channel); boolean signed = delegate.isSigningEnabled(channel); for (ByteBuf chunkBuffer : chunkBuffers) { char chunkType = (char) chunkBuffer.getByte(3); chunkBuffer.skipBytes(SecureMessageHeader.SECURE_MESSAGE_HEADER_SIZE); delegate.readSecurityHeader(channel, chunkBuffer); if (encrypted) { decryptChunk(delegate, channel, chunkBuffer); }//from www . j a v a 2 s .c o m int encryptedStart = chunkBuffer.readerIndex(); chunkBuffer.readerIndex(0); if (signed) { delegate.verifyChunk(channel, chunkBuffer); } int paddingSize = encrypted ? getPaddingSize(cipherTextBlockSize, signatureSize, chunkBuffer) : 0; int bodyEnd = chunkBuffer.readableBytes() - signatureSize - paddingSize; chunkBuffer.readerIndex(encryptedStart); SequenceHeader sequenceHeader = SequenceHeader.decode(chunkBuffer); long sequenceNumber = sequenceHeader.getSequenceNumber(); lastRequestId = sequenceHeader.getRequestId(); if (lastSequenceNumber == -1) { lastSequenceNumber = sequenceNumber; } else { if (lastSequenceNumber + 1 != sequenceNumber) { String message = String.format("expected sequence number %s but received %s", lastSequenceNumber + 1, sequenceNumber); logger.error(message); logger.error(ByteBufUtil.hexDump(chunkBuffer, 0, chunkBuffer.writerIndex())); throw new UaException(StatusCodes.Bad_SecurityChecksFailed, message); } lastSequenceNumber = sequenceNumber; } ByteBuf bodyBuffer = chunkBuffer.readSlice(bodyEnd - chunkBuffer.readerIndex()); if (chunkType == 'A') { ErrorMessage errorMessage = ErrorMessage.decode(bodyBuffer); throw new MessageAbortedException(errorMessage.getError(), errorMessage.getReason()); } composite.addComponent(bodyBuffer); composite.writerIndex(composite.writerIndex() + bodyBuffer.readableBytes()); } return composite.order(ByteOrder.LITTLE_ENDIAN); }
From source file:com.digitalpetri.opcua.stack.core.channel.ChunkEncoder.java
License:Apache License
private List<ByteBuf> encode(Delegate delegate, SecureChannel channel, MessageType messageType, ByteBuf messageBuffer, long requestId) throws UaException { List<ByteBuf> chunks = new ArrayList<>(); boolean encrypted = delegate.isEncryptionEnabled(channel); int securityHeaderSize = delegate.getSecurityHeaderSize(channel); int cipherTextBlockSize = delegate.getCipherTextBlockSize(channel); int plainTextBlockSize = delegate.getPlainTextBlockSize(channel); int signatureSize = delegate.getSignatureSize(channel); int maxChunkSize = parameters.getLocalSendBufferSize(); int headerSizes = SecureMessageHeader.SECURE_MESSAGE_HEADER_SIZE + securityHeaderSize; int paddingOverhead = encrypted ? (cipherTextBlockSize > 256 ? 2 : 1) : 0; int maxBlockCount = (maxChunkSize - headerSizes - signatureSize - paddingOverhead) / cipherTextBlockSize; int maxBodySize = (plainTextBlockSize * maxBlockCount - SequenceHeader.SEQUENCE_HEADER_SIZE); while (messageBuffer.readableBytes() > 0) { int bodySize = Math.min(messageBuffer.readableBytes(), maxBodySize); int paddingSize = encrypted ? plainTextBlockSize//from w w w . j a v a 2 s . c om - (SequenceHeader.SEQUENCE_HEADER_SIZE + bodySize + signatureSize + paddingOverhead) % plainTextBlockSize : 0; int plainTextContentSize = SequenceHeader.SEQUENCE_HEADER_SIZE + bodySize + signatureSize + paddingSize + paddingOverhead; assert (plainTextContentSize % plainTextBlockSize == 0); int chunkSize = SecureMessageHeader.SECURE_MESSAGE_HEADER_SIZE + securityHeaderSize + (plainTextContentSize / plainTextBlockSize) * cipherTextBlockSize; ByteBuf chunkBuffer = BufferUtil.buffer(chunkSize); /* Message Header */ SecureMessageHeader messageHeader = new SecureMessageHeader(messageType, messageBuffer.readableBytes() > bodySize ? 'C' : 'F', chunkSize, channel.getChannelId()); SecureMessageHeader.encode(messageHeader, chunkBuffer); /* Security Header */ delegate.encodeSecurityHeader(channel, chunkBuffer); /* Sequence Header */ SequenceHeader sequenceHeader = new SequenceHeader(sequenceNumber.getAndIncrement(), requestId); SequenceHeader.encode(sequenceHeader, chunkBuffer); /* Message Body */ chunkBuffer.writeBytes(messageBuffer, bodySize); /* Padding and Signature */ if (encrypted) { writePadding(cipherTextBlockSize, paddingSize, chunkBuffer); } if (delegate.isSigningEnabled(channel)) { ByteBuffer chunkNioBuffer = chunkBuffer.nioBuffer(0, chunkBuffer.writerIndex()); byte[] signature = delegate.signChunk(channel, chunkNioBuffer); chunkBuffer.writeBytes(signature); } /* Encryption */ if (encrypted) { chunkBuffer.readerIndex(SecureMessageHeader.SECURE_MESSAGE_HEADER_SIZE + securityHeaderSize); assert (chunkBuffer.readableBytes() % plainTextBlockSize == 0); try { int blockCount = chunkBuffer.readableBytes() / plainTextBlockSize; ByteBuffer chunkNioBuffer = chunkBuffer.nioBuffer(chunkBuffer.readerIndex(), blockCount * cipherTextBlockSize); ByteBuf copyBuffer = chunkBuffer.copy(); ByteBuffer plainTextNioBuffer = copyBuffer.nioBuffer(); Cipher cipher = delegate.getAndInitializeCipher(channel); if (delegate instanceof AsymmetricDelegate) { for (int blockNumber = 0; blockNumber < blockCount; blockNumber++) { int position = blockNumber * plainTextBlockSize; int limit = (blockNumber + 1) * plainTextBlockSize; plainTextNioBuffer.position(position).limit(limit); int bytesWritten = cipher.doFinal(plainTextNioBuffer, chunkNioBuffer); assert (bytesWritten == cipherTextBlockSize); } } else { cipher.doFinal(plainTextNioBuffer, chunkNioBuffer); } copyBuffer.release(); } catch (GeneralSecurityException e) { throw new UaException(StatusCodes.Bad_SecurityChecksFailed, e); } } chunkBuffer.readerIndex(0).writerIndex(chunkSize); chunks.add(chunkBuffer); } lastRequestId = requestId; return chunks; }
From source file:com.digitalpetri.opcua.stack.core.channel.messages.TcpMessageEncoder.java
License:Apache License
/** * Encode a simple UA TCP message./* w ww.j a va 2 s .c o m*/ * * @param messageType {@link MessageType#Hello}, {@link MessageType#Acknowledge}, or {@link MessageType#Error}. * @param messageEncoder a function that encodes the message payload. * @param buffer the {@link ByteBuf} to encode into. */ private static ByteBuf encode(MessageType messageType, Consumer<ByteBuf> messageEncoder, ByteBuf buffer) throws UaException { buffer.writeMedium(MessageType.toMediumInt(messageType)); buffer.writeByte('F'); int lengthIndex = buffer.writerIndex(); buffer.writeInt(0); int indexBefore = buffer.writerIndex(); messageEncoder.accept(buffer); int indexAfter = buffer.writerIndex(); int bytesWritten = indexAfter - indexBefore; buffer.writerIndex(lengthIndex); buffer.writeInt(8 + bytesWritten); buffer.writerIndex(indexAfter); return buffer; }
From source file:com.ebay.jetstream.messaging.transport.netty.serializer.KryoObjectEncoder.java
License:MIT License
@Override protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception { KryoContext kryoContext = kryoContextHolder.get(); Kryo kryo = kryoContext.getKryo();/*w ww . ja v a 2 s . c o m*/ Output output = kryoContext.getOut(); output.clear(); ByteBufOutputStream bout = new ByteBufOutputStream(out); int startIdx = out.writerIndex(); bout.write(LENGTH_PLACEHOLDER); output.setOutputStream(bout); output.writeByte(StreamMessageDecoder.KRYO_STREAM_VERSION); kryo.writeClassAndObject(output, msg); output.flush(); bout.flush(); bout.close(); output.close(); int endIdx = out.writerIndex(); out.setInt(startIdx, endIdx - startIdx - 4); }