Skip to content

Решение 2 контрольной#8

Open
Nigma-Ks wants to merge 1 commit intomainfrom
test2
Open

Решение 2 контрольной#8
Nigma-Ks wants to merge 1 commit intomainfrom
test2

Conversation

@Nigma-Ks
Copy link
Copy Markdown
Owner

@Nigma-Ks Nigma-Ks commented Apr 6, 2024

No description provided.

Copy link
Copy Markdown

@yurii-litvinov yurii-litvinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В CI тест не прошёл. Но в целом ок, плюс-минус проблемы с корректной остановкой и некоторые небольшие проблемы со стилем кодирования. 7 баллов из 10.

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.NET 7 умер. .NET 9 Preview 2 даже давно доступен. Переходите на .NET 8

using System.Net;
using System.Net.Sockets;

public class Client
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Надо комментарии

/// <summary>
/// Starts client, that writes messages from concole, communicates witn server and stops when get "exit" from server or from comsole.
/// </summary>
/// <returns></returns>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Пустые тэги не нужны

Comment on lines +31 to +33
var stream = client.GetStream();
var writer = new StreamWriter(stream) { AutoFlush = true };
var reader = new StreamReader(stream);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Кто-то из них должен быть объявлен с using, чтобы корректно закрыть поток

Comment on lines +35 to +36
async void actionWriteEntered() => await WriteEntered(writer, lockObject);
async void actionReadPrintResponse() => await ReadPrintResponse(reader, lockObject);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нет нужды именованные функции объявлять. Так можно, но можно просто лямбды.

Comment on lines +53 to +56
lock (lockObject)
{
Console.WriteLine(message);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем, мы ведь его только что ввели. Вообще, Console.WriteLine внутри синхронизируется, lockObject не нужен

Comment on lines +83 to +86




Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Много лишних пустых строчек

/// Starts client, that writes messages from concole, communicates witn server and stops when get "exit" from server or from comsole.
/// </summary>
/// <returns></returns>
public async Task Start()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

По соглашению, принятому в .NET, все async-методы должны иметь суффикс "Async"

{
object lockObject = new();
listener.Start();
var tcpClient = await listener.AcceptTcpClientAsync();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вызывать длительную операцию без способа её отменить может быть плохой идеей, потому что можно задедлочиться. Тут, например, если к нам так и не подключится никакой клиент, AcceptTcpClientAsync никогда не вернёт управление и остановить сервер будет нельзя. Чтобы так не было, у AcceptTcpClientAsync есть перегрузка, принимающая CancellationToken.

{
if (message == "exit")
{
IsInterrupted = true;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут тоже, если IsInterrupted true, то ReadPrintResponse не закончит свою работу, пока клиент нам что-нибудь не пришлёт. Потенциальный дедлок.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants