Esempio n. 1
0
        /// <summary>
        /// Connects to the dispatcher and then enters the main loop.
        /// While alive, checks incoming messages from dispatcher, and processes them asynchronously
        /// </summary>
        public void Run()
        {
            while (true)
            {
                try
                {
                    m_client = new TcpClient();
                    m_client.Connect(m_dispatcherAddr);
                    Running  = true;
                    m_serlzr = new NetSerializer <Datagram>(m_client.GetStream(), new object());

                    Console.WriteLine("Web client started");

                    Datagram[] tempData = new Datagram[m_unsent.Count];
                    m_unsent.CopyTo(tempData);
                    foreach (Datagram data in tempData)
                    {
                        SendData(data);
                    }

                    m_unsent.Clear();

                    Task.Run(() =>
                    {
                        Thread.Sleep(1000);
                        while (Running)
                        {
                            Datagram data = m_serlzr.ExtractFromStream();
                            ProcessData(data);
                        }
                    });

                    while (true)
                    {
                        m_serlzr.WriteToStream(Datagram.CreateDummy());
                        Thread.Sleep(1000);
                    }
                }
                catch
                {
                    if (Running)
                    {
                        Running = false;
                        try { m_client.Close(); }
                        catch { }
                        ConnectionLost();
                        Console.WriteLine("Connection lost");
                    }
                    Thread.Sleep(1000);
                }
            }
        }