using FirewallWindowsServer.Methods;
|
using FirewallWindowsServer.Model;
|
using System;
|
using System.Linq;
|
using System.ServiceProcess;
|
using System.Threading;
|
using System.Threading.Tasks;
|
|
namespace FirewallWindowsServer
|
{
|
public partial class Service1 : ServiceBase
|
{
|
private Task t;
|
|
public Service1()
|
{
|
InitializeComponent();
|
t = new Task(() => {
|
Start();
|
});
|
}
|
|
protected override void OnStart(string[] args)
|
{
|
t.Start();
|
}
|
|
protected override void OnStop()
|
{
|
t.Dispose();
|
}
|
|
private void Start()
|
{
|
Console.WriteLine("Hello, World!");
|
var config = Tool.GetSystemConfig<Config>("Config.json");
|
if (config.Ports == null)
|
{
|
Console.WriteLine("配置读取错误");
|
return;
|
}
|
ListenPort lp = new ListenPort();
|
Console.WriteLine("等待连接...");
|
var ports = config.Ports;
|
|
foreach (var a in ports)
|
{
|
if (a.IndexOf("-") > -1)
|
{
|
var left = int.Parse(a.Split('-')[0]);
|
var right = int.Parse(a.Split('-')[1]);
|
|
for (int i = left; i < right; i++)
|
{
|
new Thread(() =>
|
{
|
lp.InitListen(i);
|
}).Start();
|
Task.Delay(50).Wait();
|
}
|
}
|
else
|
{
|
new Thread(() =>
|
{
|
lp.InitListen(int.Parse(a));
|
}).Start();
|
Task.Delay(50).Wait();
|
}
|
|
}
|
|
int dicNumber = 0;
|
while (true)
|
{
|
if (lp.sckdic.Count != dicNumber)
|
{
|
dicNumber = lp.sckdic.Count;
|
if (lp.sckdic.Count == 0) { continue; }
|
System.Net.Sockets.Socket sk = lp.sckdic.Last().Value;
|
string guid = lp.sckdic.Last().Key;
|
Console.WriteLine("【{0}】已连接", guid);
|
lp.ReceiveMsg(sk, guid);
|
}
|
Task.Delay(500).Wait(); //间隔500ms监听一次,避免无意义的占用资源
|
}
|
}
|
}
|
}
|