コード例 #1
0
        public static void StopAllThread()
        {
            /*listen80.ClientTask.Stop();
             * listen80.Stop();
             * th[ 2 ].Abort();*/

            //worldThread1ms.Abort();

            /*listen80.Stop();
            *  th[ 0 ].Abort();*/
            end = true;
            realmServer.Dispose();
            authServer.Dispose();
            httpServer.Dispose();

            Process p = Process.GetCurrentProcess();

            ProcessThreadCollection procs = p.Threads;

            World.notEnded = false;
            //Thread.Sleep( 2000 );
            foreach (ProcessThread thread in procs)
            {
                Console.WriteLine("thread id {0} CPU {1}", thread.Id, thread.TotalProcessorTime.TotalMilliseconds);
            }
            p.Close();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: blockspacer/niveum
        private static IServer StartProtocol(Configuration c, ChatProtocolConfiguration pc, ServerContext ServerContext, TaskFactory Factory, TaskFactory PurifierFactory)
        {
            if (pc.OnTcp)
            {
                var s = pc.Tcp;

                if (!(s.SerializationProtocolType == SerializationProtocolType.Binary || s.SerializationProtocolType == SerializationProtocolType.Json))
                {
                    throw new InvalidOperationException("未知协议类型: " + s.SerializationProtocolType.ToString());
                }

                Func <ISessionContext, IBinaryTransformer, KeyValuePair <IServerImplementation, IStreamedVirtualTransportServer> > VirtualTransportServerFactory;
                if (s.SerializationProtocolType == SerializationProtocolType.Binary)
                {
                    VirtualTransportServerFactory = (Context, t) =>
                    {
                        var p    = ServerContext.CreateServerImplementationWithBinaryAdapter(Factory, Context);
                        var si   = p.Key;
                        var a    = p.Value;
                        var bcps = new BinaryCountPacketServer(a, CommandName => true, t);
                        return(new KeyValuePair <IServerImplementation, IStreamedVirtualTransportServer>(si, bcps));
                    };
                }
                else if (s.SerializationProtocolType == SerializationProtocolType.Json)
                {
                    VirtualTransportServerFactory = (Context, t) =>
                    {
                        var p    = ServerContext.CreateServerImplementationWithJsonAdapter(Factory, Context);
                        var si   = p.Key;
                        var a    = p.Value;
                        var bcps = new JsonLinePacketServer(a, CommandName => true, t);
                        return(new KeyValuePair <IServerImplementation, IStreamedVirtualTransportServer>(si, bcps));
                    };
                }
                else
                {
                    throw new InvalidOperationException();
                }

                var Server  = new TcpServer(ServerContext, VirtualTransportServerFactory, a => Factory.StartNew(a), a => PurifierFactory.StartNew(a));
                var Success = false;

                try
                {
                    Server.Bindings           = s.Bindings.Select(b => new IPEndPoint(IPAddress.Parse(b.IpAddress), b.Port)).ToArray();
                    Server.SessionIdleTimeout = s.SessionIdleTimeout;
                    Server.UnauthenticatedSessionIdleTimeout = s.UnauthenticatedSessionIdleTimeout;
                    Server.MaxConnections          = s.MaxConnections;
                    Server.MaxConnectionsPerIP     = s.MaxConnectionsPerIP;
                    Server.MaxUnauthenticatedPerIP = s.MaxUnauthenticatedPerIP;
                    Server.MaxBadCommands          = s.MaxBadCommands;

                    Server.Start();

                    Console.WriteLine(@"TCP/{0}服务器已启动。结点: {1}".Formats(s.SerializationProtocolType.ToString(), String.Join(", ", Server.Bindings.Select(b => b.ToString() + "(TCP)"))));

                    Success = true;
                }
                finally
                {
                    if (!Success)
                    {
                        Server.Dispose();
                    }
                }

                return(Server);
            }
            else if (pc.OnUdp)
            {
                var s = pc.Udp;

                if (!(s.SerializationProtocolType == SerializationProtocolType.Binary || s.SerializationProtocolType == SerializationProtocolType.Json))
                {
                    throw new InvalidOperationException("未知协议类型: " + s.SerializationProtocolType.ToString());
                }

                Func <ISessionContext, IBinaryTransformer, KeyValuePair <IServerImplementation, IStreamedVirtualTransportServer> > VirtualTransportServerFactory;
                if (s.SerializationProtocolType == SerializationProtocolType.Binary)
                {
                    VirtualTransportServerFactory = (Context, t) =>
                    {
                        var p    = ServerContext.CreateServerImplementationWithBinaryAdapter(Factory, Context);
                        var si   = p.Key;
                        var a    = p.Value;
                        var bcps = new BinaryCountPacketServer(a, CommandName => true, t);
                        return(new KeyValuePair <IServerImplementation, IStreamedVirtualTransportServer>(si, bcps));
                    };
                }
                else if (s.SerializationProtocolType == SerializationProtocolType.Json)
                {
                    VirtualTransportServerFactory = (Context, t) =>
                    {
                        var p    = ServerContext.CreateServerImplementationWithJsonAdapter(Factory, Context);
                        var si   = p.Key;
                        var a    = p.Value;
                        var bcps = new JsonLinePacketServer(a, CommandName => true, t);
                        return(new KeyValuePair <IServerImplementation, IStreamedVirtualTransportServer>(si, bcps));
                    };
                }
                else
                {
                    throw new InvalidOperationException();
                }

                var Server  = new UdpServer(ServerContext, VirtualTransportServerFactory, a => Factory.StartNew(a), a => PurifierFactory.StartNew(a));
                var Success = false;

                try
                {
                    Server.Bindings           = s.Bindings.Select(b => new IPEndPoint(IPAddress.Parse(b.IpAddress), b.Port)).ToArray();
                    Server.SessionIdleTimeout = s.SessionIdleTimeout;
                    Server.UnauthenticatedSessionIdleTimeout = s.UnauthenticatedSessionIdleTimeout;
                    Server.MaxConnections          = s.MaxConnections;
                    Server.MaxConnectionsPerIP     = s.MaxConnectionsPerIP;
                    Server.MaxUnauthenticatedPerIP = s.MaxUnauthenticatedPerIP;
                    Server.MaxBadCommands          = s.MaxBadCommands;

                    Server.TimeoutCheckPeriod = s.TimeoutCheckPeriod;

                    Server.Start();

                    Console.WriteLine(@"UDP/{0}服务器已启动。结点: {1}".Formats(s.SerializationProtocolType.ToString(), String.Join(", ", Server.Bindings.Select(b => b.ToString() + "(UDP)"))));

                    Success = true;
                }
                finally
                {
                    if (!Success)
                    {
                        Server.Dispose();
                    }
                }

                return(Server);
            }
            else if (pc.OnHttp)
            {
                var s = pc.Http;

                Func <ISessionContext, KeyValuePair <IServerImplementation, IHttpVirtualTransportServer> > VirtualTransportServerFactory = Context =>
                {
                    var p    = ServerContext.CreateServerImplementationWithJsonAdapter(Factory, Context);
                    var si   = p.Key;
                    var a    = p.Value;
                    var jhps = new JsonHttpPacketServer(a, CommandName => true);
                    return(new KeyValuePair <IServerImplementation, IHttpVirtualTransportServer>(si, jhps));
                };

                var Server  = new HttpServer(ServerContext, VirtualTransportServerFactory, a => Factory.StartNew(a), a => PurifierFactory.StartNew(a));
                var Success = false;

                try
                {
                    Server.Bindings           = s.Bindings.Select(b => b.Prefix).ToArray();
                    Server.SessionIdleTimeout = s.SessionIdleTimeout;
                    Server.UnauthenticatedSessionIdleTimeout = s.UnauthenticatedSessionIdleTimeout;
                    Server.MaxConnections          = s.MaxConnections;
                    Server.MaxConnectionsPerIP     = s.MaxConnectionsPerIP;
                    Server.MaxUnauthenticatedPerIP = s.MaxUnauthenticatedPerIP;
                    Server.MaxBadCommands          = s.MaxBadCommands;

                    Server.TimeoutCheckPeriod = s.TimeoutCheckPeriod;
                    Server.ServiceVirtualPath = s.ServiceVirtualPath;

                    Server.Start();

                    Console.WriteLine(@"HTTP/{0}服务器已启动。结点: {1}".Formats("Json", String.Join(", ", Server.Bindings.Select(b => b.ToString()))));

                    Success = true;
                }
                finally
                {
                    if (!Success)
                    {
                        Server.Dispose();
                    }
                }

                return(Server);
            }
            else if (pc.OnHttpStatic)
            {
                var s = pc.HttpStatic;

                var Server  = new StaticHttpServer(ServerContext, a => Factory.StartNew(a), 8 * 1024);
                var Success = false;

                try
                {
                    Server.Bindings                = s.Bindings.Select(b => b.Prefix).ToArray();
                    Server.SessionIdleTimeout      = Math.Min(s.SessionIdleTimeout, s.UnauthenticatedSessionIdleTimeout);
                    Server.MaxConnections          = s.MaxConnections;
                    Server.MaxConnectionsPerIP     = s.MaxConnectionsPerIP;
                    Server.MaxBadCommands          = s.MaxBadCommands;
                    Server.MaxUnauthenticatedPerIP = s.MaxUnauthenticatedPerIP;

                    Server.ServiceVirtualPath = s.ServiceVirtualPath;
                    Server.PhysicalPath       = s.PhysicalPath;
                    Server.Indices            = s.Indices.Split(',').Select(Index => Index.Trim(' ')).Where(Index => Index != "").ToArray();

                    Server.Start();

                    Console.WriteLine(@"HTTP静态服务器已启动。结点: {0}".Formats(String.Join(", ", Server.Bindings.Select(b => b.ToString()))));

                    Success = true;
                }
                finally
                {
                    if (!Success)
                    {
                        Server.Dispose();
                    }
                }

                return(Server);
            }
            else
            {
                throw new InvalidOperationException("未知服务器类型: " + pc._Tag.ToString());
            }
        }