2022-10-06 17:44:08 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2023-08-17 20:37:22 +08:00
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Net;
|
2022-10-06 17:44:08 +08:00
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
2023-06-19 21:44:21 +08:00
|
|
|
using System.Threading;
|
2022-10-06 17:44:08 +08:00
|
|
|
|
|
|
|
|
namespace BarrageGrab
|
|
|
|
|
{
|
|
|
|
|
public class Program
|
|
|
|
|
{
|
|
|
|
|
static void Main(string[] args)
|
2023-08-17 20:37:22 +08:00
|
|
|
{
|
|
|
|
|
if (CheckAlreadyRunning())
|
2022-10-06 17:44:08 +08:00
|
|
|
{
|
2023-08-17 20:37:22 +08:00
|
|
|
Console.WriteLine("已经有一个监听程序在运行,按任意键退出...");
|
|
|
|
|
Console.ReadKey();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WinApi.SetConsoleCtrlHandler(cancelHandler, true);//捕获控制台关闭
|
|
|
|
|
WinApi.DisableQuickEditMode();//禁用控制台快速编辑模式
|
|
|
|
|
Console.Title = "抖音弹幕监听推送";
|
2023-11-14 20:29:18 +08:00
|
|
|
|
|
|
|
|
if(Appsetting.Current.HideConsole)
|
|
|
|
|
{
|
|
|
|
|
var hWnd = WinApi.FindWindow(null, Console.Title);
|
|
|
|
|
if (hWnd != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
WinApi.ShowWindow(hWnd,WinApi.CmdShow.SW_HIDE);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-17 20:37:22 +08:00
|
|
|
|
|
|
|
|
bool exited = false;
|
|
|
|
|
AppRuntime.WssService.StartListen();
|
|
|
|
|
AppRuntime.WssService.OnClose += (s, e) =>
|
|
|
|
|
{
|
|
|
|
|
//退出程序
|
2023-06-19 21:44:21 +08:00
|
|
|
exited = true;
|
|
|
|
|
};
|
2022-10-06 17:44:08 +08:00
|
|
|
|
2023-06-19 21:44:21 +08:00
|
|
|
while (!exited)
|
2023-08-17 20:37:22 +08:00
|
|
|
{
|
2023-06-19 21:44:21 +08:00
|
|
|
Thread.Sleep(500);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 20:37:22 +08:00
|
|
|
Console.WriteLine("服务器已关闭...");
|
|
|
|
|
|
2023-06-19 21:44:21 +08:00
|
|
|
//退出程序,不显示 按任意键退出
|
2023-08-17 20:37:22 +08:00
|
|
|
Environment.Exit(0);
|
2022-10-06 17:44:08 +08:00
|
|
|
}
|
|
|
|
|
|
2023-08-17 20:37:22 +08:00
|
|
|
private static WinApi.ControlCtrlDelegate cancelHandler = new WinApi.ControlCtrlDelegate((CtrlType) =>
|
2022-10-06 17:44:08 +08:00
|
|
|
{
|
|
|
|
|
switch (CtrlType)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
//Console.WriteLine("0工具被强制关闭"); //Ctrl+C关闭
|
|
|
|
|
//server.Close();
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
Console.WriteLine("2工具被强制关闭");//按控制台关闭按钮关闭
|
2023-08-17 20:37:22 +08:00
|
|
|
AppRuntime.WssService.Close();
|
2022-10-06 17:44:08 +08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2023-08-17 20:37:22 +08:00
|
|
|
});
|
2023-06-19 21:44:21 +08:00
|
|
|
|
2023-08-17 20:37:22 +08:00
|
|
|
//检测程序是否多开
|
|
|
|
|
private static bool CheckAlreadyRunning()
|
2023-06-19 21:44:21 +08:00
|
|
|
{
|
2023-08-17 20:37:22 +08:00
|
|
|
const string mutexName = "DyBarrageGrab";
|
|
|
|
|
// Try to create a new named mutex.
|
|
|
|
|
using (Mutex mutex = new Mutex(true, mutexName, out bool createdNew))
|
|
|
|
|
{
|
|
|
|
|
return !createdNew;
|
|
|
|
|
}
|
2023-06-19 21:44:21 +08:00
|
|
|
}
|
2022-10-06 17:44:08 +08:00
|
|
|
}
|
|
|
|
|
}
|