Exemplo n.º 1
0
        public void automatically_allocate_larger_payload_buffer()
        {
            var unix = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            var now = new DateTime(2014, 10, 13, 13, 0, 0, DateTimeKind.Local).ToUniversalTime();
            var seconds = now.Subtract(unix).TotalSeconds;

            byte[] length = BitConverter.GetBytes(4096);
            if (BitConverter.IsLittleEndian)
                Array.Reverse(length);
            var buffer = new byte[4096 + 12];
            buffer[0] = (byte)FrameFlags.LargeFrame; //flags,
            buffer[1] = 0; //sequence
            buffer[2] = 2;
            buffer[3] = 2;//destination length
            buffer[4] = (byte)'M';
            buffer[5] = (byte)'Q';
            buffer[6] = 0; // filter length
            buffer[7] = 0; // filter length
            buffer[8] = length[0];//payload length
            buffer[9] = length[1];
            buffer[10] = length[2];
            buffer[11] = length[3];
            var offset = 0;
            var count = buffer.Length;

            var sut = new MessageFrame();
            var actual = sut.Read(buffer, ref offset, ref count);

            actual.Should().Be(true);
            sut.PayloadBuffer.Array.Length.Should().BeGreaterThan(4096);
        }
        public void Send(MessageFrame frame)
        {
            if (frame == null) throw new ArgumentNullException("frame");

            //if (!_authenticationEvent.WaitOne(100000))
            //    throw new InvalidOperationException("Handshake was not completed in a reasonable time.");

            if (_ackReceiver != null)
            {
                // we can allow all requests to send messages
                // if
                if (_messageStore != null)
                {
                    _messageStore.Enqueue(frame);
                    if (_ackReceiver.CanSend(frame))
                        _ackReceiver.Send(frame);
                }
                else
                {
                    if (!_ackReceiver.CanSend(frame))
                        throw new InvalidOperationException("Cannot enqueue more messages that the given threshold.");
                    _ackReceiver.Send(frame);
                }
            }
            else
                DeliverMessage(frame);
        }
Exemplo n.º 3
0
        private void OnFrame(ServerClient channel, MessageFrame frame)
        {
            if (frame.SequenceNumber == 65535)
            {
                _batchCounter ++;
                Console.WriteLine(DateTime.UtcNow.ToString("HH:mm:ss.fff") + " " + (_batchCounter*65535) + " messages.");
            }

            if (frame.Payload != null)
            {
                if (frame.Payload.ToString()[0] == 'm')
                {
                    Console.WriteLine(DateTime.UtcNow.ToString("HH:mm:ss.fff") + " done");
                    var buffer = Encoding.ASCII.GetBytes("completed");
                    channel.Send(new MessageFrame(buffer));
                }

            }
            else if (frame.PayloadBuffer.Array[1] == 'm')
            {
                Console.WriteLine(DateTime.UtcNow.ToString("HH:mm:ss.fff") + " done");
                var buffer = Encoding.ASCII.GetBytes("completed");
                channel.Send(new MessageFrame(buffer));
            }
        }
Exemplo n.º 4
0
        public bool AckFrame(MessageFrame frame)
        {
            if (_lastAckTime == DateTime.MinValue)
                _lastAckTime = DateTime.UtcNow;

            //send acks directly for frames that we've already acked.
            if (ShouldReAck(frame))
            {
                _lastAckTime = DateTime.UtcNow;
                _lastAckedSequenceNumber = _lastReceivedSequenceNumber;
                _numberOfFramesToAck = 0;
                _connection.Send(new AckFrame(_extensionId, (ushort) _lastReceivedSequenceNumber));
                return false;
            }

            lock (_syncLock)
            {
                _lastReceivedSequenceNumber = frame.SequenceNumber;
                ++_amountNotAcked;
                if (_amountNotAcked < Threshold)
                {
                    return true;
                }

                _lastAckTime = DateTime.UtcNow;
                _lastAckedSequenceNumber = frame.SequenceNumber;
                _connection.Send(new AckFrame(_extensionId, frame.SequenceNumber));
                _amountNotAcked = 0;
            }

            return true;
        }
