Exemple #1
0
        public override ICommunicationEntity GetOrCreateEntity(string findKey)
        {
            MqComunicationEntity entity;

            _entitiesPool.TryTake(out entity, 100);
            return(entity ?? new MqComunicationEntity());
        }
Exemple #2
0
 /// <summary>
 /// 解压缩字节数据
 /// </summary>
 /// <param name="data">原字节数据</param>
 /// <returns>解压缩后字节数据</returns>
 public static byte[] Decompress(this byte[] data)
 {
     byte[] buffer;
     _bytesQueue.TryTake(out buffer, 100);
     buffer = buffer ?? new byte[3 * 1024];
     try
     {
         var ms  = new MemoryStream(data);
         var zip = new GZipStream(ms, CompressionMode.Decompress, true);
         using (var msreader = new MemoryStream())
         {
             while (true)
             {
                 var reader = zip.Read(buffer, 0, buffer.Length);
                 if (reader <= 0)
                 {
                     break;
                 }
                 msreader.Write(buffer, 0, reader);
             }
             zip.Close();
             ms.Close();
             msreader.Position = 0;
             return(msreader.ToArray());
         }
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
     finally
     {
         _bytesQueue.TryAdd(buffer, 100);
     }
 }
Exemple #3
0
        void Update()
        {
            // get all processed jobs

            Job <object> job;

            while (s_processedJobs.TryTake(out job, 0))
            {
                if (job.exception != null)
                {
                    // error happened

                    if (job.callbackError != null)
                    {
                        Utilities.F.RunExceptionSafe(() => job.callbackError(job.exception));
                    }

                    Debug.LogException(job.exception);
                }
                else
                {
                    // success
                    if (job.callbackSuccess != null)
                    {
                        Utilities.F.RunExceptionSafe(() => job.callbackSuccess(job.result));
                    }
                }

                // invoke finish callback
                if (job.callbackFinish != null)
                {
                    F.RunExceptionSafe(() => job.callbackFinish(job.result));
                }
            }
        }
Exemple #4
0
        // 循环执行操作
        private void CycleWorking()
        {
            var curentThread = System.Threading.Thread.CurrentThread;

            curentThread.Priority = System.Threading.ThreadPriority.Highest;
            while (_mainToken != null && !_mainToken.IsCancellationRequested)
            {
                TEntity entity;
                // 使用阻塞方法,保证线程不会长时间占用CPU
                if (_entityQueue.TryTake(out entity, 3000))
                {
                    TryCatchDowork(entity);
                }
            }
        }
Exemple #5
0
        static void ThreadFunction()
        {
            while (!ShouldThreadExit())
            {
                Job <object> job;
                if (!s_jobs.TryTake(out job, 200))
                {
                    continue;
                }

                try
                {
                    job.result = job.action();
                }
                catch (System.Exception ex)
                {
                    job.exception = ex;
                }

                s_processedJobs.Add(job);
            }
        }
Exemple #6
0
        private bool deleteOlder()
        {
            DateTime old = DateTime.Now;
            bool     ret = false;

            foreach (DateTime item in col)
            {
                if (DateTime.Now.Subtract(item) >= timeout)
                {
                    col.TryTake(out old);
                    ret = true;
                    break;
                }
            }
            if (ret == true)
            {
                return(deleteOlder());
            }
            else
            {
                return(ret);
            }
        }