public bool reachedTime(UInt32 elaspedTime_ms, int p)
        {
            NetworkClockDatagram ndgram = new NetworkClockDatagram(elaspedTime_ms, clockID, ndCount);

            ndCount++;
            byte [] buffer = ndgram.toByteStream();
            Dictionary <UdpClient, IAsyncResult> results = new Dictionary <UdpClient, IAsyncResult>();

            foreach (UdpClient client in clients.Values)
            {
                results.Add(client, client.BeginSend(buffer, NetworkClockDatagram.datagramByteLength, null, null));
#if DEBUG
                logMessage(new MessageEvent("Began sending datagram to client " + client.ToString(), 2, MessageEvent.MessageTypes.Debug, MessageEvent.MessageCategories.Networking));
#endif
            }

            foreach (UdpClient client in results.Keys)
            {
                client.EndSend(results[client]);
#if DEBUG
                logMessage(new MessageEvent("Finished sending datagram to client " + client.ToString(), 2, MessageEvent.MessageTypes.Debug, MessageEvent.MessageCategories.Networking));
#endif
            }

            if (maxTime > 0 && elaspedTime_ms > maxTime)
            {
                return(false);
            }

            return(true);
        }
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }
            if (!(obj is NetworkClockDatagram))
            {
                return(false);
            }
            NetworkClockDatagram other = (NetworkClockDatagram)obj;

            if (other.clockID != clockID)
            {
                return(false);
            }
            if (other.elaspedTime != elaspedTime)
            {
                return(false);
            }
            if (other.datagramCount != datagramCount)
            {
                return(false);
            }
            return(true);
        }
 public void toByteStreamTest()
 {
     Random rnd = new Random();
     for (int i = 0; i < 30; i++)
     {
         NetworkClockDatagram testGram = new NetworkClockDatagram((uint)rnd.Next(), (uint)rnd.Next(), (uint)rnd.Next());
         byte []bytes = testGram.toByteStream();
         NetworkClockDatagram resultGram = new NetworkClockDatagram(bytes);
         Assert.AreEqual(testGram, resultGram);
         //Assert.AreEqual(testGram.ElaspedTime, resultGram.ElaspedTime);
         //Assert.AreEqual(testGram.ClockID, resultGram.ClockID);
     }
 }
        private static void listenerThreadProc()
        {
            staticLogMessage(new MessageEvent("Network Clock listener thread is alive", 1, MessageEvent.MessageTypes.Log, MessageEvent.MessageCategories.Networking));

            while (true)
            {
                // Wait for a datagram from the udpClient.
                // This makes use of a temporary local reference to udpClient so that we can be sure
                // that even if the static udpClient member is changed when we don't hold the static lock
                // we will at least run the callback on the correct instance of udpClient.
                // (I didn't want to lock the receive callback on staticLockObj, because the callback may
                // take arbitrarily long to return, and I don't want to be locked out of other static activities like
                // shutting down the listener.

                System.Net.IPEndPoint remoteEnd = null;
                IAsyncResult result;
                byte[] received;
                UdpClient udpClientNonstatic;
                lock (staticLockObj)
                {
                    udpClientNonstatic = udpClient;
                    result = udpClient.BeginReceive(null, null);
                }
                received = udpClientNonstatic.EndReceive(result, ref remoteEnd);

                if (received.Length != NetworkClockDatagram.datagramByteLength)
                {
                    staticLogMessage(new MessageEvent("Received wrong sized (" + received.Length + ") datagram. Dropping.", 1, MessageEvent.MessageTypes.Error, MessageEvent.MessageCategories.Networking));
                    continue;
                }

                NetworkClockDatagram ndgram = new NetworkClockDatagram(received);

            #if DEBUG
                staticLogMessage(new MessageEvent("Received network clock datagram " + ndgram.ToString(), 2, MessageEvent.MessageTypes.Debug, MessageEvent.MessageCategories.Networking));
            #endif
                if (ndgram.DatagramCount != lastNGramID + 1)
                {
                    staticLogMessage(new MessageEvent("Warning! Received network clock datagram #" + ndgram.DatagramCount + ", expected #" + (lastNGramID + 1) + ".", 0, MessageEvent.MessageTypes.Warning, MessageEvent.MessageCategories.Networking));
                }
                lastNGramID = ndgram.DatagramCount;

                NetworkClockProvider pr = null;

                // Pull designated provider out of provider's dictionary (locked on staticLockObj since
                // we are accessing static providers dictionary)
                // Will leave pr as null if no such ClockID listener exists.
                // Will throw an exception if clockID does exist, but listener is null.
                lock (staticLockObj)
                {
                    if (providers.ContainsKey(ndgram.ClockID))
                    {
                        pr = providers[ndgram.ClockID];
                        if (pr == null)
                            throw new SoftwareClockProviderException("Unexpected null network clock provider.");
                    }
                }

                // If provider does exist, (is not null), relay to it the latest
                // clock message
                if (pr != null)
                {
                    // Thread lock on pr object
                    lock (pr.instanceLockObj)
                    {
                        if (pr.isRunning)
                        {
                            if (!pr.receivedFirstClock)
                            {
                                staticLogMessage(new MessageEvent("Received first clock datagram for clockID " + Shared.clockIDToString(pr.clockID),
                                    0, MessageEvent.MessageTypes.Routine, MessageEvent.MessageCategories.SoftwareClock));
                                pr.receivedFirstClock = true;
                            }
                            pr.reachTime(ndgram.ElaspedTime);
                        }
                    }
                }
            }
        }
