Esempio n. 1
0
        public void Update(UpdateContext updateContext)
        {
            int count = this._sessionsToRemove.Count;

            for (int i = 0; i < count; i++)
            {
                NetSessionBase        session  = this._sessionsToRemove[i];
                List <NetSessionBase> sessions = this._typeToSession[session.type];
                sessions.Remove(session);
                if (sessions.Count == 0)
                {
                    this._typeToSession.Remove(session.type);
                }
                NetSessionPool.instance.Push(session);
            }
            this._sessionsToRemove.Clear();
        }
Esempio n. 2
0
        /// <summary>
        /// 广播消息到指定的session
        /// 不支持消息转发
        /// </summary>
        /// <param name="sessionIds">session id</param>
        /// <param name="msg">消息</param>
        public void Broadcast(IEnumerable <uint> sessionIds, IMessage msg)
        {
            StreamBuffer buffer = this._bufferPool.Pop();

            NetSessionBase.EncodeMessage(buffer, msg);
            byte[] data   = buffer.GetBuffer();
            int    length = buffer.length;

            var enumerator = sessionIds.GetEnumerator();             //据说用var会减少gc

            while (enumerator.MoveNext())
            {
                uint        sid     = enumerator.Current;
                INetSession session = this.GetSession(sid);
                (( NetSessionBase )session)?.Send(data, 0, length);
            }
            enumerator.Dispose();

            this._bufferPool.Push(buffer);
        }