//
// Copyright (c) slskd Team. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see https://www.gnu.org/licenses/.
//
namespace slskd
{
using System;
using System.Net.Sockets;
using Soulseek;
///
/// Core extensions; extensions for types specific to Soulseek or slskd.
///
public static class Extensions
{
///
/// Redacts this instance of Options, replacing properties marked with with '*****'.
///
///
/// Creates a deep clone before redacting.
///
/// The Options instance to redact.
/// A redacted instance.
public static Options Redact(this Options options)
{
var redacted = options.ToJson().FromJson();
Redactor.Redact(redacted, redactWith: "*****");
return redacted;
}
///
/// Creates a copy of this instance with the specified parameters changed.
///
/// The options instance to copy.
/// The read buffer size for underlying TCP connections.
/// The write buffer size for underlying TCP connections.
/// The size of the write queue for double buffered writes.
/// The connection timeout, in milliseconds, for client and peer TCP connections.
/// The inactivity timeout, in milliseconds, for peer TCP connections.
/// Optional SOCKS 5 proxy configuration options.
///
/// The delegate invoked during instantiation to configure the server Socket instance.
///
/// The new instance.
public static ConnectionOptions With(
this ConnectionOptions o,
int? readBufferSize = null,
int? writeBufferSize = null,
int? writeQueueSize = null,
int? connectTimeout = null,
int? inactivityTimeout = null,
ProxyOptions proxyOptions = null,
Action configureSocketAction = null) => new ConnectionOptions(
readBufferSize: readBufferSize ?? o.ReadBufferSize,
writeBufferSize: writeBufferSize ?? o.WriteBufferSize,
writeQueueSize: writeQueueSize ?? o.WriteQueueSize,
connectTimeout: connectTimeout ?? o.ConnectTimeout,
inactivityTimeout: inactivityTimeout ?? o.InactivityTimeout,
proxyOptions: proxyOptions ?? o.ProxyOptions,
configureSocket: configureSocketAction ?? o.ConfigureSocket);
///
/// Creates a new instance of from this instance of .
///
/// The UserStatistics instance from which to copy data.
/// The new instance.
public static UserStatisticsState ToUserStatisticsState(this UserStatistics stats) => new()
{
AverageSpeed = stats.AverageSpeed,
DirectoryCount = stats.DirectoryCount,
FileCount = stats.FileCount,
UploadCount = stats.UploadCount,
};
}
}