Example usage for io.netty.buffer ByteBuf readShort

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

Introduction

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

Prototype

public abstract short readShort();

Source Link

Document

Gets a 16-bit short integer at the current readerIndex and increases the readerIndex by 2 in this buffer.

Usage

From source file:com.farsunset.cim.sdk.server.filter.decoder.WebMessageDecoder.java

License:Apache License

@Override
public void decode(ChannelHandlerContext arg0, ByteBuf iobuffer, List<Object> queue) throws Exception {

    iobuffer.markReaderIndex();//  w  w w . jav a  2 s.c  o m

    /**
     * ?fin??1 0 ??
     */
    byte tag = iobuffer.readByte();
    int frameFin = tag > 0 ? 0 : 1; // ?byte ?1 ?0 fin 0  1
    if (frameFin == 0) {
        iobuffer.resetReaderIndex();
        return;
    }

    /**
     * ?protobuf?? OPCODE_BINARY? OPCODE_CLOSE
     */
    int frameOqcode = tag & TAG_MASK;

    if (OPCODE_BINARY == frameOqcode) {

        byte head = iobuffer.readByte();
        byte datalength = (byte) (head & PAYLOADLEN);
        int realLength = 0;

        /**
         * Payload len7?7+16?7+64????? 0-125payload
         * data 1267????2payload data
         * 1277????8payload data
         */
        if (datalength == HAS_EXTEND_DATA) {
            realLength = iobuffer.readShort();
        } else if (datalength == HAS_EXTEND_DATA_CONTINUE) {
            realLength = (int) iobuffer.readLong();
        } else {
            realLength = datalength;
        }

        boolean masked = (head >> 7 & MASK) == 1;
        if (masked) {// ?
            // ??
            byte[] mask = new byte[4];
            iobuffer.readBytes(mask);

            byte[] data = new byte[realLength];
            iobuffer.readBytes(data);
            for (int i = 0; i < realLength; i++) {
                // ??
                data[i] = (byte) (data[i] ^ mask[i % 4]);
            }

            handleMessage(data, queue);
        }

    } else if (OPCODE_CLOSE == frameOqcode) {
        handleClose(arg0);
    } else {
        // ?
        iobuffer.readBytes(new byte[iobuffer.readableBytes()]);
    }

}

From source file:com.github.liyp.netty.App.java

License:Apache License

