SocketMessage.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using InABox.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection.Emit;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace InABox.WebSocket.Shared
  10. {
  11. public class ByteReader
  12. {
  13. public byte[] Data;
  14. public int Index;
  15. public ByteReader(byte[] data)
  16. {
  17. Data = data;
  18. Index = 0;
  19. }
  20. public Guid ReadGuid()
  21. {
  22. var guid = new Guid(new ReadOnlySpan<byte>(Data, Index, 16));
  23. Index += 16;
  24. return guid;
  25. }
  26. public string ReadUTF8String()
  27. {
  28. int i = Index;
  29. while (i < Data.Length && Data[i] != 0)
  30. {
  31. i++;
  32. }
  33. var str = Encoding.UTF8.GetString(Data, Index, i - Index);
  34. Index = i + 1;
  35. return str;
  36. }
  37. public byte ReadByte()
  38. {
  39. return Data[Index++];
  40. }
  41. public ByteReader Skip(int count)
  42. {
  43. Index += count;
  44. return this;
  45. }
  46. }
  47. public class ByteWriter
  48. {
  49. private Stream Stream;
  50. public ByteWriter(Stream stream)
  51. {
  52. Stream = stream;
  53. }
  54. public ByteWriter WriteByte(byte b)
  55. {
  56. Stream.WriteByte(b);
  57. return this;
  58. }
  59. public ByteWriter WriteGuid(Guid guid)
  60. {
  61. Stream.Write(guid.ToByteArray());
  62. return this;
  63. }
  64. /// <summary>
  65. /// Writes a null-terminated UTF-8 encoded string to the buffer
  66. /// </summary>
  67. /// <param name="str"></param>
  68. /// <returns></returns>
  69. public ByteWriter WriteUTF8String(string str)
  70. {
  71. Stream.Write(Encoding.UTF8.GetBytes(str));
  72. Stream.WriteByte(0);
  73. return this;
  74. }
  75. }
  76. public abstract class SocketMessage
  77. {
  78. public enum MessageType
  79. {
  80. Initial = 0x00,
  81. Notify = 0x01
  82. }
  83. public abstract MessageType Type { get; }
  84. protected abstract void Write(ByteWriter writer);
  85. public void Write(Stream stream)
  86. {
  87. var writer = new ByteWriter(stream)
  88. .WriteByte((byte)Type);
  89. Write(writer);
  90. }
  91. public byte[] WriteToBytes()
  92. {
  93. using(var stream = new MemoryStream())
  94. {
  95. Write(stream);
  96. return stream.GetBuffer().ToArray();
  97. }
  98. }
  99. public static SocketMessage? ReadMessage(byte[] data)
  100. {
  101. try
  102. {
  103. var type = (MessageType)data[0];
  104. var byteReader = new ByteReader(data).Skip(1);
  105. switch (type)
  106. {
  107. case MessageType.Initial:
  108. return InitialMessage.ReadMessage(byteReader);
  109. case MessageType.Notify:
  110. return NotifyMessage.ReadMessage(byteReader);
  111. }
  112. }
  113. catch (Exception)
  114. {
  115. return null;
  116. }
  117. return null;
  118. }
  119. }
  120. public class InitialMessage : SocketMessage
  121. {
  122. public override MessageType Type => MessageType.Initial;
  123. public Guid SessionID { get; set; }
  124. public Platform Platform { get; set; }
  125. public InitialMessage(Guid sessionID, Platform platform)
  126. {
  127. SessionID = sessionID;
  128. Platform = platform;
  129. }
  130. protected override void Write(ByteWriter writer)
  131. {
  132. writer.WriteGuid(SessionID)
  133. .WriteByte((byte)Platform);
  134. }
  135. public static InitialMessage ReadMessage(ByteReader data)
  136. {
  137. var session = data.ReadGuid();
  138. var platform = (Platform)data.ReadByte();
  139. return new InitialMessage(session, platform);
  140. }
  141. }
  142. public class NotifyMessage : SocketMessage
  143. {
  144. public override MessageType Type => MessageType.Notify;
  145. public string EntityType;
  146. public string EntityData;
  147. private NotifyMessage(string entityType, string entityData)
  148. {
  149. EntityType = entityType;
  150. EntityData = entityData;
  151. }
  152. protected override void Write(ByteWriter writer)
  153. {
  154. writer.WriteUTF8String(EntityType);
  155. writer.WriteUTF8String(EntityData);
  156. }
  157. public static NotifyMessage ReadMessage(ByteReader reader)
  158. {
  159. var type = reader.ReadUTF8String();
  160. var data = reader.ReadUTF8String();
  161. return new NotifyMessage(type, data);
  162. }
  163. public static NotifyMessage Notify<TNotification>(TNotification notification)
  164. {
  165. return new NotifyMessage(typeof(TNotification).EntityName(), Serialization.Serialize(notification));
  166. }
  167. public static NotifyMessage Notify(Type TNotification, object notification)
  168. {
  169. return new NotifyMessage(TNotification.EntityName(), Serialization.Serialize(notification));
  170. }
  171. }
  172. }