commit bea2b4f180eaa4399da80163020f2e08b61db0ef Author: retr0volkov Date: Thu Sep 24 21:39:36 2026 +0300 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a0cd31f --- /dev/null +++ b/.gitignore @@ -0,0 +1,365 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Oo]ut/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd + +onnx-models \ No newline at end of file diff --git a/IPClab.4/App.xaml b/IPClab.4/App.xaml new file mode 100644 index 0000000..bea455b --- /dev/null +++ b/IPClab.4/App.xaml @@ -0,0 +1,16 @@ + + + + + + + + + + + + diff --git a/IPClab.4/App.xaml.cs b/IPClab.4/App.xaml.cs new file mode 100644 index 0000000..b763eba --- /dev/null +++ b/IPClab.4/App.xaml.cs @@ -0,0 +1,50 @@ +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Controls.Primitives; +using Microsoft.UI.Xaml.Data; +using Microsoft.UI.Xaml.Input; +using Microsoft.UI.Xaml.Media; +using Microsoft.UI.Xaml.Navigation; +using Microsoft.UI.Xaml.Shapes; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; +using Windows.ApplicationModel; +using Windows.ApplicationModel.Activation; +using Windows.Foundation; +using Windows.Foundation.Collections; + +// To learn more about WinUI, the WinUI project structure, +// and more about our project templates, see: http://aka.ms/winui-project-info. + +namespace IPClab._4 +{ + /// + /// Provides application-specific behavior to supplement the default Application class. + /// + public partial class App : Application + { + private Window? _window; + + /// + /// Initializes the singleton application object. This is the first line of authored code + /// executed, and as such is the logical equivalent of main() or WinMain(). + /// + public App() + { + InitializeComponent(); + } + + /// + /// Invoked when the application is launched. + /// + /// Details about the launch request and process. + protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args) + { + _window = new MainWindow(); + _window.Activate(); + } + } +} diff --git a/IPClab.4/Assets/LockScreenLogo.scale-200.png b/IPClab.4/Assets/LockScreenLogo.scale-200.png new file mode 100644 index 0000000..7440f0d Binary files /dev/null and b/IPClab.4/Assets/LockScreenLogo.scale-200.png differ diff --git a/IPClab.4/Assets/SplashScreen.scale-200.png b/IPClab.4/Assets/SplashScreen.scale-200.png new file mode 100644 index 0000000..32f486a Binary files /dev/null and b/IPClab.4/Assets/SplashScreen.scale-200.png differ diff --git a/IPClab.4/Assets/Square150x150Logo.scale-200.png b/IPClab.4/Assets/Square150x150Logo.scale-200.png new file mode 100644 index 0000000..53ee377 Binary files /dev/null and b/IPClab.4/Assets/Square150x150Logo.scale-200.png differ diff --git a/IPClab.4/Assets/Square44x44Logo.scale-200.png b/IPClab.4/Assets/Square44x44Logo.scale-200.png new file mode 100644 index 0000000..f713bba Binary files /dev/null and b/IPClab.4/Assets/Square44x44Logo.scale-200.png differ diff --git a/IPClab.4/Assets/Square44x44Logo.targetsize-24_altform-unplated.png b/IPClab.4/Assets/Square44x44Logo.targetsize-24_altform-unplated.png new file mode 100644 index 0000000..dc9f5be Binary files /dev/null and b/IPClab.4/Assets/Square44x44Logo.targetsize-24_altform-unplated.png differ diff --git a/IPClab.4/Assets/StoreLogo.png b/IPClab.4/Assets/StoreLogo.png new file mode 100644 index 0000000..a4586f2 Binary files /dev/null and b/IPClab.4/Assets/StoreLogo.png differ diff --git a/IPClab.4/Assets/Wide310x150Logo.scale-200.png b/IPClab.4/Assets/Wide310x150Logo.scale-200.png new file mode 100644 index 0000000..8b4a5d0 Binary files /dev/null and b/IPClab.4/Assets/Wide310x150Logo.scale-200.png differ diff --git a/IPClab.4/IPClab.4.csproj b/IPClab.4/IPClab.4.csproj new file mode 100644 index 0000000..c572d9d --- /dev/null +++ b/IPClab.4/IPClab.4.csproj @@ -0,0 +1,66 @@ + + + WinExe + net8.0-windows10.0.19041.0 + 10.0.17763.0 + IPClab._4 + app.manifest + x86;x64;ARM64 + win-x86;win-x64;win-arm64 + win-$(Platform).pubxml + true + false + true + enable + None + true + true + 14.0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + False + True + False + True + + \ No newline at end of file diff --git a/IPClab.4/MainWindow.xaml b/IPClab.4/MainWindow.xaml new file mode 100644 index 0000000..55269e8 --- /dev/null +++ b/IPClab.4/MainWindow.xaml @@ -0,0 +1,19 @@ + + + + + + + + + + + diff --git a/IPClab.4/MainWindow.xaml.cs b/IPClab.4/MainWindow.xaml.cs new file mode 100644 index 0000000..74f53aa --- /dev/null +++ b/IPClab.4/MainWindow.xaml.cs @@ -0,0 +1,31 @@ +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Controls.Primitives; +using Microsoft.UI.Xaml.Data; +using Microsoft.UI.Xaml.Input; +using Microsoft.UI.Xaml.Media; +using Microsoft.UI.Xaml.Navigation; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; +using Windows.Foundation; +using Windows.Foundation.Collections; + +// To learn more about WinUI, the WinUI project structure, +// and more about our project templates, see: http://aka.ms/winui-project-info. + +namespace IPClab._4 +{ + /// + /// An empty window that can be used on its own or navigated to within a Frame. + /// + public sealed partial class MainWindow : Window + { + public MainWindow() + { + InitializeComponent(); + } + } +} diff --git a/IPClab.4/Models/NetworkEvent.cs b/IPClab.4/Models/NetworkEvent.cs new file mode 100644 index 0000000..4982706 --- /dev/null +++ b/IPClab.4/Models/NetworkEvent.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IPClab._4.Models; + +public class NetworkEvent +{ + public string Message { get; set; } = string.Empty; + public long BytesTransferred { get; set; } + public string StatusText { get; set; } = string.Empty; + public bool IsError { get; set; } +} \ No newline at end of file diff --git a/IPClab.4/Package.appxmanifest b/IPClab.4/Package.appxmanifest new file mode 100644 index 0000000..649ab76 --- /dev/null +++ b/IPClab.4/Package.appxmanifest @@ -0,0 +1,53 @@ + + + + + + + + + + IPClab.4 + retrd + Assets\StoreLogo.png + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/IPClab.4/Properties/launchSettings.json b/IPClab.4/Properties/launchSettings.json new file mode 100644 index 0000000..7f522a4 --- /dev/null +++ b/IPClab.4/Properties/launchSettings.json @@ -0,0 +1,10 @@ +{ + "profiles": { + "IPClab.4 (Package)": { + "commandName": "MsixPackage" + }, + "IPClab.4 (Unpackaged)": { + "commandName": "Project" + } + } +} \ No newline at end of file diff --git a/IPClab.4/Services/TcpServerService.cs b/IPClab.4/Services/TcpServerService.cs new file mode 100644 index 0000000..10f68d2 --- /dev/null +++ b/IPClab.4/Services/TcpServerService.cs @@ -0,0 +1,128 @@ +using IPClab._4.Models; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace IPClab._4.Services; + +public class TcpServerService +{ + private TcpListener? _listener; + private CancellationTokenSource? _cts; + private long _totalBytesReceived; + private long _totalBytesSent; + + public bool IsListening { get; private set; } + + public async Task StartAsync(string ipAddress, int port, IProgress progress) + { + if (IsListening) return; + + _totalBytesReceived = 0; + _totalBytesSent = 0; + _cts = new CancellationTokenSource(); + + try + { + IPAddress localAddr = IPAddress.Parse(ipAddress); + _listener = new TcpListener(localAddr, port); + _listener.Start(); + IsListening = true; + + progress.Report(new NetworkEvent { StatusText = "Listening", Message = $"Server started on {ipAddress}:{port}" }); + + // Run the acceptance loop on a background thread task + _ = Task.Run(() => AcceptClientsAsync(_cts.Token, progress), _cts.Token); + } + catch (Exception ex) + { + IsListening = false; + progress.Report(new NetworkEvent { StatusText = "Faulted", Message = $"Failed to start server: {ex.Message}", IsError = true }); + throw; + } + } + + private async Task AcceptClientsAsync(CancellationToken token, IProgress progress) + { + while (!token.IsCancellationRequested) + { + try + { + // Accept incoming connection + TcpClient client = await _listener!.AcceptTcpClientAsync(token).ConfigureAwait(false); + progress.Report(new NetworkEvent { StatusText = "Connected", Message = "Client connected to server." }); + + // Handle this client session + await HandleClientSessionAsync(client, token, progress).ConfigureAwait(false); + } + catch (OperationCanceledException) when (token.IsCancellationRequested) + { + break; + } + catch (Exception ex) + { + progress.Report(new NetworkEvent { StatusText = "Listening", Message = $"Error accepting client: {ex.Message}", IsError = true }); + } + } + } + + private async Task HandleClientSessionAsync(TcpClient client, CancellationToken token, IProgress progress) + { + using (client) + using (NetworkStream stream = client.GetStream()) + { + // Quality Control Step: Enforce timeout boundaries + stream.ReadTimeout = 5000; + stream.WriteTimeout = 5000; + + using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) + { + while (!token.IsCancellationRequested && client.Connected) + { + try + { + // Delimiter-based Framing: Read line until '\n' boundary + string? receivedLine = await reader.ReadLineAsync(token).ConfigureAwait(false); + + // TCP Disconnect Signal: stream returns null/0 bytes + if (receivedLine == null) break; + + long messageBytes = Encoding.UTF8.GetByteCount(receivedLine) + Environment.NewLine.Length; + _totalBytesReceived += messageBytes; + + progress.Report(new NetworkEvent + { + StatusText = "Data Received", + Message = receivedLine, + BytesTransferred = _totalBytesReceived + }); + + // Lab 6 Echo/Response confirmation logic + byte[] responseBytes = Encoding.UTF8.GetBytes($"ACK: {receivedLine}\n"); + await stream.WriteAsync(responseBytes, 0, responseBytes.Length, token).ConfigureAwait(false); + _totalBytesSent += responseBytes.Length; + } + catch (Exception ex) + { + progress.Report(new NetworkEvent { StatusText = "Disconnected", Message = $"Session terminated: {ex.Message}", IsError = true }); + break; + } + } + } + } + progress.Report(new NetworkEvent { StatusText = "Listening", Message = "Client disconnected clean." }); + } + + public void Stop() + { + _cts?.Cancel(); + _listener?.Stop(); + IsListening = false; + } +} \ No newline at end of file diff --git a/IPClab.4/ViewModels/MainWindowViewModel.cs b/IPClab.4/ViewModels/MainWindowViewModel.cs new file mode 100644 index 0000000..8aa6cff --- /dev/null +++ b/IPClab.4/ViewModels/MainWindowViewModel.cs @@ -0,0 +1,12 @@ +using CommunityToolkit.Mvvm.ComponentModel; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IPClab._4.ViewModels; + +public partial class MainWindowViewModel : ObservableObject +{ +} diff --git a/IPClab.4/app.manifest b/IPClab.4/app.manifest new file mode 100644 index 0000000..4dd1ecd --- /dev/null +++ b/IPClab.4/app.manifest @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + PerMonitorV2 + + + \ No newline at end of file diff --git a/IPClab.5/App.xaml b/IPClab.5/App.xaml new file mode 100644 index 0000000..cdd58d0 --- /dev/null +++ b/IPClab.5/App.xaml @@ -0,0 +1,16 @@ + + + + + + + + + + + + diff --git a/IPClab.5/App.xaml.cs b/IPClab.5/App.xaml.cs new file mode 100644 index 0000000..93bea4e --- /dev/null +++ b/IPClab.5/App.xaml.cs @@ -0,0 +1,50 @@ +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Controls.Primitives; +using Microsoft.UI.Xaml.Data; +using Microsoft.UI.Xaml.Input; +using Microsoft.UI.Xaml.Media; +using Microsoft.UI.Xaml.Navigation; +using Microsoft.UI.Xaml.Shapes; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; +using Windows.ApplicationModel; +using Windows.ApplicationModel.Activation; +using Windows.Foundation; +using Windows.Foundation.Collections; + +// To learn more about WinUI, the WinUI project structure, +// and more about our project templates, see: http://aka.ms/winui-project-info. + +namespace IPClab._5 +{ + /// + /// Provides application-specific behavior to supplement the default Application class. + /// + public partial class App : Application + { + private Window? _window; + + /// + /// Initializes the singleton application object. This is the first line of authored code + /// executed, and as such is the logical equivalent of main() or WinMain(). + /// + public App() + { + InitializeComponent(); + } + + /// + /// Invoked when the application is launched. + /// + /// Details about the launch request and process. + protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args) + { + _window = new MainWindow(); + _window.Activate(); + } + } +} diff --git a/IPClab.5/Assets/LockScreenLogo.scale-200.png b/IPClab.5/Assets/LockScreenLogo.scale-200.png new file mode 100644 index 0000000..7440f0d Binary files /dev/null and b/IPClab.5/Assets/LockScreenLogo.scale-200.png differ diff --git a/IPClab.5/Assets/SplashScreen.scale-200.png b/IPClab.5/Assets/SplashScreen.scale-200.png new file mode 100644 index 0000000..32f486a Binary files /dev/null and b/IPClab.5/Assets/SplashScreen.scale-200.png differ diff --git a/IPClab.5/Assets/Square150x150Logo.scale-200.png b/IPClab.5/Assets/Square150x150Logo.scale-200.png new file mode 100644 index 0000000..53ee377 Binary files /dev/null and b/IPClab.5/Assets/Square150x150Logo.scale-200.png differ diff --git a/IPClab.5/Assets/Square44x44Logo.scale-200.png b/IPClab.5/Assets/Square44x44Logo.scale-200.png new file mode 100644 index 0000000..f713bba Binary files /dev/null and b/IPClab.5/Assets/Square44x44Logo.scale-200.png differ diff --git a/IPClab.5/Assets/Square44x44Logo.targetsize-24_altform-unplated.png b/IPClab.5/Assets/Square44x44Logo.targetsize-24_altform-unplated.png new file mode 100644 index 0000000..dc9f5be Binary files /dev/null and b/IPClab.5/Assets/Square44x44Logo.targetsize-24_altform-unplated.png differ diff --git a/IPClab.5/Assets/StoreLogo.png b/IPClab.5/Assets/StoreLogo.png new file mode 100644 index 0000000..a4586f2 Binary files /dev/null and b/IPClab.5/Assets/StoreLogo.png differ diff --git a/IPClab.5/Assets/Wide310x150Logo.scale-200.png b/IPClab.5/Assets/Wide310x150Logo.scale-200.png new file mode 100644 index 0000000..8b4a5d0 Binary files /dev/null and b/IPClab.5/Assets/Wide310x150Logo.scale-200.png differ diff --git a/IPClab.5/IPClab.5.csproj b/IPClab.5/IPClab.5.csproj new file mode 100644 index 0000000..f49a188 --- /dev/null +++ b/IPClab.5/IPClab.5.csproj @@ -0,0 +1,66 @@ + + + WinExe + net8.0-windows10.0.19041.0 + 10.0.17763.0 + IPClab._5 + app.manifest + x86;x64;ARM64 + win-x86;win-x64;win-arm64 + win-$(Platform).pubxml + true + false + true + enable + None + true + true + 14.0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + False + True + False + True + + \ No newline at end of file diff --git a/IPClab.5/MainWindow.xaml b/IPClab.5/MainWindow.xaml new file mode 100644 index 0000000..ea9e709 --- /dev/null +++ b/IPClab.5/MainWindow.xaml @@ -0,0 +1,19 @@ + + + + + + + + + + + diff --git a/IPClab.5/MainWindow.xaml.cs b/IPClab.5/MainWindow.xaml.cs new file mode 100644 index 0000000..5b2eb9a --- /dev/null +++ b/IPClab.5/MainWindow.xaml.cs @@ -0,0 +1,31 @@ +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Controls.Primitives; +using Microsoft.UI.Xaml.Data; +using Microsoft.UI.Xaml.Input; +using Microsoft.UI.Xaml.Media; +using Microsoft.UI.Xaml.Navigation; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; +using Windows.Foundation; +using Windows.Foundation.Collections; + +// To learn more about WinUI, the WinUI project structure, +// and more about our project templates, see: http://aka.ms/winui-project-info. + +namespace IPClab._5 +{ + /// + /// An empty window that can be used on its own or navigated to within a Frame. + /// + public sealed partial class MainWindow : Window + { + public MainWindow() + { + InitializeComponent(); + } + } +} diff --git a/IPClab.5/Models/NetworkEvent.cs b/IPClab.5/Models/NetworkEvent.cs new file mode 100644 index 0000000..294f6fb --- /dev/null +++ b/IPClab.5/Models/NetworkEvent.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IPClab._5.Models; + +public class NetworkEvent +{ + public string Message { get; set; } = string.Empty; + public long BytesTransferred { get; set; } + public string StatusText { get; set; } = string.Empty; + public bool IsError { get; set; } +} \ No newline at end of file diff --git a/IPClab.5/Package.appxmanifest b/IPClab.5/Package.appxmanifest new file mode 100644 index 0000000..510de80 --- /dev/null +++ b/IPClab.5/Package.appxmanifest @@ -0,0 +1,53 @@ + + + + + + + + + + IPClab.5 + retrd + Assets\StoreLogo.png + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/IPClab.5/Properties/launchSettings.json b/IPClab.5/Properties/launchSettings.json new file mode 100644 index 0000000..fb31efd --- /dev/null +++ b/IPClab.5/Properties/launchSettings.json @@ -0,0 +1,10 @@ +{ + "profiles": { + "IPClab.5 (Package)": { + "commandName": "MsixPackage" + }, + "IPClab.5 (Unpackaged)": { + "commandName": "Project" + } + } +} \ No newline at end of file diff --git a/IPClab.5/Service/TcpClientService.cs b/IPClab.5/Service/TcpClientService.cs new file mode 100644 index 0000000..01311e4 --- /dev/null +++ b/IPClab.5/Service/TcpClientService.cs @@ -0,0 +1,131 @@ +using IPClab._5.Models; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net.Sockets; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace IPClab._5.Service; + +public class TcpClientService +{ + private TcpClient? _client; + private NetworkStream? _stream; + private CancellationTokenSource? _cts; + private long _totalBytesReceived; + private long _totalBytesSent; + + public bool IsConnected => _client?.Connected ?? false; + + public async Task ConnectAsync(string ipAddress, int port, IProgress progress) + { + if (IsConnected) return; + + _totalBytesReceived = 0; + _totalBytesSent = 0; + _client = new TcpClient(); + _cts = new CancellationTokenSource(); + + try + { + progress.Report(new NetworkEvent { StatusText = "Connecting", Message = $"Connecting to {ipAddress}:{port}..." }); + + await _client.ConnectAsync(ipAddress, port).ConfigureAwait(false); + _stream = _client.GetStream(); + + // Quality Control Step: Apply structural operation timeouts + _stream.ReadTimeout = 5000; + _stream.WriteTimeout = 5000; + + progress.Report(new NetworkEvent { StatusText = "Connected", Message = "Successfully connected to server." }); + + // Fire and forget background incoming processing loop + _ = Task.Run(() => ListenForServerPayloadsAsync(_cts.Token, progress), _cts.Token); + } + catch (Exception ex) + { + CleanUp(); + progress.Report(new NetworkEvent { StatusText = "Disconnected", Message = $"Connection failed: {ex.Message}", IsError = true }); + throw; + } + } + + public async Task SendMessageAsync(string message, IProgress progress) + { + if (!IsConnected || _stream == null) + { + progress.Report(new NetworkEvent { StatusText = "Disconnected", Message = "Cannot send message. Client is offline.", IsError = true }); + return; + } + + try + { + // Quality Control Requirement: Delimiter-based separation + byte[] data = Encoding.UTF8.GetBytes(message + "\n"); + await _stream.WriteAsync(data, 0, data.Length).ConfigureAwait(false); + + _totalBytesSent += data.Length; + progress.Report(new NetworkEvent { StatusText = "Connected", Message = $"Sent: {message}", BytesTransferred = _totalBytesSent }); + } + catch (Exception ex) + { + progress.Report(new NetworkEvent { StatusText = "Disconnected", Message = $"Send fault occurred: {ex.Message}", IsError = true }); + CleanUp(); + } + } + + private async Task ListenForServerPayloadsAsync(CancellationToken token, IProgress progress) + { + using (StreamReader reader = new StreamReader(_stream!, Encoding.UTF8)) + { + while (!token.IsCancellationRequested && IsConnected) + { + try + { + string? incomingPayload = await reader.ReadLineAsync(token).ConfigureAwait(false); + + if (incomingPayload == null) break; // Clean TCP drop detected + + long rawBytesCount = Encoding.UTF8.GetByteCount(incomingPayload) + Environment.NewLine.Length; + _totalBytesReceived += rawBytesCount; + + progress.Report(new NetworkEvent + { + StatusText = "Connected", + Message = $"Received: {incomingPayload}", + BytesTransferred = _totalBytesReceived + }); + } + catch (OperationCanceledException) when (token.IsCancellationRequested) + { + break; + } + catch (Exception ex) + { + progress.Report(new NetworkEvent { StatusText = "Disconnected", Message = $"Read thread error: {ex.Message}", IsError = true }); + break; + } + } + } + + CleanUp(); + progress.Report(new NetworkEvent { StatusText = "Disconnected", Message = "Disconnected from server cleanly." }); + } + + public void Disconnect() + { + CleanUp(); + } + + private void CleanUp() + { + _cts?.Cancel(); + _stream?.Dispose(); + _client?.Dispose(); + _stream = null; + _client = null; + } +} \ No newline at end of file diff --git a/IPClab.5/ViewModels/MainWindowViewModel.cs b/IPClab.5/ViewModels/MainWindowViewModel.cs new file mode 100644 index 0000000..e0b835f --- /dev/null +++ b/IPClab.5/ViewModels/MainWindowViewModel.cs @@ -0,0 +1,12 @@ +using CommunityToolkit.Mvvm.ComponentModel; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IPClab._5.ViewModels; + +public partial class MainWindowViewModel : ObservableObject +{ +} diff --git a/IPClab.5/app.manifest b/IPClab.5/app.manifest new file mode 100644 index 0000000..829422f --- /dev/null +++ b/IPClab.5/app.manifest @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + PerMonitorV2 + + + \ No newline at end of file diff --git a/IPClab456.slnx b/IPClab456.slnx new file mode 100644 index 0000000..3409cbc --- /dev/null +++ b/IPClab456.slnx @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + +