Implement IDisposable for CommandStream
This commit is contained in:
@@ -52,36 +52,37 @@ namespace PlantBox.Broker
|
|||||||
|
|
||||||
private void ClientLoop(TcpClient client)
|
private void ClientLoop(TcpClient client)
|
||||||
{
|
{
|
||||||
var commandStream = new CommandStream(client.GetStream());
|
using (var commandStream = new CommandStream(client.GetStream()))
|
||||||
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
while (client.Connected)
|
try
|
||||||
{
|
{
|
||||||
var packet = commandStream.Receive();
|
while (client.Connected)
|
||||||
Log($"Received command from {client.Client.RemoteEndPoint}");
|
|
||||||
Log(packet.ToString());
|
|
||||||
|
|
||||||
switch (packet.Command)
|
|
||||||
{
|
{
|
||||||
case Command.Captors:
|
var packet = commandStream.Receive();
|
||||||
CaptorsCommand(commandStream, packet);
|
Log($"Received command from {client.Client.RemoteEndPoint}");
|
||||||
break;
|
Log(packet.ToString());
|
||||||
case Command.Historic:
|
|
||||||
HistoricCommand(commandStream, packet);
|
switch (packet.Command)
|
||||||
break;
|
{
|
||||||
case Command.Info:
|
case Command.Captors:
|
||||||
InfoCommand(commandStream, packet);
|
CaptorsCommand(commandStream, packet);
|
||||||
break;
|
break;
|
||||||
case Command.Ping:
|
case Command.Historic:
|
||||||
PingCommand(commandStream, packet);
|
HistoricCommand(commandStream, packet);
|
||||||
break;
|
break;
|
||||||
|
case Command.Info:
|
||||||
|
InfoCommand(commandStream, packet);
|
||||||
|
break;
|
||||||
|
case Command.Ping:
|
||||||
|
PingCommand(commandStream, packet);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
catch (Exception ex)
|
||||||
catch (Exception ex)
|
{
|
||||||
{
|
Log($"Client disconnected: {ex.Message}");
|
||||||
Log($"Client disconnected: {ex.Message}");
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,10 +21,7 @@ namespace PlantBox.Client.ViewModels
|
|||||||
|
|
||||||
public HomeViewModel()
|
public HomeViewModel()
|
||||||
{
|
{
|
||||||
var ids = App.Settings.IDs.ToList();
|
Plants = LoadPlants(App.Settings.IDs);
|
||||||
ids.Add(152);
|
|
||||||
|
|
||||||
Plants = LoadPlants(ids);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ObservableCollection<PlantInfo> LoadPlants(IEnumerable<ulong> ids)
|
public ObservableCollection<PlantInfo> LoadPlants(IEnumerable<ulong> ids)
|
||||||
@@ -42,8 +39,8 @@ namespace PlantBox.Client.ViewModels
|
|||||||
private IEnumerable<PlantInfo> GetPlantInfos(IEnumerable<ulong> ids)
|
private IEnumerable<PlantInfo> GetPlantInfos(IEnumerable<ulong> ids)
|
||||||
{
|
{
|
||||||
using (var client = new TcpClient(App.Settings.BrokerIP, Connection.TCP_CLIENT_PORT))
|
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)
|
foreach (ulong id in ids)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -123,8 +123,8 @@ namespace PlantBox.Client.ViewModels
|
|||||||
private void LoadValues()
|
private void LoadValues()
|
||||||
{
|
{
|
||||||
using (var client = new TcpClient(App.Settings.BrokerIP, Connection.TCP_CLIENT_PORT))
|
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
|
// Captors info
|
||||||
stream.Send(new CaptorsRequest().ToCommandPacket(PlantInfo.ID));
|
stream.Send(new CaptorsRequest().ToCommandPacket(PlantInfo.ID));
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ namespace PlantBox.Shared.Communication
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Wrap a <see cref="NetworkStream"/> to send <see cref="CommandPacket"/> easily, see Wiki > Protocol
|
/// Wrap a <see cref="NetworkStream"/> to send <see cref="CommandPacket"/> easily, see Wiki > Protocol
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class CommandStream
|
public class CommandStream : IDisposable
|
||||||
{
|
{
|
||||||
private NetworkStream _stream;
|
private NetworkStream _stream;
|
||||||
|
|
||||||
@@ -23,6 +23,14 @@ namespace PlantBox.Shared.Communication
|
|||||||
_stream = networkStream;
|
_stream = networkStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Release resources associated with the underlying stream
|
||||||
|
/// </summary>
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_stream.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Read a <see cref="CommandPacket"/> from the stream
|
/// Read a <see cref="CommandPacket"/> from the stream
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user