Java Code Examples for io.netty.buffer.ByteBuf.writeByte()

The following are Jave code examples for showing how to use writeByte() of the io.netty.buffer.ByteBuf class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: Quavo   File: ConnectionEncoder.java   Source Code and License Vote up 6 votes
@Override
protected void encode(ChannelHandlerContext ctx, ConnectionResponse msg, ByteBuf out) throws Exception {
	ChannelPipeline pipeline = ctx.pipeline();

	switch (msg.getType()) {
	case HANDSHAKE_CONNECTION:
		pipeline.addAfter("decoder", "handshake.encoder", new HandshakeEncoder());
		pipeline.replace("decoder", "handshake.decoder", new HandshakeDecoder());
		break;
	case LOGIN_CONNECTION:
		out.writeByte(ClientMessage.SUCCESSFUL_CONNECTION.getId());
		pipeline.addAfter("decoder", "login.encoder", new LoginEncoder());
		pipeline.replace("decoder", "login.decoder", new LoginDecoder());
		break;
	}

	pipeline.remove(this);
}
 
Example 2
Project: incubator-plc4x   File: IsoTPProtocolTest.java   Source Code and License Vote up 6 votes
@Test
@Tag("fast")
public void decodeConnectionConfirm() throws Exception {
    ChannelHandlerContext ctx = new MockChannelHandlerContext();
    ByteBuf buf = Unpooled.buffer();
    ArrayList<Object> out = new ArrayList<>();

    buf.writeByte(0x6); // header length
    buf.writeByte(TpduCode.CONNECTION_CONFIRM.getCode());
    buf.writeShort(0x01); // destination reference
    buf.writeShort(0x02); // source reference
    buf.writeByte(ProtocolClass.CLASS_0.getCode());
    IsoOnTcpMessage in = new IsoOnTcpMessage(buf);

    isoTPProtocol.decode(ctx, in, out);

    assertTrue(out.size() == 1, "Message not decoded");

    ConnectionConfirmTpdu requestTpdu = (ConnectionConfirmTpdu) ((IsoTPMessage)out.get(0)).getTpdu();

    assertTrue(requestTpdu.getTpduCode() == TpduCode.CONNECTION_CONFIRM, "Message code not correct");
    assertTrue(requestTpdu.getDestinationReference() == (short) 0x1, "Message destination reference not correct");
    assertTrue(requestTpdu.getSourceReference() == (short) 0x2, "Message source reference not correct");
    assertTrue(requestTpdu.getProtocolClass() == ProtocolClass.CLASS_0, "Message protocol class reference not correct");
    assertTrue(requestTpdu.getParameters().isEmpty(), "Message contains paramaters");
}
 
Example 3
Project: incubator-plc4x   File: IsoTPProtocolTest.java   Source Code and License Vote up 6 votes
@Test
@Tag("fast")
public void decodeConnectionRequest() throws Exception {
    ChannelHandlerContext ctx = new MockChannelHandlerContext();
    ByteBuf buf = Unpooled.buffer();
    ArrayList<Object> out = new ArrayList<>();

    buf.writeByte(0x6); // header length
    buf.writeByte(TpduCode.CONNECTION_REQUEST.getCode());
    buf.writeShort(0x01); // destination reference
    buf.writeShort(0x02); // source reference
    buf.writeByte(ProtocolClass.CLASS_0.getCode());
    IsoOnTcpMessage in = new IsoOnTcpMessage(buf);

    isoTPProtocol.decode(ctx, in, out);

    assertTrue(out.size() == 1, "Message not decoded");

    ConnectionRequestTpdu requestTpdu = (ConnectionRequestTpdu) ((IsoTPMessage)out.get(0)).getTpdu();

    assertTrue(requestTpdu.getTpduCode() == TpduCode.CONNECTION_REQUEST, "Message code not correct");
    assertTrue(requestTpdu.getDestinationReference() == (short) 0x1, "Message destination reference not correct");
    assertTrue(requestTpdu.getSourceReference() == (short) 0x2, "Message source reference not correct");
    assertTrue(requestTpdu.getProtocolClass() == ProtocolClass.CLASS_0, "Message protocol class reference not correct");
    assertTrue(requestTpdu.getParameters().isEmpty(), "Message contains paramaters");
}
 
