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(); }
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); }
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); }
// No need for these to be public! public void SendSubscribeRequest(string channel) { FrameBuilder blder = new FrameBuilder(); blder.WriteString(channel); socket.Send(blder.Build(FrameType.Subscribe)); }
public virtual void SendUnsubReply(string channel) { FrameBuilder theBuilder = new FrameBuilder(); theBuilder.WriteString(channel); socket.Send(theBuilder.Build(FrameType.UnsubReply)); }
public void WriteString_WritesLengthAndData() { builder.WriteString("Foo"); var data = builder.GetData(); var decoded = Util.ExtractMultiByte(data, 0).value; Assert.Equal(3u, decoded); }
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(); }
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(); }
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)); }
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)); }
public void Subscribe_SendsSubscriptionRequest() { FrameBuilder blder = new FrameBuilder(); blder.WriteString("Fnord"); var theFrame = blder.Build(FrameType.Subscribe); client.SendSubscribeRequest("Fnord"); A.CallTo(() => socket.Send(A <byte[]> .That.IsSameSequenceAs(theFrame))).MustHaveHappened(); }
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(); }
public IFrame ToFrame() { var builder = new FrameBuilder(); builder.WriteByte((byte)MessageType); builder.WriteString(ClientHostAddress.IpAddress); builder.WriteInt(ClientHostAddress.Port); WriteInternal(builder); return(builder.ToFrame()); }
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(); }
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)); }
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(); }
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(); }
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(); }