diff --git a/BBWY.Client/Helpers/PipeHelper.cs b/BBWY.Client/Helpers/PipeHelper.cs new file mode 100644 index 00000000..c0b49ef7 --- /dev/null +++ b/BBWY.Client/Helpers/PipeHelper.cs @@ -0,0 +1,80 @@ +using BBWY.Common.Models; +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Pipes; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace BBWY.Client.Helpers +{ + public class PipeHelper: IDenpendency + { + public PipeHelper() + { + _pipeServerDictionary = new Dictionary(); + } + + private IDictionary _pipeServerDictionary; + + public Action ServerReceiveCallback; + + private async Task WaitForClientConnectionsAsync(NamedPipeServerStream pipeServer) + { + while (true) + { + await pipeServer.WaitForConnectionAsync().ConfigureAwait(false); + try + { + const int bufferLength = 1024; + var buffer = new byte[bufferLength]; + using (var stream = new MemoryStream()) + { + while (true) + { + var bytesRead = await pipeServer.ReadAsync(buffer.AsMemory(0, bufferLength), CancellationToken.None).ConfigureAwait(false); + if (bytesRead == 0) + { + break; + } + stream.Write(buffer, 0, bytesRead); + } + + stream.Seek(0, SeekOrigin.Begin); + ServerReceiveCallback?.Invoke(Encoding.UTF8.GetString(stream.ToArray())); + } + } + finally + { + pipeServer.Disconnect(); + } + } + } + + public void CreateServer(string pipeName) + { + var _pipeServer = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 100, PipeTransmissionMode.Byte, PipeOptions.Asynchronous | PipeOptions.CurrentUserOnly); + _ = WaitForClientConnectionsAsync(_pipeServer); + } + + public void CloseServer(string pipeName) + { + if (_pipeServerDictionary.TryGetValue(pipeName, out NamedPipeServerStream pipeServer)) + { + try + { + pipeServer.Disconnect(); + pipeServer.Dispose(); + pipeServer.Close(); + _pipeServerDictionary.Remove(pipeName); + } + catch (Exception ex) + { + + } + } + } + } +} + diff --git a/BBWY.Client/ViewModels/Order/OrderListViewModel.cs b/BBWY.Client/ViewModels/Order/OrderListViewModel.cs index 09e6e323..d3f51486 100644 --- a/BBWY.Client/ViewModels/Order/OrderListViewModel.cs +++ b/BBWY.Client/ViewModels/Order/OrderListViewModel.cs @@ -1,4 +1,5 @@ using BBWY.Client.APIServices; +using BBWY.Client.Helpers; using BBWY.Client.Models; using BBWY.Client.Views.Order; using BBWY.Common.Extensions; @@ -157,7 +158,7 @@ namespace BBWY.Client.ViewModels /// public bool IsSDGroup { get => isSDGroup; set { Set(ref isSDGroup, value); } } - public OrderListViewModel(OrderService orderService, StatisticsService statisticsService, AfterOrderService afterOrderService, GlobalContext globalContext, ChoosePurchaseSchemeViewModel choosePurchaseSchemeViewModel) + public OrderListViewModel(OrderService orderService, StatisticsService statisticsService, AfterOrderService afterOrderService, GlobalContext globalContext, ChoosePurchaseSchemeViewModel choosePurchaseSchemeViewModel, PipeHelper pipeHelper) { IsSDGroup = globalContext.User.TeamName == "刷单组"; if (IsSDGroup) @@ -171,6 +172,7 @@ namespace BBWY.Client.ViewModels } random = new Random(); + this.pipeHelper = pipeHelper; this.globalContext = globalContext; this.orderService = orderService; this.statisticsService = statisticsService; @@ -182,7 +184,7 @@ namespace BBWY.Client.ViewModels StartDate = DateTime.Now.Date; ToDayOrderAchievement = new ToDayOrderAchievement(); InitSearchParam(); - + InitDongDong(); SetOrderStateCommand = new RelayCommand(SetOrderState); SearchOrderCommand = new RelayCommand(() => { @@ -1092,8 +1094,8 @@ namespace BBWY.Client.ViewModels } } else - { - + { + } } } diff --git a/BBWY.Client/ViewModels/Order/OrderListVoewModel_DongDong.cs b/BBWY.Client/ViewModels/Order/OrderListVoewModel_DongDong.cs new file mode 100644 index 00000000..c607871b --- /dev/null +++ b/BBWY.Client/ViewModels/Order/OrderListVoewModel_DongDong.cs @@ -0,0 +1,21 @@ +using BBWY.Client.Helpers; +using System; + +namespace BBWY.Client.ViewModels +{ + public partial class OrderListViewModel + { + private PipeHelper pipeHelper; + + public void InitDongDong() + { + pipeHelper.ServerReceiveCallback = OnDongDongSendMessage; + pipeHelper.CreateServer("bbwyPipeServer"); + } + + private void OnDongDongSendMessage(string msg) + { + Console.WriteLine(msg); + } + } +} diff --git a/BBWY.Client/Views/MainWindow.xaml b/BBWY.Client/Views/MainWindow.xaml index 3e8c2a75..65a6c06a 100644 --- a/BBWY.Client/Views/MainWindow.xaml +++ b/BBWY.Client/Views/MainWindow.xaml @@ -26,7 +26,7 @@ - +