From 8360f2d39fc8f3cdeea241414538b37439ca6979 Mon Sep 17 00:00:00 2001 From: ren Date: Mon, 29 Apr 2024 22:08:25 +0300 Subject: [PATCH] =?UTF-8?q?DARKMILK-6:=20=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=20EF=20=D0=B8=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=B4=D0=BA=D0=BB=D1=8E=D1=87=D0=B8=D1=82=D1=8C=D1=81?= =?UTF-8?q?=D1=8F=20=D0=BA=20=D0=B1=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LauncherDM.sln | 6 --- LauncherDM/LauncherDM.csproj | 4 +- .../Interfaces/ILoadingWindowService.cs | 4 +- .../Interfaces/IServerRequestService.cs | 4 +- LauncherDM/Services/LoadingWindowService.cs | 11 ++-- LauncherDM/Services/ServerRequestService.cs | 23 ++++----- .../ViewModels/LoadingWindowViewModel.cs | 6 +-- ServerTCP/ConsoleExtension.cs | 21 ++++++++ ServerTCP/DataBase/DataBaseCommands.cs | 30 +++++++++++ ServerTCP/MessageHeader.cs | 1 + ServerTCP/Models/Data/ApplicationContext.cs | 21 ++++++++ ServerTCP/Models/User.cs | 16 ++++++ ServerTCP/Program.cs | 50 +++++++++++++++---- ServerTCP/ServerTCP.csproj | 8 ++- 14 files changed, 159 insertions(+), 46 deletions(-) create mode 100644 ServerTCP/ConsoleExtension.cs create mode 100644 ServerTCP/DataBase/DataBaseCommands.cs create mode 100644 ServerTCP/Models/Data/ApplicationContext.cs create mode 100644 ServerTCP/Models/User.cs diff --git a/LauncherDM.sln b/LauncherDM.sln index 7d821a7..e6920cf 100644 --- a/LauncherDM.sln +++ b/LauncherDM.sln @@ -9,8 +9,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServerTCP", "ServerTCP\Serv EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebAppDM", "WebAppDM\WebAppDM.csproj", "{C448F5B1-3C10-4BB7-89E8-9CE7BF5126CB}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClientTCP", "ClientTCP\ClientTCP.csproj", "{FEB492ED-5E67-48DA-810B-4FDF1C32B44E}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -29,10 +27,6 @@ Global {C448F5B1-3C10-4BB7-89E8-9CE7BF5126CB}.Debug|Any CPU.Build.0 = Debug|Any CPU {C448F5B1-3C10-4BB7-89E8-9CE7BF5126CB}.Release|Any CPU.ActiveCfg = Release|Any CPU {C448F5B1-3C10-4BB7-89E8-9CE7BF5126CB}.Release|Any CPU.Build.0 = Release|Any CPU - {FEB492ED-5E67-48DA-810B-4FDF1C32B44E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {FEB492ED-5E67-48DA-810B-4FDF1C32B44E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {FEB492ED-5E67-48DA-810B-4FDF1C32B44E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {FEB492ED-5E67-48DA-810B-4FDF1C32B44E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/LauncherDM/LauncherDM.csproj b/LauncherDM/LauncherDM.csproj index 31d88d1..302ce82 100644 --- a/LauncherDM/LauncherDM.csproj +++ b/LauncherDM/LauncherDM.csproj @@ -2,7 +2,7 @@ WinExe - net6.0-windows + net8.0-windows enable true latest @@ -37,7 +37,7 @@ - + diff --git a/LauncherDM/Services/Interfaces/ILoadingWindowService.cs b/LauncherDM/Services/Interfaces/ILoadingWindowService.cs index 32ff7f4..b0fd2f4 100644 --- a/LauncherDM/Services/Interfaces/ILoadingWindowService.cs +++ b/LauncherDM/Services/Interfaces/ILoadingWindowService.cs @@ -4,8 +4,8 @@ interface ILoadingWindowService { bool CheckRequestServer(); - void GetTitle(); + string GetTitle(); - void DescInfoConnect(); + string DescInfoConnect(); } } diff --git a/LauncherDM/Services/Interfaces/IServerRequestService.cs b/LauncherDM/Services/Interfaces/IServerRequestService.cs index 77387d0..e733d5e 100644 --- a/LauncherDM/Services/Interfaces/IServerRequestService.cs +++ b/LauncherDM/Services/Interfaces/IServerRequestService.cs @@ -11,8 +11,6 @@ interface IServerRequestService { public MessageHeader SendMessageRequest(string data, MessageHeader.MessageType messageType, int length); - public MessageHeader SendMessageRequest(MessageHeader.MessageType messageType, int length); - - //public string SendMessageRequestT(T data, MessageHeader.MessageType messageType, int length); + public MessageHeader SendMessageRequest(MessageHeader.MessageType messageType); } } diff --git a/LauncherDM/Services/LoadingWindowService.cs b/LauncherDM/Services/LoadingWindowService.cs index 94e5a33..b4262b9 100644 --- a/LauncherDM/Services/LoadingWindowService.cs +++ b/LauncherDM/Services/LoadingWindowService.cs @@ -13,26 +13,27 @@ public bool CheckRequestServer() if (serverRequest is null) serverRequest = new ServerRequestService(); - var requestMessageServer = serverRequest.SendMessageRequest(MessageHeader.MessageType.Check, 0); + var requestMessageServer = serverRequest.SendMessageRequest(MessageHeader.MessageType.Check); return requestMessageServer.Message == "1"; } - public void GetTitle() + public string GetTitle() { if (serverRequest is null) serverRequest = new ServerRequestService(); - var requestMessageServer = serverRequest.SendMessageRequest(string.Empty, - MessageHeader.MessageType.Check, string.Empty.Length); + var requestMessageServer = serverRequest.SendMessageRequest(MessageHeader.MessageType.TitleLoading); + return requestMessageServer.Message; } - public void DescInfoConnect() + public string DescInfoConnect() { if (serverRequest is null) serverRequest = new ServerRequestService(); var requestMessageServer = serverRequest.SendMessageRequest(string.Empty, MessageHeader.MessageType.Check, string.Empty.Length); + return requestMessageServer.Message; } } } diff --git a/LauncherDM/Services/ServerRequestService.cs b/LauncherDM/Services/ServerRequestService.cs index b14cb13..7e671b2 100644 --- a/LauncherDM/Services/ServerRequestService.cs +++ b/LauncherDM/Services/ServerRequestService.cs @@ -1,11 +1,11 @@ using LauncherDM.Services.Interfaces; -using ServerTCP; using System; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Sockets; using System.Windows.Markup; +using ServerTCP; namespace LauncherDM.Services { @@ -28,11 +28,12 @@ public ServerRequestService() public MessageHeader SendMessageRequest(string data, MessageHeader.MessageType messageType, int length) { + TcpClient tcpClient = null; try { while (true) { - var tcpClient = new TcpClient(); + tcpClient = new TcpClient(); tcpClient.Connect(endPoint); var messageHeader = new MessageHeader(data, messageType, length + MessageHeader.LengthAndDataType); @@ -52,11 +53,6 @@ public MessageHeader SendMessageRequest(string data, MessageHeader.MessageType m getBytes = getBytes.Take(size + MessageHeader.LengthAndDataType).ToArray(); return MessageHeader.FromArray(getBytes); } while (tcpStream.DataAvailable); - - //WriteColorTextCmd("Получил сообщение: "); - //Console.Write(returnDataStrBuild); - tcpClient.Close(); - //return string.Empty; } } @@ -64,14 +60,15 @@ public MessageHeader SendMessageRequest(string data, MessageHeader.MessageType m { IDialogMessageBoxService dialogMessageBox = new DialogMessageBoxService(); dialogMessageBox.DialogShow("Error Server Reques", "Error Server Reques"); - //return string.Empty; + return null; + } + finally + { + tcpClient?.Close(); } - - - return null; } - public MessageHeader SendMessageRequest(MessageHeader.MessageType messageType, int length) + public MessageHeader SendMessageRequest(MessageHeader.MessageType messageType) { TcpClient tcpClient = null; try @@ -81,7 +78,7 @@ public MessageHeader SendMessageRequest(MessageHeader.MessageType messageType, i tcpClient = new TcpClient(); tcpClient.Connect(endPoint); - var messageHeader = new MessageHeader(messageType, length); + var messageHeader = new MessageHeader(messageType, 0); byte[] headerBytes = messageHeader.MessageToArray(); NetworkStream tcpStream = tcpClient.GetStream(); diff --git a/LauncherDM/ViewModels/LoadingWindowViewModel.cs b/LauncherDM/ViewModels/LoadingWindowViewModel.cs index c44adb9..7f04b71 100644 --- a/LauncherDM/ViewModels/LoadingWindowViewModel.cs +++ b/LauncherDM/ViewModels/LoadingWindowViewModel.cs @@ -77,15 +77,15 @@ public LoadingWindowViewModel() private void Load() { ICheckNetworkService checkNetwork = new CheckNetworkService(); - ILoadingWindowService ServerCheck = new LoadingWindowService(); + ILoadingWindowService Server = new LoadingWindowService(); Task.Run(() => { if (checkNetwork.CheckingNetworkConnection()) { - if (ServerCheck.CheckRequestServer()) + if (Server.CheckRequestServer()) { - DescInfoConnect = "sd"; + DescInfoConnect = Server.GetTitle(); Thread.Sleep(5000); _loadingWindow.Dispatcher.Invoke(() => diff --git a/ServerTCP/ConsoleExtension.cs b/ServerTCP/ConsoleExtension.cs new file mode 100644 index 0000000..17e5fa8 --- /dev/null +++ b/ServerTCP/ConsoleExtension.cs @@ -0,0 +1,21 @@ +namespace ServerTCP +{ + public static class ConsoleExtension + { + public static void WriteLineColor(string text, ConsoleColor foregroundColor = System.ConsoleColor.Gray, ConsoleColor backgroundColor = System.ConsoleColor.Black) + { + Console.ForegroundColor = foregroundColor; + Console.BackgroundColor = backgroundColor; + Console.WriteLine(text); + Console.ResetColor(); + } + + public static void WriteColor(string text, ConsoleColor foregroundColor = System.ConsoleColor.Gray, ConsoleColor backgroundColor = System.ConsoleColor.Black) + { + Console.ForegroundColor = foregroundColor; + Console.BackgroundColor = backgroundColor; + Console.Write(text); + Console.ResetColor(); + } + } +} diff --git a/ServerTCP/DataBase/DataBaseCommands.cs b/ServerTCP/DataBase/DataBaseCommands.cs new file mode 100644 index 0000000..735a4ce --- /dev/null +++ b/ServerTCP/DataBase/DataBaseCommands.cs @@ -0,0 +1,30 @@ +using System.Threading.Channels; +using ServerTCP.Models; +using ServerTCP.Models.Data; + +namespace ServerTCP.DataBase +{ + internal class DataBaseCommands + { + public static void Insert(object table, MessageHeader.MessageType messageType) + { + using (var db = new ApplicationContext()) + { + if (db.Database.CanConnect()) + ConsoleExtension.WriteLineColor("База данных доступна.", ConsoleColor.DarkGreen); + else + ConsoleExtension.WriteLineColor("База данных не доступна!", ConsoleColor.DarkRed); + + switch (messageType) + { + case MessageHeader.MessageType.Registration: + if (table is User user) + db.users.Add(user); + break; + } + db.SaveChanges(); + } + } + + } +} diff --git a/ServerTCP/MessageHeader.cs b/ServerTCP/MessageHeader.cs index 3d48a7f..06ae8a3 100644 --- a/ServerTCP/MessageHeader.cs +++ b/ServerTCP/MessageHeader.cs @@ -29,6 +29,7 @@ public enum MessageType : byte Data, Check, Title, + TitleLoading, } public MessageHeader(MessageType type, int length) diff --git a/ServerTCP/Models/Data/ApplicationContext.cs b/ServerTCP/Models/Data/ApplicationContext.cs new file mode 100644 index 0000000..05c020e --- /dev/null +++ b/ServerTCP/Models/Data/ApplicationContext.cs @@ -0,0 +1,21 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Options; +using System.Collections.Generic; + +namespace ServerTCP.Models.Data +{ + internal class ApplicationContext : DbContext + { + public DbSet users { get; set; } + + public ApplicationContext() + { + //Database.EnsureCreated(); // гарантирует, что БД существует или создаст её и таблицы там + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) // подключение к бд + { + optionsBuilder.UseNpgsql("Host=81.31.246.203;Port=5432;Database=DarkMilkBD;Username=gen_user;Password=lY8\\b@slc^g\\xJ"); + } + } +} diff --git a/ServerTCP/Models/User.cs b/ServerTCP/Models/User.cs new file mode 100644 index 0000000..f40fff1 --- /dev/null +++ b/ServerTCP/Models/User.cs @@ -0,0 +1,16 @@ +namespace ServerTCP.Models +{ + internal class User + { + public int id { get; set; } + public string username { get; set; } + public string login { get; set; } + public string email { get; set; } + public string password { get; set; } + + public User() + { + + } + } +} diff --git a/ServerTCP/Program.cs b/ServerTCP/Program.cs index 0ed9963..4175668 100644 --- a/ServerTCP/Program.cs +++ b/ServerTCP/Program.cs @@ -1,6 +1,7 @@ using System.Net; using System.Net.Sockets; -using System.Text; +using ServerTCP.DataBase; +using ServerTCP.Models; namespace ServerTCP { @@ -34,20 +35,49 @@ static void Main(string[] args) size = listener.Receive(buffer); // получение данных, количество, значение //buffer = buffer.Where(x => x != 0).ToArray(); var header = MessageHeader.FromArray(buffer); + + MessageHeader headerRequest = null; + byte[] headerRequestBytes; switch (header.Type) { case MessageHeader.MessageType.Check: - var headerRequest = new MessageHeader("1", MessageHeader.MessageType.Check, 1); - byte[] headerBytes = headerRequest.MessageToArray(); - listener.Send(headerBytes); + headerRequest = new MessageHeader("1", MessageHeader.MessageType.Check, 1); + headerRequestBytes = headerRequest.MessageToArray(); + listener.Send(headerRequestBytes); + break; + case MessageHeader.MessageType.Session: + break; + case MessageHeader.MessageType.Token: + break; + case MessageHeader.MessageType.Registration: + var user = new User + { + id = 1, login = "Gustaf", email = "gustavo@milk.su", username = "Gustavo", password = "sjafhfjhlkj12pje12j31kl23j1l123j" + }; + DataBaseCommands.Insert(user, MessageHeader.MessageType.Registration); + break; + case MessageHeader.MessageType.Log: + break; + case MessageHeader.MessageType.File: + break; + case MessageHeader.MessageType.Photo: + break; + case MessageHeader.MessageType.Data: + break; + case MessageHeader.MessageType.Title: + break; + case MessageHeader.MessageType.TitleLoading: + headerRequest = new MessageHeader("1", MessageHeader.MessageType.TitleLoading, 1); + headerRequestBytes = headerRequest.MessageToArray(); + listener.Send(headerRequestBytes); break; - //case MessageHeader.MessageType.Title: - // string messageRequest = "Привет, пупс!"; - // var header1 = new MessageHeader(messageRequest, MessageHeader.MessageType.Check, Encoding.UTF8.GetBytes(messageRequest).Length); - // byte[] headerBytes = header1.MessageToArray(); - // listener.Send(headerBytes); - // break; + //case MessageHeader.MessageType.Title: + // string messageRequest = "Привет, пупс!"; + // var header1 = new MessageHeader(messageRequest, MessageHeader.MessageType.Check, Encoding.UTF8.GetBytes(messageRequest).Length); + // byte[] headerBytes = header1.MessageToArray(); + // listener.Send(headerBytes); + // break; } //data.Append(Encoding.UTF8.GetString(buffer, 0, size)); // сохраняем, добавляем данные. Данные передаются в кодированном формате, будем использовать кодировку UTF8, раскодируем байты } diff --git a/ServerTCP/ServerTCP.csproj b/ServerTCP/ServerTCP.csproj index 74abf5c..d0adf2b 100644 --- a/ServerTCP/ServerTCP.csproj +++ b/ServerTCP/ServerTCP.csproj @@ -1,10 +1,14 @@ - + Exe - net6.0 + net8.0 enable enable + + + +