Beispiel #1
0
        public int Parse(LPLoopBuf loopBuf)
        {
            bool   result      = false;
            int    parseCount  = -1;
            UInt16 MsgId       = 0;
            UInt16 MsgSize     = 0;
            int    MsgHeadSize = 4;

            byte[] MsgIdBytes = new byte[MsgHeadSize];

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

            if (loopBuf.GetTotalReadableLen() >= MsgHeadSize)
            {
                if (LOG_ERROR(MsgIdBytes != null))
                {
                    goto Exit0;
                }
                result = loopBuf.Read(MsgIdBytes, 0, MsgHeadSize, false);
                if (LOG_ERROR(result))
                {
                    goto Exit0;
                }

                MsgId = (UInt16)(MsgIdBytes[0] | MsgIdBytes[1]);
                if (LOG_ERROR(MsgId > LPProtocol.min_message_id && MsgId < LPProtocol.max_message_id))
                {
                    goto Exit0;
                }

                MsgSize = (UInt16)(MsgIdBytes[2] | MsgIdBytes[3]);
                if (loopBuf.GetTotalReadableLen() >= MsgSize)
                {
                    parseCount = MsgSize;
                    if (SUCCESS(true))
                    {
                        goto Exit1;
                    }
                }
            }

            parseCount = 0;
            if (SUCCESS(true))
            {
                goto Exit1;
            }

Exit0:
            parseCount = -1;

Exit1:
            return(parseCount);
        }
Beispiel #2
0
 public static void GenException(int nType)
 {
     if(nType == 0)
     {
         int i = 0;
         int j = 10 / i;
     }
     else if(nType == 1)
     {
         LPLoopBuf lb = null;
         lb.FinishRead(3);
     }
 }
Beispiel #3
0
        public override bool UnInit()
        {
            PTF_ERROR(m_bInit);

            if (m_bRun)
            {
                m_bRun = false;
                m_oThread.Join();
            }

            m_oThread = null;
            _CloseFile();
            m_oLoopBuf = null;

            return(true);
        }
Beispiel #4
0
        internal bool Init(ILPNetMessageHandler netMessageHandler, int eventListCount)
        {
            bool result = false;

            if (LOG_ERROR(eventListCount > 0))
            {
                goto Exit0;
            }
            if (LOG_ERROR(netMessageHandler != null))
            {
                goto Exit0;
            }

            m_NetMessageHandler = netMessageHandler;

            m_EventBuf = new LPLoopBuf();
            if (LOG_ERROR(m_EventBuf != null))
            {
                goto Exit0;
            }

            result = m_EventBuf.Init(LP.NetEventBufSize);
            if (LOG_ERROR(result))
            {
                goto Exit0;
            }

            m_TempPacketBuf = new byte[LP.MaxPacketLen];
            if (LOG_ERROR(m_TempPacketBuf != null))
            {
                goto Exit0;
            }

            m_EventListCount = eventListCount;

            m_EventLists = new LPBaseList[m_EventListCount];
            if (LOG_ERROR(m_EventLists != null))
            {
                goto Exit0;
            }
            for (int i = 0; i < m_EventListCount; ++i)
            {
                m_EventLists[i] = new LPBaseList();
                if (LOG_ERROR(m_EventLists[i] != null))
                {
                    goto Exit0;
                }
            }

            m_EventListLocker = new object[m_EventListCount];
            if (LOG_ERROR(m_EventListLocker != null))
            {
                goto Exit0;
            }
            for (int i = 0; i < m_EventListCount; ++i)
            {
                m_EventListLocker[i] = new object();
                if (LOG_ERROR(m_EventListLocker[i] != null))
                {
                    goto Exit0;
                }
            }

            return(true);

Exit0:
            return(false);
        }
