using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Paho.MqttDotnet
{
///
/// 表示mqtt连接选项
///
public class ConnectOption
{
///
/// 获取或设置心跳检测时间隔(s)
///
public int KeepAliveInterval { get; set; }
///
/// 获取或设置是否清除会话
///
public bool CleanSession { get; set; }
///
/// 获取或设置消息最大飞行窗口
///
public int MaxInflight { get; set; }
///
/// 获取或设置遗属
///
public MqttWill Will { get; set; }
///
/// 获取或设置账号
///
public string Username { get; set; }
///
/// 获取或设置密码
///
public string Password { get; set; }
///
/// 获取或设置连接超时时间(s)
///
public int ConnectTimeout { get; set; }
///
/// 获取或设置超时重连的时间间隔(s)
///
public int RetryInterval { get; set; }
///
/// 获取或设置当断开后是否自动重连
///
public bool AutomaticReconnect { get; set; }
///
/// 获取或设置最小重连时间间隔(s)
///
public int MinRetryInterval { get; set; }
///
/// 获取或设置最大重连时间间隔(s)
///
public int MaxRetryInterval { get; set; }
///
/// mqtt连接选项
///
public ConnectOption()
{
this.CleanSession = true;
this.KeepAliveInterval = 60;
this.MaxInflight = 10;
this.ConnectTimeout = 30;
this.MinRetryInterval = 1;
this.MaxRetryInterval = 60;
}
///
/// 转换为结构体
///
///
internal MQTTAsync_connectOptions ToStruct()
{
var opt = new MQTTAsync_connectOptions();
opt.Init();
opt.keepAliveInterval = this.KeepAliveInterval;
opt.cleansession = this.CleanSession ? 1 : 0;
opt.maxInflight = this.MaxInflight;
opt.username = this.Username.ToUnmanagedPointer();
opt.password = this.Password.ToUnmanagedPointer();
opt.connectTimeout = this.ConnectTimeout;
opt.retryInterval = this.RetryInterval;
opt.automaticReconnect = this.AutomaticReconnect ? 1 : 0;
opt.minRetryInterval = this.MinRetryInterval;
opt.maxRetryInterval = this.MaxRetryInterval;
if (this.Will != null)
{
var will = this.Will.ToStruct();
var willPtr = Marshal.AllocHGlobal(Marshal.SizeOf(will));
Marshal.StructureToPtr(will, willPtr, true);
opt.will = willPtr;
}
return opt;
}
}
}