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

时间:2016-12-21 03:04:29

标签: c# messages

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

    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)构造函数是否有办法根据我传入其中的字节数组的大小返回ExMessage个对象的列表。

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

2 个答案:

答案 0 :(得分: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

这让我:

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"));

答案 1 :(得分:1)

没有

我在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();     
   }
   ...
}