修复控制台点击阻塞问题;

添加程序关闭指令入口;
This commit is contained in:
小白猿
2023-06-19 21:44:21 +08:00
parent 6f5ae1664b
commit e3a73c0151
4 changed files with 72 additions and 18 deletions
+47 -15
View File
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace BarrageGrab
@@ -14,24 +15,26 @@ namespace BarrageGrab
static WsBarrageService server = null;
static void Main(string[] args)
{
SetConsoleCtrlHandler(cancelHandler, true);//捕获控制台关闭
Console.Title = "抖音弹幕监听推送";
SetConsoleCtrlHandler(cancelHandler, true);//捕获控制台关闭
Console.Title = "抖音弹幕监听推送";
DisableQuickEditMode();
bool exited = false;
server = new WsBarrageService();
server.StartListen();
while (true)
server.StartListen();
server.OnClose += (s, e) =>
{
var input = Console.ReadKey();
switch (input.Key)
{
case ConsoleKey.Escape: goto end; break;
}
}
//退出程序
exited = true;
};
end:
server.Close();
Console.WriteLine("服务器已正常关闭,按任意键结束...");
Console.ReadKey();
while (!exited)
{
Thread.Sleep(500);
}
Console.WriteLine("服务器已关闭...");
//退出程序,不显示 按任意键退出
Environment.Exit(0);
}
@@ -54,5 +57,34 @@ namespace BarrageGrab
}
return false;
}
const int STD_INPUT_HANDLE = -10;
const uint ENABLE_QUICK_EDIT_MODE = 0x0040;
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern IntPtr GetStdHandle(int hConsoleHandle);
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint mode);
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint mode);
public static void EnableQuickEditMode()
{
IntPtr hStdin = GetStdHandle(STD_INPUT_HANDLE);
uint mode;
GetConsoleMode(hStdin, out mode);
mode |= ENABLE_QUICK_EDIT_MODE;
SetConsoleMode(hStdin, mode);
}
public static void DisableQuickEditMode()
{
IntPtr hStdin = GetStdHandle(STD_INPUT_HANDLE);
uint mode;
GetConsoleMode(hStdin, out mode);
mode &= ~ENABLE_QUICK_EDIT_MODE;
SetConsoleMode(hStdin, mode);
}
}
}
+23 -2
View File
@@ -31,6 +31,11 @@ namespace BarrageGrab
WssBarrageGrab grab = new WssBarrageGrab();
Appsetting Appsetting = Appsetting.Current;
bool debug = false;
/// <summary>
/// 服务关闭后触发
/// </summary>
public event EventHandler OnClose;
public WsBarrageService()
{
@@ -399,6 +404,22 @@ namespace BarrageGrab
socketList[clientUrl].Socket = socket;
}
//接收指令
socket.OnMessage = (message) =>
{
try
{
var cmdPack = JsonConvert.DeserializeObject<Command>(message);
if (cmdPack == null) return;
if (cmdPack.Cmd == CommandCode.Close)
{
this.Close();
}
}
catch (Exception) {}
};
socket.OnClose = () =>
{
socketList.Remove(clientUrl);
@@ -440,7 +461,7 @@ namespace BarrageGrab
}
/// <summary>
/// 关闭服务器连接
/// 关闭服务器连接,并关闭系统代理
/// </summary>
public void Close()
{
@@ -449,7 +470,7 @@ namespace BarrageGrab
socketServer.Dispose();
grab.Dispose();
console.WriteLine("服务器已关闭...");
this.OnClose?.Invoke(this, EventArgs.Empty);
}
class UserState
+1 -1
View File
@@ -81,7 +81,7 @@ namespace BarrageGrab
public void Dispose()
{
proxy.Dispose();
proxy.Dispose();
}
//域名过滤器
+1
View File
@@ -108,6 +108,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Appsetting.cs" />
<Compile Include="JsonEntity\Command.cs" />
<Compile Include="Proxy\EventArgs\HttpResponseEventArgs.cs" />
<Compile Include="Proxy\EventArgs\WsMessageEventArgs.cs" />
<Compile Include="Proxy\ISystemProxy.cs" />