Example usage for io.netty.buffer ByteBuf writerIndex

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

Introduction

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

Prototype

public abstract int writerIndex();

Source Link

Document

Returns the writerIndex of this buffer.

Usage

From source file:com.tesora.dve.db.mysql.portal.protocol.MSPServerGreetingRequestMessage.java

License:Open Source License

public static void write(ChannelHandlerContext ctx, int connectionId, String salt, int serverCapabilities,
        String serverVersion, byte serverCharSet, String pluginData) {
    ByteBuf out = ctx.channel().alloc().heapBuffer(50).order(ByteOrder.LITTLE_ENDIAN);

    String scrambleBuffer1st = salt.substring(0, 8);
    String scrambleBuffer2nd = salt.substring(8) + '\0';
    Integer scrambleBufferSize = scrambleBuffer1st.length() + scrambleBuffer2nd.length();

    ByteBuf serverCapabilitiesBuf = ctx.channel().alloc().heapBuffer(4).order(ByteOrder.LITTLE_ENDIAN);
    try {//from   w  w  w.ja  v  a2s . c o m
        serverCapabilitiesBuf.writeInt(serverCapabilities);
        out.writeMedium(0);
        out.writeByte(0);
        out.writeByte(MYSQL_PROTOCOL_VERSION);

        out.writeBytes(serverVersion.getBytes());
        out.writeZero(1);
        out.writeInt(connectionId);
        out.writeBytes(scrambleBuffer1st.getBytes()); // Salt
        out.writeZero(1);
        out.writeByte(serverCapabilitiesBuf.getByte(0));
        out.writeByte(serverCapabilitiesBuf.getByte(1));

        out.writeByte(serverCharSet);
        out.writeShort(MyProtocolDefs.SERVER_STATUS_AUTOCOMMIT);
        out.writeByte(serverCapabilitiesBuf.getByte(2));
        out.writeByte(serverCapabilitiesBuf.getByte(3));
        out.writeByte(scrambleBufferSize.byteValue());
        out.writeZero(10); // write 10 unused bytes
        out.writeBytes(scrambleBuffer2nd.getBytes()); // Salt

        out.writeBytes(pluginData.getBytes()); // payload
        out.writeZero(1);

        out.setMedium(0, out.writerIndex() - 4);

        ctx.channel().writeAndFlush(out);
    } finally {
        serverCapabilitiesBuf.release();
    }
}

From source file:com.turn.ttorrent.client.io.PeerMessageCodec.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, PeerMessage value, ByteBuf out) throws Exception {
    // LOG.info("encode: " + value);
    int lengthIndex = out.writerIndex();
    out.writeInt(0);/*from ww w  . ja va2 s .c  om*/
    int startIndex = out.writerIndex();
    value.toWire(out, listener.getExtendedMessageTypes());
    out.setInt(lengthIndex, out.writerIndex() - startIndex);
}

From source file:com.uber.tchannel.codecs.MessageCodec.java

License:Open Source License

public static TFrame encode(ByteBufAllocator allocator, Frame msg) {
    ByteBuf buffer = null;
    if (msg instanceof CallFrame) {
        buffer = ((CallFrame) msg).getPayload();
    } else {/*  w  ww .  ja  va2 s  .c om*/
        buffer = msg.encodeHeader(allocator);
    }

    TFrame frame = new TFrame(buffer.writerIndex(), msg.getType(), msg.getId(), buffer);
    return frame;
}

From source file:com.vethrfolnir.game.network.login.LoginServerClientHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf buff = (ByteBuf) msg;

    int opcode = buff.readUnsignedByte();

    switch (opcode) {
    case 0xBB:/*from   w  w  w  .  j a v a 2s .co m*/
        ServerKill.read(serverClient, buff);
        break;
    case 0xA1:
        int newId = buff.readInt();
        RecievedAlternativeId.read(serverClient, buff, newId);
        break;
    default:
        log.warn("Unknown packet 0x" + PrintData.fillHex(opcode, 2) + ". Dump: "
                + PrintData.printData(buff.nioBuffer(0, buff.writerIndex())));
        break;
    }

    buff.release();
}

From source file:com.vethrfolnir.game.network.mu.crypt.MuDecoder.java

