Пример #1
0
 public void OnContentHeader(Content content_header)
 {
     if (headerContent != content_header)
     {
         headerContent = content_header;
         lastPacket    = headerContent;
         contentPacketQueue.Enqueue(new Packet(Packet.ContentType.Header, content_header));
     }
 }
Пример #2
0
            public void OnChannelInfo(ChannelInfo channel_info)
            {
                var info = new AtomCollection(channel_info.Extra);

                info.SetChanInfoType("TS");
                info.SetChanInfoStreamType("video/mp2t");
                info.SetChanInfoStreamExt(".ts");
                msgQueue.Enqueue(new ContentMessage {
                    Type = ContentMessage.MessageType.ChannelInfo, ChannelInfo = new ChannelInfo(info)
                });
            }
Пример #3
0
 public void OnContent(Content content)
 {
     lastContent = content;
     if (process == null)
     {
         StartProcess();
     }
     if (writable)
     {
         pipePackets.Enqueue(content.Data);
     }
 }
        public async Task ProcessServerPackagesSequence()
        {
            var port = this.GetPortNumber(PacketServerPort);

            var serializer = new BinarySerializer(BinarySerializationMethod.UnsafeSerialization);
            var queue      = new WaitableQueue <Packet>();

            using (var packetServer = new PacketServer(IPAddress.Any, port))
            {
                packetServer.OnNewPacketReceived += (sender, value) => queue.Enqueue(value);
                packetServer.Start();

                using (var client = new TcpClient("localhost", port))
                    using (var stream = client.GetStream())
                    {
                        var packetSequence = PacketFixture.GetPacketSequence(PacketSequenceLength);

                        foreach (var packet in packetSequence)
                        {
                            await serializer.Serialize(packet, stream).WithTimeOut(Timeout);
                        }

                        foreach (var packet in packetSequence)
                        {
                            var answer = await queue.Dequeue(Timeout);

                            Assert.AreEqual <PacketEx>(packet, answer);
                        }
                    }
            }
        }
        public async Task ProcessDuplexExchange()
        {
            var port = this.GetPortNumber(PacketServerPort);

            var serializer = new BinarySerializer(BinarySerializationMethod.UnsafeSerialization);
            var queue      = new WaitableQueue <Packet>();

            using (var packetServer = new PacketServer(IPAddress.Any, port))
            {
                packetServer.OnNewPacketReceived += (sender, value) => queue.Enqueue(value);
                packetServer.Start();

                using (var client = new TcpClient("localhost", port))
                    using (var stream = client.GetStream())
                    {
                        var serverSequence = PacketFixture.GetPacketSequence(PacketSequenceLength);
                        var clientSequence = PacketFixture.GetPacketSequence(PacketSequenceLength);

                        foreach (var(serverPacket, clientPacket) in serverSequence.JoinByIndex(clientSequence))
                        {
                            await Task.WhenAll(packetServer.SendPacket(serverPacket),
                                               serializer.Serialize(clientPacket, stream));

                            var results = await Task.WhenAll(queue.Dequeue(Timeout), serializer.Deserialize <Packet>(stream).WithTimeOut(Timeout));

                            Assert.AreEqual <PacketEx>(clientPacket, results[0]);
                            Assert.AreEqual <PacketEx>(serverPacket, results[1]);
                        }
                    }
            }
        }
Пример #6
0
        public async Task ConcurrentAccess()
        {
            var queue = new WaitableQueue <int>();

            var sequence = new int[128];

            for (var i = 0; i < sequence.Length; ++i)
            {
                sequence[i] = i;
            }

            Parallel.ForEach(sequence, value => queue.Enqueue(value));

            var dequeued = new List <int>();

            try
            {
                while (true)
                {
                    dequeued.Add(await queue.Dequeue(TimeSpan.FromSeconds(1)));
                }
            }
            catch (TimeoutException)
            {
                //ok
            }

            dequeued.Sort();

            Assert.AreEqual(sequence.Length, dequeued.Count);
            Assert.IsTrue(dequeued.SequenceEqual(sequence));
        }
