Ejemplo n.º 1
0
 /// <summary>
 /// TCP 服务器端同步调用任务处理
 /// </summary>
 protected override void run()
 {
     do
     {
         WaitHandle.Wait();
         QueueLock.EnterYield();
         ServerCallBase value = head;
         end  = null;
         head = null;
         QueueLock.Exit();
         do
         {
             try
             {
                 while (value != null)
                 {
                     current = null;
                     value.RunTask(ref value);
                 }
                 break;
             }
             catch (Exception error)
             {
                 log.Exception(error, null, LogLevel.Exception | LogLevel.AutoCSer);
             }
         }while (true);
     }while (!isDisposed);
 }
Ejemplo n.º 2
0
 /// <summary>
 /// 消息队列读取操作任务处理
 /// </summary>
 protected override void run()
 {
     do
     {
         WaitHandle.Wait();
         QueueLock.EnterYield();
         T value = head;
         end  = null;
         head = null;
         QueueLock.Exit();
         do
         {
             try
             {
                 do
                 {
                     value.RunTask(ref value);
                 }while (value != null);
                 break;
             }
             catch (Exception error)
             {
                 AutoCSer.LogHelper.Exception(error);
             }
         }while (value != null);
     }while (true);
 }
Ejemplo n.º 3
0
 /// <summary>
 /// TCP 服务器端同步调用任务处理
 /// </summary>
 protected override void run()
 {
     do
     {
         WaitHandle.Wait();
         QueueLock.EnterYield();
         Buffer value = head;
         end  = null;
         head = null;
         QueueLock.Exit();
         do
         {
             try
             {
                 while (value != null)
                 {
                     value.OnPacket(ref value, onPacket);
                 }
                 break;
             }
             catch (Exception error)
             {
                 log.Exception(error, null, LogLevel.Exception | LogLevel.AutoCSer);
             }
         }while (true);
     }while (!isDisposed);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Flushs the update queue. Takes the list of all updates queued for the client and calls the client's callback, passing this list as parameter
        /// </summary>
        private void FlushUpdateQueue()
        {
            while (true)
            {
                bool gotLock = false;
                try
                {
                    QueueLock.Enter(ref gotLock);

                    if (UpdateQueue.Count > 0)
                    {
                        InvokeClientCallbacks();
                        UpdateQueue.Clear();
                    }
                }
                finally
                {
                    if (gotLock)
                    {
                        QueueLock.Exit();
                    }
                }

                // Wait for updates to accumulate (to send them in batches)
                Thread.Sleep(10);
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// TCP 服务器端同步调用任务处理
 /// </summary>
 protected override void run()
 {
     do
     {
         WaitHandle.Wait();
         QueueLock.EnterYield();
         ClientCommand.CommandBase value = head;
         end  = null;
         head = null;
         QueueLock.Exit();
         do
         {
             try
             {
                 do
                 {
                     value.OnReceiveTask(ref value);
                 }while (value != null);
                 break;
             }
             catch (Exception error)
             {
                 AutoCSer.LogHelper.Exception(error, null, LogLevel.Exception | LogLevel.AutoCSer);
             }
         }while (value != null);
     }while (true);
 }
    public IDisposable LockingDequeue(out T data)
    {
        var queueLock = new QueueLock(queueLocker);

        data = internalQueue.Dequeue();
        return(queueLock);
    }
Ejemplo n.º 7
0
        public bool TryEnqueue(byte[] message, TimeSpan timeout)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var sw = new Stopwatch();

            sw.Start();
            for (int i = 0; i < int.MaxValue; ++i)
            {
                if (sw.Elapsed >= timeout)
                {
                    return(false);
                }

                using (var @lock = new QueueLock(_accessor, _queueName))
                {
                    if (@lock.Acquire(1))
                    {
                        if (@lock.AvailableWriteLength >= message.Length)
                        {
                            @lock.WriteMessage(message);
                            return(true);
                        }
                    }
                }

                int sleepTime;
                if (i < 10)
                {
                    sleepTime = 0;
                }
                else if (i < 100)
                {
                    sleepTime = 1;
                }
                else if (i < 1000)
                {
                    sleepTime = 10;
                }
                else
                {
                    sleepTime = 100;
                }

                Thread.Sleep(sleepTime);
            }

            return(false);
        }
Ejemplo n.º 8
0
        public async Task RestartAsync()
        {
            if (Player == null || !Player.IsConnected || NowPlaying.Track.TrackString == null)
            {
                return;
            }

            await QueueLock.WaitAsync();

            QueueList.Insert(0, NowPlaying);
            await Player.StopAsync();

            QueueLock.Release();
        }
Ejemplo n.º 9
0
        public bool TryDequeue(out byte[] message, TimeSpan timeout)
        {
            var sw = new Stopwatch();

            sw.Start();
            for (var i = 0; i < int.MaxValue; ++i)
            {
                if (sw.Elapsed >= timeout)
                {
                    break;
                }

                using (var @lock = new QueueLock(_accessor, _queueName))
                {
                    if (@lock.Acquire(1))
                    {
                        if (@lock.AvailableReadLength > 0)
                        {
                            message = @lock.ReadMessage();
                            return(true);
                        }
                    }
                }

                int sleepTime;
                if (i < 10)
                {
                    sleepTime = 0;
                }
                else if (i < 100)
                {
                    sleepTime = 1;
                }
                else if (i < 1000)
                {
                    sleepTime = 10;
                }
                else
                {
                    sleepTime = 100;
                }

                Thread.Sleep(sleepTime);
            }

            message = null;
            return(false);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// 添加任务
 /// </summary>
 /// <param name="value"></param>
 internal void Add(T value)
 {
     QueueLock.EnterYield();
     if (head == null)
     {
         end  = value;
         head = value;
         QueueLock.Exit();
         WaitHandle.Set();
     }
     else
     {
         end.LinkNext = value;
         end          = value;
         QueueLock.Exit();
     }
 }
Ejemplo n.º 11
0
 /// <summary>
 /// 添加任务
 /// </summary>
 /// <param name="head"></param>
 /// <param name="end"></param>
 internal void Add(T head, T end)
 {
     QueueLock.EnterYield();
     if (this.head == null)
     {
         this.end  = end;
         this.head = head;
         QueueLock.Exit();
         WaitHandle.Set();
     }
     else
     {
         this.end.LinkNext = head;
         this.end          = end;
         QueueLock.Exit();
     }
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Adds update information about an entity to the update queue after having received an attributeChanged event
        /// </summary>
        /// <param name="sender">Entity that invoked the attribute change event</param>
        /// <param name="e">Event Arguments</param>
        private void AddEntityUpdateToQueue(Object sender, ChangedAttributeEventArgs e)
        {
            bool gotLock = false;

            try
            {
                QueueLock.Enter(ref gotLock);
                UpdateQueue.Add(CreateUpdateInfoFromEventArgs((Entity)sender, e));
            }
            finally
            {
                if (gotLock)
                {
                    QueueLock.Exit();
                }
            }
        }
Ejemplo n.º 13
0
 /// <summary>
 /// 添加任务
 /// </summary>
 /// <param name="value"></param>
 internal void Add(ClientCommand.CommandBase value)
 {
     QueueLock.EnterYield();
     if (head == null)
     {
         end  = value;
         head = value;
         QueueLock.Exit();
         WaitHandle.Set();
     }
     else
     {
         end.NextTask = value;
         end          = value;
         QueueLock.Exit();
     }
 }
Ejemplo n.º 14
0
 /// <summary>
 /// 添加任务
 /// </summary>
 /// <param name="value"></param>
 /// <returns></returns>
 public bool CheckAdd(T value)
 {
     QueueLock.EnterYield();
     if (value.LinkNext == null && value != end)
     {
         if (head == null)
         {
             end  = value;
             head = value;
             QueueLock.Exit();
             WaitHandle.Set();
         }
         else
         {
             end.LinkNext = value;
             end          = value;
             QueueLock.Exit();
         }
         return(true);
     }
     QueueLock.Exit();
     return(false);
 }
Ejemplo n.º 15
0
        /// <summary>
        /// Removes all unsent update information of a removed entity from the update queue
        /// </summary>
        /// <param name="entityGuid">Guid of the removed entity/param>
        private void RemoveEntityFromQueue(Entity entity)
        {
            bool gotLock = false;

            try
            {
                QueueLock.Enter(ref gotLock);

                foreach (UpdateInfo entityUpdate in UpdateQueue)
                {
                    if (entityUpdate.entityGuid.Equals(entity.Guid))
                    {
                        UpdateQueue.Remove(entityUpdate);
                    }
                }
            }
            finally
            {
                if (gotLock)
                {
                    QueueLock.Exit();
                }
            }
        }