List of usage examples for io.netty.buffer ByteBuf writeShort
public abstract ByteBuf writeShort(int value);
From source file:com.dempe.chat.common.mqtt.codec.PubRelEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext chc, PubRelMessage msg, ByteBuf out) { out.writeByte(AbstractMessage.PUBREL << 4 | 0x02); out.writeBytes(Utils.encodeRemainingLength(2)); out.writeShort(msg.getMessageID()); }
From source file:com.dempe.chat.common.mqtt.codec.SubAckEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext chc, SubAckMessage message, ByteBuf out) { if (message.types().isEmpty()) { throw new IllegalArgumentException("Found a suback message with empty topics"); }//from ww w .jav a 2s. c o m int variableHeaderSize = 2 + message.types().size(); ByteBuf buff = chc.alloc().buffer(6 + variableHeaderSize); try { buff.writeByte(AbstractMessage.SUBACK << 4); buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize)); buff.writeShort(message.getMessageID()); for (AbstractMessage.QOSType c : message.types()) { buff.writeByte(c.byteValue()); } out.writeBytes(buff); } finally { buff.release(); } }
From source file:com.dempe.chat.common.mqtt.codec.SubscribeEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext chc, SubscribeMessage message, ByteBuf out) { if (message.subscriptions().isEmpty()) { throw new IllegalArgumentException("Found a subscribe message with empty topics"); }//from w w w . ja va 2 s . co m if (message.getQos() != AbstractMessage.QOSType.LEAST_ONE) { throw new IllegalArgumentException("Expected a message with QOS 1, found " + message.getQos()); } ByteBuf variableHeaderBuff = chc.alloc().buffer(4); ByteBuf buff = null; try { variableHeaderBuff.writeShort(message.getMessageID()); for (SubscribeMessage.Couple c : message.subscriptions()) { variableHeaderBuff.writeBytes(Utils.encodeString(c.topicFilter)); variableHeaderBuff.writeByte(c.qos); } int variableHeaderSize = variableHeaderBuff.readableBytes(); byte flags = Utils.encodeFlags(message); buff = chc.alloc().buffer(2 + variableHeaderSize); buff.writeByte(AbstractMessage.SUBSCRIBE << 4 | flags); buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize)); buff.writeBytes(variableHeaderBuff); out.writeBytes(buff); } finally { variableHeaderBuff.release(); buff.release(); } }
From source file:com.dempe.chat.common.mqtt.codec.UnsubscribeEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext chc, UnsubscribeMessage message, ByteBuf out) { if (message.topicFilters().isEmpty()) { throw new IllegalArgumentException("Found an unsubscribe message with empty topics"); }//from w ww.ja v a2s.co m if (message.getQos() != AbstractMessage.QOSType.LEAST_ONE) { throw new IllegalArgumentException("Expected a message with QOS 1, found " + message.getQos()); } ByteBuf variableHeaderBuff = chc.alloc().buffer(4); ByteBuf buff = null; try { variableHeaderBuff.writeShort(message.getMessageID()); for (String topic : message.topicFilters()) { variableHeaderBuff.writeBytes(Utils.encodeString(topic)); } int variableHeaderSize = variableHeaderBuff.readableBytes(); byte flags = Utils.encodeFlags(message); buff = chc.alloc().buffer(2 + variableHeaderSize); buff.writeByte(AbstractMessage.UNSUBSCRIBE << 4 | flags); buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize)); buff.writeBytes(variableHeaderBuff); out.writeBytes(buff); } finally { variableHeaderBuff.release(); buff.release(); } }
From source file:com.dempe.chat.common.mqtt.codec.Utils.java
License:Open Source License
/** * Return the IoBuffer with string encoded as MSB, LSB and bytes array content. *//*from ww w . ja v a2 s .c o m*/ public static ByteBuf encodeFixedLengthContent(byte[] content) { ByteBuf out = Unpooled.buffer(2); out.writeShort(content.length); out.writeBytes(content); return out; }
From source file:com.dempe.chat.common.netty.PublishEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, PublishMessage message, ByteBuf out) { if (message.getQos() == AbstractMessage.QOSType.RESERVED) { throw new IllegalArgumentException("Found a message with RESERVED Qos"); }/*from w w w .j a v a2 s .com*/ if (message.getTopicName() == null || message.getTopicName().isEmpty()) { throw new IllegalArgumentException("Found a message with empty or null topic name"); } ByteBuf variableHeaderBuff = ctx.alloc().buffer(2); ByteBuf buff = null; try { variableHeaderBuff.writeBytes(Utils.encodeString(message.getTopicName())); if (message.getQos() == AbstractMessage.QOSType.LEAST_ONE || message.getQos() == AbstractMessage.QOSType.EXACTLY_ONCE) { if (message.getMessageID() == null) { throw new IllegalArgumentException("Found a message with QOS 1 or 2 and not MessageID setted"); } variableHeaderBuff.writeShort(message.getMessageID()); } variableHeaderBuff.writeBytes(message.getPayload()); int variableHeaderSize = variableHeaderBuff.readableBytes(); byte flags = Utils.encodeFlags(message); buff = ctx.alloc().buffer(2 + variableHeaderSize); buff.writeByte(AbstractMessage.PUBLISH << 4 | flags); buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize)); buff.writeBytes(variableHeaderBuff); out.writeBytes(buff); } finally { variableHeaderBuff.release(); if (buff != null) { buff.release(); } } }
From source file:com.digitalpetri.modbus.codec.MbapHeader.java
License:Apache License
public static ByteBuf encode(MbapHeader header, ByteBuf buffer) { buffer.writeShort(header.transactionId); buffer.writeShort(header.protocolId); buffer.writeShort(header.length);/*from w w w .j a v a 2 s . c om*/ buffer.writeByte(header.unitId); return buffer; }
From source file:com.digitalpetri.modbus.codec.ModbusRequestEncoder.java
License:Apache License
public ByteBuf encodeReadCoils(ReadCoilsRequest request, ByteBuf buffer) { buffer.writeByte(request.getFunctionCode().getCode()); buffer.writeShort(request.getAddress()); buffer.writeShort(request.getQuantity()); return buffer; }
From source file:com.digitalpetri.modbus.codec.ModbusRequestEncoder.java
License:Apache License
public ByteBuf encodeReadDiscreteInputs(ReadDiscreteInputsRequest request, ByteBuf buffer) { buffer.writeByte(request.getFunctionCode().getCode()); buffer.writeShort(request.getAddress()); buffer.writeShort(request.getQuantity()); return buffer; }
From source file:com.digitalpetri.modbus.codec.ModbusRequestEncoder.java
License:Apache License
public ByteBuf encodeReadHoldingRegisters(ReadHoldingRegistersRequest request, ByteBuf buffer) { buffer.writeByte(request.getFunctionCode().getCode()); buffer.writeShort(request.getAddress()); buffer.writeShort(request.getQuantity()); return buffer; }