コード例 #1
0
        public void ConnectMessageTest()
        {
            ConnectMessage m = new ConnectMessage();
            ConnectMessage d = (ConnectMessage)UdpTrackerMessage.DecodeMessage(m.Encode(), 0, m.ByteLength, MessageType.Request);

            Check(m, MessageType.Request);

            Assert.AreEqual(0, m.Action, "#0");
            Assert.AreEqual(m.Action, d.Action, "#1");
            Assert.AreEqual(m.ConnectionId, d.ConnectionId, "#2");
            Assert.AreEqual(m.TransactionId, d.TransactionId, "#3");
            Assert.IsTrue(Toolbox.ByteMatch(m.Encode(), d.Encode()), "#4");
        }
コード例 #2
0
ファイル: PiraeusMqttClient.cs プロジェクト: lulzzz/piraeus-2
        public async Task <ConnectAckCode> ConnectAsync(string clientId, string username, string password,
                                                        int keepaliveSeconds)
        {
            code = null;

            ConnectMessage msg = new ConnectMessage(clientId, username, password, keepaliveSeconds, true);

            if (!Channel.IsConnected)
            {
                try
                {
                    await Channel.OpenAsync();
                }
                catch (Exception ex)
                {
                    OnChannelError?.Invoke(this, new ChannelErrorEventArgs(Channel.Id, ex));
                    return(ConnectAckCode.ServerUnavailable);
                }

                try
                {
                    Receive(Channel);
                }
                catch (Exception ex)
                {
                    OnChannelError?.Invoke(this, new ChannelErrorEventArgs(Channel.Id, ex));
                    return(ConnectAckCode.ServerUnavailable);
                }
            }

            try
            {
                await Channel.SendAsync(msg.Encode());

                DateTime expiry = DateTime.UtcNow.AddMilliseconds(timeoutMilliseconds);
                while (!code.HasValue)
                {
                    await Task.Delay(10);

                    if (DateTime.UtcNow > expiry)
                    {
                        throw new TimeoutException("MQTT connection timed out.");
                    }
                }

                return(code.Value);
            }
            catch (Exception ex)
            {
                OnChannelError?.Invoke(this, new ChannelErrorEventArgs(Channel.Id, ex));
                return(ConnectAckCode.ServerUnavailable);
            }
        }
コード例 #3
0
        public async Task ConnectAsync(string clientId, string username, string password, int keepaliveSeconds, bool cleanSession)
        {
            code = null;

            ConnectMessage msg = new ConnectMessage(clientId, username, password, keepaliveSeconds, cleanSession);

            if (!channel.IsConnected)
            {
                await channel.OpenAsync();

                Task task = channel.ReceiveAsync();
                await Task.WhenAll(task);
            }

            await channel.SendAsync(msg.Encode());
        }