2025-02-19 15:57:44 +01:00

57 lines
2.0 KiB
C#

using System.Threading.Tasks;
using System.IO;
namespace Passer.RoboidControl {
/// <summary>
/// Root structure for all communcation messages
/// </summary>
public class IMessage {
public IMessage() { }
/// <summary>
/// Create a message for receiving
/// </summary>
/// <param name="buffer">The byte array to parse</param>
public IMessage(byte[] buffer) {
//Deserialize(buffer);
}
/// <summary>
/// Serialize the message into a byte array for sending
/// </summary>
/// <param name="buffer">The buffer to serilize into</param>
/// <returns>The length of the message in the buffer</returns>
public virtual byte Serialize(ref byte[] buffer) { return 0; }
//public virtual void Deserialize(byte[] buffer) { }
// public bool SendTo(Participant client) {
// Serialize(ref client.buffer);
// return client.SendBuffer(client.buffer.Length);
// }
// public static bool SendMsg(Participant client, IMessage msg) {
// msg.Serialize(ref client.buffer);
// return client.SendBuffer(client.buffer.Length);
// }
// public static bool PublishMsg(Participant client, IMessage msg) {
// msg.Serialize(ref client.buffer);
// return client.PublishBuffer(client.buffer.Length);
// }
// public static async Task<byte[]> Receive(Stream dataStream, byte packetSize) {
// byte[] buffer = new byte[packetSize - 1]; // without msgId
// int byteCount = dataStream.Read(buffer, 0, packetSize - 1);
// while (byteCount < packetSize - 1) {
// // not all bytes have been read, wait and try again
// await Task.Delay(1);
// byteCount += dataStream.Read(buffer, byteCount, packetSize - 1 - byteCount);
// }
// return buffer;
// }
}
}