Skip to content

Commit

Permalink
Merge pull request #144 from ARKlab/feature/AddEncodingToArxOneFtpClient
Browse files Browse the repository at this point in the history
Add nullable FtpClientParameters to FtpConfig
  • Loading branch information
PeterRimmer71 authored Jan 8, 2024
2 parents 87dcfd1 + 348b62c commit 47072c0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
25 changes: 15 additions & 10 deletions Ark.Tools.FtpClient.ArxOne/FtpClientPoolArxOne.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,35 @@

namespace Ark.Tools.FtpClient
{

public class FtpClientPoolArxOne : FtpClientBase, IFtpClientPool
{
private readonly ArxOne.Ftp.FtpClient _client;
private readonly SemaphoreSlim _semaphore;
private readonly Action<FtpClientParameters>? _configurer;

private bool _isDisposed = false;

public FtpClientPoolArxOne(int maxPoolSize, FtpConfig ftpConfig)
public FtpClientPoolArxOne(int maxPoolSize, FtpConfig ftpConfig, Action<FtpClientParameters>? configurer = null)
: base(ftpConfig, maxPoolSize)
{
_client = _getClient();
_configurer = configurer;
_semaphore = new SemaphoreSlim(maxPoolSize, maxPoolSize);
_client = _getClient();
}

private protected virtual ArxOne.Ftp.FtpClient _getClient()
{
return new ArxOne.Ftp.FtpClient(
this.Uri, this.Credentials, new FtpClientParameters()
{
ConnectTimeout = TimeSpan.FromSeconds(60),
ReadWriteTimeout = TimeSpan.FromMinutes(3),
Passive = true,
});
var ftpClientParameters = new FtpClientParameters()
{
ConnectTimeout = TimeSpan.FromSeconds(60),
ReadWriteTimeout = TimeSpan.FromMinutes(3),
Passive = true,
};

if (_configurer != null)
_configurer(ftpClientParameters);

return new ArxOne.Ftp.FtpClient(this.Uri, this.Credentials, ftpClientParameters);
}

public override async Task<byte[]> DownloadFileAsync(string path, CancellationToken ctk = default)
Expand Down
13 changes: 12 additions & 1 deletion Ark.Tools.FtpClient.ArxOne/FtpClientPoolArxOneFactory.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
// Copyright (c) 2023 Ark Energy S.r.l. All rights reserved.
// Licensed under the MIT License. See LICENSE file for license information.
using Ark.Tools.FtpClient.Core;

using ArxOne.Ftp;

using EnsureThat;
using System;
using System.Net;
using System.Text;

namespace Ark.Tools.FtpClient
{
public class FtpClientPoolArxOneFactory : IFtpClientPoolFactory
{
private readonly Action<FtpClientParameters>? _configurer;

public FtpClientPoolArxOneFactory(Action<FtpClientParameters>? configurer = null)
{
_configurer = configurer;
}

public IFtpClientPool Create(int maxPoolSize, FtpConfig ftpConfig)
{
EnsureArg.IsNotNull(ftpConfig);
EnsureArg.IsNotNull(ftpConfig.Uri);
EnsureArg.IsNotNull(ftpConfig.Credentials);

return new FtpClientPoolArxOne(maxPoolSize, ftpConfig);
return new FtpClientPoolArxOne(maxPoolSize, ftpConfig, _configurer);
}
}
}
16 changes: 12 additions & 4 deletions Ark.Tools.FtpClient.ArxOne/FtpClientPoolArxOneWithSocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,29 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Ark.Tools.FtpClient
{
public class FtpClientPoolArxOneWithSocks : FtpClientPoolArxOne
{
private readonly ISocksConfig _config;
private readonly Action<FtpClientParameters>? _configurer;

public FtpClientPoolArxOneWithSocks(ISocksConfig config, int maxPoolSize, FtpConfig ftpConfig)
public FtpClientPoolArxOneWithSocks(ISocksConfig config, int maxPoolSize, FtpConfig ftpConfig, Action<FtpClientParameters>? configurer = null)
: base(maxPoolSize, ftpConfig)
{
this._configurer = configurer;
this._config = config;
}

private protected override ArxOne.Ftp.FtpClient _getClient()
{
var client = new ArxOne.Ftp.FtpClient(this.Uri, this.Credentials, new FtpClientParameters()
var ftpClientParameters = new FtpClientParameters()
{
ConnectTimeout = TimeSpan.FromSeconds(60),
ReadWriteTimeout = TimeSpan.FromMinutes(3),
Passive = true,
ProxyConnect = e =>
{
var s = new ProxySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Expand All @@ -49,9 +54,12 @@ private protected override ArxOne.Ftp.FtpClient _getClient()

return s;
}
});
};

return client;
if (_configurer != null)
_configurer(ftpClientParameters);

return new ArxOne.Ftp.FtpClient(this.Uri, this.Credentials, ftpClientParameters);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
using EnsureThat;
using System;
using System.Net;
using System.Text;
using ArxOne.Ftp;

namespace Ark.Tools.FtpClient
{
public class FtpClientPoolArxOneWithSocksFactory : IFtpClientPoolFactory
{
private readonly ISocksConfig _config;
private readonly Action<FtpClientParameters>? _configurer;

public FtpClientPoolArxOneWithSocksFactory(ISocksConfig config)
public FtpClientPoolArxOneWithSocksFactory(ISocksConfig config, Action<FtpClientParameters>? configurer = null)
{
EnsureArg.IsNotNull(config);

_configurer = configurer;
_config = config;
}

Expand All @@ -24,7 +28,7 @@ public IFtpClientPool Create(int maxPoolSize, FtpConfig ftpConfig)
EnsureArg.IsNotNull(ftpConfig.Uri);
EnsureArg.IsNotNull(ftpConfig.Credentials);

return new FtpClientPoolArxOneWithSocks(_config, maxPoolSize, ftpConfig);
return new FtpClientPoolArxOneWithSocks(_config, maxPoolSize, ftpConfig, _configurer);
}
}
}
1 change: 1 addition & 0 deletions Ark.Tools.FtpClient.Core/IFtpClientPoolFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See LICENSE file for license information.
using System;
using System.Net;
using System.Text;

namespace Ark.Tools.FtpClient.Core
{
Expand Down

0 comments on commit 47072c0

Please # to comment.