Exemplo n.º 5
0
        private static void OnFrame(ServerClient channel, MessageFrame frame)
        {
            var msg = Encoding.ASCII.GetString(frame.PayloadBuffer.Array, frame.PayloadBuffer.Offset,
                frame.PayloadBuffer.Count);

            Console.WriteLine("Received '" + msg + "' from " + channel.RemoteEndPoint);
        }
 public void Serialize(MessageFrame frame)
 {
     var str = JSON.ToJSON(frame.Payload);
     if (frame.PayloadBuffer.Count >= Encoding.UTF8.GetByteCount(str))
     {
         var buf = frame.PayloadBuffer;
         var count = Encoding.UTF8.GetBytes(str, 0, str.Length, buf.Array, buf.Offset);
         frame.PayloadBuffer = new ArraySegment<byte>(buf.Array, buf.Offset, count);
     }
     else
     {
         var buf = Encoding.UTF8.GetBytes(str);
         frame.PayloadBuffer = new ArraySegment<byte>(buf, 0, buf.Length);
     }
 }
        private void OnFrame(ServerClient arg1, MessageFrame arg2)
        {
            var destination = arg2.Destination;
            IMessagingService service;
            if (!_items.TryGetValue(destination, out service))
            {
                if (destination.StartsWith("topic/"))
                {
                    if (!AllowDynamicTopics)
                    {
                        var errorFrame = new ErrorFrame("Topics must exist before messages are sent. Cannot create " + destination);
                        //arg1.Send(errorFrame);
                    }
                }

            }
        }
Exemplo n.º 8
0
        public void code_decode_test()
        {
            var bufMgr = new BufferManager(65535, 1);
            var context = new WriterContext(bufMgr);
            var buf = bufMgr.Dequeue();
            bufMgr.Enqueue(buf);

            var sut = new MessageFrame();
            sut.SequenceNumber = 22;
            var payload = Encoding.UTF8.GetBytes("Hello world");
            sut.PayloadBuffer = new ArraySegment<byte>(payload, 0, payload.Length);
            sut.Write(context);

            var pos = 0;
            var len = context.GetPackets().First().Count;
            var frame2 = new MessageFrame();
            frame2.Read(buf.Array, ref pos, ref len);
            frame2.SequenceNumber.Should().Be(22);
            Encoding.ASCII.GetString(sut.PayloadBuffer.Array, 0, sut.PayloadBuffer.Count).Should().Be("Hello world");
        }
