Implement IDisposable for CommandStream

This commit is contained in:
2019-04-27 13:59:01 +02:00
parent 76bbefd729
commit 7467d5ba81
4 changed files with 38 additions and 32 deletions

View File

@@ -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}");
}
}
}

View File

@@ -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<PlantInfo> LoadPlants(IEnumerable<ulong> ids)
@@ -42,8 +39,8 @@ namespace PlantBox.Client.ViewModels
private IEnumerable<PlantInfo> GetPlantInfos(IEnumerable<ulong> 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)
{

View File

@@ -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));

View File

@@ -10,7 +10,7 @@ namespace PlantBox.Shared.Communication
/// <summary>
/// Wrap a <see cref="NetworkStream"/> to send <see cref="CommandPacket"/> easily, see Wiki > Protocol
/// </summary>
public class CommandStream
public class CommandStream : IDisposable
{
private NetworkStream _stream;
@@ -23,6 +23,14 @@ namespace PlantBox.Shared.Communication
_stream = networkStream;
}
/// <summary>
/// Release resources associated with the underlying stream
/// </summary>
public void Dispose()
{
_stream.Dispose();
}
/// <summary>
/// Read a <see cref="CommandPacket"/> from the stream
/// </summary>