Example usage for io.netty.buffer ByteBuf writeLong

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

Introduction

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

Prototype

public abstract ByteBuf writeLong(long value);

Source Link

Document

Sets the specified 64-bit long integer at the current writerIndex and increases the writerIndex by 8 in this buffer.

Usage

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

License:Apache License

@Test
public void testHttpPongFrame() throws Exception {
    int length = 8;
    byte flags = 0x01; // PONG
    int streamId = 0; // connection identifier
    long data = RANDOM.nextLong();

    ByteBuf frame = pingFrame(length, flags, streamId);
    frame.writeLong(data);
    decoder.decode(frame);//from www.j a  v a2s . c  o m

    verify(delegate).readPingFrame(data, true);
    verifyNoMoreInteractions(delegate);
}

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

License:Apache License

@Test
public void testHttpPingFrameReservedBits() throws Exception {
    int length = 8;
    byte flags = (byte) 0xFE; // should ignore any unknown flags
    int streamId = 0; // connection identifier
    long data = RANDOM.nextLong();

    ByteBuf frame = pingFrame(length, flags, streamId);
    frame.writeLong(data);
    decoder.decode(frame);/*from w  w  w  . jav a 2 s. c  om*/

    verify(delegate).readPingFrame(data, false);
    verifyNoMoreInteractions(delegate);
}

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

License:Apache License

@Test
public void testInvalidHttpPingFrame() throws Exception {
    int length = 12; // invalid length
    byte flags = 0;
    int streamId = 0; // connection identifier
    long data = RANDOM.nextLong();

    ByteBuf frame = pingFrame(length, flags, streamId);
    frame.writeLong(data);
    decoder.decode(frame);/*  www. j  a  v a  2s.  c om*/

    verify(delegate).readFrameError(anyString());
    verifyNoMoreInteractions(delegate);
}

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

License:Apache License

@Test
public void testIllegalHttpPingFrame() throws Exception {
    int length = 8;
    byte flags = 0;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; // illegal stream identifier
    long data = RANDOM.nextLong();

    ByteBuf frame = pingFrame(length, flags, streamId);
    frame.writeLong(data);
    decoder.decode(frame);// w  w  w  .j av  a  2 s.  c  o  m

    verify(delegate).readFrameError(anyString());
    verifyNoMoreInteractions(delegate);
}

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

License:Apache License

/**
 * Encode an HTTP/2 PING Frame//from w  w  w . j av a2s.co  m
 */
public ByteBuf encodePingFrame(long data, boolean ack) {
    int length = 8;
    byte flags = ack ? HTTP_FLAG_ACK : 0;
    int streamId = 0;
    ByteBuf frame = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE + length);
    writeFrameHeader(frame, length, HTTP_PING_FRAME, flags, streamId);
    frame.writeLong(data);
    return frame;
}

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

License:Open Source License

public static void encodeTrace(Trace trace, ByteBuf buffer) {
    buffer.writeLong(trace.spanId).writeLong(trace.parentId).writeLong(trace.traceId)
            .writeByte(trace.traceFlags);
}

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