Exemple #5
0
        private static void listenerThreadProc()
        {
            staticLogMessage(new MessageEvent("Network Clock listener thread is alive", 1, MessageEvent.MessageTypes.Log, MessageEvent.MessageCategories.Networking));


            while (true)
            {
                // Wait for a datagram from the udpClient.
                // This makes use of a temporary local reference to udpClient so that we can be sure
                // that even if the static udpClient member is changed when we don't hold the static lock
                // we will at least run the callback on the correct instance of udpClient.
                // (I didn't want to lock the receive callback on staticLockObj, because the callback may
                // take arbitrarily long to return, and I don't want to be locked out of other static activities like
                // shutting down the listener.

                System.Net.IPEndPoint remoteEnd = null;
                IAsyncResult          result;
                byte[]    received;
                UdpClient udpClientNonstatic;
                lock (staticLockObj)
                {
                    udpClientNonstatic = udpClient;
                    result             = udpClient.BeginReceive(null, null);
                }
                received = udpClientNonstatic.EndReceive(result, ref remoteEnd);

                if (received.Length != NetworkClockDatagram.datagramByteLength)
                {
                    staticLogMessage(new MessageEvent("Received wrong sized (" + received.Length + ") datagram. Dropping.", 1, MessageEvent.MessageTypes.Error, MessageEvent.MessageCategories.Networking));
                    continue;
                }

                NetworkClockDatagram ndgram = new NetworkClockDatagram(received);

#if DEBUG
                staticLogMessage(new MessageEvent("Received network clock datagram " + ndgram.ToString(), 2, MessageEvent.MessageTypes.Debug, MessageEvent.MessageCategories.Networking));
#endif
                if (ndgram.DatagramCount != lastNGramID + 1)
                {
                    staticLogMessage(new MessageEvent("Warning! Received network clock datagram #" + ndgram.DatagramCount + ", expected #" + (lastNGramID + 1) + ".", 0, MessageEvent.MessageTypes.Warning, MessageEvent.MessageCategories.Networking));
                }
                lastNGramID = ndgram.DatagramCount;

                NetworkClockProvider pr = null;

                // Pull designated provider out of provider's dictionary (locked on staticLockObj since
                // we are accessing static providers dictionary)
                // Will leave pr as null if no such ClockID listener exists.
                // Will throw an exception if clockID does exist, but listener is null.
                lock (staticLockObj)
                {
                    if (providers.ContainsKey(ndgram.ClockID))
                    {
                        pr = providers[ndgram.ClockID];
                        if (pr == null)
                        {
                            throw new SoftwareClockProviderException("Unexpected null network clock provider.");
                        }
                    }
                }

                // If provider does exist, (is not null), relay to it the latest
                // clock message
                if (pr != null)
                {
                    // Thread lock on pr object
                    lock (pr.instanceLockObj)
                    {
                        if (pr.isRunning)
                        {
                            if (!pr.receivedFirstClock)
                            {
                                staticLogMessage(new MessageEvent("Received first clock datagram for clockID " + Shared.clockIDToString(pr.clockID),
                                                                  0, MessageEvent.MessageTypes.Routine, MessageEvent.MessageCategories.SoftwareClock));
                                pr.receivedFirstClock = true;
                            }
                            pr.reachTime(ndgram.ElaspedTime);
                        }
                    }
                }
            }
        }
        public bool reachedTime(UInt32 elaspedTime_ms, int p)
        {
            NetworkClockDatagram ndgram = new NetworkClockDatagram(elaspedTime_ms, clockID, ndCount);
            ndCount++;
            byte [] buffer = ndgram.toByteStream();
            Dictionary<UdpClient, IAsyncResult> results = new Dictionary<UdpClient,IAsyncResult>();
            foreach (UdpClient client in clients.Values)
            {
                results.Add(client, client.BeginSend(buffer, NetworkClockDatagram.datagramByteLength, null, null));
            #if DEBUG
                logMessage(new MessageEvent("Began sending datagram to client " + client.ToString(), 2, MessageEvent.MessageTypes.Debug, MessageEvent.MessageCategories.Networking));
            #endif
            }

            foreach (UdpClient client in results.Keys)
            {
                client.EndSend(results[client]);
            #if DEBUG
                logMessage(new MessageEvent("Finished sending datagram to client " + client.ToString(), 2, MessageEvent.MessageTypes.Debug, MessageEvent.MessageCategories.Networking));
            #endif
            }

            if (maxTime > 0 && elaspedTime_ms > maxTime)
                return false;

            return true;
        }