Files
DanMuRead/BarrageGrab/Program.cs
T

116 lines
3.7 KiB
C#
Raw Normal View History

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
{
2024-03-29 11:30:03 +08:00
static FormView mainForm = null;
static bool exited = false;
static bool formExited = false;
2022-10-06 17:44:08 +08:00
static void Main(string[] args)
2023-11-23 16:43:21 +08:00
{
2023-08-17 20:37:22 +08:00
if (CheckAlreadyRunning())
2022-10-06 17:44:08 +08:00
{
Logger.PrintColor("已经有一个监听程序在运行,按任意键退出...");
2023-08-17 20:37:22 +08:00
Console.ReadKey();
return;
}
2024-03-29 11:30:03 +08:00
SetTitle("抖音弹幕监听推送");
2023-08-17 20:37:22 +08:00
2024-03-29 11:30:03 +08:00
WinApi.SetConsoleCtrlHandler(ControlCtrlDelegate, true);//捕获控制台关闭
2023-12-17 23:56:10 +08:00
WinApi.DisableQuickEditMode();//禁用控制台快速编辑模式
2024-03-29 11:30:03 +08:00
AppRuntime.DisplayConsole(!Appsetting.Current.HideConsole);//控制控制台可见
AppRuntime.WssService.Grab.Proxy.SetUpstreamProxy(Appsetting.Current.UpstreamProxy);//设置上游代理
AppRuntime.WssService.StartListen();//启动WS服务
2023-11-14 20:29:18 +08:00
Logger.LogSucc($"{AppRuntime.WssService.ServerLocation} 弹幕服务已启动...");
2024-03-29 11:30:03 +08:00
SetTitle($"抖音弹幕监听推送 [{AppRuntime.WssService.ServerLocation}]");
2023-11-23 16:43:21 +08:00
2024-03-29 11:30:03 +08:00
AppRuntime.WssService.OnClose += (s, e) => exited = true;
//显示窗体
2023-11-23 16:43:21 +08:00
if (Appsetting.Current.ShowWindow)
{
var uiThread = new Thread(new ThreadStart(() =>
{
mainForm = new FormView();
//开启消息循环
System.Windows.Forms.Application.Run(mainForm);
formExited = true;
}));
uiThread.SetApartmentState(ApartmentState.STA);
uiThread.IsBackground = true;
uiThread.Start();
}
2023-11-16 09:26:40 +08:00
2023-06-19 21:44:21 +08:00
while (!exited)
2023-08-17 20:37:22 +08:00
{
2023-11-23 16:43:21 +08:00
if (formExited && !exited)
{
AppRuntime.WssService.Close();
}
2023-06-19 21:44:21 +08:00
Thread.Sleep(500);
}
Logger.PrintColor("服务器已关闭...");
2023-11-23 16:43:21 +08:00
//if (!mainForm.IsDisposed)
//{
// mainForm.Invoke(new Action(() =>
// {
// mainForm.Close();
// }));
//}
2023-06-19 21:44:21 +08:00
//退出程序,不显示 按任意键退出
2023-11-23 16:43:21 +08:00
//Environment.Exit(0);
2022-10-06 17:44:08 +08:00
}
2024-03-29 11:30:03 +08:00
//检测设置控制台标题
private static void SetTitle(string title)
{
if (WinApi.GetConsoleWindow() != IntPtr.Zero)
{
Console.Title = title;
}
}
//监听控制台消息事件
private static bool ControlCtrlDelegate(int CtrlType)
2022-10-06 17:44:08 +08:00
{
switch (CtrlType)
{
case 0:
//Logger.PrintColor("0工具被强制关闭"); //Ctrl+C关闭
2022-10-06 17:44:08 +08:00
//server.Close();
break;
case 2:
Logger.PrintColor("2工具被强制关闭");//按控制台关闭按钮关闭
2023-08-17 20:37:22 +08:00
AppRuntime.WssService.Close();
2022-10-06 17:44:08 +08:00
break;
}
return false;
2024-03-29 11:30:03 +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-11-16 09:26:40 +08:00
const string mutexName = "DyBarrageGrab";
2023-08-17 20:37:22 +08:00
// Try to create a new named mutex.
2023-11-16 09:26:40 +08:00
bool createdNew;
using (Mutex mutex = new Mutex(true, mutexName, out createdNew))
2023-08-17 20:37:22 +08:00
{
return !createdNew;
}
2023-06-19 21:44:21 +08:00
}
2022-10-06 17:44:08 +08:00
}
}