| 1234567891011121314151617181920212223242526272829303132333435 |
- using System;
- using System.Text;
- namespace MAX
- {
- public class MessageBuilder
- {
- private StringBuilder _builder;
- public MessageBuilder()
- {
- _builder = new StringBuilder(1024);
- _builder.Append("\0\0");
- }
- public MessageBuilder Append<T>(T value)
- {
- _builder.Append(value);
- return this;
- }
- public byte[] GetBytes()
- {
- int length = _builder.Length - 2;
- if (length <= 0)
- throw new Exception("Message is too short");
- else if (length > 65535)
- throw new Exception("Message is too long");
- _builder[0] = (char)(length / 256);
- _builder[1] = (char)(length % 256);
- return Encoding.ASCII.GetBytes(_builder.ToString());
- }
- }
- }
|