Esempio n. 1
0
        // Sender
        static void sender()
        {
            DateTime now;
            DateTime last = System.DateTime.Now;

            UdpClient client = new UdpClient();

            client.Connect(new IPEndPoint(IPAddress.Parse(serverIP), serverPort));

            while (true)
            {
                now = System.DateTime.Now;
                if (now >= last.AddMilliseconds(senderDelay) && buffer.Count() > 0)
                {
                    try
                    {
                        Console.WriteLine("\tSending Package at {0}", now.ToString());
                        TrackerPkg package  = buffer.TryPeek();
                        byte[]     pkgBytes = package.GetBytes();
                        client.Send(pkgBytes, pkgBytes.Length);
                    }
                    catch (System.Exception)
                    {
                        // See: https://github.com/dotnet/runtime/issues/923
                        Console.WriteLine("\t\t(Sending aborted by O.S.)");
                    }
                    last = last.AddMilliseconds(locatorDelay);
                }
            }
        }
        // Saver
        static void saver()
        {
            UdpClient client = new UdpClient();

            while (true)
            {
                if (buffer.Count > 0)
                {
                    // Decoding the stream
                    TrackerPkg package  = new TrackerPkg(buffer.Dequeue());
                    IPEndPoint remoteIp = remoteSockets.Dequeue();

                    // Printing the location
                    Console.WriteLine("\tLocation received from device {0}: {1}.", package.device, package.message);

                    // Sending confirmation
                    TrackerPkg response = new TrackerPkg(1, 0, package.device.ToString(), "");
                    byte[]     resBytes = response.GetBytes();
                    remoteIp.Port = 9999;
                    client.Send(resBytes, resBytes.Length, remoteIp);
                }
            }
        }