Esempio 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);
        }
Esempio n. 2
0
        public virtual void SendConnectionReply(ConnectionAttemptStatus status)
        {
            FrameBuilder theBuilder = new FrameBuilder();

            theBuilder.WriteByte((byte)status);

            socket.Send(theBuilder.Build(FrameType.ConReply));
        }
Esempio n. 3
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));
        }
Esempio n. 4
0
        public virtual void SendSubscribeReply(string channel, SubscribeStatus status)
        {
            FrameBuilder theBuilder = new FrameBuilder();

            theBuilder.WriteString(channel);
            theBuilder.WriteByte((byte)status);

            socket.Send(theBuilder.Build(FrameType.SubReply));
        }
Esempio n. 5
0
        public IFrame ToFrame()
        {
            var builder = new FrameBuilder();

            builder.WriteByte((byte)MessageType);
            builder.WriteString(ClientHostAddress.IpAddress);
            builder.WriteInt(ClientHostAddress.Port);
            WriteInternal(builder);

            return(builder.ToFrame());
        }
Esempio n. 6
0
        public void SendConnectReply_ConRefused_SendsConnectionReply()
        {
            FrameBuilder bld = new FrameBuilder();

            bld.WriteByte((byte)ConnectionAttemptStatus.Refused);
            var expectedFrame = bld.Build(FrameType.ConReply);

            p.SendConnectionReply(ConnectionAttemptStatus.Refused);

            A.CallTo(() => socket.Send(A <byte[]> .That.IsSameSequenceAs(expectedFrame))).MustHaveHappened();
        }
Esempio n. 7
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();
        }
Esempio n. 8
0
        public void SendSubscribeReply_SendsSubscribeReply()
        {
            FrameBuilder bld = new FrameBuilder();

            bld.WriteString("Test");
            bld.WriteByte((byte)SubscribeStatus.Success);
            var expectedFrame = bld.Build(FrameType.SubReply);

            p.SendSubscribeReply("Test", SubscribeStatus.Success);

            A.CallTo(() => socket.Send(A <byte[]> .That.IsSameSequenceAs(expectedFrame))).MustHaveHappened();
        }
Esempio 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();
        }
Esempio n. 10
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));
        }
Esempio 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);
        }
Esempio n. 12
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();
        }
Esempio n. 13
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();
        }
Esempio n. 14
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();
        }
Esempio n. 15
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();
        }
Esempio n. 16
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();
        }
Esempio n. 17
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);
        }
Esempio n. 18
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();
        }
Esempio n. 19
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);
        }
Esempio n. 20
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));
        }
Esempio n. 21
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();
        }
Esempio n. 22
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));
            }
        }