diff --git a/PlantBox.Shared/Communication/Command.cs b/PlantBox.Shared/Communication/Command.cs new file mode 100644 index 0000000..7744d63 --- /dev/null +++ b/PlantBox.Shared/Communication/Command.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace PlantBox.Shared.Communication +{ + public enum Command + { + Captors, + Historic, + Invalid, + Ping + } +} diff --git a/PlantBox.Shared/Communication/CommandPacket.cs b/PlantBox.Shared/Communication/CommandPacket.cs new file mode 100644 index 0000000..3b9b9fe --- /dev/null +++ b/PlantBox.Shared/Communication/CommandPacket.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace PlantBox.Shared.Communication +{ + public class CommandPacket + { + public Command Command { get; } + public ulong ID { get; } + public string[] Arguments { get; } + + public CommandPacket(Command command, ulong id, string[] arguments) + { + Command = command; + ID = id; + Arguments = arguments ?? Array.Empty(); + } + public CommandPacket(Command command, params string[] arguments) + { + Command = command; + ID = ulong.Parse(arguments[0]); + Arguments = arguments != null && arguments.Length > 1 ? arguments.Skip(1).ToArray() : Array.Empty(); + } + + public override string ToString() + { + return $"{Command.ToString().ToUpperInvariant()}\n{ID}{(Arguments.Length > 0 ? $";{string.Join(";", Arguments)}" : string.Empty)}\n"; + } + } +} diff --git a/PlantBox.Shared/Communication/CommandStream.cs b/PlantBox.Shared/Communication/CommandStream.cs new file mode 100644 index 0000000..cd39b39 --- /dev/null +++ b/PlantBox.Shared/Communication/CommandStream.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace PlantBox.Shared.Communication +{ + public class CommandStream + { + private NetworkStream _stream; + + public CommandStream(NetworkStream networkStream) + { + _stream = networkStream; + } + + public CommandPacket Receive() + { + // Length + byte[] buffer = new byte[4]; + _stream.Read(buffer, 0, buffer.Length); + + int length = BitConverter.ToInt32(buffer, 0); + + // Data + buffer = new byte[length]; + _stream.Read(buffer, 0, buffer.Length); + + string[] packet = Encoding.UTF8.GetString(buffer).Split('\n'); + + if (!Enum.TryParse(packet[0], true, out Command command)) + { + command = Command.Invalid; + } + + return new CommandPacket(command, packet[1].Split(';')); + } + + public async Task ReceiveAsync() + { + // Length + byte[] buffer = new byte[4]; + await _stream.ReadAsync(buffer, 0, buffer.Length); + + int length = BitConverter.ToInt32(buffer, 0); + + // Data + buffer = new byte[length]; + await _stream.ReadAsync(buffer, 0, buffer.Length); + + string[] packet = Encoding.UTF8.GetString(buffer).Split('\n'); + + if (!Enum.TryParse(packet[0], true, out Command command)) + { + command = Command.Invalid; + } + + return new CommandPacket(command, packet[1].Split(';')); + } + + public void Send(CommandPacket command) + { + string packet = command.ToString(); + byte[] data = Encoding.UTF8.GetBytes(packet); + + _stream.Write(BitConverter.GetBytes(data.Length), 0, 4); + _stream.Write(data, 0, data.Length); + } + + public async Task SendAsync(CommandPacket command) + { + string packet = command.ToString(); + byte[] data = Encoding.UTF8.GetBytes(packet); + + await _stream.WriteAsync(BitConverter.GetBytes(data.Length), 0, 4); + await _stream.WriteAsync(data, 0, data.Length); + } + } +} diff --git a/PlantBox.Shared/Communication/Connection.cs b/PlantBox.Shared/Communication/Connection.cs new file mode 100644 index 0000000..d74b471 --- /dev/null +++ b/PlantBox.Shared/Communication/Connection.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace PlantBox.Shared.Communication +{ + public static class Connection + { + // UDP + public const int UDP_PORT = 1401; + public static readonly byte[] UDP_REQUEST = { 102, 210, 48, 255 }; + public static readonly byte[] UDP_REPLY = { 102, 210, 48, 0 }; + + // TCP + public const int TCP_PORT = 1402; + } +}