在 C# 中将消息分成更小的块

Chunking a message into smaller chunks in C#

我有一个 C# class 来创建消息。

    public class ExMessage
    {
        private UInt16 m_SendingId = 123;
        private byte m_control_byte_1;
        private enum destination_service : byte
        {
          serv_1 = 0x0,
          serv_2 = 0x1
        };
        private byte m_pd;
        private byte m_filler_bytes;
        private byte[] m_payload;

        public byte[] Payload
        {
            get
            {
                return m_payload;
            }
        }


        /// <summary>
        /// Constructor.
        /// </summary>
        public ExMessage(byte[] payload)
        {
            m_pd = 0x0;
            m_control_byte_1 = 0xA5;
            m_payload = payload;
            m_filler_bytes = 0xF;

        }

        protected void WriteBody(Stream stream)
        {
            stream.WriteByte(m_control_byte_1); 
            stream.WriteByte(0x0); 

            stream.WriteWordLittleEndian(m_SendingId); 
            stream.WriteByte((byte)destination_service.serv_2); 

            stream.WriteByte((byte)m_pd); // payload details

            stream.WriteByte((byte)m_payload.Length); //write the payload length

            var count = 0;
            while (count < m_payload.Length)  //copy over the actual payload.
            {
                stream.WriteByte((byte)m_payload[count]);
                count++;
            }

            var len = 48 - (5 + m_payload.Length);
            count = 0;
            while (count < len) //copy over spare bytes 0x3
            {
                stream.WriteByte(m_filler_bytes);
                count++;
            }
        }

        public byte[] ToBytes()
        {
            MemoryStream stream = new MemoryStream();
            WriteBody(stream);

            return stream.ToArray();
        }

我创建消息如下

 var Message = new ExMessage(Encoding.UTF8.GetBytes("HelloWorld"));

现在我的使用场景如下

1) 我使用的协议对负载施加了 40 字节的大小限制。如果我传递的有效负载中的字节数大于 40 字节,则应将其拆分为两条消息。

2) 有没有办法让构造函数根据我传递给它的字节数组的大小 return ExMessage 对象的列表。

3) 如果不是,处理这种情况的最佳方法是什么

我会这样做:

var size = 4;
var messages =
    Encoding.UTF8.GetBytes("HelloWorld") //48 65 6C 6C 6F 57 6F 72 6C 64
        .Select((b, i) => new { b, i }) // Enumerable of byte and index
        .GroupBy(x => x.i / size, x => x.b) // group by index divide by block size
        .Select(x => new ExMessage(x.ToArray())); // create messages

这给了我:

你可以这样实现它:

public class ExMessage
{
    private const int __size = 4;
    public static ExMessage[] Create(byte[] payload)
    {
        return payload
            .Select((b, i) => new { b, i })
              .GroupBy(x => x.i / __size, x => x.b)
              .Select(x => new ExMessage(x.ToArray()))
              .ToArray();
    }
    /* rest of class */
}

然后你可以这样称呼它:

var messages = ExMessage.Create(Encoding.UTF8.GetBytes("HelloWorld"));

没有

我会在 ExMessage 上使用静态函数,像这样:

class ExMessage
{
   ...
   public static List<ExMessage> CreateMessages(byte[] payload)
   {
      List<byte[]> chunks = ... split payload into 40byte chunks...
      return chunks.Select(p => new ExMessage(p).ToList();     
   }
   ...
}