public static void RegisterMessage(int messageid, long threadid, Type handler, ProtoBuf.IExtensible msgInstance)
        {
            log.Debug("注册消息处理器 id: " + messageid + " type: " + handler.Name + " msgInstance:" + msgInstance.GetType().Name);

            MessageHandlers[messageid] = new MessageBean()
            {
                MsgId = messageid, ThreadID = threadid, Handler = handler, MsgInstance = msgInstance
            };
        }
Beispiel #2
0
 public override void Run()
 {
     session._Action();
     if (!"favicon.ico".Equals(session.Http_Url))
     {
         if (log.IsDebugEnabled())
         {
             log.Debug("Create Http Socket Remote Socket LocalEndPoint:" + session.LocalEndPoint + " RemoteEndPoint:" + session.RemoteEndPoint.ToString());
         }
         HttpActionBean httpbean = null;
         if (!httpServer.httpHandlers.TryGetValue(session.Http_Url, out httpbean))
         {
             httpServer.httpHandlers.TryGetValue("*", out httpbean);
         }
         if (httpbean != null)
         {
             HttpActionRun taskmodel = new HttpActionRun(session, httpbean.ihttpHandler);
             ThreadPool.AddTask(httpbean.threadId, taskmodel);
             return;
         }
         if (log.IsErrorEnabled())
         {
             log.Error("未找到监听状态:" + session.Http_Url, new Exception("未找到监听状态:" + session.Http_Url));
         }
     }
     session.Close();
 }
Beispiel #3
0
        /// <summary>
        /// 线程处理器
        /// </summary>
        protected virtual void Run()
        {
            while (!this.IsDelete)
            {
                while ((taskQueue.Count > 0))
                {
                    TaskModel task = null;
                    /*获取并移除当前任务*/
                    if (!taskQueue.TryDequeue(out task))
                    {
                        break;
                    }

                    /* 执行任务 */
                    long submitTime = TimeUtil.CurrentTimeMillis();
                    try
                    {
                        if (!task.Cancel)
                        {
                            task.Run();
                        }
                    }
                    catch (Exception e)
                    {
                        if (log.IsErrorEnabled())
                        {
                            log.Error(System.Threading.Thread.CurrentThread.Name + " 执行任务:" + task.GetType().FullName + " 遇到错误", e);
                        }
                    }
                    if (task is TimerTaskModel)
                    {
                        ((TimerTaskModel)task).LastRun = true;
                    }

                    long timeL1 = TimeUtil.CurrentTimeMillis() - submitTime;

                    if (!task.GetType().FullName.StartsWith("Net.Sz.Framework"))
                    {
                        if (timeL1 > 10)
                        {
                            if (log.IsDebugEnabled())
                            {
                                log.Debug(System.Threading.Thread.CurrentThread.Name + " 完成了任务:" + task.GetType().FullName + " 执行耗时:" + timeL1);
                            }
                        }
                    }
                    task = null;
                }
                are.Reset();
                /*队列为空等待200毫秒继续*/
                are.WaitOne(200);
            }
            if (log.IsErrorEnabled())
            {
                log.Error(DateTime.Now.NowString() + " " + System.Threading.Thread.CurrentThread.Name + " Destroying");
            }
        }
Beispiel #4
0
 /// <summary>
 ///
 /// </summary>
 public override void Connect()
 {
     if (NettyPool.ClientSessionHandler == null)
     {
         throw new Exception("NettyPool.SessionHandler 尚未初始化");
     }
     try
     {
         log.Debug("Try Connect Tcp Socket Remote:" + IP + ":" + Port);
         this._Socket = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         this._Socket.Connect(IP, Port);
         log.Debug("Connect Tcp Socket Remote Socket RemoteEndPoint:" + this.RemoteEndPoint.ToString() + " LocalEndPoint:" + this.LocalEndPoint.ToString());
         this.SetSocket();
     }
     catch (SocketException se)
     {
         this.Close("Try Connect Tcp Socket Remote:" + IP + ":" + Port + " SocketException:" + se.ToString());
     }
     catch (Exception ex)
     {
         NettyPool.ClientSessionHandler.ExceptionCaught(this, ex);
     }
 }