Beispiel #5
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 #6
0
        private static void _ThreadProc(Object param)
        {
            bool bResult                 = false;
            int  nSleepMicSec            = 10;       // 每次睡眠毫秒数
            int  nFlushIntervalSec       = 3000;     // flush间隔(毫秒)
            int  nLastFlushTimeMicSecInc = 0;        // 上次flush后的时间增量(毫秒)
            int  nStopRunWaitCount       = 0;

            int          nOnceReadableLen = 0;
            LPFileLogger oFileLogger      = null;
            LPLoopBuf    oLoopBuf         = null;

            int nLen = 0;

            byte[] lenBytes = null;

            oFileLogger = (LPFileLogger)param;
            if (PTF_ERROR(oFileLogger != null))
            {
                goto Exit0;
            }
            oLoopBuf = oFileLogger.m_oLoopBuf;
            if (PTF_ERROR(oLoopBuf != null))
            {
                goto Exit0;
            }

            lenBytes = new byte[sizeof(int)];
            if (PTF_ERROR(lenBytes != null))
            {
                goto Exit0;
            }

            while (oFileLogger.m_bRun || oLoopBuf.GetOnceReadableLen() > 0)
            {
                //防止LPLoopBuf出错造成不退出(等待500 * 10毫米)
                if (!oFileLogger.m_bRun)
                {
                    ++nStopRunWaitCount;
                    if (nStopRunWaitCount > 500)
                    {
                        PTF_ERROR(false);
                        break;
                    }
                }

                while (oLoopBuf.GetTotalReadableLen() > sizeof(int))
                {
                    bResult = oLoopBuf.Read(lenBytes, 0, sizeof(int), false);
                    PTF_ERROR(bResult);

                    nLen = BitConverter.ToInt32(lenBytes, 0);
                    if (oLoopBuf.GetTotalReadableLen() < nLen)
                    {
                        break;
                    }

                    oLoopBuf.FinishRead(sizeof(int));
                    oFileLogger._UpdateFilePointer();

                    nOnceReadableLen = oLoopBuf.GetOnceReadableLen();

                    if ((oFileLogger.m_nOutputMask & (int)EOutputType.File) > 0)
                    {
                        if (nOnceReadableLen < nLen)
                        {
                            oFileLogger.m_oFile.Write(oLoopBuf.BufBytes,
                                                      oLoopBuf.ReadInx, nOnceReadableLen);
                            oFileLogger.m_oFile.Write(oLoopBuf.BufBytes,
                                                      0, nLen - nOnceReadableLen);
                        }
                        else
                        {
                            oFileLogger.m_oFile.Write(oLoopBuf.BufBytes,
                                                      oLoopBuf.ReadInx, nLen);
                        }
                    }

                    oLoopBuf.FinishRead(nLen);

                    oFileLogger.m_bDirty = true;
                }

                if (oFileLogger.m_bErrorLog)
                {
                    oLoopBuf.FinishRead(oLoopBuf.GetTotalReadableLen());
                    oFileLogger.m_bErrorLog = false;
                }

                nLastFlushTimeMicSecInc += nSleepMicSec;
                if (nLastFlushTimeMicSecInc > nFlushIntervalSec)
                {
                    nLastFlushTimeMicSecInc = 0;
                    oFileLogger._Flush();
                }

                if (oLoopBuf.GetTotalReadableLen() <= sizeof(int))
                {
                    Thread.Sleep(nSleepMicSec);
                }
            }

Exit0:

            oFileLogger._Flush();

            return;
        }
Beispiel #7
0
        public override bool Init(string sDir, string sFileName,
                                  ELogMode eLogMode, int nOutputMask, string sPrefix, int nLogLevel, bool bUseLock)
        {
            bool          bResult = false;
            DirectoryInfo dirInfo = null;

            bResult = base.Init(sDir, sFileName, eLogMode, nOutputMask, sPrefix, nLogLevel, bUseLock);
            if (PTF_ERROR(bResult))
            {
                goto Exit0;
            }

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

            if (PTF_ERROR(m_oLoopBuf == null))
            {
                goto Exit0;
            }
            m_oLoopBuf = new LPLoopBuf();
            if (PTF_ERROR(m_oLoopBuf != null))
            {
                goto Exit0;
            }

            bResult = m_oLoopBuf.Init(CNT_LOG_BUF_SIZE);
            if (PTF_ERROR(bResult))
            {
                goto Exit0;
            }

            m_eLogMode = eLogMode;
            if (PTF_ERROR(m_eLogMode > ELogMode.Invalid))
            {
                goto Exit0;
            }
            if (PTF_ERROR(m_eLogMode < ELogMode.Total))
            {
                goto Exit0;
            }

            m_sDir      = sDir;
            m_sFileName = sFileName;
            if (PTF_ERROR(m_eLogMode > ELogMode.Invalid))
            {
                goto Exit0;
            }
            dirInfo = Directory.CreateDirectory(m_sDir);
            if (PTF_ERROR(dirInfo != null))
            {
                goto Exit0;
            }

            _UpdateFilePointer();
            if (PTF_ERROR(m_oFile != null))
            {
                goto Exit0;
            }

            m_bRun    = true;
            m_oThread = new Thread(_ThreadProc);
            m_oThread.Start(this);

            m_bInit = true;

            return(true);

Exit0:
            return(false);
        }