public async Task TenSendsTenRecieves()
        {
            int expected = 10;
            int recieves = 0;
            var reciever = new UdpReciever <TestMessage>((value) => { Interlocked.Increment(ref recieves); });

            reciever.Start();
            await Task.Delay(20);

            var sender = new UdpBroadcaster();

            for (int i = 0; i < expected; i++)
            {
                await sender.Send(new TestMessage());
            }

            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));

            while (recieves < 10)
            {
                cts.Token.ThrowIfCancellationRequested();
                await Task.Delay(10, cts.Token);
            }

            reciever.Stop();

            Assert.AreEqual(expected, recieves);
        }
Exemple #2
0
        public SettingsForm(UdpReciever udpReciever, UdpSender udpSender)
        {
            InitializeComponent();

            this.udpReciever = udpReciever;
            this.udpSender   = udpSender;
        }
        public async Task TenSendsTenRecieves()
        {
            int expected = 10;
            int recieves = 0;
            var reciever = new UdpReciever<TestMessage>((value) => { Interlocked.Increment(ref recieves); });
            reciever.Start();
            await Task.Delay(20);

            var sender = new UdpBroadcaster();

            for (int i = 0; i < expected; i++)
            {
                await sender.Send(new TestMessage());
            }

            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
            while (recieves < 10)
            {
                cts.Token.ThrowIfCancellationRequested();
                await Task.Delay(10, cts.Token);
            }

            reciever.Stop();

            Assert.AreEqual(expected, recieves);
        }
        public async Task EntireMessageReceived()
        {
            var expected = new TestMessage()
            {
                Text   = "Some text goes here",
                Bit    = false,
                Number = 1245423534
            };

            TestMessage actual = null;

            var reciever = new UdpReciever <TestMessage>((value) => { actual = value; });

            reciever.Start();
            await Task.Delay(20);

            var sender = new UdpBroadcaster();

            await sender.Send(expected);

            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));

            while (actual == null)
            {
                cts.Token.ThrowIfCancellationRequested();
                await Task.Delay(10, cts.Token);
            }

            reciever.Stop();

            Assert.AreEqual(expected, actual);
        }
Exemple #5
0
        public CarData(UdpReciever udpReciever)
        {
            this.udpReciever = udpReciever;
            this.udpReciever.carDataEventHandler += new UdpRecievedEventHandler(packetRecieved);

            this.canPacketList = new List <CanPacket>();

            // Move this logic to the reciever
            Timer timer = new Timer();

            timer.Interval = (100);
            timer.Tick    += new EventHandler(timerTick);
            timer.Start();
        }
        private void timerTick(object sender, EventArgs e)
        {
            if (!this.isLogging)
            {
                udpReciever.loggerFormEventHandler -= new UdpRecievedEventHandler(packetRecieved);
                timer.Stop();
                ioStreamWriter.Close();
                ioStream.Close();
                udpReciever = null;
                return;
            }

            if (!this.isNewPacket)
            {
                return;
            }

            try
            {
                CanPacket[] canPacketListCopy = new CanPacket[canPacketList.Count];
                String      logEntry;
                this.canPacketList.CopyTo(canPacketListCopy, 0);
                canPacketList.Clear();

                currentTime = DateTime.Now;
                timeSpan    = currentTime - epochTime;
                logEntry    = "DateTime:" + Convert.ToInt64(timeSpan.TotalMilliseconds).ToString() +
                              " Data:";

                foreach (CanPacket cp in canPacketListCopy)
                {
                    ioStreamWriter.WriteLine(logEntry + cp.getRawBytesString());
                }

                this.isNewPacket = false;
            }
            catch
            {
                // Welcome to how to deal with concurrent threads 101
            }
        }
        public async Task OneSendOneRecieve()
        {
            var wasRecieved = false;
            var reciever = new UdpReciever<TestMessage>((value) => { wasRecieved = true; });
            reciever.Start();
            await Task.Delay(20);

            var sender = new UdpBroadcaster();

            await sender.Send(new TestMessage());

            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
            while(!wasRecieved)
            {
                cts.Token.ThrowIfCancellationRequested();
                await Task.Delay(10, cts.Token);
            }

            reciever.Stop();

            Assert.IsTrue(wasRecieved);
        }
        public DataLoggerForm(UdpReciever udpReciever)
        {
            InitializeComponent();

            this.udpReciever   = udpReciever;
            this.canPacketList = new List <CanPacket>();

            timer          = new Timer();
            timer.Interval = (100);
            timer.Tick    += new EventHandler(timerTick);

            epochTime = new DateTime(1970, 1, 1);

            this.isLogRawData         = true;
            this.isLogParsedData      = false;
            this.rbDataRaw.Checked    = this.isLogRawData;
            this.rbDataParsed.Checked = this.isLogParsedData;
            this.rbDataParsed.Enabled = false;

            //this.btnStartStop.Enabled = false;
            this.btnStop.Enabled = false;
        }
        public async Task OneSendOneRecieve()
        {
            var wasRecieved = false;
            var reciever    = new UdpReciever <TestMessage>((value) => { wasRecieved = true; });

            reciever.Start();
            await Task.Delay(20);

            var sender = new UdpBroadcaster();

            await sender.Send(new TestMessage());

            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));

            while (!wasRecieved)
            {
                cts.Token.ThrowIfCancellationRequested();
                await Task.Delay(10, cts.Token);
            }

            reciever.Stop();

            Assert.IsTrue(wasRecieved);
        }
        public async Task EntireMessageReceived()
        {
            var expected = new TestMessage()
            {
                Text = "Some text goes here",
                Bit = false,
                Number = 1245423534
            };

            TestMessage actual = null;

            var reciever = new UdpReciever<TestMessage>((value) => { actual = value; });
            reciever.Start();
            await Task.Delay(20);

            var sender = new UdpBroadcaster();

            await sender.Send(expected);

            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
            while (actual == null)
            {
                cts.Token.ThrowIfCancellationRequested();
                await Task.Delay(10, cts.Token);
            }

            reciever.Stop();

            Assert.AreEqual(expected, actual);
        }
Exemple #11
0
 public void Detach()
 {
     // Detach the event and delete the list
     udpReciever.recieverFormEventHandler -= new UdpRecievedEventHandler(packetRecieved);
     udpReciever = null;
 }