From 18df69cdbb20143d0bdcbc2ebcc39e135d0fc42f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Thu, 2 Feb 2023 10:14:06 +0100 Subject: [PATCH] feat: add packet providers --- .../Models/Packets/FilePacketProvider.cs | 98 +++++++++++++++++++ .../Models/Packets/IPacketProvider.cs | 48 +++++++++ .../Models/Packets/IPacketSender.cs | 34 +++++++ src/PacketLogger/Models/Packets/PacketInfo.cs | 12 +++ 4 files changed, 192 insertions(+) create mode 100644 src/PacketLogger/Models/Packets/FilePacketProvider.cs create mode 100644 src/PacketLogger/Models/Packets/IPacketProvider.cs create mode 100644 src/PacketLogger/Models/Packets/IPacketSender.cs create mode 100644 src/PacketLogger/Models/Packets/PacketInfo.cs diff --git a/src/PacketLogger/Models/Packets/FilePacketProvider.cs b/src/PacketLogger/Models/Packets/FilePacketProvider.cs new file mode 100644 index 0000000..5f9650d --- /dev/null +++ b/src/PacketLogger/Models/Packets/FilePacketProvider.cs @@ -0,0 +1,98 @@ +// +// FilePacketProvider.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.IO; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; +using DynamicData; +using NosSmooth.PacketSerializer.Abstractions.Attributes; +using PacketLogger.Models.Filters; +using Remora.Results; + +namespace PacketLogger.Models.Packets; + +/// +/// Provides packets using a file from the file system. +/// +public class FilePacketProvider : IPacketProvider +{ + private readonly string _fileName; + private IReadOnlyList? _packets; + private ObservableCollection? _filteredPackets; + + /// + /// Initializes a new instance of the class. + /// + /// The name of the file. + public FilePacketProvider(string fileName) + { + _fileName = fileName; + } + + /// + public bool IsOpen => false; + + /// + public SourceList Packets => throw new InvalidOperationException("File client not initialized yet."); + + /// + public async Task Open() + { + if (!File.Exists(_fileName)) + { + return new NotFoundError($"Could not find file {_fileName}"); + } + + var packets = new List(); + var index = 0; + foreach (var line in await File.ReadAllLinesAsync(_fileName)) + { + if (line.Length <= 1) + { + continue; + } + + var splitted = line.Split('\t', 3); + if (splitted.Length != 3) + { + continue; + } + + packets.Add + ( + new PacketInfo + ( + index++, + DateTime.Parse(splitted[0].Trim('[', ']')), + splitted[1] == "[Recv]" ? PacketSource.Server : PacketSource.Client, + splitted[2] + ) + ); + } + + _packets = packets.AsReadOnly(); + _filteredPackets = new ObservableCollection(_packets); + return Result.FromSuccess(); + } + + /// + public Task Close() + => Task.FromResult(Result.FromSuccess()); + + /// + public void Clear() + { + // Clearing packets from file does not make any sense... + } + + /// + public event PropertyChangedEventHandler? PropertyChanged; +} \ No newline at end of file diff --git a/src/PacketLogger/Models/Packets/IPacketProvider.cs b/src/PacketLogger/Models/Packets/IPacketProvider.cs new file mode 100644 index 0000000..c7b256f --- /dev/null +++ b/src/PacketLogger/Models/Packets/IPacketProvider.cs @@ -0,0 +1,48 @@ +// +// IPacketProvider.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Threading.Tasks; +using DynamicData; +using PacketLogger.Models.Filters; +using Remora.Results; + +namespace PacketLogger.Models.Packets; + +/// +/// Provides packets from some kind of a source. +/// +public interface IPacketProvider : INotifyPropertyChanged +{ + /// + /// Gets whether was called and successfully finished. + /// + public bool IsOpen { get; } + + /// + /// Gets the filtered packets from this provider. + /// + public SourceList Packets { get; } + + /// + /// Open the provider/connection, load etc. + /// + /// A result that may or may not have succeeded. + public Task Open(); + + /// + /// Close the connection, dispose. + /// + /// A result that may or may not have succeeded. + public Task Close(); + + /// + /// Clear all packets. + /// + public void Clear(); +} \ No newline at end of file diff --git a/src/PacketLogger/Models/Packets/IPacketSender.cs b/src/PacketLogger/Models/Packets/IPacketSender.cs new file mode 100644 index 0000000..4b040f0 --- /dev/null +++ b/src/PacketLogger/Models/Packets/IPacketSender.cs @@ -0,0 +1,34 @@ +// +// IPacketSender.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Threading; +using System.Threading.Tasks; +using Avalonia.Controls; +using Remora.Results; + +namespace PacketLogger.Models.Packets; + +/// +/// A provider that may as well send or receive packets. +/// +public interface IPacketSender : IPacketProvider +{ + /// + /// Send the given packets. + /// + /// The packet to send. + /// The cancellation token used for cancelling the operation. + /// A result that may or may not have succeeded. + Task SendPacket(string packetString, CancellationToken ct = default); + + /// + /// Receive the given packet. + /// + /// The packet to send. + /// The cancellation token used for cancelling the operation. + /// A result that may or may not have succeeded. + Task ReceivePacket(string packetString, CancellationToken ct = default); +} \ No newline at end of file diff --git a/src/PacketLogger/Models/Packets/PacketInfo.cs b/src/PacketLogger/Models/Packets/PacketInfo.cs new file mode 100644 index 0000000..9d10c2c --- /dev/null +++ b/src/PacketLogger/Models/Packets/PacketInfo.cs @@ -0,0 +1,12 @@ +// +// PacketInfo.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using NosSmooth.PacketSerializer.Abstractions.Attributes; + +namespace PacketLogger.Models.Packets; + +public record PacketInfo(long PacketIndex, DateTime Date, PacketSource Source, string PacketString); \ No newline at end of file -- 2.48.1