using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using Grpc.Core;
using Rpcscanning;
using RpcScannerServer;

namespace ScanningRpcService
{
    public partial class RpcScanningWindowsService : ServiceBase
    {
        private Server _rpcServer = null;
        public RpcScanningWindowsService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            StartRpcServer().ConfigureAwait(false);
        }

        private async Task StartRpcServer()
        {
            _rpcServer = new Server
            {
                Services = { RpcScanner.BindService(new RpcScannerServerImplementation()) },
                Ports = { new ServerPort("localhost", 50051, ServerCredentials.Insecure) }
            };
            _rpcServer.Start();
        }

        protected override void OnStop()
        {
            _rpcServer.ShutdownAsync().Wait();
        }

        public void RunAsConsole(string[] args)
        {
            OnStart(args);
            Console.WriteLine("Rpc scanning service is running");
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
            OnStop();
        }
    }
}

