/// <summary> /// 保存世界服务 /// </summary> internal void OnSave() { if (SpinLockEx.QuickTryEnter(ref m_LockSave) == false) { LOGs.WriteLine(LogMessageType.MSG_NOTICE, LanguageString.SingletonInstance.BaseWorldString003); return; } else { // 保存数据 EventHandler <BaseWorldEventArgs> tempEvent = m_EventSaveWorld; if (tempEvent != null) { // 保存数据中...... m_Saving = true; LOGs.WriteLine(LogMessageType.MSG_NOTICE, LanguageString.SingletonInstance.BaseWorldString001); BaseWorldEventArgs worldEventArgs = new BaseWorldEventArgs(this); tempEvent(this, worldEventArgs); LOGs.WriteLine(LogMessageType.MSG_NOTICE, LanguageString.SingletonInstance.BaseWorldString002); // 保存数据结束...... m_Saving = false; } } m_LockSave.Exit(); }
/// <summary> /// 获取当前的数据 /// </summary> /// <returns></returns> public SendBuffer Dequeue() { SendBuffer sendGram = SendBuffer.NullBuffer; SpinLockEx.ReliableEnter(ref m_LockFlushAndPending); try { if (m_PendingBuffer.Count > 0) { sendGram = m_PendingBuffer.Dequeue(); // 再给出数据 } else if (m_FlushBuffer.IsNull == false) { sendGram = m_FlushBuffer; // 再给出数据 m_FlushBuffer = SendBuffer.NullBuffer; } // 移去已发送的数据的大小 m_WaitSendSize -= sendGram.Length; } catch (Exception e) { Logs.FatalError("SendQueue->Dequeue: Occurred error when dequeuing the data {0} ", e.Message); } finally { m_LockFlushAndPending.Exit(); } return(sendGram); }
/// <summary> /// 处理全部的已断开连接的客户,不要处理过多的断开,以影响时间片的处理 /// 主要是处理NetStateManage内的NetState /// </summary> internal void ProcessDisposed() { // 检查是否已经在处理ProcessDisposed(...)的调用 if (m_LockInProcessDisposed.InLock() == false) { return; } // 已经在处理ProcessDisposed(...)中 do { // 使用数组减少锁定时间 NetState[] netStateArray = null; if (SpinLockEx.QuickTryEnter(ref m_LockNetStateDisposed) == false) { break; } { if (m_NetStateDisposed.Count > 0) { // 断开连接的客户先进先出列队集合的数量(和中断处理比较) long iQueueCountAtNetState = m_NetStateDisposed.Count; if (iQueueCountAtNetState <= BREAK_COUNT) { netStateArray = m_NetStateDisposed.ToArray(); m_NetStateDisposed.Clear(); } else { netStateArray = new NetState[BREAK_COUNT]; for (long iIndex = 0; iIndex < BREAK_COUNT; iIndex++) { netStateArray[iIndex] = m_NetStateDisposed.Dequeue(); } } } } m_LockNetStateDisposed.Exit(); // 如果没有需要处理的数据则返回 if (netStateArray == null) { break; } for (int iIndex = 0; iIndex < netStateArray.Length; iIndex++) { netStateArray[iIndex].ExitWorld(); } } while (false); // 已经处理完ProcessDisposed(...)的调用 m_LockInProcessDisposed.OutLock(); }
/// <summary> /// /// </summary> /// <param name="flag"></param> public void Slice() { if (m_SliceQueue.Count <= 0) { return; } // 使用数组减少锁定时间 T[] arrayT = null; if (SpinLockEx.QuickTryEnter(ref m_LockSliceQueue) == false) { return; } { if (m_SliceQueue.Count > 0) { arrayT = m_SliceQueue.ToArray(); m_SliceQueue.Clear(); } } m_LockSliceQueue.Exit(); // 如果没有需要处理的数据则返回 if (arrayT == null) { return; } // 现在的时间 DateTime nowTime = DateTime.Now; // 用于计算经过的时间(因为Stopwatch的计算速度比DateTime.Now快近3倍) Stopwatch updateTime = Stopwatch.StartNew(); for (int iIndex = 0; iIndex < arrayT.Length; iIndex++) { T itemT = arrayT[iIndex]; itemT.OnProcessSlice(nowTime + updateTime.Elapsed); if (m_MultiThreadedCall == false) { itemT.OutLockProcessSlice(); } } // 计算结束 updateTime.Stop(); }
/// <summary> /// 添加多个监听端口的数据传过来 /// </summary> /// <param name="listener"></param> public void AddListener(Listener listener) { if (listener == null) { throw new ArgumentNullException("listener", "MessagePump.AddListener(...) - listener == null error!"); } if (listener.World != null && listener.World != World) { throw new ArgumentException("MessagePump.AddListener(...) - listener.World != null && listener.World != this.World error!", "listener.World"); } // 检查是否有相同的监听器 Listener[] listenerArray = m_Listeners; for (int iIndex = 0; iIndex < listenerArray.Length; iIndex++) { Listener itemListener = listenerArray[iIndex]; if (itemListener == listener) { return; } } SpinLockEx.ReliableEnter(ref m_OnlyLockAddListener); try { // 创建新的Listener数组,添加数据,交换数组数据,不需要锁定,没有引用时自动会回收数据 Listener[] tempListener = new Listener[m_Listeners.Length + 1]; for (int iIndex = 0; iIndex < m_Listeners.Length; ++iIndex) { tempListener[iIndex] = m_Listeners[iIndex]; } tempListener[m_Listeners.Length] = listener; listener.World = m_World; m_Listeners = tempListener; } finally { m_OnlyLockAddListener.Exit(); } }
/// <summary> /// 添加多个连接端口的数据传过来 /// </summary> public void AddConnecter(Connecter connecter) { if (connecter == null) { throw new ArgumentNullException("connecter", "MessagePump.AddConnecter(...) - connecter == null error!"); } if (connecter.World != null && connecter.World != World) { throw new ArgumentException("connecter.World", "MessagePump.AddConnecter(...) - connecter.World != null && connecter.World != this.World error!"); } // 检查是否有相同的连接器 Connecter[] connecterArray = m_Connecters; for (int iIndex = 0; iIndex < connecterArray.Length; iIndex++) { Connecter itemConnecter = connecterArray[iIndex]; if (itemConnecter == connecter) { return; } } SpinLockEx.ReliableEnter(ref m_OnlyLockAddConnecter); { // 创建新的Listener数组,添加数据,交换数组数据,不需要锁定,没有引用时自动会回收数据 Connecter[] tempConnecter = new Connecter[m_Connecters.Length + 1]; for (int iIndex = 0; iIndex < m_Connecters.Length; ++iIndex) { tempConnecter[iIndex] = m_Connecters[iIndex]; } tempConnecter[m_Connecters.Length] = connecter; connecter.World = m_World; m_Connecters = tempConnecter; } m_OnlyLockAddConnecter.Exit(); }
/// <summary> /// 清除数据 /// </summary> public void Clear() { SpinLockEx.ReliableEnter(ref m_LockFlushAndPending); try { while (m_PendingBuffer.Count > 0) { m_PendingBuffer.Dequeue().Release(); } if (m_FlushBuffer.IsNull == false) { m_FlushBuffer.Release(); m_FlushBuffer = SendBuffer.NullBuffer; } // 清空 m_WaitSendSize = 0; } finally { m_LockFlushAndPending.Exit(); } }
// 可能是windows操作系统的最大可发送的字节数 //private const int PENDING_MAX_BUFFER = 96 * 1024; #endregion /// <summary> /// 如果数据满了,且缓冲区内的数据是空的则返回需要发送的数据 /// 调用Enqueue(...)后调用Dequeue(...),不能直接调用Dequeue(...) /// </summary> /// <param name="byteBuffer"></param> /// <param name="iOffset"></param> /// <param name="iLength"></param> /// <returns></returns> public void Enqueue(byte[] byteBuffer, long iOffset, long iLength) { if (byteBuffer == null) { throw new ArgumentNullException("byteBuffer", "SendQueue.Enqueue(...) - byteBuffer == null error!"); } if (iOffset < 0 || iOffset >= byteBuffer.Length) { throw new Exception("SendQueue.Enqueue(...) - iOffset < 0 || iOffset >= byteBuffer.Length error!"); } if (iLength < 0 || iLength > byteBuffer.Length) // 如果iLength == 0就返回空,如果iLength == 0就跳过 { throw new Exception("SendQueue.Enqueue(...) - iLength < 0 || iLength > byteBuffer.Length error!"); } if ((byteBuffer.Length - iOffset) < iLength) { throw new Exception("SendQueue.Enqueue(...) - ( byteBuffer.Length - iOffset ) < iLength error!"); } SpinLockEx.ReliableEnter(ref m_LockFlushAndPending); try { do { if (m_FlushBuffer.IsNull == true) { // nothing yet buffered m_FlushBuffer = SendBuffer.Instance(); if (m_FlushBuffer.IsNull == true) { throw new Exception("SendQueue.Enqueue(...) - m_FlushBuffer.IsNull == true error!"); } } // 当前已经写入的字节 long iBytesWritten = m_FlushBuffer.Write(byteBuffer, iOffset, iLength); iOffset += iBytesWritten; iLength -= iBytesWritten; // 写入需要发送的数据的大小 m_WaitSendSize += iBytesWritten; // 如果数据没有满,且数据写入完毕则退出,返回空,不添加到集合内 if (m_FlushBuffer.IsFull == true) { // 如果满了添加到集合内的尾处 m_PendingBuffer.Enqueue(m_FlushBuffer); m_FlushBuffer = SendBuffer.NullBuffer; // 置空再次请求缓存 } } while (iLength > 0); } catch (Exception e) { Logs.Error("SendQueue->Enqueue: Occurred error when enqueuing the data {0} ", e); } finally { m_LockFlushAndPending.Exit(); } }