misc(core): minor changes

This commit is contained in:
genteure 2023-08-24 23:48:11 +08:00
parent f63f7b21aa
commit 00f0d58505
6 changed files with 37 additions and 66 deletions

View File

@ -66,7 +66,7 @@ namespace BililiveRecorder.Core.Api.Danmaku
StatusChanged?.Invoke(this, StatusChangedEventArgs.False); StatusChanged?.Invoke(this, StatusChangedEventArgs.False);
} }
public async Task ConnectAsync(int roomid, DanmakuTransportMode transportMode, CancellationToken cancellationToken) public async Task ConnectAsync(int roomId, DanmakuTransportMode transportMode, CancellationToken cancellationToken)
{ {
if (this.disposedValue) if (this.disposedValue)
throw new ObjectDisposedException(nameof(DanmakuClient)); throw new ObjectDisposedException(nameof(DanmakuClient));
@ -74,19 +74,19 @@ namespace BililiveRecorder.Core.Api.Danmaku
if (!Enum.IsDefined(typeof(DanmakuTransportMode), transportMode)) if (!Enum.IsDefined(typeof(DanmakuTransportMode), transportMode))
throw new ArgumentOutOfRangeException(nameof(transportMode), transportMode, "Invalid danmaku transport mode."); throw new ArgumentOutOfRangeException(nameof(transportMode), transportMode, "Invalid danmaku transport mode.");
await this.semaphoreSlim.WaitAsync().ConfigureAwait(false); await this.semaphoreSlim.WaitAsync(cancellationToken).ConfigureAwait(false);
try try
{ {
if (this.danmakuTransport != null) if (this.danmakuTransport != null)
return; return;
var serverInfo = await this.apiClient.GetDanmakuServerAsync(roomid).ConfigureAwait(false); var serverInfo = await this.apiClient.GetDanmakuServerAsync(roomId).ConfigureAwait(false);
if (serverInfo.Data is null) if (serverInfo.Data is null)
return; return;
var danmakuServerInfo = serverInfo.Data.SelectDanmakuServer(transportMode); var danmakuServerInfo = serverInfo.Data.SelectDanmakuServer(transportMode);
this.logger.Debug("连接弹幕服务器 {Mode} {Host}:{Port} 房间: {RoomId} TokenLength: {TokenLength}", danmakuServerInfo.TransportMode, danmakuServerInfo.Host, danmakuServerInfo.Port, roomid, danmakuServerInfo.Token?.Length); this.logger.Debug("连接弹幕服务器 {Mode} {Host}:{Port} 房间: {RoomId} TokenLength: {TokenLength}", danmakuServerInfo.TransportMode, danmakuServerInfo.Host, danmakuServerInfo.Port, roomId, danmakuServerInfo.Token?.Length);
IDanmakuTransport transport = danmakuServerInfo.TransportMode switch IDanmakuTransport transport = danmakuServerInfo.TransportMode switch
{ {
@ -100,7 +100,7 @@ namespace BililiveRecorder.Core.Api.Danmaku
this.danmakuTransport = transport; this.danmakuTransport = transport;
await this.SendHelloAsync(roomid, this.apiClient.GetUid(), this.apiClient.GetBuvid3(), danmakuServerInfo.Token ?? string.Empty).ConfigureAwait(false); await this.SendHelloAsync(roomId, this.apiClient.GetUid(), this.apiClient.GetBuvid3(), danmakuServerInfo.Token ?? string.Empty).ConfigureAwait(false);
await this.SendPingAsync().ConfigureAwait(false); await this.SendPingAsync().ConfigureAwait(false);
if (cancellationToken.IsCancellationRequested) if (cancellationToken.IsCancellationRequested)
@ -129,7 +129,7 @@ namespace BililiveRecorder.Core.Api.Danmaku
await this.DisconnectAsync().ConfigureAwait(false); await this.DisconnectAsync().ConfigureAwait(false);
} }
catch (Exception) { } catch (Exception) { }
}); }, CancellationToken.None);
} }
finally finally
{ {
@ -370,7 +370,7 @@ namespace BililiveRecorder.Core.Api.Danmaku
case 5: case 5:
{ {
if (buffer.Length > int.MaxValue) if (buffer.Length > int.MaxValue)
throw new ArgumentOutOfRangeException("ParseCommandNormalBody buffer length larger than int.MaxValue"); throw new ArgumentOutOfRangeException(nameof(buffer), "ParseCommandNormalBody buffer length larger than int.MaxValue");
var b = ArrayPool<byte>.Shared.Rent((int)buffer.Length); var b = ArrayPool<byte>.Shared.Rent((int)buffer.Length);
try try

View File

@ -59,7 +59,7 @@ namespace BililiveRecorder.Core.Api.Http
if (!string.IsNullOrWhiteSpace(cookie_string)) if (!string.IsNullOrWhiteSpace(cookie_string))
{ {
headers.Add("Cookie", cookie_string); headers.Add("Cookie", cookie_string);
long.TryParse(matchCookieUidRegex.Match(cookie_string).Groups[1].Value, out var uid); _ = long.TryParse(matchCookieUidRegex.Match(cookie_string).Groups[1].Value, out var uid);
this.uid = uid; this.uid = uid;
var buvid3 = matchCookieBuvid3Regex.Match(cookie_string).Groups[1].Value; var buvid3 = matchCookieBuvid3Regex.Match(cookie_string).Groups[1].Value;
if (!string.IsNullOrWhiteSpace(buvid3)) if (!string.IsNullOrWhiteSpace(buvid3))

View File

@ -59,14 +59,24 @@ namespace BililiveRecorder.Core.Recording
while (!this.ct.IsCancellationRequested) while (!this.ct.IsCancellationRequested)
{ {
#if NET6_0_OR_GREATER
var bytesRead = await stream.ReadAsync(buffer, this.ct).ConfigureAwait(false);
#else
var bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, this.ct).ConfigureAwait(false); var bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, this.ct).ConfigureAwait(false);
#endif
if (bytesRead == 0) if (bytesRead == 0)
break; break;
Interlocked.Add(ref this.ioNetworkDownloadedBytes, bytesRead); Interlocked.Add(ref this.ioNetworkDownloadedBytes, bytesRead);
this.ioDiskStopwatch.Restart(); this.ioDiskStopwatch.Restart();
#if NET6_0_OR_GREATER
await file.WriteAsync(buffer.AsMemory(0, bytesRead)).ConfigureAwait(false);
#else
await file.WriteAsync(buffer, 0, bytesRead).ConfigureAwait(false); await file.WriteAsync(buffer, 0, bytesRead).ConfigureAwait(false);
#endif
this.ioDiskStopwatch.Stop(); this.ioDiskStopwatch.Stop();
lock (this.ioDiskStatsLock) lock (this.ioDiskStatsLock)

View File

@ -244,10 +244,8 @@ namespace BililiveRecorder.Core.Recording
} }
const int DefaultQn = 10000; const int DefaultQn = 10000;
var codecItem = await this.apiClient.GetCodecItemInStreamUrlAsync(roomid: roomid, qn: DefaultQn).ConfigureAwait(false); var codecItem = await this.apiClient.GetCodecItemInStreamUrlAsync(roomid: roomid, qn: DefaultQn).ConfigureAwait(false)
?? throw new Exception("no supported stream url, qn: " + DefaultQn);
if (codecItem is null)
throw new Exception("no supported stream url, qn: " + DefaultQn);
int selected_qn; int selected_qn;
// Select first avaiable qn // Select first avaiable qn
@ -331,7 +329,7 @@ match_qn_success:
streamHostInfoBuilder.Append(originalUri.Host); streamHostInfoBuilder.Append(originalUri.Host);
streamHostInfoBuilder.Append(" ["); streamHostInfoBuilder.Append(" [");
streamHostInfoBuilder.Append(scriptIp); streamHostInfoBuilder.Append(scriptIp);
streamHostInfoBuilder.Append("]"); streamHostInfoBuilder.Append(']');
goto sendRequest; goto sendRequest;
} }
@ -367,7 +365,7 @@ match_qn_success:
streamHostInfoBuilder.Append(originalUri.Host); streamHostInfoBuilder.Append(originalUri.Host);
streamHostInfoBuilder.Append(" ["); streamHostInfoBuilder.Append(" [");
streamHostInfoBuilder.Append(selected); streamHostInfoBuilder.Append(selected);
streamHostInfoBuilder.Append("]"); streamHostInfoBuilder.Append(']');
if (selected is null) if (selected is null)
{ {

View File

@ -33,20 +33,9 @@ namespace BililiveRecorder.Core.Scripting.Runtime
} }
} }
public void Append(string name, string value) public void Append(string name, string value) => this.query.Add(name, value, nullValueHandling: NullValueHandling.NameOnly);
{ public void Delete(string name) => this.query.Remove(name);
this.query.Add(name, value, nullValueHandling: NullValueHandling.NameOnly); public string?[][] Entries() => this.query.Select(x => new string?[] { x.Name, x.Value.ToString() }).ToArray();
}
public void Delete(string name)
{
this.query.Remove(name);
}
public string?[][] Entries()
{
return this.query.Select(x => new string?[] { x.Name, x.Value.ToString() }).ToArray();
}
public void ForEach(FunctionInstance callback, JsValue thisArg) public void ForEach(FunctionInstance callback, JsValue thisArg)
{ {
@ -58,44 +47,18 @@ namespace BililiveRecorder.Core.Scripting.Runtime
} }
} }
public string? Get(string name) public string? Get(string name) => this.query.TryGetFirst(name, out var value) ? value.ToString() : null;
{ public string?[] GetAll(string name) => this.query.GetAll(name).Select(x => x.ToString()).ToArray();
return this.query.TryGetFirst(name, out var value) ? value.ToString() : null; public bool Has(string name) => this.query.Contains(name);
} public string[] Keys() => this.query.Select(x => x.Name).ToArray();
public void Set(string name, string value) => this.query.AddOrReplace(name, value, nullValueHandling: NullValueHandling.NameOnly);
public string?[] GetAll(string name)
{
return this.query.GetAll(name).Select(x => x.ToString()).ToArray();
}
public bool Has(string name)
{
return this.query.Contains(name);
}
public string[] Keys()
{
return this.query.Select(x => x.Name).ToArray();
}
public void Set(string name, string value)
{
this.query.AddOrReplace(name, value, nullValueHandling: NullValueHandling.NameOnly);
}
public void Sort() public void Sort()
{ {
// do nothing // do nothing
} }
public override string ToString() public override string ToString() => this.query.ToString();
{ public string?[] Values() => this.query.Select(x => x.Value.ToString()).ToArray();
return this.query.ToString();
}
public string?[] Values()
{
return this.query.Select(x => x.Value.ToString()).ToArray();
}
} }
} }

View File

@ -246,21 +246,21 @@ returnDefaultPath:
var fullChild = Path.GetFullPath(child); var fullChild = Path.GetFullPath(child);
var parentSegments = fullParent.Split(separator, StringSplitOptions.None).AsSpan(); var parentSegments = fullParent.Split(separator, StringSplitOptions.None).AsSpan();
if (parentSegments[parentSegments.Length - 1] == "") if (parentSegments[^1] == "")
{ {
parentSegments = parentSegments.Slice(0, parentSegments.Length - 1); parentSegments = parentSegments[..^1];
} }
var childSegments = fullChild.Split(separator, StringSplitOptions.None).AsSpan(); var childSegments = fullChild.Split(separator, StringSplitOptions.None).AsSpan();
if (childSegments[childSegments.Length - 1] == "") if (childSegments[^1] == "")
{ {
childSegments = childSegments.Slice(0, childSegments.Length - 1); childSegments = childSegments[..^1];
} }
if (parentSegments.Length >= childSegments.Length) if (parentSegments.Length >= childSegments.Length)
return false; return false;
return childSegments.Slice(0, parentSegments.Length).SequenceEqual(parentSegments); return childSegments[..parentSegments.Length].SequenceEqual(parentSegments);
} }
} }
} }