This commit is contained in:
2019-03-23 11:37:52 +01:00
parent faa54aca0e
commit 233523ca4d
5 changed files with 90 additions and 0 deletions

View File

@@ -6,15 +6,26 @@ using System.Threading.Tasks;
namespace PlantBox.Shared.Communication
{
/// <summary>
/// Wrap a <see cref="NetworkStream"/> to send <see cref="CommandPacket"/> easily, see Wiki > Protocol
/// </summary>
public class CommandStream
{
private NetworkStream _stream;
/// <summary>
/// Create a new <see cref="CommandStream"/>
/// </summary>
/// <param name="networkStream">A connected and valid <see cref="NetworkStream"/></param>
public CommandStream(NetworkStream networkStream)
{
_stream = networkStream;
}
/// <summary>
/// Read a <see cref="CommandPacket"/> from the stream
/// </summary>
/// <returns></returns>
public CommandPacket Receive()
{
// Length
@@ -37,6 +48,10 @@ namespace PlantBox.Shared.Communication
return new CommandPacket(command, packet[1].Split(';'));
}
/// <summary>
/// Read a <see cref="CommandPacket"/> from the stream asynchronously
/// </summary>
/// <returns></returns>
public async Task<CommandPacket> ReceiveAsync()
{
// Length
@@ -59,6 +74,10 @@ namespace PlantBox.Shared.Communication
return new CommandPacket(command, packet[1].Split(';'));
}
/// <summary>
/// Write a <see cref="CommandPacket"/> in the stream
/// </summary>
/// <param name="command"></param>
public void Send(CommandPacket command)
{
string packet = command.ToString();
@@ -68,6 +87,11 @@ namespace PlantBox.Shared.Communication
_stream.Write(data, 0, data.Length);
}
/// <summary>
/// Write a <see cref="CommandPacket"/> in the stream asynchronously
/// </summary>
/// <param name="command"></param>
/// <returns></returns>
public async Task SendAsync(CommandPacket command)
{
string packet = command.ToString();