Skip to content

Commit

Permalink
增加了网络状态异常导致的下载异常,现在就算你拔网线等下再插上都能续传了
Browse files Browse the repository at this point in the history
  • Loading branch information
CHKZL committed Dec 22, 2020
1 parent b3ed033 commit 616dea0
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Auxiliary/DDcenter/DdcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static void Connect()
{
try
{
MMPU.DDC采集间隔 = int.Parse(MMPU.TcpSend(30001, "{}", true,50));
MMPU.DDC采集间隔 = int.Parse(MMPU.TcpSend(Server.RequestCode.GET_DDC_TIME_NUMBER, "{}", true,50));
}
catch (Exception)
{
Expand Down
62 changes: 60 additions & 2 deletions Auxiliary/Downloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Downloader
/// <summary>
/// 关闭直播流和弹幕储存流
/// </summary>
public void Clear(bool 续命模式, DownIofoData DOL)
public static void Clear(bool 续命模式, DownIofoData DOL)
{
DOL.备注 = "下载任务结束";
DOL.下载状态 = false;
Expand Down Expand Up @@ -66,6 +66,8 @@ public class DownIofoData

public WebClient WC { set; get; }
public bool 下载状态 { set; get; } = false;
public int 最后连接时间 { set; get; } = 0;
public bool 网络超时 { set; get; } = false;
public double 已下载大小bit { set; get; }
public string 已下载大小str { set; get; }
public string 文件保存路径 { set; get; }
Expand All @@ -91,8 +93,58 @@ public class 继承
public string 继承的下载文件路径 { set; get; } = null;
public string 合并后的文件路径 { set; get; } = null;
}
public static bool 轮询检查下载任务开关 = true;
public static void 轮询检查下载任务()
{
new Task(()=> {
while (true)
{
while (轮询检查下载任务开关)
{
try
{
if (!string.IsNullOrEmpty(MMPU.TcpSend(Server.RequestCode.GET_IP, "{}", true, 50, 5000)))
{
下载任务状态检测();
}
else
{

}
}
catch (Exception)
{ }
Thread.Sleep(60 * 1000);
}
Thread.Sleep(500);
}
}).Start();
}
public static void 下载任务状态检测()
{
foreach (var item in MMPU.DownList)
{
if(item.DownIofo.下载状态&&item.DownIofo.已下载大小bit>10000&& item.DownIofo.最后连接时间!=0&& Gettime()-item.DownIofo.最后连接时间>120)
{
InfoLog.InfoPrintf(item.DownIofo.房间_频道号 + "下载状态异常,重置下载任务", InfoLog.InfoClass.下载必要提示);
item.DownIofo.网络超时 = true;
item.DownIofo.备注 = "下载状态异常,重置下载任务";
Clear(true, item.DownIofo);
item.DownIofo.下载状态 = false;
item.DownIofo.结束时间 = Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds);
item.DownIofo.WC.CancelAsync();
new Task(() =>
{
Downloader DLL = Downloader.新建下载对象(item.DownIofo.平台, item.DownIofo.房间_频道号, bilibili.根据房间号获取房间信息.获取标题(item.DownIofo.标题), Guid.NewGuid().ToString(), bilibili.根据房间号获取房间信息.下载地址(item.DownIofo.标题), "下载状态异常,重置下载任务", item.DownIofo.是否保存, item.DownIofo.主播名称, false, null);
}).Start();
}
}
}


public string Start(string 开始后显示的备注)
{
DownIofo.开始时间 = Gettime();
MMPU.DownList.Add(this);
int a = 0;
DownIofo.WC = new WebClient();
Expand Down Expand Up @@ -386,9 +438,15 @@ public static Downloader 新建下载对象(string 平台, string 唯一码, str

return 下载对象;
}
public static int Gettime()
{
int time = (int)(DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
return time;
}
private void 下载过程中事件(object sender, DownloadProgressChangedEventArgs e)
{
var bytes = e.BytesReceived;
DownIofo.最后连接时间 = Gettime();
DownIofo.已下载大小bit = bytes;
DownIofo.已下载大小str = 转换下载大小数据格式(bytes);
}
Expand All @@ -404,7 +462,7 @@ private void 下载完成事件(object sender, AsyncCompletedEventArgs e)
//DownIofo.下载状态 = true;
DownIofo.结束时间 = Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds);
DownIofo.备注 = "下载任务结束";
if (e.Cancelled)
if (e.Cancelled&&!DownIofo.网络超时)
{

if (!DownIofo.播放状态 && DownIofo.是否是播放任务)
Expand Down
2 changes: 1 addition & 1 deletion Auxiliary/InfoLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public static string GetSystemInfo()
BB += $"<br/>当前DDTVLiveRec核心版本:{MMPU.版本号}";
if(MMPU.是否有新版本)
{
BB += "<br/>检测到云端有新版本。如需更新,请上github或到" +
BB += "<br/>检测到云端有新版本。如需更新,请上github或加307156949" +
"<a target=\"_blank\" href=\"https://qm.qq.com/cgi-bin/qm/qr?k=uwgF9uM9BvSWcHWZu460S6tVcVH1N_YP&jump_from=webapi\"><img border=\"0\" src=\"//pub.idqqimg.com/wpa/images/group.png\" alt=\"每天都是单推的一天\" title=\"每天都是单推的一天\"></a>群共享下载";
}
BB += $"<br/><br/>---运行状态---<br/>下载中:{下载中}个 下载完成:{下载完成}个";
Expand Down
55 changes: 45 additions & 10 deletions Auxiliary/MMPU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ public class MMPU
public static bool Debug输出到文件 = false;
public static bool Debug打印到终端 = false;
public static bool 强制WSS连接模式 = false;
public static int 心跳打印间隔 = 180;

public static int 启动模式 = 0;//0:DDTV,1:DDTVLive
public static bool 网络环境变动监听 = true;//0:DDTV,1:DDTVLive

/// <summary>
/// 配置文件初始化
Expand All @@ -89,9 +91,11 @@ public class MMPU
public static bool 配置文件初始化(int 模式)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; //加上这一句
Debug模式 = MMPU.读取exe默认配置文件("DebugMod", "1") == "0" ? false : true;
Debug输出到文件 = MMPU.读取exe默认配置文件("DebugFile", "1") == "0" ? false : true;
Debug打印到终端 = MMPU.读取exe默认配置文件("DebugCmd", "0") == "0" ? false : true;
Debug模式 = 读取exe默认配置文件("DebugMod", "1") == "0" ? false : true;
Debug输出到文件 = 读取exe默认配置文件("DebugFile", "1") == "0" ? false : true;
Debug打印到终端 = 读取exe默认配置文件("DebugCmd", "0") == "0" ? false : true;
心跳打印间隔 = int.Parse(读取exe默认配置文件("DokiDoki", "180"));
网络环境变动监听 = 读取exe默认配置文件("NetStatusMonitor", "1") == "0" ? false : true;
if (模式 == 0)
{
InfoLog.InfoInit("./DDTVLog.out", new InfoLog.InfoClasslBool()
Expand Down Expand Up @@ -196,6 +200,7 @@ public static bool 配置文件初始化(int 模式)
VTBS.API.VTBS服务器CDN.根据CDN更新VTBS_Url();
RoomInit.start();
DokiDoki(模式);
Downloader.轮询检查下载任务();
return true;
}
/// <summary>
Expand All @@ -204,7 +209,7 @@ public static bool 配置文件初始化(int 模式)
/// <param name="模式"></param>
public static void DokiDoki(int 模式)
{

///心跳检查
new Thread(new ThreadStart(delegate {
while (true)
{
Expand All @@ -217,16 +222,22 @@ public static void DokiDoki(int 模式)
}
})).Start();
string LIP = string.Empty;
try
{
LIP = MMPU.TcpSend(Server.RequestCode.GET_IP, "{}", true, 50);
}
catch (Exception){}
int Num = 0;
///网络状态检查
new Thread(new ThreadStart(delegate {
while (true)
while (网络环境变动监听)
{
Num++;
try
{

string NIP = MMPU.TcpSend(模式 == 0 ? Server.RequestCode.SET_DokiDoki_DDTV : Server.RequestCode.SET_DokiDoki_DDTVLiveRec, "{}", true, 50);
if (Num > 10)
string NIP = MMPU.TcpSend(Server.RequestCode.GET_IP, "{}", true, 50);
if (Num > 3)
{
if (LIP != NIP&& IsCorrectIP(LIP)&& IsCorrectIP(NIP))
{
Expand Down Expand Up @@ -264,9 +275,28 @@ public static void DokiDoki(int 模式)
}
}
catch (Exception) { }
Thread.Sleep(3600 * 1000);
Thread.Sleep(30 * 1000);
}
})).Start();
new Task(()=> {
while(true)
{
try
{
int 下载中 = 0;
foreach (var item in DownList)
{
if (item.DownIofo.下载状态)
{
下载中++;
}
}
InfoLog.InfoPrintf($"[DDTVLR心跳信息]临时API监控房间数:{RoomList.Count - 已连接的直播间状态.Count},WSS长连接数:{已连接的直播间状态.Count},{下载中}个下载中", InfoLog.InfoClass.下载必要提示);
}
catch (Exception){}
Thread.Sleep(60*1000);
}
}).Start();
}
public static void BiliUser配置文件初始化(int 模式)
{
Expand Down Expand Up @@ -1151,7 +1181,7 @@ public static bool setFiles(string key, string value)
/// <param name="msg">消息内容</param>
/// <param name="是否需要回复"></param>
/// <returns></returns>
public static string TcpSend(int code, string msg, bool 是否需要回复,int 等待时间)
public static string TcpSend(int code, string msg, bool 是否需要回复,int 等待时间,int 超时时间 = 0)
{
try
{
Expand All @@ -1160,6 +1190,7 @@ public static string TcpSend(int code, string msg, bool 是否需要回复,int
tcpClient.ReceiveBufferSize = 1024 * 1024 * 8;
IPAddress ipaddress = Server.IP_ADDRESS;
EndPoint point = new IPEndPoint(ipaddress, Server.PORT);
tcpClient.SendTimeout = 超时时间;
tcpClient.Connect(point);//通过IP和端口号来定位一个所要连接的服务器端
tcpClient.Send(Encoding.UTF8.GetBytes(JSON发送拼接(code, msg)));
if (是否需要回复)
Expand Down Expand Up @@ -1276,7 +1307,11 @@ public static string 返回网页内容(string url, Dictionary<string, string> d
/// <returns></returns>
public static bool IsCorrectIP(string ip)
{
return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
bool b = Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
if (b)
return b;
else
return b;
}

public class UA
Expand Down
4 changes: 3 additions & 1 deletion Auxiliary/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ public static class RequestCode
public static readonly int GET_DYNAMIC_NOTIFICATION = 20010;
public static readonly int GET_LATEST_VERSION_NUMBER = 20011;
public static readonly int GET_UPDATE_ANNOUNCEMENT = 20012;
public static readonly int GET_VER = 20013;
public static readonly int GET_VERTEXT = 20014;
public static readonly int GET_LIVELSIT = 20016;
public static readonly int GET_VTBSROOMLIST = 20017;
public static readonly int GET_DDC_TIME_NUMBER = 30001;
public static readonly int SET_DokiDoki_DDTV = 40001;
public static readonly int SET_DokiDoki_DDTVLiveRec = 40002;
public static readonly int SET_GETIP = 50001;
public static readonly int GET_IP = 50001;
}
}
}
14 changes: 8 additions & 6 deletions Auxiliary/bilibili.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,15 @@ public static void 使用vtbsAPI更新房间状态()
}
catch (Exception)
{
InfoLog.InfoPrintf("备用缓存数据加载失败,使用原生阿Bapi开始获取开始获取", InfoLog.InfoClass.Debug);
使用B站API更新房间状态();
}

if(!string.IsNullOrEmpty(MMPU.TcpSend(Server.RequestCode.GET_IP, "{}", true, 50)))
{
InfoLog.InfoPrintf("备用缓存数据加载失败,使用原生阿Bapi开始获取开始获取", InfoLog.InfoClass.Debug);
使用B站API更新房间状态();
}
}
}
if(C1!=0)
InfoLog.InfoPrintf("使用VTBS数据库加载数据量:"+C1.ToString()+"/"+C2.ToString(), InfoLog.InfoClass.Debug);
//if(C1!=0)
//InfoLog.InfoPrintf("使用VTBS数据库加载数据量:"+C1.ToString()+"/"+C2.ToString(), InfoLog.InfoClass.Debug);
}
public static void 使用B站API更新房间状态()
{
Expand Down
4 changes: 3 additions & 1 deletion DDTVLiveRec/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
<add key="Port" value="11419"/><!--WEB监控的默认端口-->
<add key="RecordDanmu" value="0" /><!--录制弹幕信息(该功能可能会导致房间监控失效,如果必须录制,推荐监控列表中就放目标房间,并且由于是因为阿B的服务器接口限制问题,暂时无法修复)-->
<add key="BootUp" value="0" />
<add key="DebugMod" value="0" /><!--启动Debug模式,会在日志中输出debug信息,并且把debug信息储存到LOG文件夹中-->
<add key="DebugFile" value="1" />
<add key="DebugCmd" value="0" />
<add key="DebugMod" value="1" /><!--启动Debug模式,会在日志中输出debug信息,并且把debug信息储存到LOG文件夹中-->
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
Expand Down
4 changes: 2 additions & 2 deletions DDTVLiveRec/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static void Main(string[] args)
{
try
{
string 服务器版本号 = MMPU.TcpSend(20013, "{}", true, 50);
string 服务器版本号 = MMPU.TcpSend(Server.RequestCode.GET_VER, "{}", true, 50);
if (!string.IsNullOrEmpty(服务器版本号))
{
bool 检测状态 = true;
Expand All @@ -37,7 +37,7 @@ static void Main(string[] args)
if (MMPU.版本号 != 服务器版本号 && 检测状态)
{
MMPU.是否有新版本 = true;
InfoLog.InfoPrintf("检测到版本更新,更新公告:\n" + MMPU.TcpSend(20014, "{}", true, 100) + "\n\n", InfoLog.InfoClass.下载必要提示);
InfoLog.InfoPrintf("检测到版本更新,更新公告:\n" + MMPU.TcpSend(Server.RequestCode.GET_VERTEXT, "{}", true, 100) + "\n\n", InfoLog.InfoClass.下载必要提示);
//Console.ReadKey();
}
}
Expand Down

0 comments on commit 616dea0

Please sign in to comment.