License:Open Source License

/**
 * C3 C4//w w  w  .  ja v a 2 s .com
 * @param buff
 * @return 
 */
public static ByteBuf DecodePacket(ByteBuf buff) {
    if (buff.writerIndex() <= 2) {
        log.fatal("Ambiguous buffer! " + ByteBufUtil.hexDump(buff));
        return null;
    }
    if (buff.readerIndex() != 0) {
        log.warn("Buffer must be at index 0!");
        buff.readerIndex(0);
    }

    int header = GetHeaderSize(buff);
    int packetSize = GetPacketSize(buff);
    int decodedSize = GetDecodedSize(buff);

    int contentSize = packetSize - header;

    //System.out.println("Header[0x"+PrintData.fillHex(buff.getUnsignedByte(0), 2)+"] size: "+GetHeaderSize(buff)+" Packet Size: "+GetPacketSize(buff) +" Content Size: "+contentSize +" Decoded: "+ GetDecodedSize(buff));

    ByteBuf out = alloc.heapBuffer(decodedSize, decodedSize);

    int originalHead = buff.getUnsignedByte(0);

    buff.readerIndex(header);
    int size = DecodeBlock(buff, out, header, contentSize);
    //buff.clear();

    size += header - 1;

    out.writerIndex(0);
    out.writeByte(originalHead);

    switch (originalHead) {
    case 0xc3:
        out.writeByte(size);
        break;
    case 0xC4:
        out.writeByte(size >> 8);
        out.writeByte(size & 0xFF);
        break;
    }

    out.writerIndex(size);
    out.readerIndex(header);

    DecXor32(out, header, size);

    out.readerIndex(0);
    return out;
}

From source file:com.vethrfolnir.game.network.mu.MuChannelHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf buff = (msg instanceof ByteBuffer) ? ctx.alloc().buffer().writeBytes((ByteBuffer) msg)
            : (ByteBuf) msg;/* w w w. ja va  2  s  . c  o m*/

    buff.readerIndex(2);

    int opcode = buff.readUnsignedByte();
    switch (opcode) { // double opcode
    case 0xf1:
    case 0xf3:
    case 0x0e:
    case 0x03:
        buff.readerIndex(buff.readerIndex() - 1);
        opcode = buff.readUnsignedShort(); // ex 0xF1_03
        break;
    default:
        break;
    }

    if (opcode == 0xe00) { // Time packet?
        buff.clear();
        buff.release();
        return;
    }

    ReadPacket packet = clientpackets.get(opcode);

    if (packet != null) {
        MuClient client = ctx.channel().attr(MuClient.ClientKey).get();
        //System.out.println("Got opcode: 0x"+PrintData.fillHex(opcode, 2)+ " packet = \n"+packet.getClass().getSimpleName());
        packet.read(client, buff);
    } else {
        log.warn("Unknown packet[opcode = 0x" + PrintData.fillHex(opcode, 2) + "]. Dump: ");
        log.warn(PrintData.printData(buff.nioBuffer(0, buff.writerIndex())));
    }

    //log.warn(PrintData.printData(buff.nioBuffer(0, buff.writerIndex())));

    if (buff.refCnt() > 0) {
        //System.out.println("Handler Release when packet[opcode = 0x"+PrintData.fillHex(opcode, 2)+"]");
        buff.release();
    }
}

