2021-02-23 18:03:37 +08:00
|
|
|
using System;
|
|
|
|
using System.Buffers;
|
|
|
|
using System.Buffers.Binary;
|
|
|
|
using System.IO.Compression;
|
|
|
|
using System.IO.Pipelines;
|
|
|
|
using System.Net;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Timers;
|
2022-08-25 18:42:36 +08:00
|
|
|
using BililiveRecorder.Core.Config;
|
2021-02-23 18:03:37 +08:00
|
|
|
using Nerdbank.Streams;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Serilog;
|
|
|
|
using Timer = System.Timers.Timer;
|
|
|
|
|
|
|
|
namespace BililiveRecorder.Core.Api.Danmaku
|
|
|
|
{
|
2022-05-16 23:28:31 +08:00
|
|
|
internal class DanmakuClient : IDanmakuClient, IDisposable
|
2021-02-23 18:03:37 +08:00
|
|
|
{
|
|
|
|
private readonly ILogger logger;
|
|
|
|
private readonly IDanmakuServerApiClient apiClient;
|
|
|
|
private readonly Timer timer;
|
|
|
|
private readonly SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1);
|
|
|
|
|
2022-08-25 18:42:36 +08:00
|
|
|
private IDanmakuTransport? danmakuTransport;
|
2021-02-23 18:03:37 +08:00
|
|
|
private bool disposedValue;
|
|
|
|
|
2022-08-25 18:42:36 +08:00
|
|
|
public bool Connected => this.danmakuTransport != null;
|
2021-02-23 18:03:37 +08:00
|
|
|
|
|
|
|
public event EventHandler<StatusChangedEventArgs>? StatusChanged;
|
|
|
|
public event EventHandler<DanmakuReceivedEventArgs>? DanmakuReceived;
|
|
|
|
|
2023-07-06 06:37:48 +08:00
|
|
|
public Func<string, string?>? BeforeHandshake { get; set; } = null;
|
|
|
|
|
2023-07-16 16:01:50 +08:00
|
|
|
private static readonly JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
|
|
|
|
|
2021-02-23 18:03:37 +08:00
|
|
|
public DanmakuClient(IDanmakuServerApiClient apiClient, ILogger logger)
|
|
|
|
{
|
|
|
|
this.apiClient = apiClient ?? throw new ArgumentNullException(nameof(apiClient));
|
|
|
|
this.logger = logger?.ForContext<DanmakuClient>() ?? throw new ArgumentNullException(nameof(logger));
|
|
|
|
|
|
|
|
this.timer = new Timer(interval: 1000 * 30)
|
|
|
|
{
|
|
|
|
AutoReset = true,
|
|
|
|
Enabled = false
|
|
|
|
};
|
|
|
|
this.timer.Elapsed += this.SendPingMessageTimerCallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task DisconnectAsync()
|
|
|
|
{
|
|
|
|
await this.semaphoreSlim.WaitAsync().ConfigureAwait(false);
|
|
|
|
try
|
|
|
|
{
|
2022-08-25 18:42:36 +08:00
|
|
|
this.danmakuTransport?.Dispose();
|
|
|
|
this.danmakuTransport = null;
|
2021-02-23 18:03:37 +08:00
|
|
|
|
|
|
|
this.timer.Stop();
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
this.semaphoreSlim.Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
StatusChanged?.Invoke(this, StatusChangedEventArgs.False);
|
|
|
|
}
|
|
|
|
|
2022-08-25 18:42:36 +08:00
|
|
|
public async Task ConnectAsync(int roomid, DanmakuTransportMode transportMode, CancellationToken cancellationToken)
|
2021-02-23 18:03:37 +08:00
|
|
|
{
|
|
|
|
if (this.disposedValue)
|
|
|
|
throw new ObjectDisposedException(nameof(DanmakuClient));
|
|
|
|
|
2022-08-25 18:42:36 +08:00
|
|
|
if (!Enum.IsDefined(typeof(DanmakuTransportMode), transportMode))
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(transportMode), transportMode, "Invalid danmaku transport mode.");
|
|
|
|
|
2021-02-23 18:03:37 +08:00
|
|
|
await this.semaphoreSlim.WaitAsync().ConfigureAwait(false);
|
|
|
|
try
|
|
|
|
{
|
2022-08-25 18:42:36 +08:00
|
|
|
if (this.danmakuTransport != null)
|
2021-02-23 18:03:37 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
var serverInfo = await this.apiClient.GetDanmakuServerAsync(roomid).ConfigureAwait(false);
|
|
|
|
if (serverInfo.Data is null)
|
|
|
|
return;
|
|
|
|
|
2022-08-25 18:42:36 +08:00
|
|
|
var danmakuServerInfo = serverInfo.Data.SelectDanmakuServer(transportMode);
|
2021-04-30 19:35:15 +08:00
|
|
|
|
2022-08-25 18:42:36 +08:00
|
|
|
this.logger.Debug("连接弹幕服务器 {Mode} {Host}:{Port} 房间: {RoomId} TokenLength: {TokenLength}", danmakuServerInfo.TransportMode, danmakuServerInfo.Host, danmakuServerInfo.Port, roomid, danmakuServerInfo.Token?.Length);
|
|
|
|
|
|
|
|
IDanmakuTransport transport = danmakuServerInfo.TransportMode switch
|
|
|
|
{
|
|
|
|
DanmakuTransportMode.Tcp => new DanmakuTransportTcp(),
|
|
|
|
DanmakuTransportMode.Ws => new DanmakuTransportWebSocket(),
|
|
|
|
DanmakuTransportMode.Wss => new DanmakuTransportSecureWebSocket(),
|
|
|
|
_ => throw new ArgumentOutOfRangeException(nameof(transportMode), transportMode, "Invalid danmaku transport mode."),
|
|
|
|
};
|
2021-02-23 18:03:37 +08:00
|
|
|
|
2022-08-25 18:42:36 +08:00
|
|
|
var reader = await transport.ConnectAsync(danmakuServerInfo.Host, danmakuServerInfo.Port, cancellationToken).ConfigureAwait(false);
|
2021-02-23 18:03:37 +08:00
|
|
|
|
2022-08-25 18:42:36 +08:00
|
|
|
this.danmakuTransport = transport;
|
2021-02-23 18:03:37 +08:00
|
|
|
|
2023-07-16 16:01:50 +08:00
|
|
|
await this.SendHelloAsync(roomid, this.apiClient.GetUid(), this.apiClient.GetBuvid3(), danmakuServerInfo.Token ?? string.Empty).ConfigureAwait(false);
|
2022-08-25 18:42:36 +08:00
|
|
|
await this.SendPingAsync().ConfigureAwait(false);
|
2021-02-23 18:03:37 +08:00
|
|
|
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
{
|
2022-08-25 18:42:36 +08:00
|
|
|
this.danmakuTransport.Dispose();
|
|
|
|
this.danmakuTransport = null;
|
2021-02-23 18:03:37 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.timer.Start();
|
|
|
|
|
|
|
|
_ = Task.Run(async () =>
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-08-25 18:42:36 +08:00
|
|
|
await ProcessDataAsync(reader, this.ProcessCommand).ConfigureAwait(false);
|
2021-02-23 18:03:37 +08:00
|
|
|
}
|
2021-03-02 23:39:54 +08:00
|
|
|
catch (ObjectDisposedException) { }
|
2021-02-23 18:03:37 +08:00
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2021-04-30 19:35:15 +08:00
|
|
|
this.logger.Debug(ex, "Error running ProcessDataAsync");
|
2021-02-23 18:03:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await this.DisconnectAsync().ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
catch (Exception) { }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
this.semaphoreSlim.Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
StatusChanged?.Invoke(this, StatusChangedEventArgs.True);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ProcessCommand(string json)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var d = new DanmakuModel(json);
|
|
|
|
DanmakuReceived?.Invoke(this, new DanmakuReceivedEventArgs(d));
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
this.logger.Warning(ex, "Error running ProcessCommand");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma warning disable VSTHRD100 // Avoid async void methods
|
2023-08-24 23:08:56 +08:00
|
|
|
private async void SendPingMessageTimerCallback(object? sender, ElapsedEventArgs e)
|
2021-02-23 18:03:37 +08:00
|
|
|
#pragma warning restore VSTHRD100 // Avoid async void methods
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await this.semaphoreSlim.WaitAsync().ConfigureAwait(false);
|
|
|
|
try
|
|
|
|
{
|
2022-08-25 18:42:36 +08:00
|
|
|
if (this.danmakuTransport is null)
|
2021-02-23 18:03:37 +08:00
|
|
|
return;
|
|
|
|
|
2022-08-25 18:42:36 +08:00
|
|
|
await this.SendPingAsync().ConfigureAwait(false);
|
2021-02-23 18:03:37 +08:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
this.semaphoreSlim.Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
this.logger.Warning(ex, "Error running SendPingMessageTimerCallback");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#region Dispose
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
if (!this.disposedValue)
|
|
|
|
{
|
|
|
|
if (disposing)
|
|
|
|
{
|
|
|
|
// dispose managed state (managed objects)
|
|
|
|
this.timer.Dispose();
|
2022-08-25 18:42:36 +08:00
|
|
|
this.danmakuTransport?.Dispose();
|
2021-02-23 18:03:37 +08:00
|
|
|
this.semaphoreSlim.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
// free unmanaged resources (unmanaged objects) and override finalizer
|
|
|
|
// set large fields to null
|
|
|
|
this.disposedValue = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
|
|
|
|
// ~DanmakuClient()
|
|
|
|
// {
|
|
|
|
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
|
|
// Dispose(disposing: false);
|
|
|
|
// }
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
|
|
this.Dispose(disposing: true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Send
|
|
|
|
|
2023-07-16 16:01:50 +08:00
|
|
|
private Task SendHelloAsync(int roomid, long uid, string? buvid, string token)
|
2023-07-06 06:37:48 +08:00
|
|
|
{
|
|
|
|
var body = JsonConvert.SerializeObject(new
|
2021-02-23 18:03:37 +08:00
|
|
|
{
|
2023-07-16 16:01:50 +08:00
|
|
|
uid,
|
|
|
|
roomid,
|
2021-02-23 18:03:37 +08:00
|
|
|
protover = 0,
|
2023-07-16 16:01:50 +08:00
|
|
|
buvid,
|
2021-02-23 18:03:37 +08:00
|
|
|
platform = "web",
|
|
|
|
type = 2,
|
|
|
|
key = token,
|
2023-07-16 16:01:50 +08:00
|
|
|
}, Formatting.None, jsonSerializerSettings);
|
2023-07-06 06:37:48 +08:00
|
|
|
|
|
|
|
if (this.BeforeHandshake is { } func)
|
|
|
|
{
|
|
|
|
var newBody = func(body);
|
|
|
|
if (newBody is not null)
|
|
|
|
{
|
|
|
|
this.logger.Debug("Danmaku BeforeHandshake: {OldBody} => {NewBody}", body, newBody);
|
|
|
|
body = newBody;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.SendMessageAsync(7, body);
|
|
|
|
}
|
2021-02-23 18:03:37 +08:00
|
|
|
|
2022-08-25 18:42:36 +08:00
|
|
|
private Task SendPingAsync() => this.SendMessageAsync(2);
|
2021-02-23 18:03:37 +08:00
|
|
|
|
2022-08-25 18:42:36 +08:00
|
|
|
private async Task SendMessageAsync(int action, string body = "")
|
2021-02-23 18:03:37 +08:00
|
|
|
{
|
2022-08-25 18:42:36 +08:00
|
|
|
if (this.danmakuTransport is not { } transport)
|
|
|
|
return;
|
2021-02-23 18:03:37 +08:00
|
|
|
|
2023-08-24 23:08:56 +08:00
|
|
|
var payload = Encoding.UTF8.GetBytes(body);
|
2022-06-04 14:04:28 +08:00
|
|
|
const int headerLength = 16;
|
2023-08-24 23:08:56 +08:00
|
|
|
var totalLength = payload.Length + headerLength;
|
2022-08-25 18:42:36 +08:00
|
|
|
|
|
|
|
var buffer = ArrayPool<byte>.Shared.Rent(totalLength);
|
2021-02-23 18:03:37 +08:00
|
|
|
try
|
|
|
|
{
|
2022-08-25 18:42:36 +08:00
|
|
|
BinaryPrimitives.WriteUInt32BigEndian(new Span<byte>(buffer, 0, 4), (uint)totalLength);
|
2022-06-04 14:04:28 +08:00
|
|
|
BinaryPrimitives.WriteUInt16BigEndian(new Span<byte>(buffer, 4, 2), headerLength);
|
2021-02-23 18:03:37 +08:00
|
|
|
BinaryPrimitives.WriteUInt16BigEndian(new Span<byte>(buffer, 6, 2), 1);
|
|
|
|
BinaryPrimitives.WriteUInt32BigEndian(new Span<byte>(buffer, 8, 4), (uint)action);
|
|
|
|
BinaryPrimitives.WriteUInt32BigEndian(new Span<byte>(buffer, 12, 4), 1);
|
|
|
|
|
2023-08-24 23:08:56 +08:00
|
|
|
if (payload.Length > 0)
|
|
|
|
Array.Copy(payload, 0, buffer, headerLength, payload.Length);
|
2022-08-25 18:42:36 +08:00
|
|
|
|
|
|
|
await transport.SendAsync(buffer, 0, totalLength).ConfigureAwait(false);
|
2021-02-23 18:03:37 +08:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
ArrayPool<byte>.Shared.Return(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Receive
|
|
|
|
|
2022-08-25 18:42:36 +08:00
|
|
|
private static async Task ProcessDataAsync(PipeReader reader, Action<string> callback)
|
2021-02-23 18:03:37 +08:00
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
var result = await reader.ReadAsync();
|
|
|
|
var buffer = result.Buffer;
|
|
|
|
|
|
|
|
while (TryParseCommand(ref buffer, callback)) { }
|
|
|
|
|
|
|
|
reader.AdvanceTo(buffer.Start, buffer.End);
|
|
|
|
|
|
|
|
if (result.IsCompleted)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
await reader.CompleteAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool TryParseCommand(ref ReadOnlySequence<byte> buffer, Action<string> callback)
|
|
|
|
{
|
|
|
|
if (buffer.Length < 4)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int length;
|
|
|
|
{
|
|
|
|
var lengthSlice = buffer.Slice(buffer.Start, 4);
|
|
|
|
if (lengthSlice.IsSingleSegment)
|
|
|
|
{
|
|
|
|
length = BinaryPrimitives.ReadInt32BigEndian(lengthSlice.First.Span);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Span<byte> stackBuffer = stackalloc byte[4];
|
|
|
|
lengthSlice.CopyTo(stackBuffer);
|
|
|
|
length = BinaryPrimitives.ReadInt32BigEndian(stackBuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer.Length < length)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
var headerSlice = buffer.Slice(buffer.Start, 16);
|
|
|
|
buffer = buffer.Slice(headerSlice.End);
|
|
|
|
var bodySlice = buffer.Slice(buffer.Start, length - 16);
|
|
|
|
buffer = buffer.Slice(bodySlice.End);
|
|
|
|
|
|
|
|
DanmakuProtocol header;
|
|
|
|
if (headerSlice.IsSingleSegment)
|
|
|
|
{
|
|
|
|
Parse2Protocol(headerSlice.First.Span, out header);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Span<byte> stackBuffer = stackalloc byte[16];
|
|
|
|
headerSlice.CopyTo(stackBuffer);
|
|
|
|
Parse2Protocol(stackBuffer, out header);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header.Version == 2 && header.Action == 5)
|
|
|
|
ParseCommandDeflateBody(ref bodySlice, callback);
|
|
|
|
else
|
|
|
|
ParseCommandNormalBody(ref bodySlice, header.Action, callback);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void ParseCommandDeflateBody(ref ReadOnlySequence<byte> buffer, Action<string> callback)
|
|
|
|
{
|
|
|
|
using var deflate = new DeflateStream(buffer.Slice(2, buffer.End).AsStream(), CompressionMode.Decompress, leaveOpen: false);
|
|
|
|
var reader = PipeReader.Create(deflate);
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
|
|
|
|
// 全内存内运行同步返回,所以不会有问题
|
|
|
|
var result = reader.ReadAsync().Result;
|
|
|
|
#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
|
|
|
|
var inner_buffer = result.Buffer;
|
|
|
|
|
|
|
|
while (TryParseCommand(ref inner_buffer, callback)) { }
|
|
|
|
|
|
|
|
reader.AdvanceTo(inner_buffer.Start, inner_buffer.End);
|
|
|
|
|
|
|
|
if (result.IsCompleted)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
reader.Complete();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void ParseCommandNormalBody(ref ReadOnlySequence<byte> buffer, int action, Action<string> callback)
|
|
|
|
{
|
|
|
|
switch (action)
|
|
|
|
{
|
|
|
|
case 5:
|
|
|
|
{
|
|
|
|
if (buffer.Length > int.MaxValue)
|
|
|
|
throw new ArgumentOutOfRangeException("ParseCommandNormalBody buffer length larger than int.MaxValue");
|
|
|
|
|
|
|
|
var b = ArrayPool<byte>.Shared.Rent((int)buffer.Length);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
buffer.CopyTo(b);
|
|
|
|
var json = Encoding.UTF8.GetString(b, 0, (int)buffer.Length);
|
|
|
|
callback(json);
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
ArrayPool<byte>.Shared.Return(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static unsafe void Parse2Protocol(ReadOnlySpan<byte> buffer, out DanmakuProtocol protocol)
|
|
|
|
{
|
|
|
|
fixed (byte* ptr = buffer)
|
|
|
|
{
|
|
|
|
protocol = *(DanmakuProtocol*)ptr;
|
|
|
|
}
|
|
|
|
protocol.ChangeEndian();
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct DanmakuProtocol
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// 消息总长度 (协议头 + 数据长度)
|
|
|
|
/// </summary>
|
|
|
|
public int PacketLength;
|
|
|
|
/// <summary>
|
|
|
|
/// 消息头长度 (固定为16[sizeof(DanmakuProtocol)])
|
|
|
|
/// </summary>
|
|
|
|
public short HeaderLength;
|
|
|
|
/// <summary>
|
|
|
|
/// 消息版本号
|
|
|
|
/// </summary>
|
|
|
|
public short Version;
|
|
|
|
/// <summary>
|
|
|
|
/// 消息类型
|
|
|
|
/// </summary>
|
|
|
|
public int Action;
|
|
|
|
/// <summary>
|
|
|
|
/// 参数, 固定为1
|
|
|
|
/// </summary>
|
|
|
|
public int Parameter;
|
|
|
|
/// <summary>
|
|
|
|
/// 转为本机字节序
|
|
|
|
/// </summary>
|
|
|
|
public void ChangeEndian()
|
|
|
|
{
|
|
|
|
this.PacketLength = IPAddress.HostToNetworkOrder(this.PacketLength);
|
|
|
|
this.HeaderLength = IPAddress.HostToNetworkOrder(this.HeaderLength);
|
|
|
|
this.Version = IPAddress.HostToNetworkOrder(this.Version);
|
|
|
|
this.Action = IPAddress.HostToNetworkOrder(this.Action);
|
|
|
|
this.Parameter = IPAddress.HostToNetworkOrder(this.Parameter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|