-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simple FTP #5
Open
artemiipatov
wants to merge
14
commits into
main
Choose a base branch
from
simpleFTP
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Simple FTP #5
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
818450c
Initial commit
artemiipatov d5f8afb
Implement client class
artemiipatov 4841b8d
rework server and client
artemiipatov c3a2f88
Add tests; rework client; add exceptions; refactor
artemiipatov a477ee5
refactor
artemiipatov b51fd93
refactor
artemiipatov 2ea09be
Fix bugs; add documentation
artemiipatov 4ed7c10
fix tests
artemiipatov 0895f08
migrate to .NET 7.0
artemiipatov f9cc0af
update ci
artemiipatov ae21f9f
fix bugs; add test
artemiipatov 4903049
remove client field; now tcpClient is created every time when user ma…
artemiipatov 97bab45
remove useless cancellation token
artemiipatov 60ba244
fix bugs
artemiipatov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
namespace Client; | ||
|
||
using Exceptions; | ||
using System.Net.Sockets; | ||
|
||
/// <summary> | ||
/// FTP client, that can process get and list queries. | ||
/// </summary> | ||
public class Client | ||
{ | ||
/// <summary> | ||
/// Gets list of files containing in the specific directory. | ||
/// </summary> | ||
/// <param name="host">The DNS name of the remote host to which you intend to connect.</param> | ||
/// <param name="port">The port number of the remote host to which you intend to connect.</param> | ||
/// <param name="pathToDirectory">Path to the needed directory.</param> | ||
/// <returns> | ||
/// Returns list of elements contained in the directory. | ||
/// List also consists of pairs, where first item is the name of the element, | ||
/// second item is a boolean value indicating, whether the element is folder or not. | ||
/// </returns> | ||
public async Task<List<(string, bool)>> ListAsync(string host, int port, string pathToDirectory) | ||
{ | ||
using var client = new TcpClient(); | ||
await client.ConnectAsync(host, port); | ||
|
||
await using var networkStream = client.GetStream(); | ||
await using var writer = new StreamWriter(networkStream); | ||
using var reader = new StreamReader(networkStream); | ||
|
||
var query = $"1 {pathToDirectory}"; | ||
await writer.WriteLineAsync(query); | ||
await writer.FlushAsync(); | ||
|
||
var response = await reader.ReadLineAsync(); | ||
return response is null or "-1" ? new List<(string, bool)>() : ParseResponse(response); | ||
} | ||
|
||
/// <summary> | ||
/// Downloads specific file from the server. | ||
/// </summary> | ||
/// <param name="host">The DNS name of the remote host to which you intend to connect.</param> | ||
/// <param name="port">The port number of the remote host to which you intend to connect.</param> | ||
/// <param name="pathToFile">Path to the needed file.</param> | ||
/// <param name="destinationStream">Stream to which file bytes will be moved.</param> | ||
/// <returns>Size of downloaded file.</returns> | ||
/// <exception cref="DataLossException">Throws if some bytes were lost while downloading.</exception> | ||
public async Task<long> GetAsync(string host, int port, string pathToFile, Stream destinationStream) | ||
{ | ||
var client = new TcpClient(); | ||
await client.ConnectAsync(host, port); | ||
|
||
try | ||
{ | ||
await using var networkStream = client.GetStream(); | ||
await using var writer = new StreamWriter(networkStream); | ||
|
||
var query = $"2 {pathToFile}"; | ||
await writer.WriteLineAsync(query); | ||
await writer.FlushAsync(); | ||
|
||
var sizeInBytes = new byte[8]; | ||
if (await networkStream.ReadAsync(sizeInBytes.AsMemory(0, 8)) != 8) | ||
{ | ||
throw new DataLossException("Some bytes were lost."); | ||
} | ||
|
||
var size = BitConverter.ToInt64(sizeInBytes); | ||
if (size == -1) | ||
{ | ||
return -1; | ||
} | ||
|
||
await CopyStream(destinationStream, networkStream, size); | ||
|
||
return size; | ||
} | ||
finally | ||
{ | ||
client.Close(); | ||
} | ||
} | ||
|
||
private async Task CopyStream(Stream destinationStream, NetworkStream sourceStream, long size) | ||
{ | ||
var bytesLeft = size; | ||
var chunkSize = Math.Min(1024, bytesLeft); | ||
var chunkBuffer = new byte[chunkSize]; | ||
|
||
while (bytesLeft > 0) | ||
{ | ||
var readBytesCount = await sourceStream.ReadAsync(chunkBuffer, 0, (int)chunkSize); | ||
|
||
if (readBytesCount != chunkSize) | ||
{ | ||
throw new DataLossException("Data loss during transmission and reception."); | ||
} | ||
|
||
await destinationStream.WriteAsync(chunkBuffer, 0, (int)chunkSize); | ||
await destinationStream.FlushAsync(); | ||
|
||
bytesLeft -= chunkSize; | ||
chunkSize = Math.Min(chunkSize, bytesLeft); | ||
} | ||
} | ||
|
||
private List<(string, bool)> ParseResponse(string response) | ||
{ | ||
var splitResponse = response.Split(" "); | ||
var numberOfElements = int.Parse(splitResponse[0]); | ||
var listOfElements = new List<(string, bool)>(); | ||
|
||
for (var i = 1; i < numberOfElements * 2; i += 2) | ||
{ | ||
var pair = (splitResponse[i], bool.Parse(splitResponse[i + 1])); | ||
listOfElements.Add(pair); | ||
} | ||
|
||
return listOfElements; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<LangVersion>11</LangVersion> | ||
|
||
<NoWarn>$(NoWarn),SA1633,SA1200,SA1101,SA1309</NoWarn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
namespace Client.Exceptions; | ||
|
||
/// <summary> | ||
/// The exception that is thrown in case of data loss. | ||
/// </summary> | ||
[Serializable] | ||
public class DataLossException : Exception | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DataLossException"/> class. | ||
/// </summary> | ||
public DataLossException() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DataLossException"/> class. | ||
/// </summary> | ||
/// <param name="message">Exception message.</param> | ||
public DataLossException(string message) | ||
: base(message) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DataLossException"/> class. | ||
/// </summary> | ||
/// <param name="message">Exception message.</param> | ||
/// <param name="inner">Inner exception.</param> | ||
public DataLossException(string message, Exception inner) | ||
: base(message, inner) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
var host = args[0]; | ||
var port = int.Parse(args[1]); | ||
|
||
var client = new Client.Client(); | ||
|
||
while (true) | ||
{ | ||
var query = Console.ReadLine(); | ||
if (query == null) | ||
{ | ||
break; | ||
} | ||
|
||
var queryArray = query.Split(" "); | ||
|
||
if (queryArray.Length == 1 | ||
&& queryArray[0] == "-stop") | ||
{ | ||
break; | ||
} | ||
|
||
if (queryArray.Length != 2) | ||
{ | ||
Console.WriteLine("Invalid query."); | ||
} | ||
|
||
switch (queryArray[0]) | ||
{ | ||
case "-list": | ||
{ | ||
var response = await client.ListAsync(host, port, queryArray[1]); | ||
foreach (var element in response) | ||
{ | ||
Console.WriteLine(element); | ||
} | ||
|
||
break; | ||
} | ||
|
||
case "-get": | ||
{ | ||
var path = string.Empty; | ||
|
||
while (true) | ||
{ | ||
Console.WriteLine("Path to save:"); | ||
path = Console.ReadLine(); | ||
|
||
if (File.Exists(path)) | ||
{ | ||
Console.WriteLine("File already exists."); | ||
continue; | ||
} | ||
|
||
break; | ||
} | ||
|
||
var file = new FileStream(path ?? throw new ArgumentNullException(), FileMode.Open); | ||
var response = await client.GetAsync(host, port, queryArray[1], file); | ||
Console.WriteLine(response); | ||
|
||
break; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Надо бы линтер настроить, очень ему Ваш код не нравится