Exemplo n.º 9
0
        private void OnTimeSync(ServerClient channel, MessageFrame frame)
        {
            if (_timeSyncCompleted)
            {
                OnFrame(channel, frame);
                return;
            }

            var data = Encoding.ASCII.GetString(frame.PayloadBuffer.Array, frame.PayloadBuffer.Offset,
                frame.PayloadBuffer.Count);

            var parts = data.Split(';');
            var time = DateTime.Parse(parts[0]);

            var buffer =
                Encoding.ASCII.GetBytes((int)DateTime.UtcNow.Subtract(time).TotalMilliseconds + ";" +
                                        DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.ffff"));
            channel.Send(new MessageFrame(buffer));

            //completed time sync
            if (parts.Length == 2)
            {
                Console.WriteLine("Time synchronization completed");
                _timeSyncCompleted = true;
            }
        }
Exemplo n.º 10
0
        public void deserualize_Simplistic_filter()
        {
            var filter = Encoding.ASCII.GetBytes("last_name<kalle");
            var buffer = new byte[]
            {
                0, //flags,
                0, 2, // sequence number,
                2, //destination length,
                (byte)'M', (byte)'Q',
                0, 15, // filter length
                (byte)'l', (byte)'a', (byte)'s', (byte)'t', (byte)'_', (byte)'n', (byte)'a', (byte)'m', (byte)'e', //filter part 1
                (byte)':', (byte)'k', (byte)'a', (byte)'l', (byte)'l', (byte)'e', //filter part 2
                6, //payload length
                (byte) 'm', (byte) 'o', (byte) 't', (byte) 'h', (byte)'e', (byte)'r' //payload
            };
            var offset = 0;
            var count = buffer.Length;

            var sut = new MessageFrame();
            var actual = sut.Read(buffer, ref offset, ref count);

            actual.Should().Be(true);
            sut.Destination.Should().Be("MQ");
            sut.Properties["last_name"].Should().Be("kalle");
        }
Exemplo n.º 11
0
        /// <summary>
        ///     Message have been queued etc and are now OK for delivery
        /// </summary>
        /// <param name="frame"></param>
        private void DeliverMessage(MessageFrame frame)
        {
            //_extensionService.Prepare(frame, _connection);
            if (frame.Payload != null && _payloadDotNetType != frame.Payload.GetType())
            {
                _payloadDotNetType = frame.Payload.GetType();
                var dotNetFrame = _extensionService.CreateFrame("dotnet", _payloadDotNetType);
                _connection.SendMore(dotNetFrame);
            }
            if (_payloadSerializer != null)
            {
                _payloadSerializer.Serialize(frame);
            }

            _connection.Send(frame);
        }
Exemplo n.º 12
0
        private void Benchmark(SharpMessagingClient client)
        {
            var buffer = MessageSize < 12
                ? Encoding.ASCII.GetBytes("1".PadLeft(MessageSize, '0'))
                : Encoding.ASCII.GetBytes("\"hello world ".PadRight(9999) + "\"");
            var payload = new ArraySegment<byte>(buffer);

            Console.WriteLine(DateTime.UtcNow.ToString("HH:mm:ss.fff") + " Starting");
            for (var i = 1; i < MessageCount; i++)
            {
                var frame = new MessageFrame
                {
                    PayloadBuffer = payload
                };
                client.Send(frame);
            }

            // packet to mark the end.
            client.Send(new MessageFrame
            {
                PayloadBuffer =
                    new ArraySegment<byte>(new[]
                    {(byte) '"', (byte) 'm', (byte) 'o', (byte) 't', (byte) 'h', (byte) 'e', (byte) 'r', (byte) '"'})
            });
        }
        private void DeliverMessage(MessageFrame frame)
        {
            frame.SequenceNumber = ++_sequenceCounter;
            if (_sequenceCounter == ushort.MaxValue)
                _sequenceCounter = 0;

            if (frame.Payload != null && _payloadSerializer != null)
            {
                _payloadSerializer.Serialize(frame);
                if (_dotNetExtension != null && _outboundDotNetType != frame.Payload.GetType())
                {
                    _outboundDotNetType = frame.Payload.GetType();
                    var dotNetFrame = _extensionService.CreateFrame("dotnet", _outboundDotNetType);
                    _connection.SendMore(dotNetFrame);
                }
            }
            _connection.Send(frame);
        }
Exemplo n.º 14
0
        public void got_no_filter()
        {
            var buffer = new byte[]
            {
                0, //flags,
                0, 2, // sequence number,
                2, //destination length,
                (byte)'M', (byte)'Q',
                0, 0, // filter length
                6, //payload length
                (byte) 'm', (byte) 'o', (byte) 't', (byte) 'h', (byte)'e', (byte)'r' //payload
            };
            var offset = 0;
            var count = buffer.Length;

            var sut = new MessageFrame();
            var actual = sut.Read(buffer, ref offset, ref count);

            sut.Destination.Should().Be("MQ");
            sut.Properties.Should().BeEmpty();
        }
Exemplo n.º 15
0
        public void got_destination()
        {
            var buffer = new byte[]
            {
                0, //flags,
                0, 2, // sequence number,
                2, //destination length,
                (byte)'M', (byte)'Q',
                0, 0, // filter length
                6, //payload length
                (byte) 'm', (byte) 'o', (byte) 't', (byte) 'h', (byte)'e', (byte)'r' //payload
            };
            var offset = 0;
            var count = buffer.Length;

            var sut = new MessageFrame();
            var actual = sut.Read(buffer, ref offset, ref count);

            sut.Destination.Should().Be("MQ");
            Encoding.ASCII.GetString(sut.PayloadBuffer.Array, 0, sut.PayloadBuffer.Count).Should().Be("mother");
        }
Exemplo n.º 16
0
        public void doing_a_partial_payload_write_properly()
        {
            var bufMgr = new BufferManager(5010, 1);
            var context = new WriterContext(bufMgr);
            var buf1 = bufMgr.Dequeue();
            bufMgr.Enqueue(buf1);
            var sut = new MessageFrame();
            sut.SequenceNumber = 22;
            var payload = Encoding.ASCII.GetBytes("Hello world".PadRight(10000));
            sut.PayloadBuffer = new ArraySegment<byte>(payload, 0, payload.Length);

            var actual1 = sut.Write(context);
            var pos = 0;
            var len = context.GetPackets().First().Count;
            var frame2 = new MessageFrame();
            frame2.Read(buf1.Array, ref pos, ref len);

            var actual2 = sut.Write(context);
            pos = 0;
            len = context.GetPackets().Last().Count;
            frame2.Read(buf1.Array, ref pos, ref len);

            frame2.SequenceNumber.Should().Be(22);
            frame2.IsFlaggedAsSmall.Should().BeFalse();
            Encoding.ASCII.GetString(sut.PayloadBuffer.Array, 0, sut.PayloadBuffer.Count).TrimEnd(' ').Should().Be("Hello world");
            actual1.Should().BeFalse();
            actual2.Should().BeTrue();
        }
Exemplo n.º 17
0
        public bool ShouldReAck(MessageFrame frame)
        {
            if (frame.SequenceNumber >= _lastAckedSequenceNumber)
                return false;

            // regular sequence and within the last sequence.
            if (_lastAckedSequenceNumber - frame.SequenceNumber < Threshold)
                return true;

            // wrapped sequence (i.e. wrapped to zero after ushort.MaxValue)
            var span = (ushort.MaxValue - _lastAckedSequenceNumber) + frame.SequenceNumber;
            if (span > Threshold)
                return true;

            return false;
        }
 public void AckFrame(MessageFrame frame)
 {
     var ack = new AckFrame(_ackExtensionId, frame.SequenceNumber);
     _connection.Send(ack);
 }
Exemplo n.º 19
0
        private void OnTimingFrame(MessageFrame frame)
        {
            var data = Encoding.ASCII.GetString(frame.PayloadBuffer.Array, frame.PayloadBuffer.Offset,
                frame.PayloadBuffer.Count);

            var parts = data.Split(';');
            if (data == "completed")
            {
                var clockSyncAndNetworkDelay = TimeSpan.FromMilliseconds(_timings.Average());
                var elapsedTime = DateTime.UtcNow.Subtract(Started).Subtract(clockSyncAndNetworkDelay);
                var mbits = (MessageCount*MessageSize*8L/elapsedTime.TotalSeconds)/1000000;
                Console.WriteLine(DateTime.UtcNow.ToString("HH:mm:ss.fff") + " completed.");
                Console.WriteLine("Duration:      {0} ms", elapsedTime.TotalMilliseconds);
                Console.WriteLine("Message Size:  {0} bytes", MessageSize.ToString("N0"));
                Console.WriteLine("Message Count: {0}", MessageCount.ToString("N0"));
                Console.WriteLine("Total size:    {0} bytes", (MessageSize*MessageCount).ToString("N0"));
                Console.WriteLine("Msgs/sec:      {0}", (MessageCount/elapsedTime.TotalSeconds).ToString("N0"));
                Console.WriteLine("Throughput:    {0} Mbit/s", mbits.ToString("N1"));

                if (!File.Exists("result.csv"))
                {
                    File.AppendAllText("result.csv", "sep=,\r\n");
                    File.AppendAllText("result.csv", @"""Message size (bytes)"",""Message count"",""Transfer size (bytes)"",""Msgs/Ack"",""Duration (ms)"",""Msgs/sec"",""Troughput (Mbit/s)"""+"\r\n");
                }

                File.AppendAllText(@"result.csv",
                    string.Format("{0},{1},{2},{3},{4},{5},{6}\r\n",
                        MessageSize.ToString(CultureInfo.InvariantCulture),
                        MessageCount.ToString(CultureInfo.InvariantCulture),
                        (MessageSize * MessageCount).ToString(CultureInfo.InvariantCulture),
                        MessagesPerAck,
                        ((long)elapsedTime.TotalMilliseconds).ToString(CultureInfo.InvariantCulture),
                        ((long)(MessageCount/elapsedTime.TotalSeconds)).ToString(CultureInfo.InvariantCulture),
                        mbits.ToString(CultureInfo.InvariantCulture)));
                _completedEvent.Set();

                return;
            }

            var timing = int.Parse(parts[0]);
            _timings.Add(timing);
            var date = DateTime.Parse(parts[1], CultureInfo.InvariantCulture);
            var difference = DateTime.UtcNow.Subtract(date).TotalMilliseconds;
            _timings.Add((int) difference);

            if (_timings.Count == 10)
            {
                var buffer = Encoding.ASCII.GetBytes(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.ffff") + ";end");
                _client.Send(new MessageFrame(buffer));
            }
            else if (_timings.Count == 12)
            {
                Started = DateTime.UtcNow;
                Benchmark(_client);
            }
            else
            {
                var buffer = Encoding.ASCII.GetBytes(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.ffff"));
                _client.Send(new MessageFrame(buffer));
            }
        }
        private void OnMessageFrame(MessageFrame frame)
        {
            if (_state != ClientState.Ready)
                throw new Exception("Handshake not completed, should not have received a message frame.");

            // using acks and the specified frame have already been recieved.
            if (_ackSender != null && _ackSender.ShouldReAck(frame))
            {
                _ackSender.AckFrame(frame);
                return;
            }

            if (_payloadSerializer != null)
            {
                if (_inboundDotNetType != null)
                {
                    frame.Payload = frame.IsFlaggedAsSmall
                        ? _payloadSerializer.Deserialize(_inboundDotNetType, frame.PayloadBuffer.Array,
                            frame.PayloadBuffer.Offset,
                            frame.PayloadBuffer.Count)
                        : _payloadSerializer.Deserialize(_inboundDotNetType, frame.PayloadStream);
                }
                else
                {
                    frame.Payload = frame.IsFlaggedAsSmall
                        ? _payloadSerializer.Deserialize(frame.PayloadBuffer.Array,
                            frame.PayloadBuffer.Offset,
                            frame.PayloadBuffer.Count)
                        : _payloadSerializer.Deserialize(frame.PayloadStream);
                }
            }

            FrameReceived(frame);

            //Do it after the event trigger, so that any exception
            //doesn't ack the frame (as the client did not process it correctly).
            if (_ackSender != null)
                _ackSender.AckFrame(frame);
        }
Exemplo n.º 21
0
        public void got_no_place_to_go()
        {
            var buffer = new byte[]
            {
                0, //flags,
                0, 2, // sequence number,
                0, //destination length
                0, 0, // filter length
                6, //payload length
                (byte) 'm', (byte) 'o', (byte) 't', (byte) 'h', (byte)'e', (byte)'r' //payload
            };
            var offset = 0;
            var count = buffer.Length;

            var sut = new MessageFrame();
            var actual = sut.Read(buffer, ref offset, ref count);

            actual.Should().Be(true);
            sut.Flags.Should().Be(FrameFlags.None);
            sut.SequenceNumber.Should().Be(2);
            sut.Destination.Should().BeEmpty();
            Encoding.ASCII.GetString(sut.PayloadBuffer.Array, 0, sut.PayloadBuffer.Count).Should().Be("mother");
        }
Exemplo n.º 22
0
        public void serialize_Simplistic_filter()
        {
            var bufferManager = new BufferManager(8192, 1);
            var context = new WriterContext(bufferManager);
            var buffer = bufferManager.Dequeue();
            bufferManager.Enqueue(buffer);
            var expected = new byte[]
            {
                0, //flags,
                0, 2, // sequence number,
                2, //destination length,
                (byte)'M', (byte)'Q',
                0, 16, // filter length
                (byte)'l', (byte)'a', (byte)'s', (byte)'t', (byte)'_', (byte)'n', (byte)'a', (byte)'m', (byte)'e', //filter part 1
                (byte)':', (byte)'k', (byte)'a', (byte)'l', (byte)'l', (byte)'e', (byte)';', //filter part 2
                6, //payload length
                (byte) 'm', (byte) 'o', (byte) 't', (byte) 'h', (byte)'e', (byte)'r' //payload
            };

            var sut = new MessageFrame();
            sut.SequenceNumber = 2;
            sut.Destination = "MQ";
            sut.Properties.Add("last_name", "kalle");
            var payload = Encoding.ASCII.GetBytes("mother");
            sut.PayloadBuffer = new ArraySegment<byte>(payload, 0, payload.Length);
            var actual = sut.Write(context);

            for (int i = 0; i < expected.Length; i++)
            {
                if (expected[i] != buffer.Array[i])
                    throw new InvalidOperationException("Differs at " + i);
            }
        }
Exemplo n.º 23
0
        public void Send(MessageFrame frame)
        {
            frame.SequenceNumber = ++_sequenceCounter;
            if (_sequenceCounter == ushort.MaxValue)
                _sequenceCounter = 0;

            if (_ackReceiver != null)
                _ackReceiver.Send(frame);
            else
                DeliverMessage(frame);
        }