Beispiel #1
0
        /// <summary>
        /// 初始化服务器
        /// </summary>
        internal NettyServer(string ip = "0.0.0.0", int port = 9527, int threadcount = 2)
        {
            System.Net.IPEndPoint localEP = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(ip), port);
            this._IP = localEP;
            try
            {
                if (log.IsInfoEnabled())
                {
                    log.Info(string.Format("Start Listen Tcp Socket -> {0}:{1} ", ip, port));
                }
                /*绑定*/
                this._Listeners = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
                this._Listeners.Bind(this._IP);
                this._Listeners.Listen(5000);
                /*创建线程处理*/
                MessageThreadID = Framework.SzThreading.ThreadPool.GetThreadModel("Netty Session Pool Thread:" + ip + ":" + port, SzThreading.SzThread.ThreadType.Sys, threadcount);

                System.Net.Sockets.SocketAsyncEventArgs sea = new System.Net.Sockets.SocketAsyncEventArgs();
                sea.Completed += new EventHandler <System.Net.Sockets.SocketAsyncEventArgs>(this.AcceptAsync_Async);
                this.AcceptAsync(sea);
            }
            catch (Exception ex)
            {
                if (log.IsErrorEnabled())
                {
                    log.Error("Start Listen Tcp Socket Exception", ex);
                }
                this.Dispose();
            }
        }
Beispiel #2
0
 public void InitializeDatabase()
 {
     try
     {
         if (log.IsInfoEnabled())
         {
             log.Info("开始创建数据库");
         }
         if (log.IsInfoEnabled())
         {
             log.Info(this.Database.CreateIfNotExists() + "");
         }
         if (log.IsInfoEnabled())
         {
             log.Info("创建数据库完成");
         }
     }
     catch (Exception ex)
     {
         if (log.IsErrorEnabled())
         {
             log.Error("创建数据库失败" + ex);
         }
     }
 }
Beispiel #3
0
 /// <summary>
 /// 初始化线程模型
 /// </summary>
 /// <param name="name">线程名称</param>
 /// <param name="count">线程数量</param>
 public SzThread(String name, ThreadType threadtype = ThreadType.User, Int32 count = 1)
 {
     this.threadType = threadtype;
     this.Name       = name;
     this.ID         = ids.GetId();
     if (count == 1)
     {
         System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
         thread.IsBackground = true;
         //仅次于最高级
         thread.Priority = ThreadPriority.AboveNormal;
         thread.Name     = "<" + name + "线程>";
         thread.Start();
         if (log.IsInfoEnabled())
         {
             log.Info("初始化 " + thread.Name);
         }
     }
     else
     {
         for (int i = 0; i < count; i++)
         {
             System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
             thread.IsBackground = true;
             //仅次于最高级
             thread.Priority = ThreadPriority.AboveNormal;
             thread.Name     = "<" + name + "线程" + "_" + (i + 1) + ">";
             thread.Start();
             if (log.IsInfoEnabled())
             {
                 log.Info("初始化 " + thread.Name);
             }
         }
     }
 }
 public void InitializeDatabase()
 {
     try
     {
         log.Info("开始创建数据库");
         log.Info(this.Database.CreateIfNotExists() + "");
         log.Info("创建数据库完成");
     }
     catch (Exception ex)
     {
         log.Error("创建数据库失败" + ex);
     }
 }
Beispiel #5
0
        /// <summary>
        /// 处理绑定,路由
        /// </summary>
        /// <param name="url">不区分大小写的路由</param>
        /// <param name="handler">处理函数</param>
        /// <param name="threadCount">如果需要并发处理,线程数量大于1,不能等于或小于0</param>
        public void AddHandler(string url, IHttpHandler handler, int threadCount = 1)
        {
            url = url.ToLower();

            if (!this.httpHandlers.ContainsKey(url))
            {
                long tId = ThreadPool.GetThreadModel("http -> " + url, SzThreading.SzThread.ThreadType.Sys, threadCount);
                this.httpHandlers[url] = new HttpActionBean()
                {
                    ihttpHandler = handler, threadId = tId
                };
            }

            log.Info(string.Format("Http Listener -> {0}:{1}/{2} threadcount -> {3}", this._IP.Address.ToString(), this._IP.Port, url, threadCount));
        }
Beispiel #6
0
 public TimerThread()
 {
     System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
     thread.IsBackground = true;
     thread.Priority     = ThreadPriority.Highest;
     thread.Name         = "< 定时器线程处理线程 >";
     thread.Start();
     if (log.IsInfoEnabled())
     {
         log.Info("初始化 " + thread.Name);
     }
 }
Beispiel #7
0
        /// <summary>
        /// 获取一个线程模型
        /// </summary>
        /// <param name="tid"></param>
        /// <returns></returns>
        static public SzThread GetThreadModel(long tid)
        {
            SzThread tm = null;

            if (tid == 0)
            {
                if (log.IsInfoEnabled())
                {
                    log.Info("艹艹艹艹线程id==0艹艹艹");
                }
            }
            threads.TryGetValue(tid, out tm);
            return(tm);
        }
        /// <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 void LoadCSharpFile(string[] paths, List <String> extensionNames, params string[] dllName)
        {
            if (extensionNames == null)
            {
                extensionNames = csExtensionNames;
            }

            List <String> fileNames = new List <String>();

            ActionPath(paths, extensionNames, ref fileNames);
            if (fileNames.Count == 0)
            {
                return;
            }

            var asss = AppDomain.CurrentDomain.GetAssemblies();

            foreach (var item in asss)
            {
                if (!item.ManifestModule.IsResource() && item.ManifestModule.FullyQualifiedName.EndsWith(".dll") || item.ManifestModule.FullyQualifiedName.EndsWith(".DLL"))
                {
                    ddlNames.Add(item.ManifestModule.FullyQualifiedName);
                }
            }

            foreach (var item in dllName)
            {
                if (item.EndsWith(".dll") || item.EndsWith(".DLL"))
                {
                    ddlNames.Add(item);
                }
            }
            foreach (var item in ddlNames)
            {
                try
                {
                    if (log.IsInfoEnabled())
                    {
                        log.Info("加载DLL:" + item);
                    }
                }
                catch { }
            }

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

            //不输出编译文件
            parameter.GenerateExecutable = false;
            //生成调试信息
            parameter.IncludeDebugInformation = true;
            //需要调试必须输出到内存
            parameter.GenerateInMemory = true;
            //添加需要的程序集
            parameter.ReferencedAssemblies.AddRange(ddlNames.ToArray());
            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
            {
                ActionAssembly(result.CompiledAssembly);
            }
        }