56 lines
1.8 KiB
C#
56 lines
1.8 KiB
C#
namespace RoboidControl {
|
|
|
|
/// <summary>
|
|
/// Message for sending generic text
|
|
/// </summary>
|
|
public class TextMsg : IMessage {
|
|
/// <summary>
|
|
/// The message ID
|
|
/// </summary>
|
|
public const byte Id = 0xB0;
|
|
/// <summary>
|
|
/// The length of the message without the text itself
|
|
/// </summary>
|
|
public const byte length = 2;
|
|
/// <summary>
|
|
/// The length of the text without the null terminator
|
|
/// </summary>
|
|
public byte textLength;
|
|
/// <summary>
|
|
/// The text
|
|
/// </summary>
|
|
public string text = "";
|
|
|
|
/// <summary>
|
|
/// Create a new message for sending
|
|
/// </summary>
|
|
/// <param name="text">The text to send</param>
|
|
public TextMsg(string text) {
|
|
this.textLength = (byte)text.Length;
|
|
this.text = text;
|
|
}
|
|
|
|
/// @copydoc Passer::RoboidControl::IMessage::IMessage(byte[] buffer)
|
|
public TextMsg(byte[] buffer) : base(buffer) {
|
|
this.textLength = buffer[0];
|
|
this.text = System.Text.Encoding.UTF8.GetString(buffer, 1, this.textLength);
|
|
}
|
|
|
|
/// @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;
|
|
|
|
#if DEBUG
|
|
System.Console.WriteLine($"Send TextMsg {this.textLength} {this.text}");
|
|
#endif
|
|
byte ix = 0;
|
|
buffer[ix++] = TextMsg.Id;
|
|
buffer[ix++] = this.textLength;
|
|
for (int textIx = 0; textIx < this.text.Length; textIx++)
|
|
buffer[ix++] = (byte)this.text[textIx];
|
|
return ix;
|
|
}
|
|
}
|
|
|
|
} |