コード例 #1
0
 void IMessageProcessor.Configure(TcpService service)
 {
     service.Endpoints = new[] { new IPEndPoint(IPAddress.Loopback, 5999) };
 }
コード例 #2
0
        public static int Run <T>(string configuration, string[] args, IPEndPoint[] endpoints, IProtocolFactory protocolFactory)
            where T : IMessageProcessor, new()
        {
            try
            {
                AppDomain.CurrentDomain.UnhandledException += (s, e) => LogWhileDying(e.ExceptionObject);
                string name = null;
                bool   uninstall = false, install = false, benchmark = false, hasErrors = false;
                for (int i = 0; i < args.Length; i++)
                {
                    switch (args[i])
                    {
                    case "-u": uninstall = true; break;

                    case "-i": install = true; break;

                    case "-b": benchmark = true; break;

                    default:
                        if (args[i].StartsWith("-n:"))
                        {
                            name = args[i].Substring(3);
                        }
                        else
                        {
                            Console.Error.WriteLine("Unknown argument: " + args[i]);
                            hasErrors = true;
                        }
                        break;
                    }
                }
                if (hasErrors)
                {
                    Console.Error.WriteLine("Support flags:");
                    Console.Error.WriteLine("-i\tinstall service");
                    Console.Error.WriteLine("-u\tuninstall service");
                    Console.Error.WriteLine("-b\tbenchmark");
                    Console.Error.WriteLine("-n:name\toverride service name");
                    Console.Error.WriteLine("(no args) execute in console");
                    return(-1);
                }
                if (uninstall)
                {
                    Console.WriteLine("Uninstalling service...");
                    InstallerServiceName = name;
                    ManagedInstallerClass.InstallHelper(new string[] { "/u", typeof(T).Assembly.Location });
                }
                if (install)
                {
                    Console.WriteLine("Installing service...");
                    InstallerServiceName = name;
                    ManagedInstallerClass.InstallHelper(new string[] { typeof(T).Assembly.Location });
                }
                if (install || uninstall)
                {
                    Console.WriteLine("(done)");
                    return(0);
                }
                if (benchmark)
                {
                    var factory = BasicBinaryProtocolFactory.Default;
                    using (var svc = new TcpService("", new EchoProcessor(), factory))
                    {
                        svc.MaxIncomingQuota = -1;
                        Console.WriteLine("Running benchmark using " + svc.ServiceName + "....");
                        svc.StartService();
                        svc.RunEchoBenchmark(1, 500000, factory);
                        svc.RunEchoBenchmark(50, 10000, factory);
                        svc.RunEchoBenchmark(100, 5000, factory);
                        svc.StopService();
                    }
                    return(0);
                }

                if (Environment.UserInteractive)// user facing
                {
                    using (var messageProcessor = new T())
                        using (var svc = new TcpService(configuration, messageProcessor, protocolFactory))
                        {
                            svc.Endpoints = endpoints;
                            if (!string.IsNullOrEmpty(name))
                            {
                                svc.ActualServiceName = name;
                            }
                            svc.StartService();
                            Console.WriteLine("Running " + svc.ActualServiceName +
                                              " in interactive mode; press any key to quit");
                            Console.ReadKey();
                            Console.WriteLine("Exiting...");
                            svc.StopService();
                        }
                    return(0);
                }
                else
                {
                    var svc = new TcpService(configuration, new T(), protocolFactory);
                    svc.Endpoints = endpoints;
                    ServiceBase.Run(svc);
                    return(0);
                }
            }
            catch (Exception ex)
            {
                LogWhileDying(ex);
                return(-1);
            }
        }