Beispiel #5
0
 /// <summary>
 /// 关闭并释放资源
 /// </summary>
 /// <param name="msg"></param>
 public virtual void Close(string msg)
 {
     lock (this)
     {
         if (!this.IsDispose)
         {
             this.IsDispose = true;
             try
             {
                 if (log.IsDebugEnabled())
                 {
                     log.Debug("Close Tcp Socket Remote Socket LocalEndPoint:" + LocalEndPoint + " RemoteEndPoint:" + RemoteEndPoint + " : " + msg);
                 }
                 if (isServer)
                 {
                     NettyPool.SessionHandler.ChannelUnregistered(this, msg);
                 }
                 else
                 {
                     NettyPool.ClientSessionHandler.ChannelUnregistered(this, msg);
                 }
                 IOSession outsession = null;
                 NettyPool.Sessions.TryRemove(this.ID, out outsession);
                 try { this._Socket.Close(); }
                 catch { }
                 IDisposable disposable = this._Socket;
                 if (disposable != null)
                 {
                     disposable.Dispose();
                 }
                 this.Buffers = null;
                 GC.SuppressFinalize(this);
             }
             catch (Exception) { }
         }
     }
 }
Beispiel #6
0
        /// <summary>
        /// 根据指定的文件动态编译获取实例
        /// <para>如果需要加入调试信息,加入代码 System.Diagnostics.Debugger.Break();</para>
        /// <para>如果传入的是目录。默认只会加载目录中后缀“.cs”文件</para>
        /// </summary>
        /// <param name="paths">
        /// 可以是目录也可以是文件路径
        /// </param>
        /// <param name="extensionNames">需要加载目录中的文件后缀</param>
        /// <param name="dllName">加载的附加DLL文件的路径,绝对路径</param>
        public List <String> LoadCSharpFile(String[] paths, List <String> extensionNames, params String[] dllName)
        {
            List <String> retStrs = new List <String>();

            if (extensionNames == null)
            {
                extensionNames = csExtensionNames;
            }


            var asss = AppDomain.CurrentDomain.GetAssemblies();

            foreach (var item in asss)
            {
                try
                {
                    if (!item.ManifestModule.IsResource() && !"<未知>".Equals(item.ManifestModule.FullyQualifiedName))
                    {
                        String ext = System.IO.Path.GetExtension(item.ManifestModule.FullyQualifiedName).ToLower();
                        if (exts.Contains(ext))
                        {
                            ddlNames.Add(item.ManifestModule.FullyQualifiedName);
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (log.IsErrorEnabled())
                    {
                        log.Error("查找需要的dll路径错误1:" + item.ManifestModule.FullyQualifiedName, ex);
                    }
                }
            }

            foreach (var item in dllName)
            {
                try
                {
                    String ext = System.IO.Path.GetExtension(item).ToLower();
                    if (exts.Contains(ext))
                    {
                        ddlNames.Add(item);
                    }
                }
                catch (Exception ex) { if (log.IsErrorEnabled())
                                       {
                                           log.Error("查找需要的dll路径错误2:" + item, ex);
                                       }
                }
            }
            if (log.IsDebugEnabled())
            {
                foreach (var item in ddlNames)
                {
                    try
                    {
                        log.Debug("加载依赖程序集:" + System.IO.Path.GetFileName(item));
                    }
                    catch (Exception) { if (log.IsErrorEnabled())
                                        {
                                            log.Error("加载依赖程序集:" + item);
                                        }
                    }
                }
            }
            List <String> fileNames = new List <String>();

            ActionPath(paths, extensionNames, ref fileNames);
            if (fileNames.Count == 0)
            {
                retStrs.Add("目录不存在任何脚本文件"); return(retStrs);
            }

            CSharpCodeProvider provider  = new CSharpCodeProvider();
            CompilerParameters parameter = new CompilerParameters();

            //不输出编译文件
            parameter.GenerateExecutable = false;
            //生成调试信息
            parameter.IncludeDebugInformation = true;
            //需要调试必须输出到内存
            parameter.GenerateInMemory = true;
            //添加需要的程序集
            parameter.ReferencedAssemblies.AddRange(ddlNames.ToArray());
            //System.Environment.CurrentDirectory
            CompilerResults result = provider.CompileAssemblyFromFile(parameter, fileNames.ToArray());//根据制定的文件加载脚本

            if (result.Errors.HasErrors)
            {
                var item = result.Errors.GetEnumerator();
                while (item.MoveNext())
                {
                    if (log.IsErrorEnabled())
                    {
                        log.Error("动态加载文件出错了!" + item.Current.ToString());
                    }
                }
            }
            else
            {
                Dictionary <string, Dictionary <string, object> > tempInstances = new Dictionary <string, Dictionary <string, object> >();
                ActionAssembly(result.CompiledAssembly, tempInstances, retStrs);
                if (retStrs.Count == 0 && tempInstances.Count > 0)
                {
                    this.ScriptInstances = tempInstances;
                }
            }
            return(retStrs);
        }