using System.IO;
using System.Threading.Tasks;
namespace Passer.RoboidControl {
///
/// Message for sending generic text.
///
public class TextMsg : IMessage {
///
/// The message ID
///
public const byte Id = 0xB0;
///
/// The length of the message without the text itself
///
public const byte length = 2;
///
/// The text
///
public string text = "";
///
/// Create a new message for sending
///
/// The text to send
public TextMsg(string text) {
this.text = text;
}
/// @copydoc Passer::RoboidControl::IMessage::IMessage(byte[] buffer)
public TextMsg(byte[] buffer) : base(buffer) { }
/// @copydoc Passer::RoboidControl::IMessage::Serialize
public override byte Serialize(ref byte[] buffer) {
if (buffer.Length < TextMsg.length + this.text.Length || this.text.Length == 0)
return 0;
byte ix = 0;
buffer[ix++] = TextMsg.Id;
buffer[ix++] = (byte)this.text.Length;
for (int textIx = 0; textIx < this.text.Length; textIx++)
buffer[ix++] = (byte)this.text[textIx];
return ix;
}
// public override void Deserialize(byte[] buffer) {
// uint ix = 0;
// uint strlen = buffer[ix++];
// this.text = System.Text.Encoding.UTF8.GetString(buffer, (int)ix, (int)strlen);
// }
// public static async Task Receive(Stream dataStream, Participant client, byte packetSize) {
// byte[] buffer = await Receive(dataStream, packetSize);
// TextMsg msg = new TextMsg(buffer);
// client.messageQueue.Enqueue(msg);
// return true;
// }
}
}