Skip to content

Commit

Permalink
增加Socket稳定性,优化长数据包接收兼容稳定性
Browse files Browse the repository at this point in the history
优化初始化VTBS数据获取流程,增加错误处理和备用接口
  • Loading branch information
CHKZL committed Nov 24, 2020
1 parent 5828a93 commit d559301
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 34 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));
MMPU.DDC采集间隔 = int.Parse(MMPU.TcpSend(30001, "{}", true,50));
}
catch (Exception)
{
Expand Down
19 changes: 14 additions & 5 deletions Auxiliary/MMPU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class MMPU
public static int 直播更新时间 = 60;
public static string 下载储存目录 = "";
public static string 版本号 = "2.0.4.5b";
public static string[] 不检测的版本号 = {};
public static string[] 不检测的版本号 = {"2.0.4.3b"};
public static bool 第一次打开播放窗口 = true;
public static int 默认音量 = 0;
public static int 缩小功能 = 1;
Expand Down Expand Up @@ -97,7 +97,7 @@ public static bool 配置文件初始化(int 模式)
{
InfoLog.InfoInit("./DDTVLiveRecLog.out", new InfoLog.InfoClasslBool()
{
Debug = false,
Debug = true,
下载必要提示 = true,
杂项提示 = false,
系统错误信息 = true,
Expand Down Expand Up @@ -189,7 +189,7 @@ public static void DokiDoki(int 模式)
{
try
{
MMPU.TcpSend(模式 == 0 ? Server.RequestCode.SET_DokiDoki_DDTV : Server.RequestCode.SET_DokiDoki_DDTVLiveRec, "{}", true);
MMPU.TcpSend(模式 == 0 ? Server.RequestCode.SET_DokiDoki_DDTV : Server.RequestCode.SET_DokiDoki_DDTVLiveRec, "{}", true,50);
}
catch (Exception) { }
Thread.Sleep(3600 * 1000);
Expand Down Expand Up @@ -1010,19 +1010,21 @@ 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 是否需要回复)
public static string TcpSend(int code, string msg, bool 是否需要回复,int 等待时间)
{
try
{
string 回复内容 = "";
Socket tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
tcpClient.ReceiveBufferSize = 1024 * 1024 * 8;
IPAddress ipaddress = Server.IP_ADDRESS;
EndPoint point = new IPEndPoint(ipaddress, Server.PORT);
tcpClient.Connect(point);//通过IP和端口号来定位一个所要连接的服务器端
tcpClient.Send(Encoding.UTF8.GetBytes(JSON发送拼接(code, msg)));
if (是否需要回复)
{
byte[] buffer = new byte[1024 * 1024];
byte[] buffer = new byte[1024 * 1024 * 8];
Thread.Sleep(等待时间);
tcpClient.Receive(buffer);
string 收到的数据 = Encoding.UTF8.GetString(buffer).Trim('\0');
回复内容 = 收到的数据;
Expand All @@ -1032,6 +1034,13 @@ public static string TcpSend(int code, string msg, bool 是否需要回复)
回复内容 = null;
}
tcpClient.Close();
try
{
tcpClient.Dispose();
}
catch (Exception)
{
}
return 回复内容;
}
catch (Exception)
Expand Down
2 changes: 2 additions & 0 deletions Auxiliary/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public static class Server
public static readonly IPAddress IP_ADDRESS = MMPU.根据URL获取IP地址("api.ddtv.pro");
#elif false
public static readonly IPAddress IP_ADDRESS = MMPU.根据URL获取IP地址("192.168.199.100");
#elif false
public static readonly IPAddress IP_ADDRESS = MMPU.根据URL获取IP地址("127.0.0.1");
#endif
public static readonly int PORT = 11433;
public static readonly string PROJECT_ADDRESS = "https://github.com/CHKZL/DDTV2/releases/latest";
Expand Down
37 changes: 26 additions & 11 deletions Auxiliary/bilibili.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,32 +88,43 @@ public static void 更新VTBS房间数据()
{
while (true)
{
int 完整错误次数 = 0;
try
{

if(完整错误次数>=5)
{
InfoLog.InfoPrintf($"----------【重要】----------\r\n多次尝试获取服务器RoomList缓存失败,切换为纯WSS连接模式,该模式下初始化连接速度较慢,大约20连接一个房间\r\n----------【重要】----------", InfoLog.InfoClass.下载必要提示);
wss连接初始化准备已完成 = true;
return;
}
string vtbs房间数据 = string.Empty;
try
{
vtbs房间数据 = MMPU.返回网页内容_GET(VTBS.API.VTBS服务器CDN.VTBS_Url + "/v1/short", 5000);
}
catch (Exception)
{
vtbs房间数据 = MMPU.TcpSend(Server.RequestCode.GET_VTBSROOMLIST, "{}", true);
InfoLog.InfoPrintf($"通过原始数据源更新VTBS房间数据失败,切换到备用DDTV服务器获取", InfoLog.InfoClass.Debug);
vtbs房间数据 = MMPU.TcpSend(Server.RequestCode.GET_VTBSROOMLIST, "{}", true, 1500);
}

JArray JO = (JArray)JsonConvert.DeserializeObject(vtbs房间数据);
InfoLog.InfoPrintf($"获取VTBS房间数据完成:{JO}", InfoLog.InfoClass.Debug);
foreach (var item in JO)
{
if (int.Parse(item["roomid"].ToString()) != 0)
{
VtbsRoomList.Add(new VtbsRoom() { mid = int.Parse(item["mid"].ToString()), room = int.Parse(item["roomid"].ToString()), name = item["uname"].ToString() });
}
}
InfoLog.InfoPrintf($"VTBS数据数据准备完成", InfoLog.InfoClass.Debug);
wss连接初始化准备已完成 = true;
break;
}
catch (Exception)
catch (Exception )
{
完整错误次数++;
InfoLog.InfoPrintf($"VTBS获取RoomList失败,再次重试,已重试次数{完整错误次数}/5", InfoLog.InfoClass.Debug);
}

Thread.Sleep(5000);
Expand Down Expand Up @@ -215,7 +226,7 @@ public static void 使用vtbsAPI更新房间状态()
InfoLog.InfoPrintf("Vtbs数据加载失败,使用备用数据源开始获取", InfoLog.InfoClass.Debug);
try
{
JArray JO = (JArray)JsonConvert.DeserializeObject(MMPU.TcpSend(Server.RequestCode.GET_LIVELSIT, "{}", true));
JArray JO = (JArray)JsonConvert.DeserializeObject(MMPU.TcpSend(Server.RequestCode.GET_LIVELSIT, "{}", true,200));
foreach (var roomtask in RoomList)
{
if (!roomtask.名称.Contains("-NV"))
Expand Down Expand Up @@ -354,15 +365,19 @@ public static void 初始化所有房间连接()
}).Start();
while (!wss连接初始化准备已完成)
{
Thread.Sleep(1000);
InfoLog.InfoPrintf("连接初始化准备未完成3秒后重试", InfoLog.InfoClass.Debug);
Thread.Sleep(3000);
}
InfoLog.InfoPrintf("wss连接初始化准备已完成", InfoLog.InfoClass.Debug);
List<int> MTPlist = new List<int>();
InfoLog.InfoPrintf($"wss连接初始化准备已完成,列表长度:{VtbsRoomList.Count}", InfoLog.InfoClass.Debug);
foreach (var item in VtbsRoomList)
{
MTPlist.Add(item.room);
}
List<RoomInit.RoomInfo> Vtbs存在的直播间 = new List<RoomInit.RoomInfo>();
List<RoomInit.RoomInfo> Vtbs不存在的直播间 = new List<RoomInit.RoomInfo>();
InfoLog.InfoPrintf($"生成双边队列临时API房间列表数:{Vtbs存在的直播间.Count},优先连接组数:{Vtbs不存在的直播间.Count}", InfoLog.InfoClass.Debug);
foreach (var item in RoomList)
{
if(MTPlist.Contains(int.Parse(item.房间号)))
Expand All @@ -381,7 +396,7 @@ public static void 初始化所有房间连接()
InfoLog.InfoPrintf("LiveChatListener开始连接非VTBS监控范围内房间:" + item.房间号, InfoLog.InfoClass.Debug);
if (item.平台 == "bilibili")
{
item.liveChatListener = new LiveChatListener();
item.liveChatListener = new LiveChatListener();
item.liveChatListener.Connect(int.Parse(item.房间号));
item.liveChatListener.MessageReceived += 直播间状态WS变化事件;
已连接的直播间状态.Add(new 直播间状态() { 房间号 = int.Parse(item.房间号) });
Expand Down Expand Up @@ -834,11 +849,11 @@ public static string 获取真实房间号(string roomID)
try
{
JObject result = JObject.Parse(roomHtml);
string live_status = result["data"]["live_status"].ToString();
if (live_status != "1")
{
return "-1";
}
//string live_status = result["data"]["live_status"].ToString();
//if (live_status != "1")
//{
// return "-1";
//}
string roomid = result["data"]["room_id"].ToString();
DataCache.写缓存(CacheStr + roomID, roomid);
return roomid;
Expand Down
2 changes: 1 addition & 1 deletion Auxiliary/外部API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class 直播信息
public List<直播信息> 直播数据 = new List<直播信息>();
public void 更新正在直播数据()
{
string UURL = MMPU.TcpSend(30002, "{}", true);
string UURL = MMPU.TcpSend(30002, "{}", true,50);
string URL = string.IsNullOrEmpty(UURL)?"https://hiyoko.sonoj.net/dy-st/30s/6c6cb639-1d2f-4151-81c7-fd877700cf98.json": UURL;
JArray jo = (JArray)JsonConvert.DeserializeObject(MMPU.返回网页内容(URL));
for(int i =0;i< jo.Count;i++)
Expand Down
3 changes: 3 additions & 0 deletions DDTVLiveRec/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<!--绝大多数内容修改过后可能会导致录制失败或完成后文件被自动删除,修改前请咨询开发者或阅读代码-->
<add key="file" value="./tmp/" />
<add key="Livefile" value="./tmp/LiveCache/" />
<add key="DANMU" value="1" />
Expand Down Expand Up @@ -29,6 +30,8 @@
<add key="IsFirstTimeUsing" value="1" />
<add key="NotVTBStatus" value="0" />
<add key="LiveRecWebServerDefaultIP" value="0.0.0.0" />
<add key="RecordDanmu" value="0" />
<add key="BootUp" value="0" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
Expand Down
Binary file added DDTVLiveRec/DDTV.ico
Binary file not shown.
1 change: 1 addition & 0 deletions DDTVLiveRec/DDTVLiveRec.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<ApplicationIcon>DDTV.ico</ApplicationIcon>
</PropertyGroup>

<ItemGroup>
Expand Down
14 changes: 8 additions & 6 deletions DDTVLiveRec/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Auxiliary;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Drawing;
using System.IO;
Expand All @@ -15,19 +17,19 @@ static void Main(string[] args)
下载("14275133");

#else
Auxiliary.VTBS.API.VTBS服务器CDN.根据CDN更新VTBS_Url();
Auxiliary.VTBS.API.VTBS服务器CDN.根据CDN更新VTBS_Url();
MMPU.配置文件初始化(1);
new Task(() => {
DDTVLiveRecWebServer.Program.Main(new string[] { });
}).Start();


#endif
new Task(() =>
new Task(() =>
{
try
{
string 服务器版本号 = MMPU.TcpSend(20013, "{}", true);
string 服务器版本号 = MMPU.TcpSend(20013, "{}", true,50);
if (!string.IsNullOrEmpty(服务器版本号))
{
bool 检测状态 = true;
Expand All @@ -41,7 +43,7 @@ static void Main(string[] args)
if (MMPU.版本号 != 服务器版本号 && 检测状态)
{
MMPU.是否有新版本 = true;
InfoLog.InfoPrintf("检测到版本更新,更新公告:\n" + MMPU.TcpSend(20014, "{}", true) + "\n\n", InfoLog.InfoClass.下载必要提示);
InfoLog.InfoPrintf("检测到版本更新,更新公告:\n" + MMPU.TcpSend(20014, "{}", true,100) + "\n\n", InfoLog.InfoClass.下载必要提示);
//Console.ReadKey();
}
}
Expand Down
4 changes: 2 additions & 2 deletions DDTV_New/AddMonitoringList.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private void BT1_Click(object sender, RoutedEventArgs e)
NewThreadTask.Run(() =>
{
MMPU.TcpSend(Server.RequestCode.GET_NEW_MEMBER_LIST_CONTENT,
JsonConvert.SerializeObject(NEWV), true);
JsonConvert.SerializeObject(NEWV), true,50);
});

RoomBox rlc = JsonConvert.DeserializeObject<RoomBox>(ReadConfigFile(RoomConfigFile));
Expand Down Expand Up @@ -183,7 +183,7 @@ private void BT1_Click(object sender, RoutedEventArgs e)
NewThreadTask.Run(() =>
{
MMPU.TcpSend(Server.RequestCode.GET_NEW_MEMBER_LIST_CONTENT,
JsonConvert.SerializeObject(NEWV), true);
JsonConvert.SerializeObject(NEWV), true,50);
});

RoomBox rlc = JsonConvert.DeserializeObject<RoomBox>(ReadConfigFile(RoomConfigFile));
Expand Down
1 change: 1 addition & 0 deletions DDTV_New/App.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!--绝大多数内容修改过后可能会导致录制失败或完成后文件被自动删除,修改前请咨询开发者或阅读代码-->
<appSettings>
<add key="file" value="./tmp/" />
<add key="Livefile" value="./tmp/LiveCache/" />
Expand Down
12 changes: 6 additions & 6 deletions DDTV_New/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public MainWindow()

InitializeComponent();
this.Title = "DDTV2.0主窗口";

ViewModel = new MainViewModel();

this.WhenActivated(disposable =>
Expand Down Expand Up @@ -515,13 +515,13 @@ public void 公告项目启动()
NewThreadTask.Loop(runOnLocalThread =>
{
bool 动态推送1开关 = MMPU.TcpSend(
Server.RequestCode.GET_TOGGLE_DYNAMIC_NOTIFICATION, "{}", true)
Server.RequestCode.GET_TOGGLE_DYNAMIC_NOTIFICATION, "{}", true,50)
== "1" ? true : false;

if (动态推送1开关)
{
string 动态推送内容 = MMPU.TcpSend(
Server.RequestCode.GET_DYNAMIC_NOTIFICATION, "{}", true);
Server.RequestCode.GET_DYNAMIC_NOTIFICATION, "{}", true, 50);
runOnLocalThread(() => ViewModel.PushNotification = 动态推送内容);
}
}, this, 3600 * 1000);
Expand All @@ -536,7 +536,7 @@ public void 公告项目启动()
NewThreadTask.Run(() =>
{
string 服务器版本号 = MMPU.TcpSend(
Server.RequestCode.GET_LATEST_VERSION_NUMBER, "{}", true);
Server.RequestCode.GET_LATEST_VERSION_NUMBER, "{}", true, 50);

if (!string.IsNullOrEmpty(服务器版本号))
{
Expand All @@ -552,7 +552,7 @@ public void 公告项目启动()
{
MessageBoxResult dr = MessageBox.Show(
"检测到版本更新,更新公告:\n"
+ MMPU.TcpSend(Server.RequestCode.GET_UPDATE_ANNOUNCEMENT, "{}", true)
+ MMPU.TcpSend(Server.RequestCode.GET_UPDATE_ANNOUNCEMENT, "{}", true, 100)
+ "\n\n点击确定启动自动更新,点击取消忽略",
"有新版本",
MessageBoxButton.OKCancel,
Expand Down Expand Up @@ -590,7 +590,7 @@ public void 公告项目启动()
NewThreadTask.Run(runOnLocalThread =>
{
string 推送内容1text = MMPU.TcpSend(
Server.RequestCode.GET_PUSH_NOTIFICATION_1, "{}", true);
Server.RequestCode.GET_PUSH_NOTIFICATION_1, "{}", true, 100);
if (推送内容1text.Length > 0)
{
runOnLocalThread(() => ViewModel.Announcement = 推送内容1text);
Expand Down
4 changes: 2 additions & 2 deletions DDTV_New/window/增加监控列表.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private void Button_Click(object sender, RoutedEventArgs e)
try
{
roomId = int.Parse(唯一码.Text);
if (roomId < 10000)
if (roomId < 10001)
{
string roomDD = bilibili.根据房间号获取房间信息.获取真实房间号(roomId.ToString());
if(!string.IsNullOrEmpty(roomDD))
Expand Down Expand Up @@ -73,7 +73,7 @@ private void Button_Click(object sender, RoutedEventArgs e)
RB.data.Add(item);
}
}
RB.data.Add(new RoomCadr() { Name = 名称.Text + "-NV", OfficialName = 名称.Text + "-NV", RoomNumber = 唯一码.Text });
RB.data.Add(new RoomCadr() { Name = 名称.Text + "-NV", OfficialName = 名称.Text + "-NV", RoomNumber = roomId.ToString() });
string JOO = JsonConvert.SerializeObject(RB);
MMPU.储存文本(JOO, RoomConfigFile);
提示.Content = 名称.Text + "-NV["+ 唯一码.Text + "]添加完成";
Expand Down

0 comments on commit d559301

Please sign in to comment.