Exemplo n.º 1
0
        public void HandlerPackage(JSNPackageIn pkg)
        {
            if (pkg == null)
            {
                log.Error("Package can not null!");
                return;
            }

            int             code    = pkg.Code;
            IPackageHandler handler = null;

            if (m_packagesHandlers.ContainsKey(code))
            {
                handler = m_packagesHandlers[code];
            }
            else
            {
                log.ErrorFormat("Receive package's code is not exists! Code: {0}", code);
                log.Error(Marshal.ToHexDump(string.Format("Code: {0}", code), pkg.Buffer));
            }

            if (handler == null)
            {
                return;
            }

            long timeUsed = Environment.TickCount;

            try
            {
                handler.HandlePacket(m_client, pkg);
            }
            catch (Exception ex)
            {
                string ep = m_client.Socket.RemoteEndPoint;
                log.ErrorFormat("Error while processing package (handler={0}   RemoteEndPoint={1})", handler.GetType().FullName, ep);
                log.Error("Handle package error!", ex);
            }

            timeUsed = Environment.TickCount - timeUsed;

            log.InfoFormat("Package process time: {0}ms", timeUsed);

            if (timeUsed > 1000)
            {
                string source = m_client.Socket.RemoteEndPoint;
                log.WarnFormat("({0}) Handle package thread {1} {2} took {3}ms!", source, System.Threading.Thread.CurrentThread.ManagedThreadId, handler, timeUsed);
            }
        }