using LJLib.TcpHandle;
using JLHHJSvr;
using System;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.ServiceProcess;
using System.Threading;
using LJLib.D;
using LJLib.DAL.SQL;
using LJLib.HttpServer;
using LJLib.Tools.File;
using JLHHJSvr.BLL;

namespace JLHHJSvr
{
    partial class JLHHJSvr : ServiceBase
    {
        private TcpListener _tcpListener = null;
        private LJHttpServer _httpSvr = null;
        private static bool running = false;

        public JLHHJSvr()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                DbSqlHelper.Add(new SqlServerEngine());

                GlobalVar.webapp = false;
                GlobalVar.Init();

                XmlConfig xmlconfig = new XmlConfig();

                

                var strport = xmlconfig.GetXmlFileValue(GlobalVar.XmlFile, string.Empty, "port", "9075");
                var strHttpPort = xmlconfig.GetXmlFileValue(GlobalVar.XmlFile, string.Empty, "httpport", "9076");
                int port = int.Parse(strport);
                int httpport = int.Parse(strHttpPort);

                startHttpListener(httpport);

                _tcpListener = new TcpListener(IPAddress.Any, port);
                _tcpListener.Start();
                ThreadPool.QueueUserWorkItem((state) =>
                {
                    running = true;
                    // 侦听线程
                    Thread.CurrentThread.IsBackground = false;
                    try
                    {
                        while (running)
                        {
                            if (_tcpListener.Pending())
                            {
                                var c = _tcpListener.AcceptTcpClient();
                                ThreadPool.QueueUserWorkItem((st) =>
                                {
                                    try
                                    {
                                        Thread.CurrentThread.IsBackground = true;
                                        var handler = new P1Server(c, GlobalVar.server);
                                        handler.Handle();
                                    }
                                    catch (Exception ex)
                                    {
                                        Trace.Write("服务处理线程(直连)异常退出:" + ex.ToString());
                                    }
                                    DebugHelper.PrintAll();
                                });
                            }
                            else
                            {
                                Thread.Sleep(100);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace.Write("主侦听线程异常退出:" + ex.ToString());
                    }
                });
            }
            catch (Exception ex)
            {
                Trace.Write(ex.ToString());
            }
        }

        protected override void OnStop()
        {
            running = false;
            if (_httpSvr != null)
            {
                _httpSvr.Stop();
            }
        }
        private void startHttpListener(int port)
        {
#if DEBUG
            _httpSvr = new SimpleHttpServer(port, GlobalVar.server, new GlobalVar.ParkFileModel());
#else
            _httpSvr = new SimpleHttpServer(port, GlobalVar.server, new GlobalVar.ParkFileModel());
#endif
            _httpSvr.Listen();
        }
    }
}
