fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. public class Packet
  6. {
  7. private List<byte> buffer = new List<byte>();
  8. private int byteIndex = 0;
  9. private int maxByteIndex = 0;
  10.  
  11. public byte[] Buffer
  12. {
  13. get
  14. {
  15. return buffer.ToArray();
  16. }
  17. }
  18.  
  19. public int Length
  20. {
  21. get
  22. {
  23. return buffer.Count;
  24. }
  25. }
  26.  
  27. public int ByteIndex
  28. {
  29. get
  30. {
  31. return byteIndex;
  32. }
  33. set
  34. {
  35. if (value < 0 || value >= buffer.Count)
  36. {
  37. throw new ArgumentOutOfRangeException("ByteIndex must be an index in the Buffer");
  38. }
  39. byteIndex = value;
  40. }
  41. }
  42.  
  43. public bool HasHeader
  44. {
  45. get
  46. {
  47. return byteIndex >= 2;
  48. }
  49. }
  50.  
  51. public bool HasData
  52. {
  53. get
  54. {
  55. return byteIndex == maxByteIndex;
  56. }
  57. }
  58.  
  59. public void Begin()
  60. {
  61. buffer.Add(0);
  62. buffer.Add(0);
  63. byteIndex += 2;
  64. }
  65.  
  66. public void End()
  67. {
  68. buffer[0] = (byte)(buffer.Count >> 8);
  69. buffer[1] = (byte)buffer.Count;
  70. }
  71.  
  72. public bool ReadHeader()
  73. {
  74. int previousByteIndex = byteIndex;
  75. byteIndex = 0;
  76. ushort length;
  77. ReadUInt16(out length);
  78. maxByteIndex = (int)length - 1;
  79. byteIndex = previousByteIndex;
  80. return maxByteIndex > 2;
  81. }
  82.  
  83. public void WriteEventID(byte value)
  84. {
  85. buffer.Add(value);
  86. byteIndex++;
  87. }
  88.  
  89. public void WriteByte(byte value)
  90. {
  91. buffer.Add(value);
  92. byteIndex++;
  93. }
  94.  
  95. public void WriteUInt16(ushort value)
  96. {
  97. buffer.Add((byte)(value >> 8));
  98. buffer.Add((byte)value);
  99. byteIndex += 2;
  100. }
  101.  
  102. public void WriteString(string value, int maxLength = 255)
  103. {
  104. if (maxLength < 0 || maxLength > ushort.MaxValue)
  105. {
  106. throw new ArgumentOutOfRangeException("maxLength must be in the range [0, 65535]");
  107. }
  108. if (value.Length > maxLength)
  109. {
  110. throw new ArgumentOutOfRangeException("String length was over the given max length");
  111. }
  112. WriteUInt16((ushort)value.Length);
  113. var characters = System.Text.Encoding.ASCII.GetBytes(value);
  114. foreach (var character in characters)
  115. {
  116. WriteByte(character);
  117. }
  118. }
  119.  
  120. public bool ReadEventID(out byte value)
  121. {
  122. return ReadByte(out value);
  123. }
  124.  
  125. public bool ReadByte(out byte value)
  126. {
  127. value = 0;
  128. if (byteIndex >= buffer.Count)
  129. {
  130. return false;
  131. }
  132. value = buffer[byteIndex];
  133. byteIndex++;
  134. return true;
  135. }
  136.  
  137. public bool ReadUInt16(out ushort value)
  138. {
  139. value = 0;
  140. if (byteIndex + 1 >= buffer.Count)
  141. {
  142. return false;
  143. }
  144. value = (ushort)(buffer[byteIndex] << 8);
  145. value |= (ushort)buffer[byteIndex + 1];
  146. byteIndex += 2;
  147. return true;
  148. }
  149.  
  150. public bool ReadString(out string value, int maxLength = 255)
  151. {
  152. value = string.Empty;
  153. ushort length;
  154. if (!ReadUInt16(out length) || length > maxLength || byteIndex + length > buffer.Count)
  155. {
  156. return false;
  157. }
  158. value = System.Text.Encoding.ASCII.GetString(buffer.ToArray(), byteIndex, (int)length);
  159. byteIndex += length;
  160. return true;
  161. }
  162. }
  163.  
  164. public class Test
  165. {
  166. public static void Main()
  167. {
  168. var packet = new Packet();
  169. packet.Begin();
  170. packet.WriteEventID(42);
  171. packet.WriteByte(43);
  172. packet.WriteUInt16(65535);
  173. packet.WriteString("Hello World", 20);
  174. packet.End();
  175.  
  176. packet.ByteIndex = 2;
  177. byte eventID;
  178. if (!packet.ReadEventID(out eventID)) { Console.WriteLine("Error reading"); return; }
  179. Console.WriteLine(eventID);
  180. byte byteValue;
  181. if (!packet.ReadByte(out byteValue)) { Console.WriteLine("Error reading"); return; }
  182. Console.WriteLine(byteValue);
  183. ushort ushortValue;
  184. if (!packet.ReadUInt16(out ushortValue)) { Console.WriteLine("Error reading"); return; }
  185. Console.WriteLine(ushortValue);
  186. string stringValue;
  187. if (!packet.ReadString(out stringValue)) { Console.WriteLine("Error reading"); return; }
  188. Console.WriteLine(stringValue);
  189.  
  190. /*
  191. To read from a TCP stream:
  192. // packet is a per end point object that persists through read callbacks
  193. for (int readByteIndex = 0; readByteIndex < receiveBufferSize; ++readByteIndex)
  194. {
  195. packet.WriteByte(receiveBuffer[readByteIndex]);
  196. if (!packet.HasHeader)
  197. {
  198. if (!packet.ReadHeader())
  199. {
  200. DisconnectEndPoint();
  201. }
  202. }
  203. if (!packet.HasData)
  204. {
  205. // Packets is a ConcurrentQueue
  206. endPoint.Packets.Enqueue(packet);
  207. packet = new Packet();
  208. }
  209. }
  210. */
  211. }
  212. }
Success #stdin #stdout 0.03s 37016KB
stdin
Standard input is empty
stdout
42
43
65535
Hello World