List of usage examples for io.netty.buffer ByteBuf writeByte
public abstract ByteBuf writeByte(int value);
From source file:com.dempe.chat.common.mqtt.codec.ConnectEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext chc, ConnectMessage message, ByteBuf out) { ByteBuf staticHeaderBuff = chc.alloc().buffer(12); ByteBuf buff = chc.alloc().buffer(); ByteBuf variableHeaderBuff = chc.alloc().buffer(12); try {/* w ww. jav a 2 s . co m*/ staticHeaderBuff.writeBytes(Utils.encodeString("MQIsdp")); //version staticHeaderBuff.writeByte(0x03); //connection flags and Strings byte connectionFlags = 0; if (message.isCleanSession()) { connectionFlags |= 0x02; } if (message.isWillFlag()) { connectionFlags |= 0x04; } connectionFlags |= ((message.getWillQos() & 0x03) << 3); if (message.isWillRetain()) { connectionFlags |= 0x020; } if (message.isPasswordFlag()) { connectionFlags |= 0x040; } if (message.isUserFlag()) { connectionFlags |= 0x080; } staticHeaderBuff.writeByte(connectionFlags); //Keep alive timer staticHeaderBuff.writeShort(message.getKeepAlive()); //Variable part if (message.getClientID() != null) { variableHeaderBuff.writeBytes(Utils.encodeString(message.getClientID())); if (message.isWillFlag()) { variableHeaderBuff.writeBytes(Utils.encodeString(message.getWillTopic())); variableHeaderBuff.writeBytes(Utils.encodeFixedLengthContent(message.getWillMessage())); } if (message.isUserFlag() && message.getUsername() != null) { variableHeaderBuff.writeBytes(Utils.encodeString(message.getUsername())); if (message.isPasswordFlag() && message.getPassword() != null) { variableHeaderBuff.writeBytes(Utils.encodeFixedLengthContent(message.getPassword())); } } } int variableHeaderSize = variableHeaderBuff.readableBytes(); buff.writeByte(AbstractMessage.CONNECT << 4); buff.writeBytes(Utils.encodeRemainingLength(12 + variableHeaderSize)); buff.writeBytes(staticHeaderBuff).writeBytes(variableHeaderBuff); out.writeBytes(buff); } finally { staticHeaderBuff.release(); buff.release(); variableHeaderBuff.release(); } }
From source file:com.dempe.chat.common.mqtt.codec.DisconnectEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext chc, DisconnectMessage msg, ByteBuf out) { out.writeByte(AbstractMessage.DISCONNECT << 4).writeByte(0); }
From source file:com.dempe.chat.common.mqtt.codec.PingReqEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext chc, PingReqMessage msg, ByteBuf out) { out.writeByte(AbstractMessage.PINGREQ << 4).writeByte(0); }
From source file:com.dempe.chat.common.mqtt.codec.PingRespEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext chc, PingRespMessage msg, ByteBuf out) { out.writeByte(AbstractMessage.PINGRESP << 4).writeByte(0); }
From source file:com.dempe.chat.common.mqtt.codec.PubAckEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext chc, PubAckMessage msg, ByteBuf out) { ByteBuf buff = chc.alloc().buffer(4); try {/* w w w .ja v a2 s . c om*/ buff.writeByte(AbstractMessage.PUBACK << 4); buff.writeBytes(Utils.encodeRemainingLength(2)); buff.writeShort(msg.getMessageID()); out.writeBytes(buff); } finally { buff.release(); } }
From source file:com.dempe.chat.common.mqtt.codec.PubCompEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext chc, PubCompMessage msg, ByteBuf out) { out.writeByte(AbstractMessage.PUBCOMP << 4); out.writeBytes(Utils.encodeRemainingLength(2)); out.writeShort(msg.getMessageID());//from w ww . j a v a2s . c om }
From source file:com.dempe.chat.common.mqtt.codec.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 . c om*/ 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); } catch (Exception e) { LOGGER.error(e.getMessage(), e); } finally { variableHeaderBuff.release(); if (buff != null) { buff.release(); } } }
From source file:com.dempe.chat.common.mqtt.codec.PubRecEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext chc, PubRecMessage msg, ByteBuf out) { out.writeByte(AbstractMessage.PUBREC << 4); out.writeBytes(Utils.encodeRemainingLength(2)); out.writeShort(msg.getMessageID());// w ww . j a v a 2 s .c o m }
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());// w w w . j a v a 2 s.co m }
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 www . ja v a 2s . co 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(); } }