public void PingAdversary(ProximityDevice device, NotifyNfcReady notify)
        {
            if (subscribeId != -1)
            {
                proximityDevice.StopSubscribingForMessage(subscribeId);
                subscribeId = -1;
            }

            if (publishId != -1)
            {
                proximityDevice.StopPublishingMessage(publishId);
                publishId = -1;
            }

            if (state == NfcManager.ProtoState.Busy)
            {
                return;
            }

            state = NfcManager.ProtoState.NotReady;
            notifyReady = notify;
            initialMessage.devicetime = random.NextDouble();
            MemoryStream stream = new MemoryStream();
            DataContractSerializer serializer = new DataContractSerializer(initialMessage.GetType());
            serializer.WriteObject(stream, initialMessage);
            stream.Position = 0;
            var dataWriter = new DataWriter();
            dataWriter.WriteBytes(stream.GetBuffer());
            proximityDevice = device;
            publishId = proximityDevice.PublishBinaryMessage("Windows.CarTrumps", dataWriter.DetachBuffer());
            subscribeId = proximityDevice.SubscribeForMessage("Windows.CarTrumps", OnMessageReceived);
        }
        private void OnMessageReceived(ProximityDevice sender, ProximityMessage message)
        {
            if (state == NfcManager.ProtoState.Ready)
            {
                return;
            }

            state = NfcManager.ProtoState.NotReady;

            try
            {
                String                 msg        = message.DataAsString.Substring(0, message.DataAsString.IndexOf('\0'));
                MemoryStream           stream     = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(msg));
                DataContractSerializer serializer = new DataContractSerializer(typeof(InitialMessage));
                stream.Position = 0;
                InitialMessage adversaryTime = (InitialMessage)serializer.ReadObject(stream);
                isMaster = (initialMessage.devicetime > adversaryTime.devicetime);
                state    = NfcManager.ProtoState.Ready;
                proximityDevice.StopSubscribingForMessage(subscribeId);
                proximityDevice.StopPublishingMessage(publishId);
                subscribeId = -1;
                publishId   = -1;
                notifyReady();
            }
            catch (SerializationException)
            {
            }
        }
        public void PingAdversary(ProximityDevice device, NotifyNfcReady notify)
        {
            if (subscribeId != -1)
            {
                proximityDevice.StopSubscribingForMessage(subscribeId);
                subscribeId = -1;
            }

            if (publishId != -1)
            {
                proximityDevice.StopPublishingMessage(publishId);
                publishId = -1;
            }

            if (state == NfcManager.ProtoState.Busy)
            {
                return;
            }

            state       = NfcManager.ProtoState.NotReady;
            notifyReady = notify;
            initialMessage.devicetime = random.NextDouble();
            MemoryStream           stream     = new MemoryStream();
            DataContractSerializer serializer = new DataContractSerializer(initialMessage.GetType());

            serializer.WriteObject(stream, initialMessage);
            stream.Position = 0;
            var dataWriter = new DataWriter();

            dataWriter.WriteBytes(stream.GetBuffer());
            proximityDevice = device;
            publishId       = proximityDevice.PublishBinaryMessage("Windows.CarTrumps", dataWriter.DetachBuffer());
            subscribeId     = proximityDevice.SubscribeForMessage("Windows.CarTrumps", OnMessageReceived);
        }
        private void OnMessageReceived(ProximityDevice sender, ProximityMessage message)
        {
            if (state == NfcManager.ProtoState.Ready)
            {
                return;
            }

            state = NfcManager.ProtoState.NotReady;

            try
            {
                String msg = message.DataAsString.Substring(0, message.DataAsString.IndexOf('\0'));
                MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(msg));
                DataContractSerializer serializer = new DataContractSerializer(typeof(InitialMessage));
                stream.Position = 0;
                InitialMessage adversaryTime = (InitialMessage)serializer.ReadObject(stream);
                isMaster = (initialMessage.devicetime > adversaryTime.devicetime);
                state = NfcManager.ProtoState.Ready;
                proximityDevice.StopSubscribingForMessage(subscribeId);
                proximityDevice.StopPublishingMessage(publishId);
                subscribeId = -1;
                publishId = -1;
                notifyReady();
            }
            catch (SerializationException)
            {
            }
        }