public static void main(String[] args) throws Exception {

    EventLoopGroup boss = new NioEventLoopGroup();
    EventLoopGroup worker = new NioEventLoopGroup();

    try {/*from www.  ja  va 2  s .  c  o m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(boss, worker).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ReplayingDecoder() {
                            @Override
                            protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
                                    throws Exception {
                                short magicHeader = in.readShort();
                                logger.debug("Receive magic header: {}.", magicHeader);
                                if (magicHeader != HEADER) {
                                    logger.error("Receive illegal magic header: {}, close channel: {}.",
                                            magicHeader, ctx.channel().remoteAddress());
                                    ctx.close();
                                }

                                short dataLen = in.readShort();
                                logger.debug("Receive message data length: {}.", dataLen);
                                if (dataLen < 0) {
                                    logger.error("Data length is negative, close channel: {}.",
                                            ctx.channel().remoteAddress());
                                    ctx.close();
                                }

                                ByteBuf payload = in.readBytes(dataLen);
                                String cloudMsg = payload.toString(CharsetUtil.UTF_8);
                                logger.debug("Receive data: {}.", cloudMsg);
                                out.add(cloudMsg);
                            }
                        }).addLast(new MessageToByteEncoder<String>() {
                            @Override
                            protected void encode(ChannelHandlerContext ctx, String msg, ByteBuf out)
                                    throws Exception {
                                out.writeBytes(msg.getBytes());
                            }
                        }).addLast(new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                logger.info("start receive msg...");
                            }

                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                logger.info("receive msg: {}", msg);
                                logger.info("echo msg");
                                ctx.writeAndFlush(msg);
                            }
                        });
                    }
                }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
        ChannelFuture f = b.bind(PORT).sync();
        logger.info("9999");
        f.channel().closeFuture().sync();
    } finally {
        worker.shutdownGracefully();
        boss.shutdownGracefully();
    }
}

From source file:com.github.milenkovicm.kafka.protocol.Convert.java

License:Apache License

public static short decodeShort(ByteBuf buf) {
    return buf.readShort();
}

From source file:com.grandhunterman.dnvm.common.Message.java

License:Creative Commons License

private static short readShort(ByteBuf buf) {
    return buf.readShort();
}

From source file:com.heliosapm.ohcrs.core.AbstractDriverCodec.java

License:Apache License

/**
 * {@inheritDoc}//from  w w  w .j  ava  2  s.  co m
 * @see com.heliosapm.ohcrs.core.DriverCodec#readShort(io.netty.buffer.ByteBuf)
 */
@Override
public Short readShort(final ByteBuf b) throws SQLException {
    if (checkNull(b))
        return null;
    return b.readShort();
}

From source file:com.heliosapm.ohcrs.core.AbstractDriverCodec.java

License:Apache License

/**
 * {@inheritDoc}//from   ww  w .  j  av  a2s .c o  m
 * @see com.heliosapm.ohcrs.core.DriverCodec#readshort(io.netty.buffer.ByteBuf)
 */
@Override
public short readshort(final ByteBuf b) throws SQLException {
    if (checkNull(b))
        return 0;
    return b.readShort();
}

From source file:com.heliosapm.ohcrs.impls.oracle.OracleDriverCodec.java

License:Apache License

@Override
public Datum read(final DBType d, final ByteBuf b) throws SQLException {
    final short typeCode = b.readShort();
    if (checkNull(b))
        return null;
    final int size = b.readInt();
    final byte[] bytes = new byte[size];
    b.readBytes(bytes);/*from w w w. ja  v a2  s  .  co m*/
    final DBType dbType = DBType.forCode(typeCode);
    switch (dbType) {
    case BIT:
    case BIGINT:
    case DECIMAL:
    case DOUBLE:
    case FLOAT:
    case INTEGER:
    case NUMERIC:
    case TINYINT:
    case SMALLINT:
        return new NUMBER(bytes);
    case DATE:
    case TIME:
        return new oracle.sql.DATE(bytes);
    case TIMESTAMP:
        return new oracle.sql.TIMESTAMP(bytes);
    case ROWID:
        return new oracle.sql.ROWID(bytes);
    case CHAR:
    case NCHAR:
    case VARCHAR:
    case LONGVARCHAR:
    case LONGNVARCHAR:
    case NVARCHAR:
        final int charsetCode = b.readInt();
        final CharacterSet cs = CharacterSet.make(charsetCode);
        return new oracle.sql.CHAR(bytes, cs);
    default:
        throw new UnsupportedOperationException(dbType.name());
        //         case STRUCT:
        //         case BINARY:
        //         case BIT:
        //         case BLOB:
        //         case BOOLEAN:
        //         case CLOB:
        //         case DATALINK:
        //         case DISTINCT:
        //         case JAVA_OBJECT:
        //         case LONGVARBINARY:
        //         case NCLOB:
        //         case NULL:
        //         case OTHER:
        //         case REAL:
        //         case REF:
        //         case SQLXML:
        //         case VARBINARY:
    }
}

From source file:com.ibasco.agql.protocols.valve.steam.master.packets.MasterServerResponsePacket.java

License:Open Source License

@Override
public Vector<InetSocketAddress> toObject() {
    ByteBuf data = getPayloadBuffer();

    //Clear the list
    servers.clear();//from   www.j  a v  a2 s .c  om

    int firstOctet, secondOctet, thirdOctet, fourthOctet, portNumber;

    //Process the content containing list of source ips
    do {
        //Clear string
        ip.setLength(0);

        firstOctet = data.readByte() & 0xFF;
        secondOctet = data.readByte() & 0xFF;
        thirdOctet = data.readByte() & 0xFF;
        fourthOctet = data.readByte() & 0xFF;
        portNumber = data.readShort() & 0xFFFF;

        //Build our ip string
        ip.append(firstOctet).append(".").append(secondOctet).append(".").append(thirdOctet).append(".")
                .append(fourthOctet);

        //Add to the list
        servers.add(new InetSocketAddress(ip.toString(), portNumber));

        //Append port number
        ip.append(":").append(portNumber);
    } while (data.readableBytes() > 0);

    return servers;
}

From source file:com.ibm.crail.namenode.rpc.netty.common.NettyRequest.java

License:Apache License

public void update(ByteBuf buffer) throws IOException {

    this.cookie = buffer.readLong();
    this.cmd = buffer.readShort();
    this.type = buffer.readShort();

    nioBuffer.clear();/* ww  w .  j  a  v a  2  s  . c o  m*/
    buffer.readBytes(nioBuffer);
    nioBuffer.flip();

    switch (type) {
    case NameNodeProtocol.REQ_CREATE_FILE:
        this.createFileReq = new RpcRequestMessage.CreateFileReq();
        createFileReq.update(nioBuffer);
        break;
    case NameNodeProtocol.REQ_GET_FILE:
        this.fileReq = new RpcRequestMessage.GetFileReq();
        fileReq.update(nioBuffer);
        break;
    case NameNodeProtocol.REQ_SET_FILE:
        this.setFileReq = new RpcRequestMessage.SetFileReq();
        setFileReq.update(nioBuffer);
        break;
    case NameNodeProtocol.REQ_REMOVE_FILE:
        this.removeReq = new RpcRequestMessage.RemoveFileReq();
        removeReq.update(nioBuffer);
        break;
    case NameNodeProtocol.REQ_RENAME_FILE:
        this.renameFileReq = new RpcRequestMessage.RenameFileReq();
        renameFileReq.update(nioBuffer);
        break;
    case NameNodeProtocol.REQ_GET_BLOCK:
        this.getBlockReq = new RpcRequestMessage.GetBlockReq();
        getBlockReq.update(nioBuffer);
        break;
    case NameNodeProtocol.REQ_GET_LOCATION:
        this.getLocationReq = new RpcRequestMessage.GetLocationReq();
        getLocationReq.update(nioBuffer);
        break;
    case NameNodeProtocol.REQ_SET_BLOCK:
        this.setBlockReq = new RpcRequestMessage.SetBlockReq();
        setBlockReq.update(nioBuffer);
        break;
    case NameNodeProtocol.REQ_GET_DATANODE:
        this.getDataNodeReq = new RpcRequestMessage.GetDataNodeReq();
        getDataNodeReq.update(nioBuffer);
        break;
    case NameNodeProtocol.REQ_DUMP_NAMENODE:
        this.dumpNameNodeReq = new RpcRequestMessage.DumpNameNodeReq();
        dumpNameNodeReq.update(nioBuffer);
        break;
    case NameNodeProtocol.REQ_PING_NAMENODE:
        this.pingNameNodeReq = new RpcRequestMessage.PingNameNodeReq();
        pingNameNodeReq.update(nioBuffer);
        break;
    }
}

From source file:com.ibm.crail.namenode.rpc.netty.common.NettyResponse.java

License:Apache License

public void update(long cookie, ByteBuf buffer) {
    assert this.cookie == cookie;
    this.type = buffer.readShort();
    this.error = buffer.readShort();

    nioBuffer.clear();// w w w.ja  v  a  2s  .  c o m
    buffer.readBytes(nioBuffer);
    nioBuffer.flip();
    switch (type) {
    case NameNodeProtocol.RES_VOID:
        //this.voidRes = new RpcResponseMessage.VoidRes();
        voidRes.update(nioBuffer);
        voidRes.setError(error);
        break;
    case NameNodeProtocol.RES_CREATE_FILE:
        //this.createFileRes = new RpcResponseMessage.CreateFileRes();
        createFileRes.update(nioBuffer);
        createFileRes.setError(error);
        break;
    case NameNodeProtocol.RES_GET_FILE:
        //this.getFileRes = new RpcResponseMessage.GetFileRes();
        getFileRes.update(nioBuffer);
        getFileRes.setError(error);
        break;
    case NameNodeProtocol.RES_DELETE_FILE:
        //this.delFileRes = new RpcResponseMessage.DeleteFileRes();
        delFileRes.update(nioBuffer);
        delFileRes.setError(error);
        break;
    case NameNodeProtocol.RES_RENAME_FILE:
        //this.renameRes = new RpcResponseMessage.RenameRes();
        renameRes.update(nioBuffer);
        renameRes.setError(error);
        break;
    case NameNodeProtocol.RES_GET_BLOCK:
        //this.getBlockRes = new RpcResponseMessage.GetBlockRes();
        getBlockRes.update(nioBuffer);
        getBlockRes.setError(error);
        break;
    case NameNodeProtocol.RES_GET_LOCATION:
        //this.getLocationRes = new RpcResponseMessage.GetLocationRes();
        getLocationRes.update(nioBuffer);
        getLocationRes.setError(error);
        break;
    case NameNodeProtocol.RES_GET_DATANODE:
        //this.getDataNodeRes = new RpcResponseMessage.GetDataNodeRes();
        getDataNodeRes.update(nioBuffer);
        getDataNodeRes.setError(error);
        break;
    case NameNodeProtocol.RES_PING_NAMENODE:
        //this.pingNameNodeRes = new RpcResponseMessage.PingNameNodeRes();
        pingNameNodeRes.update(nioBuffer);
        pingNameNodeRes.setError(error);
        break;
    }
}