BililiveRecorder/BililiveRecorder.Core/Api/ModelExtensions.cs

56 lines
2.4 KiB
C#
Raw Normal View History

2021-02-23 18:03:37 +08:00
using System;
using System.Collections.Generic;
2021-02-23 18:03:37 +08:00
using System.Linq;
using BililiveRecorder.Core.Api.Model;
using BililiveRecorder.Core.Config;
2021-02-23 18:03:37 +08:00
namespace BililiveRecorder.Core.Api
{
internal static class ModelExtensions
2021-02-23 18:03:37 +08:00
{
private static readonly Random random = new Random();
private const string DefaultServerHost = "broadcastlv.chat.bilibili.com";
private static readonly DanmakuServerInfo[] DefaultServers = new[] {
new DanmakuServerInfo { TransportMode = DanmakuTransportMode.Tcp, Host = DefaultServerHost, Port = 2243 },
new DanmakuServerInfo { TransportMode = DanmakuTransportMode.Ws, Host = DefaultServerHost, Port = 2244 },
new DanmakuServerInfo { TransportMode = DanmakuTransportMode.Wss, Host = DefaultServerHost, Port = 443 },
};
2021-02-23 18:03:37 +08:00
public static DanmakuServerInfo SelectDanmakuServer(this DanmuInfo danmuInfo, DanmakuTransportMode transportMode)
{
static IEnumerable<DanmakuServerInfo> SelectServerInfo(DanmuInfo.HostListItem x)
{
yield return new DanmakuServerInfo { TransportMode = DanmakuTransportMode.Tcp, Host = x.Host, Port = x.Port };
yield return new DanmakuServerInfo { TransportMode = DanmakuTransportMode.Ws, Host = x.Host, Port = x.WsPort };
yield return new DanmakuServerInfo { TransportMode = DanmakuTransportMode.Wss, Host = x.Host, Port = x.WssPort };
}
2021-02-23 18:03:37 +08:00
var list = danmuInfo.HostList.Where(x => !string.IsNullOrWhiteSpace(x.Host) && x.Host != DefaultServerHost)
.SelectMany(SelectServerInfo)
.Where(x => x.Port > 0)
.Where(x => transportMode == DanmakuTransportMode.Random || transportMode == x.TransportMode)
.ToArray();
2021-02-23 18:03:37 +08:00
if (list.Length > 0)
{
var result = list[random.Next(list.Length)];
result.Token = danmuInfo.Token;
return result;
2021-02-23 18:03:37 +08:00
}
else
{
return DefaultServers[random.Next(DefaultServers.Length)];
2021-02-23 18:03:37 +08:00
}
}
internal struct DanmakuServerInfo
{
internal DanmakuTransportMode TransportMode;
internal string Host;
internal int Port;
internal string Token;
}
2021-02-23 18:03:37 +08:00
}
}