Add Ping command

This commit is contained in:
2019-03-23 13:33:36 +01:00
parent 233523ca4d
commit 4eff8e32d8
3 changed files with 70 additions and 2 deletions

View File

@@ -1,4 +1,8 @@
using System;
using PlantBox.Shared.Communication;
using PlantBox.Shared.Communication.Commands;
using System;
using System.Net;
using System.Net.Sockets;
namespace PlantBox.Broker
{
@@ -7,6 +11,29 @@ namespace PlantBox.Broker
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var listener = new TcpListener(IPAddress.Any, Connection.TCP_PORT);
listener.Start();
while (true)
{
try
{
var client = listener.AcceptTcpClient();
var stream = new CommandStream(client.GetStream());
var command = stream.Receive();
Console.WriteLine(command);
var ping = new Ping().Deserialize(command.Arguments);
Console.WriteLine($"Ping: {ping.Message}");
stream.Send(new Ping(ping.Message).ToCommandPacket(Command.Ping, command.ID));
}
catch (Exception)
{
}
}
}
}
}

View File

@@ -8,7 +8,7 @@ namespace PlantBox.Shared.Communication.Commands
/// A class serializable to a <see cref="CommandPacket"/>
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class CommandSerializable<T>
public abstract class CommandSerializable<T> where T : new()
{
/// <summary>
/// Serialize all the values in a string <see cref="Array"/>,

View File

@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace PlantBox.Shared.Communication.Commands
{
public class Ping : CommandSerializable<Ping>
{
public string Message { get; set; }
public Ping()
{
}
public Ping(string message)
{
Message = message;
}
public override Ping Deserialize(string[] arguments)
{
if (arguments == null)
{
throw new ArgumentNullException(nameof(arguments));
}
if (arguments.Length < 1)
{
throw new ArgumentException($"Excepted at least 1 argument, got {arguments.Length}");
}
Message = arguments[0];
return this;
}
public override string[] Serialize()
{
return new string[] { Message };
}
}
}