Add CommandStream.Receive<T>

This commit is contained in:
2019-03-23 14:10:51 +01:00
parent 4eff8e32d8
commit 6b7ee73597

View File

@@ -1,4 +1,5 @@
using System;
using PlantBox.Shared.Communication.Commands;
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
@@ -48,6 +49,18 @@ namespace PlantBox.Shared.Communication
return new CommandPacket(command, packet[1].Split(';'));
}
/// <summary>
/// Read a <see cref="CommandPacket"/> from the stream and deserialize data to a <see cref="CommandSerializable{T}"/>
/// </summary>
/// <typeparam name="T">A valid <see cref="CommandSerializable{T}"/></typeparam>
/// <returns></returns>
public (CommandPacket, T) Receive<T>() where T : CommandSerializable<T>, new()
{
var packet = Receive();
return (packet, new T().Deserialize(packet.Arguments));
}
/// <summary>
/// Read a <see cref="CommandPacket"/> from the stream asynchronously
/// </summary>
@@ -74,6 +87,18 @@ namespace PlantBox.Shared.Communication
return new CommandPacket(command, packet[1].Split(';'));
}
/// <summary>
/// Read a <see cref="CommandPacket"/> from the stream and deserialize data to a <see cref="CommandSerializable{T}"/> asynchronously
/// </summary>
/// <typeparam name="T">A valid <see cref="CommandSerializable{T}"/></typeparam>
/// <returns></returns>
public async Task<(CommandPacket, T)> ReceiveAsync<T>() where T : CommandSerializable<T>, new()
{
var packet = await ReceiveAsync();
return (packet, new T().Deserialize(packet.Arguments));
}
/// <summary>
/// Write a <see cref="CommandPacket"/> in the stream
/// </summary>