Beispiel #1
0
        internal bool PushRecvEvent(LPSocker socker, LPLoopBuf recvLoopBuf, int len)
        {
            bool           result     = false;
            int            retryCount = 0;
            int            lineSize   = 0;
            LPRecvNetEvent recvEvent  = null;

            if (LOG_ERROR(socker != null))
            {
                goto Exit0;
            }
            if (LOG_ERROR(recvLoopBuf != null))
            {
                goto Exit0;
            }

            recvEvent = (LPRecvNetEvent)_CreateEvent(ENetEventType.Recv);
            if (LOG_ERROR(recvEvent != null))
            {
                goto Exit0;
            }

            while (m_EventBuf.GetTotalWritableLen() < len)
            {
                LP.Logger.P_WRN("event buf not enough, sleep and try again !");
                Thread.Sleep(1);
                ++retryCount;

                // 超过10秒,丢弃
                if (retryCount > 10000)
                {
                    recvLoopBuf.FinishRead(len);
                    if (LOG_ERROR(false))
                    {
                        goto Exit0;
                    }
                }
            }

            lineSize = recvLoopBuf.GetOnceReadableLen();
            if (lineSize > len)
            {
                lineSize = len;
            }

            result = m_EventBuf.Write(recvLoopBuf.BufBytes, recvLoopBuf.ReadInx, lineSize);
            LOG_ERROR(result);

            recvLoopBuf.FinishRead(lineSize);

            if (lineSize < len)
            {
                result = m_EventBuf.Write(recvLoopBuf.BufBytes, recvLoopBuf.ReadInx, len - lineSize);
                LOG_ERROR(result);

                recvLoopBuf.FinishRead(len - lineSize);
            }

            recvEvent.Flag       = socker.ID;
            recvEvent.RecvLength = len;
            recvEvent.Socker     = socker;

            lock (m_EventListLocker[recvEvent.Flag % m_EventListCount])
            {
                result = m_EventLists[recvEvent.Flag % m_EventListCount].PushRear(recvEvent);
                if (LOG_ERROR(result))
                {
                    goto Exit0;
                }
            }

            return(true);

Exit0:
            return(false);
        }
Beispiel #2
0
        protected override void LogText(byte[] logBytes, int nStartIndex, int nLen)
        {
            bool bResult     = false;
            int  nSleepCount = 0;

            int nTotalLen = 0;

            byte[] totalLenBytes = null;

            string sMsgPrefix = null;

            byte[] msgPrefixBytes = null;

            if (PTF_ERROR(logBytes != null))
            {
                goto Exit0;
            }
            if (PTF_ERROR(logBytes.Length >= nLen + nStartIndex))
            {
                goto Exit0;
            }
            if (PTF_ERROR(m_oLoopBuf != null))
            {
                goto Exit0;
            }

            //添加时间和id编号前缀
            sMsgPrefix = string.Format("{0}[{1:00000000}]", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ++m_nMsgId);
            if (PTF_ERROR(sMsgPrefix != null))
            {
                goto Exit0;
            }
            msgPrefixBytes = Encoding.UTF8.GetBytes(sMsgPrefix.ToCharArray());
            if (PTF_ERROR(msgPrefixBytes != null))
            {
                goto Exit0;
            }

            nTotalLen = msgPrefixBytes.Length + nLen;

            if (PTF_ERROR(m_bErrorLog == false))
            {
                goto Exit0;
            }

            if (m_oLoopBuf.GetTotalWritableLen() < nTotalLen + sizeof(int))
            {
                PTF_ERROR(false);
                Thread.Sleep(1);
                while (m_oLoopBuf.GetTotalWritableLen() < nTotalLen + sizeof(int))
                {
                    Thread.Sleep(1);
                    nSleepCount++;
                    if (nSleepCount > 3000)
                    {
                        if (PTF_ERROR(false))
                        {
                            goto Exit0;
                        }
                    }
                }
            }

            totalLenBytes = BitConverter.GetBytes(nTotalLen);
            if (PTF_ERROR(totalLenBytes != null))
            {
                goto Exit0;
            }
            if (PTF_ERROR(totalLenBytes.Length == sizeof(int)))
            {
                goto Exit0;
            }

            bResult = m_oLoopBuf.Write(totalLenBytes, 0, totalLenBytes.Length);
            if (PTF_ERROR(bResult))
            {
                goto Exit0;
            }

            bResult = m_oLoopBuf.Write(msgPrefixBytes, 0, msgPrefixBytes.Length);
            if (PTF_ERROR(bResult))
            {
                goto Exit0;
            }

            bResult = m_oLoopBuf.Write(logBytes, 0, nLen);
            if (PTF_ERROR(bResult))
            {
                goto Exit0;
            }

            return;

Exit0:

            m_bErrorLog = true;
            Console.WriteLine("Log fail: {0}", logBytes);

            return;
        }