Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overloaded Connect method and accepting password in byte format. #111

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions M2Mqtt/Messages/MqttMsgConnect.cs
Original file line number Diff line number Diff line change
@@ -205,6 +205,8 @@ public ushort KeepAlivePeriod
private string username;
// password
private string password;
// password
private byte[] passwordInByte;
// clean session flag
private bool cleanSession;
// keep alive period (in sec)
@@ -251,7 +253,8 @@ public MqttMsgConnect(string clientId,
string willMessage,
bool cleanSession,
ushort keepAlivePeriod,
byte protocolVersion
byte protocolVersion,
byte[] passwordInBytes = null
)
{
this.type = MQTT_MSG_CONNECT_TYPE;
@@ -269,8 +272,11 @@ byte protocolVersion
// [v.3.1.1] added new protocol name and version
this.protocolVersion = protocolVersion;
this.protocolName = (this.protocolVersion == PROTOCOL_VERSION_V3_1_1) ? PROTOCOL_NAME_V3_1_1 : PROTOCOL_NAME_V3_1;
this.passwordInByte = passwordInBytes;
}



/// <summary>
/// Parse bytes for a CONNECT message
/// </summary>
@@ -388,6 +394,7 @@ public static MqttMsgConnect Parse(byte fixedHeaderFirstByte, byte protocolVersi
Array.Copy(buffer, index, passwordUtf8, 0, passwordUtf8Length);
index += passwordUtf8Length;
msg.password = new String(Encoding.UTF8.GetChars(passwordUtf8));

}

return msg;
@@ -406,7 +413,8 @@ public override byte[] GetBytes(byte protocolVersion)
byte[] willTopicUtf8 = (this.willFlag && (this.willTopic != null)) ? Encoding.UTF8.GetBytes(this.willTopic) : null;
byte[] willMessageUtf8 = (this.willFlag && (this.willMessage != null)) ? Encoding.UTF8.GetBytes(this.willMessage) : null;
byte[] usernameUtf8 = ((this.username != null) && (this.username.Length > 0)) ? Encoding.UTF8.GetBytes(this.username) : null;
byte[] passwordUtf8 = ((this.password != null) && (this.password.Length > 0)) ? Encoding.UTF8.GetBytes(this.password) : null;
byte[] passwordUtf8 = ((this.password != null) && (this.password.Length > 0)) ? Encoding.UTF8.GetBytes(this.password)
: (((this.passwordInByte != null) && (this.passwordInByte.Length > 0)) ? this.passwordInByte : null);

// [v3.1.1]
if (this.protocolVersion == PROTOCOL_VERSION_V3_1_1)
39 changes: 37 additions & 2 deletions M2Mqtt/MqttClient.cs
Original file line number Diff line number Diff line change
@@ -507,6 +507,20 @@ public byte Connect(string clientId,
return this.Connect(clientId, username, password, false, MqttMsgConnect.QOS_LEVEL_AT_MOST_ONCE, false, null, null, true, MqttMsgConnect.KEEP_ALIVE_PERIOD_DEFAULT);
}

/// <summary>
/// Connect to broker
/// </summary>
/// <param name="clientId">Client identifier</param>
/// <param name="username">Username</param>
/// <param name="password">Password</param>
/// <returns>Return code of CONNACK message from broker</returns>
public byte Connect(string clientId,
string username,
byte[] password)
{
return this.Connect(clientId, username, null, false, MqttMsgConnect.QOS_LEVEL_AT_MOST_ONCE, false, null, null, true, MqttMsgConnect.KEEP_ALIVE_PERIOD_DEFAULT, password);
}

/// <summary>
/// Connect to broker
/// </summary>
@@ -525,6 +539,24 @@ public byte Connect(string clientId,
return this.Connect(clientId, username, password, false, MqttMsgConnect.QOS_LEVEL_AT_MOST_ONCE, false, null, null, cleanSession, keepAlivePeriod);
}

/// <summary>
/// Connect to broker
/// </summary>
/// <param name="clientId">Client identifier</param>
/// <param name="username">Username</param>
/// <param name="password">Password</param>
/// <param name="cleanSession">Clean sessione flag</param>
/// <param name="keepAlivePeriod">Keep alive period</param>
/// <returns>Return code of CONNACK message from broker</returns>
public byte Connect(string clientId,
string username,
byte[] password,
bool cleanSession,
ushort keepAlivePeriod)
{
return this.Connect(clientId, username, null, false, MqttMsgConnect.QOS_LEVEL_AT_MOST_ONCE, false, null, null, cleanSession, keepAlivePeriod, password);
}

/// <summary>
/// Connect to broker
/// </summary>
@@ -548,7 +580,9 @@ public byte Connect(string clientId,
string willTopic,
string willMessage,
bool cleanSession,
ushort keepAlivePeriod)
ushort keepAlivePeriod,
byte[] passwordInBytes = null
)
{
// create CONNECT message
MqttMsgConnect connect = new MqttMsgConnect(clientId,
@@ -561,7 +595,8 @@ public byte Connect(string clientId,
willMessage,
cleanSession,
keepAlivePeriod,
(byte)this.ProtocolVersion);
(byte)this.ProtocolVersion,
passwordInBytes);

try
{