Example usage for io.netty.buffer ByteBuf order

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

Introduction

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

Prototype

@Deprecated
public abstract ByteOrder order();

Source Link

Document

Returns the <a href="http://en.wikipedia.org/wiki/Endianness">endianness</a> of this buffer.

Usage

From source file:eu.xworlds.util.raknet.protocol.BaseMessage.java

License:Open Source License

/**
 * writes an unsigned medium to byte buf
 * /*  w  w w.  j  a v  a  2  s  .c  om*/
 * @param target target byte buffer.
 * @param value the unsigned medium value.
 */
protected void writeUnsignedMedium(ByteBuf target, int value) {
    if (value < 0 || value > 0xffffff) {
        throw new IllegalArgumentException(value + " exceeds allowed size"); //$NON-NLS-1$
    }
    if (target.order() == ByteOrder.BIG_ENDIAN) {
        target.writeByte(value >> 16);
        target.writeByte(value >> 8);
        target.writeByte(value);
    } else {
        target.writeByte(value);
        target.writeByte(value >> 8);
        target.writeByte(value >> 16);
    }
}

From source file:org.l2junity.loginserver.network.client.crypt.Crypt.java

License:Open Source License

@Override
public void encrypt(ByteBuf buf) {
    // Ensure that byte order is little endian because we set new packet size in first 2 bytes
    if (buf.order() != ByteOrder.LITTLE_ENDIAN) {
        buf = buf.order(ByteOrder.LITTLE_ENDIAN);
    }/*from  ww w  . j a  v a  2s  . c om*/

    // Checksum & XOR Key or Checksum only
    buf.writeZero(_static ? 8 : 4);

    // Padding
    buf.writeZero(8 - (buf.readableBytes() % 8));

    if (_static) {
        _static = false;

        int key = Rnd.nextInt();
        buf.skipBytes(4); // The first 4 bytes are ignored
        while (buf.readerIndex() < (buf.writerIndex() - 8)) {
            int data = buf.readInt();
            key += data;
            data ^= key;
            buf.setInt(buf.readerIndex() - 4, data);
        }
        buf.setInt(buf.readerIndex(), key);

        buf.resetReaderIndex();

        final byte[] block = new byte[8];
        while (buf.isReadable(8)) {
            buf.readBytes(block);
            STATIC_BLOWFISH_ENGINE.encryptBlock(block, 0);
            buf.setBytes(buf.readerIndex() - block.length, block);
        }
    } else {
        int checksum = 0;
        while (buf.isReadable(8)) {
            checksum ^= buf.readInt();
        }
        buf.setInt(buf.readerIndex(), checksum);

        buf.resetReaderIndex();

        final byte[] block = new byte[8];
        while (buf.isReadable(8)) {
            buf.readBytes(block);
            _blowfishEngine.encryptBlock(block, 0);
            buf.setBytes(buf.readerIndex() - block.length, block);
        }
    }
}

From source file:org.l2junity.network.codecs.PacketDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
    if ((in == null) || !in.isReadable()) {
        return;/*from  w  ww  .  j  a  v a  2s  .  com*/
    }

    if (in.order() != _byteOrder) {
        in = in.order(_byteOrder);
    }

    try {
        final short packetId = in.readUnsignedByte();
        if (packetId >= _incomingPackets.length) {
            LOGGER.debug("Unknown packet: {}", Integer.toHexString(packetId));
            return;
        }

        final IIncomingPackets<T> incomingPacket = _incomingPackets[packetId];
        if (incomingPacket == null) {
            LOGGER.debug("Unknown packet: {}", Integer.toHexString(packetId));
            return;
        }

        final IConnectionState connectionState = ctx.channel().attr(IConnectionState.ATTRIBUTE_KEY).get();
        if ((connectionState == null) || !incomingPacket.getConnectionStates().contains(connectionState)) {
            LOGGER.warn("{}: Connection at invalid state: {} Required States: {}", incomingPacket,
                    connectionState, incomingPacket.getConnectionStates());
            return;
        }

        final IIncomingPacket<T> packet = incomingPacket.newIncomingPacket();
        if ((packet != null) && packet.read(_client, new PacketReader(in))) {
            out.add(packet);
        }
    } finally {
        // We always consider that we read whole packet.
        in.readerIndex(in.writerIndex());
    }
}

From source file:org.l2junity.network.codecs.PacketEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, IOutgoingPacket packet, ByteBuf out) {
    if (out.order() != _byteOrder) {
        out = out.order(_byteOrder);//from   www . j a  v a2 s  .c o m
    }

    try {
        if (packet.write(new PacketWriter(out))) {
            if (out.writerIndex() > _maxPacketSize) {
                throw new IllegalStateException("Packet (" + packet + ") size (" + out.writerIndex()
                        + ") is bigger than the limit (" + _maxPacketSize + ")");
            }
        } else {
            // Avoid sending the packet
            out.clear();
        }
    } catch (Throwable e) {
        LOGGER.warn("Failed sending Packet({})", packet, e);
        // Avoid sending the packet if some exception happened
        out.clear();
    }
}