Exemplo n.º 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);
        }
Exemplo n.º 2
0
        public void WriteArray_WritesArray()
        {
            builder.WriteArray(new byte[] { 0x01, 0x02, 0x03 });

            var data = builder.GetData();

            Assert.True(data.SequenceEqual(new byte[] { 0x01, 0x02, 0x03 }));
        }
Exemplo n.º 3
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));
        }
Exemplo n.º 4
0
        public void SendPublishReply(UInt32 messageId, PublishStatus status)
        {
            FrameBuilder bld = new FrameBuilder();

            bld.WriteArray(BitConverter.GetBytes(messageId));
            bld.WriteByte((byte)status);
            socket.Send(bld.Build(FrameType.PubReply));
        }
Exemplo n.º 5
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();
        }
Exemplo n.º 6
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();
        }
Exemplo n.º 7
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();
        }
Exemplo n.º 8
0
        public void SendPublishReply_SendsMessageToPeer()
        {
            FrameBuilder bld = new FrameBuilder();

            bld.WriteArray(BitConverter.GetBytes((UInt32)112233));
            bld.WriteByte((byte)PublishStatus.Ack);
            var expected = bld.Build(FrameType.PubReply);

            p.SendPublishReply(112233, PublishStatus.Ack);
            A.CallTo(() => socket.Send(A <byte[]> .That.IsSameSequenceAs(expected))).MustHaveHappened();
        }
Exemplo n.º 9
0
        public void Publish_SendsPublishframe()
        {
            FrameBuilder blder = new FrameBuilder();

            blder.WriteByte((byte)QoS.BestEffort);
            blder.WriteArray(new byte[] { 0x00, 0x00, 0x00, 0x00 });
            blder.WriteString("What a great channel!");
            blder.WriteString("This is my data!");
            var theFrame = blder.Build(FrameType.Publish);

            client.PostMessage("What a great channel!", Encoding.UTF8.GetBytes("This is my data!"), QoS.BestEffort);
            A.CallTo(() => socket.Send(A <byte[]> .That.IsSameSequenceAs(theFrame))).MustHaveHappened();
        }
Exemplo n.º 10
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();
        }
Exemplo n.º 11
0
        public void PostMessage_QosGuaranteedDelivery_Fails_If_No_Reply_Arrives()
        {
            FrameBuilder bld = new FrameBuilder();

            bld.WriteByte(0x01);    // Reply Flag
            bld.WriteArray(new byte[] { 0x01, 0x00, 0x00, 0x00 });
            bld.WriteString("Fnord");
            bld.WriteString("I am a payload");

            var expected = bld.Build(FrameType.ChannelEvent);

            var result = p.PostMessage("Fnord", Encoding.UTF8.GetBytes("I am a payload"), QoS.GuaranteedDelivery);

            result.Wait();
            Assert.Equal(PostResult.DeliveryError, result.Result);
        }
Exemplo n.º 12
0
        public void PostMessage_QosGuaranteedDelivery_SetsReplyRequestBit()
        {
            FrameBuilder bld = new FrameBuilder();

            bld.WriteByte(0x01);    // Reply Flag
            bld.WriteArray(new byte[] { 0x01, 0x00, 0x00, 0x00 });
            bld.WriteString("Fnord");
            bld.WriteString("I am a payload");

            var expected = bld.Build(FrameType.ChannelEvent);

            var result = p.PostMessage("Fnord", Encoding.UTF8.GetBytes("I am a payload"), QoS.GuaranteedDelivery);

            result.Wait();

            A.CallTo(() => socket.Send(A <byte[]> .That.IsSameSequenceAs(expected))).MustHaveHappened();
        }
Exemplo n.º 13
0
        public void PostMessage_SendsMessageToPeer()
        {
            FrameBuilder bld = new FrameBuilder();

            bld.WriteByte(0x00);    // No Flags!
            bld.WriteArray(new byte[] { 0x01, 0x00, 0x00, 0x00 });
            bld.WriteString("Fnord");
            bld.WriteString("I am a payload");

            var expected = bld.Build(FrameType.ChannelEvent);

            var result = p.PostMessage("Fnord", Encoding.UTF8.GetBytes("I am a payload"), QoS.BestEffort);

            result.Wait();

            A.CallTo(() => socket.Send(A <byte[]> .That.IsSameSequenceAs(expected))).MustHaveHappened();
        }
Exemplo n.º 14
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();
        }
Exemplo n.º 15
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);
        }
Exemplo n.º 16
0
        public void ChannelEvent_Calls_EventCallback()
        {
            string chan = string.Empty;

            byte[] data = null;
            client.OnMessageReceived += (x, y) => { chan = x; data = y; };
            FrameBuilder blder = new FrameBuilder();

            blder.WriteByte(0x00);
            blder.WriteArray(new byte[] { 0x00, 0x00, 0x12, 0x13 });    // Message ID = 0x12 0x13
            blder.WriteString("YetAnotherChannel");
            blder.WriteString("TestData");
            var theFrame = blder.Build(FrameType.ChannelEvent);

            client.DataReceivedDelegate(theFrame, theFrame.Length);

            Assert.Equal(chan, "YetAnotherChannel");
            Assert.Equal("TestData", Encoding.UTF8.GetString(data));
        }
Exemplo n.º 17
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);
        }
Exemplo n.º 18
0
        public void ChannelEvent_SendsReply_If_Replybit_Is_Set()
        {
            FrameBuilder blder = new FrameBuilder();

            blder.WriteByte(0x01);
            blder.WriteArray(new byte[] { 0x00, 0x00, 0x12, 0x13 });    // Message ID = 0x12 0x13
            blder.WriteString("What a great channel!");
            blder.WriteString("This is my data!");
            var theFrame = blder.Build(FrameType.ChannelEvent);

            client.DataReceivedDelegate(theFrame, theFrame.Length);

            FrameBuilder replyBuilder = new FrameBuilder();

            replyBuilder.WriteArray(new byte[] { 0x00, 0x00, 0x12, 0x13 });
            replyBuilder.WriteByte(0x00);
            var replBuffer = replyBuilder.Build(FrameType.EventAck);

            A.CallTo(() => socket.Send(A <byte[]> .That.IsSameSequenceAs(replBuffer))).MustHaveHappened();
        }
Exemplo n.º 19
0
        private void DispatchChannelEvent(Frame f)
        {
            var flagByte  = f.payload[0];
            var messageId = new byte[4];

            Array.Copy(f.payload, 1, messageId, 0, 4);
            // var clientId = ...
            var channel = Util.ExtractByteArray(f.payload, 5);
            var payload = Util.ExtractByteArray(f.payload, 5 + channel.numBytesUsed);

            OnMessageReceived(System.Text.Encoding.UTF8.GetString(channel.data), payload.data);

            if ((flagByte & 0x01) != 0x00)
            {
                FrameBuilder builder = new FrameBuilder();
                builder.WriteArray(messageId);
                builder.WriteByte((byte)ChannelEventResult.Ok);
                socket.Send(builder.Build(FrameType.EventAck));
            }
        }