Improve ASF codebase

This commit is contained in:
Archi
2021-06-21 22:45:18 +02:00
parent c76decda35
commit ed10eee974
3 changed files with 20 additions and 12 deletions

View File

@@ -120,6 +120,16 @@ namespace ArchiSteamFarm.Compatibility {
return source.IndexOf(value.ToString(), comparisonType);
}
public static async Task<int> ReadAsync(this Stream stream, ReadOnlyMemory<byte> buffer) {
if (stream == null) {
throw new ArgumentNullException(nameof(stream));
}
byte[] byteArray = buffer.ToArray();
return await stream.ReadAsync(byteArray, 0, byteArray.Length).ConfigureAwait(false);
}
public static async Task<WebSocketReceiveResult> ReceiveAsync(this WebSocket webSocket, byte[] buffer, CancellationToken cancellationToken) {
if (webSocket == null) {
throw new ArgumentNullException(nameof(webSocket));
@@ -182,6 +192,16 @@ namespace ArchiSteamFarm.Compatibility {
}
public static void TrimExcess<TKey, TValue>(this Dictionary<TKey, TValue> _) { } // no-op
public static async Task WriteAsync(this Stream stream, ReadOnlyMemory<byte> buffer) {
if (stream == null) {
throw new ArgumentNullException(nameof(stream));
}
byte[] byteArray = buffer.ToArray();
await stream.WriteAsync(byteArray, 0, byteArray.Length).ConfigureAwait(false);
}
#endif
}
}

View File

@@ -2902,11 +2902,7 @@ namespace ArchiSteamFarm.Steam {
await using (fileStream.ConfigureAwait(false)) {
fileStream.Seek(callback.Offset, SeekOrigin.Begin);
#if NETFRAMEWORK
await fileStream.WriteAsync(callback.Data, 0, callback.BytesToWrite).ConfigureAwait(false);
#else
await fileStream.WriteAsync(callback.Data.AsMemory(0, callback.BytesToWrite)).ConfigureAwait(false);
#endif
fileSize = fileStream.Length;
fileStream.Seek(0, SeekOrigin.Begin);

View File

@@ -155,21 +155,13 @@ namespace ArchiSteamFarm.Web {
byte[] buffer = new byte[8192]; // This is HttpClient's buffer, using more doesn't make sense
while (response.Content.CanRead) {
#if NETFRAMEWORK
int read = await response.Content.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
#else
int read = await response.Content.ReadAsync(buffer.AsMemory(0, buffer.Length)).ConfigureAwait(false);
#endif
if (read == 0) {
break;
}
#if NETFRAMEWORK
await ms.WriteAsync(buffer, 0, read).ConfigureAwait(false);
#else
await ms.WriteAsync(buffer.AsMemory(0, read)).ConfigureAwait(false);
#endif
if ((batchIncreaseSize == 0) || (batch >= 99)) {
continue;