Example usage for io.netty.buffer ByteBuf readLong

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

Introduction

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

Prototype

public abstract long readLong();

Source Link

Document

Gets a 64-bit integer at the current readerIndex and increases the readerIndex by 8 in this buffer.

Usage

From source file:com.tesora.dve.mysqlapi.repl.messages.MyUserVarLogEvent.java

License:Open Source License

String processIntValue(ByteBuf cb, int valueLen) throws PEException {
    String value = StringUtils.EMPTY;

    switch (valueLen) {
    case 8:/*from  w  w  w  . j a v a 2 s  . c om*/
        value = Long.toString(cb.readLong());
        break;
    case 7:
    case 6:
    case 5:
        throw new PEException(
                "Cannot decode INT value of length '" + valueLen + "' for variable '" + variableName + "'");
    case 4:
        value = Long.toString(cb.readInt());
        break;
    case 3:
        value = Long.toString(cb.readMedium());
        break;
    case 2:
        value = Long.toString(cb.readShort());
        break;
    case 1:
        value = Byte.toString(cb.readByte());
        break;
    }
    return value;
}

From source file:com.tesora.dve.mysqlapi.repl.messages.MyXIdLogEvent.java

License:Open Source License

@Override
public void unmarshallMessage(ByteBuf cb) {
    xid = cb.readLong();
}

From source file:com.turn.ttorrent.common.protocol.udp.UDPAnnounceRequestMessage.java

License:Apache License

@Override
public void fromWire(ByteBuf in) throws MessageValidationException {
    _fromWire(in, UDP_ANNOUNCE_REQUEST_MESSAGE_SIZE);

    infoHash = new byte[20];
    in.readBytes(infoHash);//from   w w  w .java  2s  . co m
    peerId = new byte[20];
    in.readBytes(peerId);

    downloaded = in.readLong();
    uploaded = in.readLong();
    left = in.readLong();

    event = AnnounceEvent.getById(in.readInt());
    if (event == null)
        throw new MessageValidationException("Invalid event type in announce request!");

    InetAddress address;
    try {
        byte[] ipBytes = new byte[4];
        in.readBytes(ipBytes);
        address = InetAddress.getByAddress(ipBytes);
    } catch (UnknownHostException e) {
        throw new MessageValidationException("Invalid IP address in announce request!", e);
    }

    key = in.readInt();
    numWant = in.readInt();
    int port = in.readShort() & 0xFFFF;

    peerAddress = new InetSocketAddress(address, port);
}

From source file:com.turn.ttorrent.common.protocol.udp.UDPConnectResponseMessage.java

License:Apache License

@Override
public void fromWire(ByteBuf in) throws MessageValidationException {
    _fromWire(in, UDP_CONNECT_RESPONSE_MESSAGE_SIZE);
    setConnectionId(in.readLong());
}

From source file:com.twitter.http2.HttpFrameEncoderTest.java

License:Apache License

private static void assertPingFrame(ByteBuf frame, boolean pong, long data) {
    byte type = 0x06;
    byte flags = 0x00;
    if (pong) {//  w ww.  jav  a2  s . c  om
        flags |= 0x01;
    }
    assertEquals(8, assertFrameHeader(frame, type, flags, 0));
    assertEquals(data, frame.readLong());
    assertFalse(frame.isReadable());
}

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

License:Open Source License

public static Trace decodeTrace(ByteBuf buffer) {
    long spanId = buffer.readLong();
    long parentId = buffer.readLong();
    long traceId = buffer.readLong();
    byte traceFlags = buffer.readByte();

    return new Trace(spanId, parentId, traceId, traceFlags);
}

From source file:com.weibo.api.motan.transport.netty.NettyDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

    if (in.readableBytes() <= MotanConstants.NETTY_HEADER) {
        return;// www .  j  av  a 2  s  . co m
    }

    in.markReaderIndex();

    short type = in.readShort();

    if (type != MotanConstants.NETTY_MAGIC_TYPE) {
        in.resetReaderIndex();
        throw new MotanFrameworkException("NettyDecoder transport header not support, type: " + type);
    }

    byte messageType = (byte) in.readShort();
    long requestId = in.readLong();

    int dataLength = in.readInt();

    // FIXME dataLength?
    if (in.readableBytes() < dataLength) {
        in.resetReaderIndex();
        return;
    }

    if (maxContentLength > 0 && dataLength > maxContentLength) {
        LoggerUtil.warn(
                "NettyDecoder transport data content length over of limit, size: {}  > {}. remote={} local={}",
                dataLength, maxContentLength, ctx.channel().remoteAddress(), ctx.channel().localAddress());
        Exception e = new MotanServiceException(
                "NettyDecoder transport data content length over of limit, size: " + dataLength + " > "
                        + maxContentLength);

        if (messageType == MotanConstants.FLAG_REQUEST) {
            Response response = buildExceptionResponse(requestId, e);
            ctx.channel().writeAndFlush(response);
            throw e;
        } else {
            throw e;
        }
    }

    byte[] data = new byte[dataLength];

    in.readBytes(data);

    try {
        String remoteIp = getRemoteIp(ctx.channel());
        out.add(codec.decode(client, remoteIp, data));
    } catch (Exception e) {
        //????
        if (messageType == MotanConstants.FLAG_REQUEST) {
            Response response = buildExceptionResponse(requestId, e);
            ctx.channel().writeAndFlush(response);
            return;
        } else {
            out.add(buildExceptionResponse(requestId, e));
            return;
        }
    }
}

From source file:com.witjit.game.server.communication.bbmessage.MessageInHandler.java

@Override
protected long readEndpointId(ByteBuf mb) {
    return mb.readLong();
}

From source file:com.yea.remote.netty.codec.NettyMessageDecoder.java