From source file:com.vethrfolnir.login.network.mu.ClientChannelHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf buff = (ByteBuf) msg;

    switch (buff.getUnsignedByte(0)) {
    case 0xC1:/*from  w ww . ja  v  a 2s  .  c om*/
    case 0xC2:
    case 0xC3:
    case 0xC4:
        break;
    default:
        ctx.close();
        buff.release(); //TODO: maybe add a flood protector?
        log.warn("Client[" + ctx.channel() + "] is not a mu online client! Disconnecting!");
        return;
    }

    // we are not interested in the header and size;
    buff.readerIndex(2);

    int opcode = buff.readUnsignedShort();

    switch (opcode) {

    case 0xf4_03: { // Request Server information
        int serverId = buff.readUnsignedByte();
        ByteBuf buf = ctx.alloc().heapBuffer().order(ByteOrder.LITTLE_ENDIAN);

        WritePacket packet = SendServerInfo.StaticPacket;
        packet.write(null, buf, nameService, serverId);
        packet.markLength(buf);
        ;

        ctx.writeAndFlush(buf);
        break;
    }
    case 0xf4_06: { // Request Server list
        ByteBuf buf = ctx.alloc().heapBuffer().order(ByteOrder.LITTLE_ENDIAN);

        WritePacket packet = SendServerLists.StaticPacket;
        packet.write(null, buf, nameService);
        packet.markLength(buf);

        ctx.writeAndFlush(buf);
        break;
    }
    default:
        log.warn("Unkown packet[OPCODE = " + Integer.toHexString(opcode) + "] Dump: "
                + PrintData.printData(buff.nioBuffer(0, buff.writerIndex())));
        ctx.close();
        break;
    }
    buff.release();
}

From source file:com.vethrfolnir.network.ReadPacket.java

License:Open Source License

protected void invalidate(ByteBuf buff) {
    buff.readerIndex(buff.writerIndex());
}

From source file:com.vethrfolnir.network.WritePacket.java

License:Open Source License

public void markLength(ByteBuf buff) {
    int lenght = buff.writerIndex();
    switch (buff.getUnsignedByte(0)) {
    case 0xC1://from  w ww.  j  av a 2  s  . c  o  m
    case 0xC3:
        buff.setByte(1, lenght);
        break;
    case 0xC2:
    case 0xC4:
        buff.setByte(1, lenght >> 8);
        buff.setByte(2, lenght & 0xFF);
        break;
    }
}

From source file:com.whizzosoftware.hobson.radiora.api.codec.RadioRaFrameDecoder.java

License:Open Source License

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
    logger.trace("decode: {}", buffer.toString(CharsetUtil.UTF_8));

    ByteBuf frame = (ByteBuf) super.decode(ctx, buffer);

    if (frame != null) {
        // if we receive a single byte frame, it should be a '!'
        if (frame.readableBytes() == 1) {
            byte b = frame.readByte();
            if (b == '!') {
                return null;
            } else {
                throw new CorruptedFrameException("Unexpected single byte frame");
            }/*from   www .j  a  v  a 2 s  .c o  m*/
            // otherwise, we assume it's a command frame
        } else {
            int ix = frame.indexOf(frame.readerIndex(), frame.writerIndex(), (byte) ',');
            if (ix > 0) {
                String cmdName = frame.slice(0, ix).toString(CharsetUtil.UTF_8);

                switch (cmdName) {
                case ZoneMap.TYPE:
                    if (frame.readableBytes() >= ix + 33) {
                        return new ZoneMap(frame.slice(ix + 1, 32).toString(CharsetUtil.UTF_8));
                    } else {
                        throw new CorruptedFrameException("Received invalid zone map size");
                    }

                case LocalZoneChange.TYPE:
                    if (frame.readableBytes() >= ix + 3) {
                        String sZoneNum = frame.slice(ix + 1, 2).toString(CharsetUtil.UTF_8);
                        if (frame.readableBytes() >= ix + 7) {
                            String state = frame.slice(ix + 4, 3).toString(CharsetUtil.UTF_8).trim();
                            try {
                                return new LocalZoneChange(Integer.parseInt(sZoneNum),
                                        LocalZoneChange.State.valueOf(state));
                            } catch (IllegalArgumentException iae) {
                                throw new CorruptedFrameException("Invalid LZC state string");
                            }
                        } else {
                            throw new CorruptedFrameException("Invalid LZC size (state)");
                        }
                    } else {
                        throw new CorruptedFrameException("Invalid LZC size (zoneNum)");
                    }

                case LEDMap.TYPE:
                    if (frame.readableBytes() >= ix + 16) {
                        return new LEDMap(frame.slice(ix + 1, 15).toString(CharsetUtil.UTF_8));
                    } else {
                        throw new CorruptedFrameException("Invalid LED map size");
                    }

                default:
                    throw new DecoderException("Unrecognized command: " + cmdName);
                }
            } else {
                throw new CorruptedFrameException("Invalid frame format (no comma)");
            }
        }
    } else {
        return null;
    }
}