private void InitTransport(MqttConnectionArgs args) { if (args.Secure) { Transport = new TlsTransport(args.Hostname, args.Port); } else { Transport = new TcpTransport(args.Hostname, args.Port); } Transport.Version = args.Version; Transport.SetTimeouts(args.ReadTimeout, args.WriteTimeout); }
public MqttConnection(MqttConnectionArgs args, IMqttPersistence persistence = null) { if (args.Keepalive.TotalSeconds < 0 || args.Keepalive.TotalSeconds > ushort.MaxValue) { throw new ArgumentException("Keepalive should be between 0 seconds and ushort.MaxValue (18 hours)"); } this.Persistence = persistence ?? new InMemoryPersistence(); this.Keepalive = (int)args.Keepalive.TotalMilliseconds; // converts to milliseconds this.IsPublishing = false; InitTransport(args); Send(MakeConnectMessage(args)); ReceiveConnack(); ResumeOutgoingFlows(); }
public MqttConnection(MqttConnectionArgs args, IMqttPersistence persistence = null, IMqttTransport customTransport = null) { if (args == null) { throw new ArgumentNullException("args"); } if (args.Keepalive.TotalSeconds < 0 || args.Keepalive.TotalSeconds > ushort.MaxValue) { throw new ArgumentException("Keepalive should be between 0 seconds and ushort.MaxValue (18 hours)"); } this.Persistence = persistence ?? new InMemoryPersistence(); this.Keepalive = (int)args.Keepalive.TotalMilliseconds; // converts to milliseconds this.IsPublishing = false; this.ProtocolVersion = args.ProtocolVersion; this.ConnectionArgs = args; this.Transport = customTransport; }
private ConnectPacket MakeConnectMessage(MqttConnectionArgs args) { var conn = new ConnectPacket(); conn.ProtocolVersion = args.Version; conn.ClientId = args.ClientId; conn.Username = args.Username; conn.Password = args.Password; if (args.WillMessage != null) { conn.WillFlag = true; conn.WillTopic = args.WillMessage.Topic; conn.WillMessage = args.WillMessage.Message; conn.WillQosLevel = args.WillMessage.Qos; conn.WillRetain = args.WillMessage.Retain; } conn.CleanSession = args.CleanSession; conn.KeepAlivePeriod = (ushort)args.Keepalive.TotalSeconds; return(conn); }