@Override
protected void writeEndpointId(ByteBuf mb, long endpointId) {
    mb.writeLong(endpointId);
}

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

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf sendBuf) throws Exception {
    if (msg == null || msg.getHeader() == null) {
        throw new Exception("The encode message is null");
    }//from  ww w.  j a  v  a 2 s .c o m
    ISerializer serializer = serializePool.borrow();
    try {
        long basedate = new Date().getTime();
        sendBuf.writeInt((msg.getHeader().getCrcCode()));// 4
        sendBuf.writeInt((msg.getHeader().getLength()));// 4Header+BodyHeader18
        sendBuf.writeBytes((msg.getHeader().getSessionID()));// 16
        sendBuf.writeByte((msg.getHeader().getType()));// 1
        sendBuf.writeByte((msg.getHeader().getPriority()));// 1
        sendBuf.writeByte((msg.getHeader().getResult()));// 1
        if (compressionAlgorithm != null && compressionAlgorithm.code() > 0) {
            // 
            serializer.setCompress(new Compress().setCompressionAlgorithm(compressionAlgorithm.algorithm()));
            sendBuf.writeByte(compressionAlgorithm.ordinal());
        } else {
            sendBuf.writeByte(RemoteConstants.CompressionAlgorithm.NONE.ordinal());
        }
        sendBuf.writeLong(basedate);// 8??
        if (msg.getHeader().getAttachment() != null && !msg.getHeader().getAttachment().isEmpty()) {
            sendBuf.writeByte(msg.getHeader().getAttachment().size());
            Map<String, Number> mapDateType = new HashMap<String, Number>();// Date
            Map<String, String> mapStringType = new HashMap<String, String>();// String

            int lengthPos = sendBuf.writerIndex();
            sendBuf.writeBytes(new byte[1]);// 1?Date?
            byte[] keyArray = null;
            byte[] valueArray = null;
            for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) {
                if (param.getValue() instanceof Date) {
                    //Date???byte
                    long time = basedate - ((Date) param.getValue()).getTime();
                    if (time > Integer.MAX_VALUE) {
                        mapDateType.put(param.getKey(), new Long(time));
                    } else if (time > Short.MAX_VALUE) {
                        mapDateType.put(param.getKey(), new Integer((int) time));
                    } else if (time > Byte.MAX_VALUE) {
                        mapDateType.put(param.getKey(), new Short((short) time));
                    } else {
                        mapDateType.put(param.getKey(), new Byte((byte) time));
                    }
                } else if (param.getValue() instanceof String) {
                    //??getBytes()?
                    mapStringType.put(param.getKey(), (String) param.getValue());
                } else {
                    keyArray = param.getKey().getBytes("ISO-8859-1");
                    sendBuf.writeShort(keyArray.length);
                    sendBuf.writeBytes(keyArray);

                    valueArray = serializer.serialize(param.getValue());
                    sendBuf.writeShort(valueArray.length);
                    sendBuf.writeBytes(valueArray);
                }
            }
            sendBuf.setByte(lengthPos,
                    msg.getHeader().getAttachment().size() - mapDateType.size() - mapStringType.size());

            if (mapDateType.isEmpty()) {
                sendBuf.writeByte(0);// 1Date?0
            } else {
                sendBuf.writeByte(mapDateType.size());// 1Date?
                for (Map.Entry<String, Number> param : mapDateType.entrySet()) {
                    keyArray = param.getKey().getBytes("ISO-8859-1");
                    sendBuf.writeShort(keyArray.length);
                    sendBuf.writeBytes(keyArray);

                    if (param.getValue() instanceof Long) {
                        sendBuf.writeByte(8);
                        sendBuf.writeLong((Long) param.getValue());
                    } else if (param.getValue() instanceof Integer) {
                        sendBuf.writeByte(4);
                        sendBuf.writeInt((Integer) param.getValue());
                    } else if (param.getValue() instanceof Short) {
                        sendBuf.writeByte(2);
                        sendBuf.writeShort((Short) param.getValue());
                    } else {
                        sendBuf.writeByte(1);
                        sendBuf.writeByte((Byte) param.getValue());
                    }
                }
            }

            if (mapStringType.isEmpty()) {
                sendBuf.writeByte(0);// 1String?0
            } else {
                sendBuf.writeByte(mapStringType.size());// 1String?
                for (Map.Entry<String, String> param : mapStringType.entrySet()) {
                    keyArray = param.getKey().getBytes("ISO-8859-1");
                    sendBuf.writeShort(keyArray.length);
                    sendBuf.writeBytes(keyArray);

                    valueArray = param.getValue().getBytes("ISO-8859-1");
                    sendBuf.writeShort(valueArray.length);
                    sendBuf.writeBytes(valueArray);
                }
            }
        } else {
            sendBuf.writeByte(0);// 20
        }

        if (msg.getBody() != null) {
            byte[] objArray = serializer.serialize(msg.getBody());
            int lengthPos = sendBuf.writerIndex();
            sendBuf.writeBytes(LENGTH_PLACEHOLDER);// 4Body
            sendBuf.writeBytes(objArray);
            sendBuf.setInt(lengthPos, sendBuf.writerIndex() - lengthPos - 4);// Body
        } else {
            sendBuf.writeInt(0);
        }
        sendBuf.setInt(4, sendBuf.readableBytes() - 8);// Header+Body
    } finally {
        serializePool.restore(serializer);
    }
}

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

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, NettyMessage msg, ByteBuf sendBuf) throws Exception {
    if (msg == null || msg.getHeader() == null) {
        throw new Exception("The encode message is null");
    }/*from   w w w. j  a  va  2s.  c  o m*/
    sendBuf.writeInt((msg.getHeader().getCrcCode()));
    sendBuf.writeInt((msg.getHeader().getLength()));
    sendBuf.writeLong((msg.getHeader().getSessionID()));
    sendBuf.writeByte((msg.getHeader().getType()));
    sendBuf.writeByte((msg.getHeader().getPriority()));
    sendBuf.writeInt((msg.getHeader().getAttachment().size()));
    String key = null;
    byte[] keyArray = null;
    Object value = null;
    for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) {
        key = param.getKey();
        keyArray = key.getBytes("UTF-8");
        sendBuf.writeInt(keyArray.length);
        sendBuf.writeBytes(keyArray);
        value = param.getValue();
        marshallingEncoder.encode(value, sendBuf);
    }
    key = null;
    keyArray = null;
    value = null;
    if (msg.getBody() != null) {
        marshallingEncoder.encode(msg.getBody(), sendBuf);
    } else {
        sendBuf.writeInt(0); //?
    }
    //?
    sendBuf.setInt(4,
            //? crcCode,length
            sendBuf.readableBytes() - 8);
}

From source file:daxum.temporalconvergence.network.PacketDodge.java

License:Open Source License

@Override
public void toBytes(ByteBuf buf) {
    buf.writeLong(playerUUID.getMostSignificantBits());
    buf.writeLong(playerUUID.getLeastSignificantBits());
}