Example 4
Project: incubator-plc4x   File: IsoTPProtocolTest.java   Source Code and License Vote up 6 votes
@Test
@Tag("fast")
public void decodeDataEOT() throws Exception {
  ChannelHandlerContext ctx = new MockChannelHandlerContext();
  ByteBuf buf = Unpooled.buffer();
  ArrayList<Object> out = new ArrayList<>();

  buf.writeByte(0x3); // header length
  buf.writeByte(TpduCode.DATA.getCode());
  buf.writeByte((byte) 0x81); // Tpdu code + EOT
  IsoOnTcpMessage in = new IsoOnTcpMessage(buf);

  isoTPProtocol.decode(ctx, in, out);

  assertTrue(out.size() == 1, "Message not decoded");

  DataTpdu requestTpdu = (DataTpdu) ((IsoTPMessage)out.get(0)).getTpdu();

  assertTrue(requestTpdu.getTpduCode() == TpduCode.DATA, "Message code not correct");
  assertTrue(requestTpdu.getTpduRef() == (short) 0x1, "Message Tpdu reference not correct");
  assertTrue(requestTpdu.isEot(), "Message EOT not correct");
  assertTrue(requestTpdu.getParameters().isEmpty(), "Message contains paramaters");
}
 
Example 5
Project: incubator-plc4x   File: IsotpModelTests.java   Source Code and License Vote up 6 votes
@Test
@Tag("Fast")
void connectionRequestTpdu() {
    short destinationReference = 0x1;
    short sourceReference = 0x2;
    ProtocolClass protocolClass = ProtocolClass.CLASS_0;
    List<Parameter> parameters = Collections.emptyList();
    ByteBuf userData = Unpooled.buffer();

    userData.writeByte(0x33);

    ConnectionRequestTpdu tpdu = new ConnectionRequestTpdu(destinationReference, sourceReference, protocolClass, parameters, userData);

    assertTrue(tpdu.getTpduCode() == TpduCode.CONNECTION_REQUEST);
    assertTrue(tpdu.getDestinationReference() == 0x1, "Unexpected destination reference");
    assertTrue(tpdu.getSourceReference() == 0x2, "Unexpected source reference");
    assertTrue(tpdu.getProtocolClass() == ProtocolClass.CLASS_0);
    assertTrue(tpdu.getParameters().isEmpty(), "Unexpected parameters");
    assertTrue(tpdu.getUserData().readByte() == (byte) 0x33, "Unexpected user data");
}
 
Example 6
Project: PetiteRPC   File: Encoder.java   Source Code and License Vote up 5 votes
@Override
protected void encode(ChannelHandlerContext ctx, Codable msg, ByteBuf out) throws Exception {
    out.writeShort(CodecProtocol.MAGIC);
    out.writeByte(msg.getType().ordinal());
    byte[] bytes = serializer.serialize(msg);
    int length = bytes.length;
    out.writeInt(length).writeBytes(bytes);
}
 
Example 7
Project: DovakinMQ   File: MqttEncoder.java   Source Code and License Vote up 5 votes
private static ByteBuf encodeConnAckMessage(
        ByteBufAllocator byteBufAllocator,
        MqttConnAckMessage message) {
    ByteBuf buf = byteBufAllocator.buffer(4);
    buf.writeByte(getFixedHeaderByte1(message.fixedHeader()));
    buf.writeByte(2);
    buf.writeByte(message.variableHeader().isSessionPresent() ? 0x01 : 0x00);
    buf.writeByte(message.variableHeader().connectReturnCode().byteValue());

    return buf;
}
 
Example 8
Project: skylla   File: SkyllaPacketEncoder.java   Source Code and License Vote up 5 votes
@Override
protected void encode(ChannelHandlerContext channelHandlerContext, SkyllaPacket packet,
                      ByteBuf byteBuf) throws Exception {

    byte packetId = this.protocol.getPacketId(packet);

    // write packet id
    byteBuf.writeByte(packetId);

    // Encode Packet
    packet.write(new SkyllaBuffer(byteBuf));
}
 
Example 9
Project: CentauriCloud   File: PacketServerRegister.java   Source Code and License Vote up 5 votes
@Override
public void encode(ByteBuf buf) {
	this.writeString(prefix, buf);
	buf.writeByte(this.type.ordinal());
	if (type == ServerType.SPIGOT)
		buf.writeInt(this.bukkitPort);
}
 
Example 10
Project: FlexMC   File: Message.java   Source Code and License Vote up 5 votes
public static void writeItemStack( ItemStack itemStack, ByteBuf buf ) {
    if( itemStack == null || itemStack.getType() == Material.AIR ) {
        buf.writeShort( -1 );
    } else {
        buf.writeShort( itemStack.getTypeId() );
        buf.writeByte( itemStack.getAmount() );
        buf.writeShort( itemStack.getDurability() );
        writeNbtTag( new NBTTagCompound( "ItemStack" ), buf ); // todo change
    }
}
 
