Exemplo n.º 1
0
        public void TrySendAll()
        {
            List <TMSKSocket> lsSocket = new List <TMSKSocket>(2000);

            while (!this._Exit)
            {
                lsSocket.Clear();
                lock (this.BufferDict)
                {
                    lsSocket.AddRange(this.BufferDict.Keys);
                }
                int lsSocketCount = lsSocket.Count;
                for (int i = 0; i < lsSocketCount; i++)
                {
                    TMSKSocket s = lsSocket[i];
                    if (null != s)
                    {
                        SendBuffer sendBuffer = s._SendBuffer;
                        bool       bFind      = sendBuffer != null;
                        if (bFind && null != sendBuffer)
                        {
                            sendBuffer.ExternalTrySend(s, true, 0);
                        }
                        if (s.DelayClose > 0 && s.DelayClose-- <= 0)
                        {
                        }
                    }
                }
                Thread.Sleep(20);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 尝试发送所有的数据包
        /// </summary>
        public void TrySendAll()
        {
            //最大睡眠时间 毫秒
            //int maxSleepMiniSecs = 1;
            //int  sleepMiniSecs = 0;
            List <TMSKSocket> lsSocket   = new List <TMSKSocket>(2000);
            SendBuffer        sendBuffer = null;
            bool bFind = false;

            while (!_Exit)
            {
                lsSocket.Clear();

                //System.Diagnostics.Debug.WriteLine("SendBufferManager BufferDict.Count == " + BufferDict.Count);
                //遍历需要加锁吗?当这儿拿到一个buffer,Remove同时被触发,这儿的buffer仍然是有效的,最多产生异常
                //long preTicks = TimeUtil.NOW();

                //保证锁住BufferDict的时间很小
                lock (BufferDict)
                {
                    lsSocket.AddRange(BufferDict.Keys);
                }

                int lsSocketCount = lsSocket.Count;
                //foreach (var s in lsSocket)
                for (int i = 0; i < lsSocketCount; i++)
                {
                    TMSKSocket s = lsSocket[i];
                    if (null == s)
                    {
                        continue;
                    }

                    if (GameManager.FlagOptimizeLock)
                    {
                        sendBuffer = s._SendBuffer;
                        bFind      = (sendBuffer != null);
                    }
                    else
                    {
                        lock (BufferDict)
                        {
                            bFind = BufferDict.TryGetValue(s, out sendBuffer);
                        }
                    }

                    if (bFind && null != sendBuffer)
                    {
                        sendBuffer.ExternalTrySend(s, true, 0); //尝试发送,如果锁定超时,继续处理下边的指令
                    }
                }
                //foreach (var buffer in BufferDict)
                //{
                //    buffer.Value.TrySend(buffer.Key);
                //}

                /*int usedTicks = (int)(TimeUtil.NOW() - preTicks);
                 *
                 * sleepMiniSecs = Math.Max(1, maxSleepMiniSecs - usedTicks);
                 * if (sleepMiniSecs > 0)
                 * {
                 *  Thread.Sleep(sleepMiniSecs);
                 * }*/

                Thread.Sleep(20);
            }
        }