Пример #7
0
 private void Enqueue(ChannelMessage msg)
 {
     if (channelStopped.IsCancellationRequested)
     {
         return;
     }
     if (!queue.TryPeek(out var nxtMsg) || (msg.Timestamp - nxtMsg.Timestamp).TotalMilliseconds <= 5000)
     {
         queue.Enqueue(msg);
     }
Пример #8
0
        public async Task OneElementInQueue()
        {
            var queue = new WaitableQueue <int>();

            var testElement = 1;

            queue.Enqueue(testElement);

            var result = await queue.Dequeue();

            Assert.AreEqual(testElement, result);
        }
Пример #9
0
        public async Task MultipleElementInQueue()
        {
            var queue = new WaitableQueue <int>();

            for (var i = 0; i < 10; ++i)
            {
                queue.Enqueue(i);
            }

            for (var i = 0; i < 10; ++i)
            {
                var result = await queue.Dequeue();

                Assert.AreEqual(i, result);
            }
        }
Пример #10
0
        public async Task ProcessDuplexExchangeConcurrent()
        {
            var port = this.GetPortNumber(PacketServerPort);

            var serializer = new BinarySerializer(BinarySerializationMethod.UnsafeSerialization);
            var queue      = new WaitableQueue <Packet>();

            using (var packetServer = new PacketServer(IPAddress.Any, port))
            {
                packetServer.OnNewPacketReceived += (sender, value) => queue.Enqueue(value);
                packetServer.Start();

                using (var client = new TcpClient("localhost", port))
                    using (var stream = client.GetStream())
                    {
                        var  serverSequence = PacketFixture.GetPacketSequence(PacketSequenceLength);
                        Task serverTask     = null;
                        foreach (var packet in serverSequence)
                        {
                            var task = packetServer.SendPacket(packet);
                            serverTask = serverTask?.ContinueWith(t => task).Unwrap() ?? task;
                        }

                        var clientSequence = PacketFixture.GetPacketSequence(PacketSequenceLength);
                        var clientTask     = Task.CompletedTask;
                        foreach (var packet in clientSequence)
                        {
                            // ReSharper disable once AccessToDisposedClosure
                            clientTask = clientTask.ContinueWith(t => serializer.Serialize(packet, stream)).Unwrap();
                        }

                        await Task.WhenAll(serverTask, clientTask).WithTimeOut(Timeout);

                        foreach (var(serverPacket, clientPacket) in serverSequence.JoinByIndex(clientSequence))
                        {
                            var results = await Task.WhenAll(queue.Dequeue(Timeout), serializer.Deserialize <Packet>(stream).WithTimeOut(Timeout));

                            Assert.AreEqual <PacketEx>(clientPacket, results[0]);
                            Assert.AreEqual <PacketEx>(serverPacket, results[1]);
                        }
                    }
            }
        }
Пример #11
0
        public async Task CheckConcurrentSequenceOrder()
        {
            var queue = new WaitableQueue <int>();

            var sequence = new int[128];

            for (var i = 0; i < sequence.Length; ++i)
            {
                sequence[i] = i;
            }

            var task = Task.Delay(0);

            for (int i = 0; i < sequence.Length; i++)
            {
                var queueItem = i;
                task = task.ContinueWith(t => queue.Enqueue(queueItem));
            }

            await task;

            var dequeued = new List <int>();

            try
            {
                while (true)
                {
                    dequeued.Add(await queue.Dequeue(TimeSpan.FromSeconds(1)));
                }
            }
            catch (TimeoutException)
            {
                //ok
            }

            Assert.AreEqual(sequence.Length, dequeued.Count);
            Assert.IsTrue(dequeued.SequenceEqual(sequence));
        }
Пример #12
0
 static public void AddNotification(string data)
 {
     notifications.Enqueue(data);
 }
Пример #13
0
 static public void AddCommand(string data)
 {
     commands.Enqueue(data);
 }
Пример #14
0
 public void OnStopped(StopReason reason)
 {
     queue.Enqueue(new ChannelMessage {
         Type = ChannelMessage.MessageType.ChannelStopped, Content = null, Data = null
     });
 }