Example 11
Project: BaseClient   File: PingResponseHandler.java   Source Code and License Vote up 5 votes
private ByteBuf getStringBuffer(String string)
{
    ByteBuf bytebuf = Unpooled.buffer();
    bytebuf.writeByte(255);
    char[] achar = string.toCharArray();
    bytebuf.writeShort(achar.length);

    for (char c0 : achar)
    {
        bytebuf.writeChar(c0);
    }

    return bytebuf;
}
 
Example 12
Project: pnc-repressurized   File: PacketDescription.java   Source Code and License Vote up 5 votes
@Override
public void toBytes(ByteBuf buf) {
    super.toBytes(buf);
    buf.writeByte(type.ordinal());
    buf.writeInt(values.length);
    for (int i = 0; i < types.length; i++) {
        buf.writeByte(types[i]);
        PacketUpdateGui.writeField(buf, values[i], types[i]);
    }
    ByteBufUtils.writeTag(buf, extraData);
}
 
Example 13
Project: elegant-springboot   File: MsgEncoder.java   Source Code and License Vote up 5 votes
@Override
protected void encode(ChannelHandlerContext channelHandlerContext, OutboundMsg msg, ByteBuf out) throws Exception {


    byte type = msg.getType();
    byte opt = msg.getOpt();
    String msgStr = msg.getMsgStr();
    byte[] data = msg.getData();

    byte[] msgBytes = msgStr.getBytes(Charset.forName("utf-8"));

    // 文本消息长度
    int msgLength = msgBytes.length;
    // 二进制数据长度
    int dataLength = data == null ? 0 : data.length;

    // 总消息长度
    int allDataLength = msgLength + dataLength + OTHER_INFO_LENGTH;

    out.writeBytes(OutboundMsg.getHEADER());//输出头部标识
    out.writeInt(allDataLength);//其后所有的数据长度(字节)
    out.writeByte(type);//数据类型
    out.writeByte(opt);//操作类型
    out.writeInt(msgLength);//信息数据长度(字节)
    out.writeBytes(msgBytes);//信息字符串内容(已加密,字节)
    if (data != null) {
        out.writeBytes(data);//二进制数据
    }
    byte[] bytesToCheck = new byte[allDataLength - 1];
    out.getBytes(7, bytesToCheck);
    byte check = calculate(bytesToCheck);
    out.writeByte(check);//冗余校验值
}
 
Example 14
Project: neoscada   File: FrameCodec.java   Source Code and License Vote up 5 votes
private void processDATA ( final ChannelHandlerContext ctx, final byte b )
{
    final ByteBuf data = ctx.attr ( ATTR_DATA_BUFFER ).get ();
    data.writeByte ( b );
    if ( data.readableBytes () >= ctx.attr ( ATTR_EXPECTED_LENGTH ).get () )
    {
        ctx.attr ( ATTR_STATE ).set ( State.TRAILER );
    }
}
 
Example 15
Project: j1st-mqtt   File: MqttEncoder.java   Source Code and License Vote up 5 votes
private static ByteBuf encodeMessageWithOnlySingleByteFixedHeader(
        ByteBufAllocator byteBufAllocator,
        MqttMessage message) {
    MqttFixedHeader mqttFixedHeader = message.fixedHeader();
    ByteBuf buf = byteBufAllocator.buffer(2);
    buf.writeByte(getFixedHeaderByte1(mqttFixedHeader));
    buf.writeByte(0);

    return buf;
}
 
Example 16
Project: message-broker   File: ConnectionOpen.java   Source Code and License Vote up 4 votes
@Override
protected void writeMethod(ByteBuf buf) {
    virtualHost.write(buf);
    buf.writeByte(0);
    buf.writeByte(1);
}
 
Example 17
Project: message-broker   File: ConnectionOpenOk.java   Source Code and License Vote up 4 votes
@Override
protected void writeMethod(ByteBuf buf) {
    buf.writeByte(0);
}
 
Example 18
Project: CustomWorldGen   File: FMLHandshakeMessage.java   Source Code and License Vote up 4 votes
@Override
public void toBytes(ByteBuf buffer)
{
    buffer.writeByte(NetworkRegistry.FML_PROTOCOL);
    buffer.writeInt(overrideDimension);
}
 
Example 19
Project: reactive-pg-client   File: Sync.java   Source Code and License Vote up 4 votes
@Override
public void encode(ByteBuf out) {
  out.writeByte(SYNC);
  out.writeInt(4);
}
 
Example 20
Project: Mods   File: TF2Message.java   Source Code and License Vote up 4 votes
@Override
public void toBytes(ByteBuf buf) {
	buf.writeByte(id);

}