From 7467d5ba81f2a3f5bd4aa53947e51bdf381522ef Mon Sep 17 00:00:00 2001 From: Eveldee Date: Sat, 27 Apr 2019 13:59:01 +0200 Subject: [PATCH] Implement IDisposable for CommandStream --- PlantBox.Broker/TcpManager.cs | 51 ++++++++++--------- .../ViewModels/HomeViewModel.cs | 7 +-- .../ViewModels/PlantViewModel.cs | 2 +- .../Communication/CommandStream.cs | 10 +++- 4 files changed, 38 insertions(+), 32 deletions(-) diff --git a/PlantBox.Broker/TcpManager.cs b/PlantBox.Broker/TcpManager.cs index c43c46c..bd40fb9 100644 --- a/PlantBox.Broker/TcpManager.cs +++ b/PlantBox.Broker/TcpManager.cs @@ -52,36 +52,37 @@ namespace PlantBox.Broker private void ClientLoop(TcpClient client) { - var commandStream = new CommandStream(client.GetStream()); - - try + using (var commandStream = new CommandStream(client.GetStream())) { - while (client.Connected) + try { - var packet = commandStream.Receive(); - Log($"Received command from {client.Client.RemoteEndPoint}"); - Log(packet.ToString()); - - switch (packet.Command) + while (client.Connected) { - case Command.Captors: - CaptorsCommand(commandStream, packet); - break; - case Command.Historic: - HistoricCommand(commandStream, packet); - break; - case Command.Info: - InfoCommand(commandStream, packet); - break; - case Command.Ping: - PingCommand(commandStream, packet); - break; + var packet = commandStream.Receive(); + Log($"Received command from {client.Client.RemoteEndPoint}"); + Log(packet.ToString()); + + switch (packet.Command) + { + case Command.Captors: + CaptorsCommand(commandStream, packet); + break; + case Command.Historic: + HistoricCommand(commandStream, packet); + break; + case Command.Info: + InfoCommand(commandStream, packet); + break; + case Command.Ping: + PingCommand(commandStream, packet); + break; + } } } - } - catch (Exception ex) - { - Log($"Client disconnected: {ex.Message}"); + catch (Exception ex) + { + Log($"Client disconnected: {ex.Message}"); + } } } diff --git a/PlantBox.Client/PlantBox.Client/ViewModels/HomeViewModel.cs b/PlantBox.Client/PlantBox.Client/ViewModels/HomeViewModel.cs index 319e9f9..244a4b3 100644 --- a/PlantBox.Client/PlantBox.Client/ViewModels/HomeViewModel.cs +++ b/PlantBox.Client/PlantBox.Client/ViewModels/HomeViewModel.cs @@ -21,10 +21,7 @@ namespace PlantBox.Client.ViewModels public HomeViewModel() { - var ids = App.Settings.IDs.ToList(); - ids.Add(152); - - Plants = LoadPlants(ids); + Plants = LoadPlants(App.Settings.IDs); } public ObservableCollection LoadPlants(IEnumerable ids) @@ -42,8 +39,8 @@ namespace PlantBox.Client.ViewModels private IEnumerable GetPlantInfos(IEnumerable ids) { using (var client = new TcpClient(App.Settings.BrokerIP, Connection.TCP_CLIENT_PORT)) + using (var stream = new CommandStream(client.GetStream())) { - var stream = new CommandStream(client.GetStream()); foreach (ulong id in ids) { diff --git a/PlantBox.Client/PlantBox.Client/ViewModels/PlantViewModel.cs b/PlantBox.Client/PlantBox.Client/ViewModels/PlantViewModel.cs index 5657c68..40346ca 100644 --- a/PlantBox.Client/PlantBox.Client/ViewModels/PlantViewModel.cs +++ b/PlantBox.Client/PlantBox.Client/ViewModels/PlantViewModel.cs @@ -123,8 +123,8 @@ namespace PlantBox.Client.ViewModels private void LoadValues() { using (var client = new TcpClient(App.Settings.BrokerIP, Connection.TCP_CLIENT_PORT)) + using (var stream = new CommandStream(client.GetStream())) { - var stream = new CommandStream(client.GetStream()); // Captors info stream.Send(new CaptorsRequest().ToCommandPacket(PlantInfo.ID)); diff --git a/PlantBox.Shared/Communication/CommandStream.cs b/PlantBox.Shared/Communication/CommandStream.cs index 4124903..a0c18f8 100644 --- a/PlantBox.Shared/Communication/CommandStream.cs +++ b/PlantBox.Shared/Communication/CommandStream.cs @@ -10,7 +10,7 @@ namespace PlantBox.Shared.Communication /// /// Wrap a to send easily, see Wiki > Protocol /// - public class CommandStream + public class CommandStream : IDisposable { private NetworkStream _stream; @@ -23,6 +23,14 @@ namespace PlantBox.Shared.Communication _stream = networkStream; } + /// + /// Release resources associated with the underlying stream + /// + public void Dispose() + { + _stream.Dispose(); + } + /// /// Read a from the stream ///