Ejemplo n.º 1
0
        public static bool PushResponse(string MachineID, PushDataResponse response, Int64 Channel, bool Use2ndLock = false)
        {
            string Key = MachineID + "-" + Channel.ToString();
            PushServiceHelperData dd = null;

            lock (Lock)
            {
                if (Data.ContainsKey(Key) == false)
                {
                    return(false);
                }
                dd = Data[Key];
            }

            lock (dd.InnerLock)
            {
                response.MachineID = MachineID;
                dd.Responses.Add(response);
                if (Use2ndLock == false)
                {
                    dd.ResponseEventLock.Set();
                }
                else
                {
                    dd.ResponseEventLock2.Set();
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        public static void SendPushService(string MachineID, PushData data, Int64 Channel)
        {
            string Key = MachineID + "-" + Channel.ToString();
            PushServiceHelperData dd = null;

            lock (Lock)
            {
                if (Data.ContainsKey(Key) == false)
                {
                    return;
                }
                dd = Data[Key];
            }

            lock (dd.InnerLock)
            {
                dd.ToDo.Add(data);
                dd.EventLock.Set();
            }
        }
Ejemplo n.º 3
0
        public static PushDataResponse PopResponse(string MachineID, Int64 Channel, string SpecificID, bool Use2ndLock = false, int Timeout = 60)
        {
            string Key = MachineID + "-" + Channel.ToString();
            PushServiceHelperData dd   = null;
            PushDataResponse      resp = null;

            lock (Lock)
            {
                if (Data.ContainsKey(Key) == false)
                {
                    return(null);
                }
                dd = Data[Key];
            }

            DateTime now = DateTime.UtcNow;

            do
            {
                if ((DateTime.UtcNow - now).TotalSeconds > 30)
                {
                    break;
                }

                int Counter = 0;
                int Timer   = 0;
                lock (dd.InnerLock)
                {
                    Counter = dd.Responses.Count;
                }

                if (Counter == 0)
                {
                    if (Use2ndLock == false)
                    {
                        while (Counter == 0)
                        {
                            lock (dd.InnerLock)
                            {
                                Counter = dd.Responses.Count;
                            }
                            dd.ResponseEventLock.WaitOne(LockWaitOne);
                            Timer++;
                            if (Timer > Timeout)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        while (Counter == 0)
                        {
                            lock (dd.InnerLock)
                            {
                                Counter = dd.Responses.Count;
                            }
                            dd.ResponseEventLock2.WaitOne(LockWaitOne);
                            Timer++;
                            if (Timer > Timeout)
                            {
                                break;
                            }
                        }
                    }
                }

                if (string.IsNullOrWhiteSpace(SpecificID) == true)
                {
                    lock (dd.InnerLock)
                    {
                        if (dd.Responses.Count == 0)
                        {
                            if (Use2ndLock == false)
                            {
                                dd.ResponseEventLock.Reset();
                            }
                            else
                            {
                                dd.ResponseEventLock2.Reset();
                            }
                            continue;
                        }
                        resp = dd.Responses[0];
                        dd.Responses.RemoveAt(0);
                        if (dd.Responses.Count == 0)
                        {
                            if (Use2ndLock == false)
                            {
                                dd.ResponseEventLock.Reset();
                            }
                            else
                            {
                                dd.ResponseEventLock2.Reset();
                            }
                        }
                        break;
                    }
                }
                else
                {
                    lock (dd.InnerLock)
                    {
                        if (dd.Responses.Count == 0)
                        {
                            if (Use2ndLock == false)
                            {
                                dd.ResponseEventLock.Reset();
                            }
                            else
                            {
                                dd.ResponseEventLock2.Reset();
                            }
                            continue;
                        }
                        foreach (PushDataResponse r in dd.Responses)
                        {
                            if (string.IsNullOrWhiteSpace(r.ReplyID) == false)
                            {
                                if (r.ReplyID.ToLower() == SpecificID.ToLower())
                                {
                                    resp = r;
                                    break;
                                }
                            }
                        }
                        if (resp != null)
                        {
                            dd.Responses.Remove(resp);
                            if (dd.Responses.Count == 0)
                            {
                                if (Use2ndLock == false)
                                {
                                    dd.ResponseEventLock.Reset();
                                }
                                else
                                {
                                    dd.ResponseEventLock2.Reset();
                                }
                            }
                            break;
                        }
                        if (Use2ndLock == false)
                        {
                            dd.ResponseEventLock.Reset();
                        }
                        else
                        {
                            dd.ResponseEventLock2.Reset();
                        }
                    }
                }
            } while (true);
#if DEBUG
            //StackTrace stacky = new StackTrace();
            //Debug.WriteLine(">> PopResponse: \n  " + JsonConvert.SerializeObject(resp) + "\n       for\n   " + stacky.ToString());
#endif
            return(resp);
        }