Beispiel #1
0
        /// <summary>
        /// 切包
        /// </summary>
        private void SplitPackets()
        {
            try
            {
                int offset = 0; //recvBuffer的整体读取游标
                while (m_recvUnreadBytes > NetEnCoder.GetIntLength())
                {
                    try
                    {
                        //备注:消息协议=长度(nCode和body所占字节长度) + sessionId +  nCode + body
                        int nLength = NetEnCoder.DecodeInt(m_recvBuffer, ref offset);//nocde + body 所占字节数
                        m_recvUnreadBytes -= offset;

                        if (m_recvUnreadBytes >= nLength)
                        {
                            uint uSession = NetEnCoder.DecodeUInt(m_recvBuffer, ref offset);

                            uint uCode = NetEnCoder.DecodeUInt(m_recvBuffer, ref offset);

                            int    nCount = nLength - 2 * NetEnCoder.GetIntLength();
                            object msg    = PBEnCoder.Decode(uCode, m_recvBuffer, offset, nCount);

                            TcpPacket packet = new TcpPacket(uSession, uCode, msg);
                            lock (m_recvQueueLocker)
                            {
                                LoggerHelper.Log(packet.ToString());
                                m_recvQueue.Enqueue(packet);
                            }

                            offset            += nCount;
                            m_recvUnreadBytes -= nLength;
                        }
                        else
                        {
                            m_recvUnreadBytes += offset;
                            offset            -= NetEnCoder.GetIntLength();
                            break;
                        }
                    }
                    catch (Exception e)
                    {
                        LoggerHelper.Except(e);
                        break;
                    }
                }

                // 整理 RecvBuffer, 将buffer 内容前移
                Buffer.BlockCopy(m_recvBuffer, offset, m_recvBuffer, 0, m_recvUnreadBytes);
            }
            catch (Exception e)
            {
                LoggerHelper.Except(e);
                LoggerHelper.Critical("SplitPackets error.");
                Close();
            }
        }
Beispiel #2
0
    public static T Get <T>(this T[] array, int index)
    {
        T local2;

        if (array == null)
        {
            LoggerHelper.Critical("Array is null.", true);
            local2 = default(T);
            return((local2 == null) ? GetDefaultValue <T>() : default(T));
        }
        if (array.Length <= index)
        {
            LoggerHelper.Critical(string.Format("Index '{0}' is out of range.", index), true);
            local2 = default(T);
            return((local2 == null) ? GetDefaultValue <T>() : default(T));
        }
        return(array[index]);
    }
Beispiel #3
0
    public static TValue Get <TKey, TValue>(this IDictionary <TKey, TValue> dictionary, TKey key)
    {
        TValue local2;

        if (dictionary == null)
        {
            LoggerHelper.Critical("Dictionary is null.", true);
            local2 = default(TValue);
            return((local2 == null) ? GetDefaultValue <TValue>() : default(TValue));
        }
        if (!dictionary.ContainsKey(key))
        {
            LoggerHelper.Critical(string.Format("Key '{0}' is not exist.", key), true);
            local2 = default(TValue);
            return((local2 == null) ? GetDefaultValue <TValue>() : default(TValue));
        }
        return(dictionary[key]);
    }
Beispiel #4
0
    public static T Get <T>(this List <T> list, int index)
    {
        T local2;

        if (list == null)
        {
            LoggerHelper.Critical("List is null.", true);
            local2 = default(T);
            return((local2 == null) ? GetDefaultValue <T>() : default(T));
        }
        if (list.Count <= index)
        {
            LoggerHelper.Critical(string.Format("Index '{0}' is out of range.", index), true);
            local2 = default(T);
            return((local2 == null) ? GetDefaultValue <T>() : default(T));
        }
        return(list[index]);
    }