Skip to content

Commit

Permalink
项目结构优化
Browse files Browse the repository at this point in the history
  • Loading branch information
ape-byte committed Aug 17, 2023
1 parent 2258101 commit e9c7c71
Show file tree
Hide file tree
Showing 9 changed files with 437 additions and 126 deletions.
34 changes: 34 additions & 0 deletions BarrageGrab/AppRuntime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using BarrageGrab.Modles;
using ColorConsole;

namespace BarrageGrab
{
/// <summary>
/// 程序运行时信息
/// </summary>
public static class AppRuntime
{
/// <summary>
/// Ws弹幕服务示例
/// </summary>
public static WsBarrageService WssService { get; private set; } = new WsBarrageService();

/// <summary>
/// 程序进程信息
/// </summary>
public static Process CurrentProcess { get; private set; } = System.Diagnostics.Process.GetCurrentProcess();


static AppRuntime()
{

}
}
}
175 changes: 175 additions & 0 deletions BarrageGrab/ObjectExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace BarrageGrab
{
/// <summary>
/// 扩展类
/// </summary>
public static class ObjectExtension
{
/// <summary>
/// 判断字符串是否为空或空白
/// </summary>
public static bool IsNullOrWhiteSpace(this string str) => string.IsNullOrWhiteSpace(str);

/// <summary>
/// 判断字符串是否为空
/// </summary>
public static bool IsNullOrEmpty(this string str) => string.IsNullOrEmpty(str);

/// <summary>
/// 转为Json
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string ToJson(this object obj)
{
if (obj is string)
{
return obj.ToString();
}
return JsonConvert.SerializeObject(obj);
}

/// <summary>
/// 转为对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <returns></returns>
public static T AsObject<T>(this string json)
{
return JsonConvert.DeserializeObject<T>(json);
}

/// <summary>
/// 获取Value或其Value默认值
/// </summary>
public static TValue ValueOrDefault<Tkey, TValue>(this IDictionary<Tkey, TValue> dic, Tkey key)
{
if (dic.ContainsKey(key))
{
return dic[key];
}
return default;
}

/// <summary>
/// 获取Value或其Value默认值
/// </summary>
public static TValue ValueOrDefault<Tkey, TValue>(this IDictionary<Tkey, TValue> dic, Tkey key, TValue defaultValue)
{
if (dic.ContainsKey(key))
{
return dic[key];
}
return defaultValue;
}

/// <summary>
/// 遍历集合
/// </summary>
public static void Foreach<T>(this IEnumerable<T> list, Action<T> func)
{
foreach (var item in list) func(item);
}

/// <summary>
/// 遍历集合
/// </summary>
public static void Foreach<T>(this IEnumerable<T> list, Action<T, int> func)
{
int i = 0;
foreach (var item in list) func(item, i++);
}


public static CookieCollection ToCookies(this string cookieStr, string domain)
{
var cookies = new CookieCollection();
if (!string.IsNullOrEmpty(cookieStr))
{
var cookieArray = cookieStr.Split(';');
foreach (var cookie in cookieArray)
{
var cookieParts = cookie.Split('=');
if (cookieParts.Length == 2)
{
var cookieName = cookieParts[0].Trim();
var cookieValue = cookieParts[1].Trim();
var newCookie = new Cookie(cookieName, Uri.EscapeDataString(cookieValue));
newCookie.Path = "/";
newCookie.Domain = domain;
cookies.Add(newCookie);
}
}
}
return cookies;
}

/// <summary>
/// 追加另一个字典
/// </summary>
/// <param name="dic"></param>
/// <param name="other"></param>
/// <returns></returns>
public static IDictionary<string, string> Append(this IEnumerable<KeyValuePair<string, string>> dic, IEnumerable<KeyValuePair<string, string>> other)
{
var res = new Dictionary<string, string>();
foreach (var item in dic)
{
res.Add(item.Key, item.Value);
}
foreach (var item in other)
{
if (res.ContainsKey(item.Key))
{
res[item.Key] = item.Value;
}
else
{
res.Add(item.Key, item.Value);
}
}
return res;
}


/// <summary>
/// 转换为Cookie对象列表
/// </summary>
/// <param name="dic"></param>
/// <param name="domain"></param>
/// <returns></returns>
public static CookieCollection ToCookies(this IDictionary<string, string> dic, string domain)
{
var cookieCollection = new CookieCollection();
foreach (var item in dic)
{
cookieCollection.Add(new Cookie(item.Key, item.Value, "/", domain));
}
return cookieCollection;
}

/// <summary>
/// 添加Cookie
/// </summary>
/// <param name="cookies"></param>
/// <param name="dic"></param>
/// <param name="domain"></param>
public static void PushCookies(this CookieContainer cookies, string domain, IDictionary<string, string> dic)
{
if (cookies == null) return;
var cookieCollection = dic.ToCookies(domain);
cookies.Add(cookieCollection);
}
}
}
81 changes: 32 additions & 49 deletions BarrageGrab/Program.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace BarrageGrab
{
public class Program
{
private delegate bool ControlCtrlDelegate(int CtrlType);

static WsBarrageService server = null;
static void Main(string[] args)
{
SetConsoleCtrlHandler(cancelHandler, true);//捕获控制台关闭
Console.Title = "抖音弹幕监听推送";
DisableQuickEditMode();
{
if (CheckAlreadyRunning())
{
Console.WriteLine("已经有一个监听程序在运行,按任意键退出...");
Console.ReadKey();
return;
}

WinApi.SetConsoleCtrlHandler(cancelHandler, true);//捕获控制台关闭
WinApi.DisableQuickEditMode();//禁用控制台快速编辑模式
Console.Title = "抖音弹幕监听推送";

bool exited = false;
server = new WsBarrageService();
server.StartListen();
server.OnClose += (s, e) =>
AppRuntime.WssService.StartListen();
AppRuntime.WssService.OnClose += (s, e) =>
{
//退出程序
//退出程序
exited = true;
};

while (!exited)
{
{
Thread.Sleep(500);
}
Console.WriteLine("服务器已关闭...");

Console.WriteLine("服务器已关闭...");

//退出程序,不显示 按任意键退出
Environment.Exit(0);
Environment.Exit(0);
}


[DllImport("kernel32.dll")]
private static extern bool SetConsoleCtrlHandler(ControlCtrlDelegate HandlerRoutine, bool Add);
private static ControlCtrlDelegate cancelHandler = new ControlCtrlDelegate(HandlerRoutine);

public static bool HandlerRoutine(int CtrlType)
private static WinApi.ControlCtrlDelegate cancelHandler = new WinApi.ControlCtrlDelegate((CtrlType) =>
{
switch (CtrlType)
{
Expand All @@ -52,39 +53,21 @@ public static bool HandlerRoutine(int CtrlType)
break;
case 2:
Console.WriteLine("2工具被强制关闭");//按控制台关闭按钮关闭
server.Close();
AppRuntime.WssService.Close();
break;
}
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()
//检测程序是否多开
private static bool CheckAlreadyRunning()
{
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);
const string mutexName = "DyBarrageGrab";
// Try to create a new named mutex.
using (Mutex mutex = new Mutex(true, mutexName, out bool createdNew))
{
return !createdNew;
}
}

}
}
12 changes: 6 additions & 6 deletions BarrageGrab/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("LiveCompanionBarrageGrab")]
[assembly: AssemblyTitle("抖音弹幕监听")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LiveCompanionBarrageGrab")]
[assembly: AssemblyCompany("小白不圆")]
[assembly: AssemblyProduct("抖音弹幕监听器")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
Expand All @@ -20,7 +20,7 @@
[assembly: ComVisible(false)]

// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("a07bb1da-d5c2-4eea-9f3f-e42aadb9c1d6")]
[assembly: Guid("a07bb1da-d5c2-4bea-9f3f-e42aadb9c1d5")]

// 程序集的版本信息由下列四个值组成:
//
Expand All @@ -32,5 +32,5 @@
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("2.6.8.0")]
[assembly: AssemblyFileVersion("2.6.8.0")]
Loading

0 comments on commit e9c7c71

Please sign in to comment.