Add doc
This commit is contained in:
@@ -4,6 +4,9 @@ using System.Text;
|
||||
|
||||
namespace PlantBox.Shared.Communication
|
||||
{
|
||||
/// <summary>
|
||||
/// All valid commands as described in the Wiki > Commands
|
||||
/// </summary>
|
||||
public enum Command
|
||||
{
|
||||
Captors,
|
||||
|
||||
@@ -6,18 +6,41 @@ using System.Text;
|
||||
|
||||
namespace PlantBox.Shared.Communication
|
||||
{
|
||||
/// <summary>
|
||||
/// Represent a Packet as described in the Wiki > Protocol
|
||||
/// </summary>
|
||||
public class CommandPacket
|
||||
{
|
||||
/// <summary>
|
||||
/// A valid packet command, see the Wiki > Commands
|
||||
/// </summary>
|
||||
public Command Command { get; }
|
||||
/// <summary>
|
||||
/// A valid id
|
||||
/// </summary>
|
||||
public ulong ID { get; }
|
||||
/// <summary>
|
||||
/// Any arguments, see the Wiki page of a command for more infos
|
||||
/// </summary>
|
||||
public string[] Arguments { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Create a <see cref="CommandPacket"/>
|
||||
/// </summary>
|
||||
/// <param name="command"></param>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="arguments"></param>
|
||||
public CommandPacket(Command command, ulong id, string[] arguments)
|
||||
{
|
||||
Command = command;
|
||||
ID = id;
|
||||
Arguments = arguments ?? Array.Empty<string>();
|
||||
}
|
||||
/// <summary>
|
||||
/// Create a <see cref="CommandPacket"/>
|
||||
/// </summary>
|
||||
/// <param name="command"></param>
|
||||
/// <param name="arguments"></param>
|
||||
public CommandPacket(Command command, params string[] arguments)
|
||||
{
|
||||
Command = command;
|
||||
@@ -25,6 +48,10 @@ namespace PlantBox.Shared.Communication
|
||||
Arguments = arguments != null && arguments.Length > 1 ? arguments.Skip(1).ToArray() : Array.Empty<string>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a <see cref="CommandPacket"/> to a valid text representation of a packet, see Wiki > Protocol
|
||||
/// </summary>
|
||||
/// <returns>A valid packet text representation, ready to be sent</returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Command.ToString().ToUpperInvariant()}\n{ID}{(Arguments.Length > 0 ? $";{string.Join(";", Arguments)}" : string.Empty)}\n";
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -4,11 +4,32 @@ using System.Text;
|
||||
|
||||
namespace PlantBox.Shared.Communication.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// A class serializable to a <see cref="CommandPacket"/>
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public abstract class CommandSerializable<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Serialize all the values in a string <see cref="Array"/>,
|
||||
/// should contains everything to deserialize
|
||||
/// </summary>
|
||||
/// <returns>Valid arguments for a <see cref="CommandPacket"/></returns>
|
||||
public abstract string[] Serialize();
|
||||
|
||||
/// <summary>
|
||||
/// Create an instance of the class from a string <see cref="Array"/>
|
||||
/// </summary>
|
||||
/// <param name="arguments">Arguments from the <see cref="CommandSerializable{T}.Serialize"/> method</param>
|
||||
/// <returns><see cref="T"/> initilized accordingly</returns>
|
||||
public abstract T Deserialize(string[] arguments);
|
||||
|
||||
/// <summary>
|
||||
/// Convert a <see cref="CommandSerializable{T}"/> to a <see cref="CommandPacket"/>
|
||||
/// </summary>
|
||||
/// <param name="command">Command type</param>
|
||||
/// <param name="id">A valid id</param>
|
||||
/// <returns></returns>
|
||||
public CommandPacket ToCommandPacket(Command command, ulong id)
|
||||
{
|
||||
return new CommandPacket(command, id, Serialize());
|
||||
|
||||
@@ -4,14 +4,29 @@ using System.Text;
|
||||
|
||||
namespace PlantBox.Shared.Communication
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains all informations used to communicate
|
||||
/// </summary>
|
||||
public static class Connection
|
||||
{
|
||||
// UDP
|
||||
/// <summary>
|
||||
/// Port used by broadcast discovery
|
||||
/// </summary>
|
||||
public const int UDP_PORT = 1401;
|
||||
/// <summary>
|
||||
/// Request sent for a discovery
|
||||
/// </summary>
|
||||
public static readonly byte[] UDP_REQUEST = { 102, 210, 48, 255 };
|
||||
/// <summary>
|
||||
/// Reply sent for a discovery
|
||||
/// </summary>
|
||||
public static readonly byte[] UDP_REPLY = { 102, 210, 48, 0 };
|
||||
|
||||
// TCP
|
||||
/// <summary>
|
||||
/// Port used by the broker to accept clients
|
||||
/// </summary>
|
||||
public const int TCP_PORT = 1402;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user