public override void Encode(IByteBuffer buffer) { var buf = Unpooled.Buffer(); try { //variable header buf.WriteString(ProtocolName); //byte 1 - 8 buf.WriteByte(ProtocolLevel); //byte 9 //connect flags; //byte 10 var flags = UsernameFlag.ToByte() << 7; flags |= PasswordFlag.ToByte() << 6; flags |= WillRetain.ToByte() << 5; flags |= ((byte)WillQos) << 3; flags |= WillFlag.ToByte() << 2; flags |= CleanSession.ToByte() << 1; buf.WriteByte((byte)flags); //keep alive buf.WriteShort(KeepAlive); //byte 11 - 12 //payload buf.WriteString(ClientId); if (WillFlag) { buf.WriteString(WillTopic); buf.WriteString(WillMessage); } if (UsernameFlag) { buf.WriteString(UserName); } if (PasswordFlag) { buf.WriteString(Password); } FixedHeader.RemaingLength = buf.WriterIndex; FixedHeader.WriteTo(buffer); buffer.WriteBytes(buf); } finally { buf?.Release(); buf = null; } }
public override void Encode(Stream stream) { using (var body = new MemoryStream()) { //variable header body.WriteString(ProtocolName); //byte 1 - 8 body.WriteByte(ProtocolVersion); //byte 9 //ConnectFlags.WriteTo(body); //byte 10 var flags = UsernameFlag.ToByte() << 7; flags |= PasswordFlag.ToByte() << 6; flags |= WillRetain.ToByte() << 5; flags |= ((byte)WillQos) << 3; flags |= WillFlag.ToByte() << 2; flags |= CleanSession.ToByte() << 1; body.WriteByte((byte)flags); //keep alive body.WriteShort(KeepAlive); //byte 11 - 12 //payload body.WriteString(ClientId); if (WillFlag) { body.WriteString(WillTopic); body.WriteString(WillMessage); } if (UsernameFlag) { body.WriteString(Username); } if (PasswordFlag) { body.WriteString(Password); } FixedHeader.RemaingLength = (int)body.Length; FixedHeader.WriteTo(stream); body.WriteTo(stream); } }
public override int GetHashCode() { unchecked // Overflow is fine, just wrap { int hash = (int)2166136261; // Suitable nullity checks etc, of course :) hash = (hash * 16777619) ^ ClientId.GetHashCode(); hash = (hash * 16777619) ^ CleanSession.GetHashCode(); hash = (hash * 16777619) ^ ClientIndex.GetHashCode(); hash = (hash * 16777619) ^ IsConnected.GetHashCode(); if (WillFlag) { hash = (hash * 16777619) ^ WillFlag.GetHashCode(); hash = (hash * 16777619) ^ WillMessage.GetHashCode(); hash = (hash * 16777619) ^ WillQosLevel.GetHashCode(); hash = (hash * 16777619) ^ WillRetain.GetHashCode(); hash = (hash * 16777619) ^ WillTopic.GetHashCode(); } return(hash); } }