예제 #1
0
        public async Task <PostResult> PostMessage(string topic, byte[] data, QoS qos)
        {
            FrameBuilder bld = new FrameBuilder();

            bld.WriteByte((byte)(qos == QoS.BestEffort ? 0x00 : 0x01));
            // ToDo: Generate Frame Number to receive ACK.
            currentPacketId++;
            bld.WriteArray(BitConverter.GetBytes(currentPacketId));
            bld.WriteString(topic);
            bld.WriteMultiByte(data.Length);
            bld.WriteArray(data);

            if (qos == QoS.GuaranteedDelivery)
            {
                if (!syncSendInProcess.WaitOne(3000))
                {
                    return(PostResult.DispatchError);
                }
                waitForPacketId = currentPacketId;
            }

            socket.Send(bld.Build(FrameType.ChannelEvent));

            if (qos == QoS.GuaranteedDelivery)
            {
                var replyReceived = waitQosEvent.WaitOne(3000);

                syncSendInProcess.ReleaseMutex();
                return(replyReceived ? PostResult.Delivered
                                     : PostResult.DeliveryError);
            }

            return(PostResult.Dispatched);
        }
예제 #2
0
        public void CanExtract_FrameBuilderOutput()
        {
            FrameBuilder blder = new FrameBuilder();

            blder.WriteByte(0x44);     //Protocol Version 1
            // empty client id for now
            blder.WriteArray(new byte[16]);
            blder.WriteMultiByte(0);    // no secdata
            blder.WriteMultiByte(0);    // no lastwill channel
            blder.WriteMultiByte(0);    // no lastwill data

            var theFrame = blder.Build(FrameType.Connect);

            FrameExtractor fex = new FrameExtractor();

            Assert.True(fex.TryExtract(theFrame, 23).success == FrameExtractor.ResultState.Ok);
        }
예제 #3
0
        public void Connect_SendsConnectionRequest()
        {
            FrameBuilder blder = new FrameBuilder();

            blder.WriteByte(1);     //Protocol Version 1
            // empty client id for now
            blder.WriteArray(new byte[16]);
            blder.WriteMultiByte(0);    // no secdata
            blder.WriteMultiByte(0);    // no lastwill channel
            blder.WriteMultiByte(0);    // no lastwill data

            var theFrame = blder.Build(FrameType.Connect);

            client.Connect();

            A.CallTo(() => socket.Send(A <byte[]> .That.IsSameSequenceAs(theFrame))).MustHaveHappened();
        }
예제 #4
0
        public void WriteMultiByte_WritesMultiByte()
        {
            builder.WriteMultiByte(331);
            var data    = builder.GetData();
            var decoded = Util.ExtractMultiByte(data, 0).value;

            Assert.Equal(331u, decoded);
        }
예제 #5
0
        public void Connect()
        {
            FrameBuilder builder = new FrameBuilder();

            // Build connection request.
            FrameBuilder blder = new FrameBuilder();

            blder.WriteByte(1);
            // empty client id for now
            blder.WriteArray(new byte[16]);

            blder.WriteMultiByte(0);    // no secdata
            blder.WriteMultiByte(0);    // no lastwill channel
            blder.WriteMultiByte(0);    // no lastwill data

            var theFrame = blder.Build(FrameType.Connect);

            socket.Send(theFrame);
        }
예제 #6
0
        public Task <PostResult> PostMessage(string topic, byte[] data, QoS qos)
        {
            FrameBuilder blder = new FrameBuilder();

            blder.WriteByte((byte)qos);
            // Empty packet id for now!
            blder.WriteArray(new byte[] { 0x00, 0x00, 0x00, 0x00 });
            blder.WriteString(topic);
            blder.WriteMultiByte(data.Length);
            blder.WriteArray(data);
            var theFrame = blder.Build(FrameType.Publish);

            socket.Send(theFrame);
            return(new Task <PostResult>(() => PostResult.Dispatched));
        }
예제 #7
0
        public void UnsubscribeFrame_CallsSubscriptionManager()
        {
            FrameBuilder fb    = new FrameBuilder();
            var          bytes = Encoding.UTF8.GetBytes("Test");

            fb.WriteMultiByte(bytes.Length);
            fb.WriteArray(bytes);
            var data = fb.GetData();
            var frm  = new Frame();

            frm.payload   = data;
            frm.frameType = FrameType.Unsub;
            peerMan.FrameReceived(frm, rpFake);
            A.CallTo(() => subManFake.Unsubscribe(rpFake, "Test")).MustHaveHappened();
            A.CallTo(() => rpFake.SendUnsubReply("Test")).MustHaveHappened();
        }
예제 #8
0
        public void PublishFrame_Qos_GuaranteedDelivery_SendsFailureNotice_IfNooneIsListening()
        {
            FrameBuilder fb = new FrameBuilder();

            fb.WriteByte((byte)QoS.GuaranteedDelivery);
            fb.WriteArray(BitConverter.GetBytes((UInt32)234567));
            fb.WriteString("Test");
            fb.WriteMultiByte(3);
            fb.WriteArray(new byte[] { 0xAA, 0xBB, 0xCC });
            var data = fb.GetData();
            var frm  = new Frame();

            frm.payload   = data;
            frm.frameType = FrameType.Publish;
            peerMan.FrameReceived(frm, rpFake);
            A.CallTo(() => rpFake.SendPublishReply(234567, Core.StatusCodes.PublishStatus.NakDelivery)).MustHaveHappened();
        }
예제 #9
0
        public void PublishFrame_CallsSubscriptionManager()
        {
            FrameBuilder fb = new FrameBuilder();

            fb.WriteByte((byte)QoS.BestEffort);
            fb.WriteArray(new byte[] { 0xCC, 0xDD, 0xEE, 0xFF });
            fb.WriteString("Test");
            fb.WriteMultiByte(3);
            fb.WriteArray(new byte[] { 0xAA, 0xBB, 0xCC });
            var data = fb.GetData();
            var frm  = new Frame();

            frm.payload   = data;
            frm.frameType = FrameType.Publish;
            peerMan.FrameReceived(frm, rpFake);
            A.CallTo(() => subManFake.DispatchMessage("Test", A <byte[]> .That.IsSameSequenceAs(new byte[] { 0xAA, 0xBB, 0xCC }), A <QoS> .Ignored)).MustHaveHappened();
        }
예제 #10
0
        public void PublishFrame_Qos_GuaranteedDispatch_SendsReply()
        {
            FrameBuilder fb = new FrameBuilder();

            fb.WriteByte((byte)QoS.GuaranteedDispatch);
            fb.WriteArray(BitConverter.GetBytes((UInt32)123456));
            fb.WriteString("Test");
            fb.WriteMultiByte(3);
            fb.WriteArray(new byte[] { 0xAA, 0xBB, 0xCC });
            var data = fb.GetData();
            var frm  = new Frame();

            frm.payload   = data;
            frm.frameType = FrameType.Publish;
            A.CallTo(() => subManFake.DispatchMessage(A <string> .Ignored, A <byte[]> .Ignored, A <QoS> .Ignored)).Returns(PostResult.Dispatched);
            peerMan.FrameReceived(frm, rpFake);
            A.CallTo(() => rpFake.SendPublishReply(123456, Core.StatusCodes.PublishStatus.Ack)).MustHaveHappened();
        }