License:Apache License

@Override
public Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf frame = (ByteBuf) super.decode(ctx, in);
    if (frame == null) {
        return null;
    }//w  w w. ja  va2  s. c om
    ISerializer serializer = serializePool.borrow();
    try {
        Message message = new Message();
        byte[] sessionID = new byte[16];
        Header header = new Header();
        header.setCrcCode(frame.readInt());
        header.setLength(frame.readInt());
        frame.readBytes(sessionID);
        header.setSessionID(sessionID);
        header.setType(frame.readByte());
        header.setPriority(frame.readByte());
        header.setResult(frame.readByte());
        RemoteConstants.CompressionAlgorithm compressionAlgorithm = RemoteConstants.CompressionAlgorithm
                .valueOf(frame.readByte());
        if (compressionAlgorithm != null && compressionAlgorithm.code() > 0) {
            //
            serializer.setCompress(new Compress().setCompressionAlgorithm(compressionAlgorithm.algorithm()));
        }
        long basedate = frame.readLong();

        int attachmentSize = frame.readByte();
        if (attachmentSize > 0) {
            attachmentSize = frame.readByte();//?Date?String?
            if (attachmentSize > 0) {
                byte[] keyArray = null;
                byte[] valueArray = null;
                for (int i = 0; i < attachmentSize; i++) {
                    keyArray = new byte[frame.readShort()];
                    frame.readBytes(keyArray);
                    valueArray = new byte[frame.readShort()];
                    frame.readBytes(valueArray);

                    header.addAttachment(new String(keyArray, "ISO-8859-1"),
                            serializer.deserialize(valueArray));
                }
            }
            attachmentSize = frame.readByte();//Date?
            if (attachmentSize > 0) {
                byte[] keyArray = null;
                byte length = 0;
                for (int i = 0; i < attachmentSize; i++) {
                    keyArray = new byte[frame.readShort()];
                    frame.readBytes(keyArray);
                    length = frame.readByte();
                    if (length == 1) {
                        header.addAttachment(new String(keyArray, "ISO-8859-1"),
                                new Date(basedate - frame.readByte()));
                    } else if (length == 2) {
                        header.addAttachment(new String(keyArray, "ISO-8859-1"),
                                new Date(basedate - frame.readShort()));
                    } else if (length == 4) {
                        header.addAttachment(new String(keyArray, "ISO-8859-1"),
                                new Date(basedate - frame.readInt()));
                    } else {
                        header.addAttachment(new String(keyArray, "ISO-8859-1"),
                                new Date(basedate - frame.readLong()));
                    }
                }
            }
            attachmentSize = frame.readByte();//String?
            if (attachmentSize > 0) {
                byte[] keyArray = null;
                byte[] valueArray = null;
                for (int i = 0; i < attachmentSize; i++) {
                    keyArray = new byte[frame.readShort()];
                    frame.readBytes(keyArray);
                    valueArray = new byte[frame.readShort()];
                    frame.readBytes(valueArray);

                    header.addAttachment(new String(keyArray, "ISO-8859-1"),
                            new String(valueArray, "ISO-8859-1"));
                }
            }
        }
        header.addAttachment(NettyConstants.MessageHeaderAttachment.SEND_DATE.value(), new Date(basedate));
        header.addAttachment(NettyConstants.MessageHeaderAttachment.RECIEVE_DATE.value(), new Date());
        message.setHeader(header);

        if (frame.readableBytes() > 4) {
            int length = frame.readInt();//Body
            byte[] objArray = new byte[length];
            frame.readBytes(objArray);
            try {
                Object body = serializer.deserialize(objArray);
                message.setBody(body);
            } catch (Exception ex) {
                if (RemoteConstants.MessageType.SERVICE_RESP.value() == message.getHeader().getType()) {
                    message.getHeader().setType(RemoteConstants.MessageType.SUSPEND_RESP.value());
                    message.getHeader().setResult(RemoteConstants.MessageResult.FAILURE.value());
                    message.setBody(ex);
                } else if (RemoteConstants.MessageType.SERVICE_REQ.value() == message.getHeader().getType()) {
                    message.getHeader().setType(RemoteConstants.MessageType.SUSPEND_REQ.value());
                    message.getHeader().setResult(RemoteConstants.MessageResult.FAILURE.value());
                    message.setBody(ex);
                } else {
                    throw ex;
                }
            }
        }

        return message;
    } finally {
        serializePool.restore(serializer);
        frame.release();
    }
}

From source file:com.zz.learning.netty5.chap12.codec.NettyMessageDecoder.java

License:Apache License

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf frame = (ByteBuf) super.decode(ctx, in);
    if (frame == null) {
        return null;
    }//from  w  w w .  j ava  2s .  com

    NettyMessage message = new NettyMessage();
    Header header = new Header();
    header.setCrcCode(frame.readInt());
    header.setLength(frame.readInt());
    header.setSessionID(frame.readLong());
    header.setType(frame.readByte());
    header.setPriority(frame.readByte());

    int size = frame.readInt(); // attachment
    if (size > 0) {
        Map<String, Object> attch = new HashMap<String, Object>(size);
        int keySize = 0;
        byte[] keyArray = null;
        String key = null;
        for (int i = 0; i < size; i++) {
            keySize = frame.readInt(); // key
            keyArray = new byte[keySize];
            frame.readBytes(keyArray);
            key = new String(keyArray, "UTF-8");
            attch.put(key, marshallingDecoder.decode(frame));
        }
        keyArray = null;
        key = null;
        header.setAttachment(attch);
    }
    if (frame.readableBytes() > 4) {
        //?4??0
        message.setBody(marshallingDecoder.decode(frame));
    }
    message.setHeader(header);
    return message;
}