List of usage examples for io.netty.buffer ByteBuf order
@Deprecated public abstract ByteBuf order(ByteOrder endianness);
From source file:org.eclipse.neoscada.protocol.iec60870.asdu.MessageManager.java
License:Open Source License
public void encodeMessage(final Object msg, ByteBuf buf) { logger.debug("Encode message: {}", msg); final ASDU asdu = msg.getClass().getAnnotation(ASDU.class); if (asdu == null) { throw new IllegalStateException( String.format("Unable to send message of type %s, no %s annotation found", msg.getClass(), ASDU.class.getName())); }// ww w.j a v a 2 s . co m final MessageCodec codec = this.codecs.get(new MessageTypeId(asdu.id(), asdu.informationStructure())); if (codec == null) { throw new IllegalStateException( String.format("Unable to send message of type %s, no codec is registered", msg.getClass())); } try { buf = buf.order(ByteOrder.LITTLE_ENDIAN); codec.encode(this.options, msg, buf); logger.debug("Encoded to {} bytes", buf.writerIndex()); } catch (final Exception e) { logger.warn("Failed to encode message", e); throw new RuntimeException(e); } }
From source file:org.evilco.network.rcon.common.codec.FrameCodec.java
License:Apache License
/** * {@inheritDoc}/*from w w w. j a v a 2 s.c om*/ */ @Override protected void encode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, ByteBuf byteBuf2) throws Exception { // set order byteBuf2 = byteBuf2.order(ByteOrder.LITTLE_ENDIAN); // log getLogger().debug("Encoding frame for " + byteBuf.readableBytes() + " bytes of data."); // ensure output buffer is writable byteBuf2.ensureWritable((4 + byteBuf.readableBytes())); // write packet length byteBuf2.writeInt(byteBuf.readableBytes()); // write packet byteBuf2.writeBytes(byteBuf); }
From source file:org.evilco.network.rcon.common.codec.FrameCodec.java
License:Apache License
/** * {@inheritDoc}//from ww w . j a va2 s. co m */ @Override protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> objects) throws Exception { // set order byteBuf = byteBuf.order(ByteOrder.LITTLE_ENDIAN); // read all available packets while (byteBuf.readableBytes() >= 4) { // log getLogger().trace("Decoding frame with maximal size of " + byteBuf.readableBytes() + " bytes."); // mark reader index byteBuf.markReaderIndex(); // read length int length = byteBuf.readInt(); // log getLogger().trace("Detected frame length of " + length + " bytes."); // check whether enough data is available if (length > byteBuf.readableBytes()) { // log getLogger().debug("There are only " + byteBuf.readableBytes() + " out of " + length + " bytes available. Skipping frame until more data is available."); // reset buffer byteBuf.resetReaderIndex(); // exit loop break; } // log getLogger().trace("Frame seems to be complete reading data."); // construct buffer ByteBuf packetBuffer = channelHandlerContext.alloc().buffer(length); // read data byteBuf.readBytes(packetBuffer, length); // add to list objects.add(packetBuffer); // log getLogger().trace("Frame decoded. " + byteBuf.readableBytes() + " bytes left in buffer."); } }
From source file:org.evilco.network.rcon.common.codec.RconCodec.java
License:Apache License
/** * {@inheritDoc}//from ww w .j a v a 2 s . co m */ @Override protected void encode(ChannelHandlerContext channelHandlerContext, PacketWrapper packetWrapper, ByteBuf byteBuf) throws Exception { // set order byteBuf = byteBuf.order(ByteOrder.LITTLE_ENDIAN); // fire event PacketSendEvent event = new PacketSendEvent(packetWrapper); this.eventBus.post(event); // cancel if (event.isCancelled()) return; packetWrapper = event.getPacket(); // find packetID int packetID = this.configuration.getOutboundRegistry().findPacketID(packetWrapper.getPacket()); // log getLogger().debug("Encoding packet of type " + packetWrapper.getPacket().getClass().getName() + " (packetID: " + packetID + ") with identifier " + packetWrapper.getIdentifier() + "."); // write identifier byteBuf.writeInt(packetWrapper.getIdentifier()); // write packet identifier byteBuf.writeInt(packetID); // allocate buffer ByteBuf buffer = channelHandlerContext.alloc().buffer(); // write body packetWrapper.getPacket().write(buffer); // write data byteBuf.writeBytes(buffer); // write terminator buffer.writeBytes(new byte[] { 0x00, 0x00 }); // fire event this.eventBus.post(new PacketSentEvent(packetWrapper)); }
From source file:org.evilco.network.rcon.common.codec.RconCodec.java
License:Apache License
/** * {@inheritDoc}//w w w. ja va2s .c o m */ @Override protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> objects) throws Exception { // set order byteBuf = byteBuf.order(ByteOrder.LITTLE_ENDIAN); // read identifier int identifier = byteBuf.readInt(); // read packet identifier int packetID = byteBuf.readInt(); // allocate buffer ByteBuf buffer = channelHandlerContext.alloc().buffer((byteBuf.readableBytes() - 1)); // read data byteBuf.readBytes(buffer, (byteBuf.readableBytes() - 2)); // read null byte Preconditions.checkState((byteBuf.readByte() == 0x00 && byteBuf.readByte() == 0x00), "The last byte is not null"); Preconditions.checkState((byteBuf.readableBytes() == 0), "There is still data left in the packet."); // read data IPacket packet = this.configuration.getInboundRegistry().createPacketInstance(packetID, buffer); // log getLogger().debug("Decoded packet of type " + packet.getClass().getName() + " (packetID: " + packetID + ", identifier: " + identifier + ")."); // construct wrapper PacketWrapper wrapper = new PacketWrapper(identifier, packet); // fire event PacketReceiveEvent event = new PacketReceiveEvent(wrapper); this.eventBus.post(event); // process if (!event.isCancelled()) objects.add(event.getPacket()); else getLogger().debug( "Ignoring packet with identifier %s (packetID: %s, type: %s): Event has been cancelled.", identifier, packetID, packet.getClass()); }
From source file:org.fiware.kiara.transport.tcp.TcpServerInitializer.java
License:Open Source License
@Override public void initChannel(SocketChannel ch) { ChannelPipeline p = ch.pipeline();/*w ww . j a v a 2s . c o m*/ // Enable TCPS if necessary. if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); } p.addLast("logger", new LoggingHandler(LogLevel.DEBUG)); p.addLast(new LengthFieldBasedFrameDecoder(ByteOrder.LITTLE_ENDIAN, Integer.MAX_VALUE, 0, 4, 0, 4, true)); p.addLast(new ByteBufferDecoder()); p.addLast(new LengthFieldPrepender(4, 0, false) { @Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { ByteBuf outWithLittleEndian = out.order(ByteOrder.LITTLE_ENDIAN); super.encode(ctx, msg, outWithLittleEndian); } }); p.addLast(new ByteBufferEncoder()); p.addLast(new TcpHandler(transportFactory, path, connectionListener)); }
From source file:org.spout.vanilla.protocol.rcon.codec.RconCodec.java
License:Open Source License
@Override public ByteBuf encode(T message) { byte[] bytes = message.getPayload().getBytes(CharsetUtil.US_ASCII); ByteBuf buffer = Unpooled.buffer(bytes.length + 2); buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.writeBytes(bytes);//from w w w . ja va 2 s . c o m buffer.writeByte(0); buffer.writeByte(0); return buffer; }
From source file:org.spout.vanilla.protocol.rcon.codec.RconCodec.java
License:Open Source License
@Override public T decode(ByteBuf buffer) { buffer.order(ByteOrder.LITTLE_ENDIAN); ByteBuf expandingBytes = Unpooled.buffer(buffer.writerIndex()); byte b;// w ww . j av a 2 s. com while ((b = buffer.readByte()) != 0) { expandingBytes.writeByte(b); } assert buffer.readByte() == 0; // Second null byte String value = new String(expandingBytes.array(), CharsetUtil.US_ASCII); return createMessage(value); }
From source file:provider.nx.core.util.SeekableLittleEndianAccessor.java
License:Open Source License
/** * Creates an immutable {@code SeekableLittleEndianAccessor} wrapping a * {@code ByteBuf}.//from www. j ava 2 s .co m * * @param buf * the buffer to wrap */ public SeekableLittleEndianAccessor(ByteBuf buf) { this.buf = buf.order(ByteOrder.LITTLE_ENDIAN); }
From source file:provider.pkgnx.util.SeekableLittleEndianAccessor.java
License:Open Source License
/** * Creates an immutable {@code SeekableLittleEndianAccessor} wrapping a {@code ByteBuf}. * * @param buf the buffer to wrap// www .ja v a2 s .c o m */ public SeekableLittleEndianAccessor(final ByteBuf buf) { this.buf = buf.order(ByteOrder.LITTLE_ENDIAN); localBuf = new ThreadLocal<ByteBuf>() { @Override protected ByteBuf initialValue() { return SeekableLittleEndianAccessor.this.buf.duplicate(); } }; }