PipeMessageData.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace Vinno.FIS.TRTCClient.Common.Pipe
  5. {
  6. public class PipeTransferData
  7. {
  8. protected void WriteInt(int value, Stream stream)
  9. {
  10. stream.Write(BitConverter.GetBytes(value), 0, sizeof(int));
  11. }
  12. protected void WriteString(string value, Stream stream)
  13. {
  14. var valueData = Encoding.Unicode.GetBytes(value);
  15. stream.Write(BitConverter.GetBytes(valueData.Length), 0, sizeof(int));
  16. stream.Write(valueData, 0, valueData.Length);
  17. }
  18. protected void WriteBytes(byte[] value, Stream stream)
  19. {
  20. var dataLength = value.Length;
  21. stream.Write(BitConverter.GetBytes(dataLength), 0, sizeof(int));
  22. stream.Write(value, 0, dataLength);
  23. }
  24. protected void WriteBool(bool value, Stream stream)
  25. {
  26. stream.Write(BitConverter.GetBytes(value), 0, sizeof(bool));
  27. }
  28. protected static int ReadInt(Stream stream)
  29. {
  30. var data = new byte[sizeof(int)];
  31. stream.Read(data, 0, sizeof(int));
  32. return BitConverter.ToInt32(data, 0);
  33. }
  34. protected static string ReadString(Stream stream)
  35. {
  36. var dataLength = ReadInt(stream);
  37. var data = new byte[dataLength];
  38. stream.Read(data, 0, dataLength);
  39. return Encoding.Unicode.GetString(data, 0, dataLength);
  40. }
  41. protected static byte[] ReadBytes(Stream stream)
  42. {
  43. var dataLength = ReadInt(stream);
  44. var data = new byte[dataLength];
  45. stream.Read(data, 0, dataLength);
  46. return data;
  47. }
  48. protected static bool ReadBool(Stream stream)
  49. {
  50. var data = new byte[sizeof(bool)];
  51. stream.Read(data, 0, sizeof(bool));
  52. return BitConverter.ToBoolean(data, 0);
  53. }
  54. }
  55. public class PipeMessageData : PipeTransferData
  56. {
  57. public PipeMessage PipeMessage { get; }
  58. public byte[] MessageData { get; }
  59. public PipeMessageData(PipeMessage pipeMessage, byte[] data)
  60. {
  61. PipeMessage = pipeMessage;
  62. MessageData = data;
  63. }
  64. public byte[] ToBytes()
  65. {
  66. byte[] result = new byte[0];
  67. using (var stream = new MemoryStream())
  68. {
  69. WriteInt((int)PipeMessage, stream);
  70. WriteBytes(MessageData, stream);
  71. result = stream.ToArray();
  72. }
  73. return result;
  74. }
  75. public static PipeMessageData FromBytes(byte[] bytes)
  76. {
  77. using (var stream = new MemoryStream(bytes))
  78. {
  79. stream.Position = 0;
  80. var pipeMessage = (PipeMessage)ReadInt(stream);
  81. var messageData = ReadBytes(stream);
  82. return new PipeMessageData(pipeMessage, messageData);
  83. }
  84. }
  85. }
  86. public class PipeConnectOtherRoomMessage : PipeTransferData
  87. {
  88. public string UserId { get; }
  89. public int RoomId { get; }
  90. public PipeConnectOtherRoomMessage(string userId, int roomId)
  91. {
  92. UserId = userId;
  93. RoomId = roomId;
  94. }
  95. public byte[] ToBytes()
  96. {
  97. byte[] result = new byte[0];
  98. using (var stream = new MemoryStream())
  99. {
  100. WriteString(UserId, stream);
  101. WriteInt(RoomId, stream);
  102. result = stream.ToArray();
  103. }
  104. return result;
  105. }
  106. public static PipeConnectOtherRoomMessage FromBytes(byte[] bytes)
  107. {
  108. using (var stream = new MemoryStream(bytes))
  109. {
  110. stream.Position = 0;
  111. var userId = ReadString(stream);
  112. var roomId = ReadInt(stream);
  113. return new PipeConnectOtherRoomMessage(userId, roomId);
  114. }
  115. }
  116. }
  117. public class PipeBoolMessage : PipeTransferData
  118. {
  119. public bool Value { get; }
  120. public PipeBoolMessage(bool value)
  121. {
  122. Value = value;
  123. }
  124. public byte[] ToBytes()
  125. {
  126. byte[] result = new byte[0];
  127. using (var stream = new MemoryStream())
  128. {
  129. WriteBool(Value, stream);
  130. result = stream.ToArray();
  131. }
  132. return result;
  133. }
  134. public static PipeBoolMessage FromBytes(byte[] bytes)
  135. {
  136. using (var stream = new MemoryStream(bytes))
  137. {
  138. stream.Position = 0;
  139. var isMute = ReadBool(stream);
  140. return new PipeBoolMessage(isMute);
  141. }
  142. }
  143. }
  144. public class PipeRemoteUserLeaveRoomMessage : PipeTransferData
  145. {
  146. public string UserId { get; }
  147. public int Reason { get; }
  148. public PipeRemoteUserLeaveRoomMessage(string userId, int reason)
  149. {
  150. UserId = userId;
  151. Reason = reason;
  152. }
  153. public byte[] ToBytes()
  154. {
  155. byte[] result = new byte[0];
  156. using (var stream = new MemoryStream())
  157. {
  158. WriteString(UserId, stream);
  159. WriteInt(Reason, stream);
  160. result = stream.ToArray();
  161. }
  162. return result;
  163. }
  164. public static PipeRemoteUserLeaveRoomMessage FromBytes(byte[] bytes)
  165. {
  166. using (var stream = new MemoryStream(bytes))
  167. {
  168. stream.Position = 0;
  169. var userId = ReadString(stream);
  170. var reason = ReadInt(stream);
  171. return new PipeRemoteUserLeaveRoomMessage(userId, reason);
  172. }
  173. }
  174. }
  175. public class PipeUserIdMessage : PipeTransferData
  176. {
  177. public string UserId { get; }
  178. public PipeUserIdMessage(string userId)
  179. {
  180. UserId = userId;
  181. }
  182. public byte[] ToBytes()
  183. {
  184. byte[] result = new byte[0];
  185. using (var stream = new MemoryStream())
  186. {
  187. WriteString(UserId, stream);
  188. result = stream.ToArray();
  189. }
  190. return result;
  191. }
  192. public static PipeUserIdMessage FromBytes(byte[] bytes)
  193. {
  194. using (var stream = new MemoryStream(bytes))
  195. {
  196. stream.Position = 0;
  197. var userId = ReadString(stream);
  198. return new PipeUserIdMessage(userId);
  199. }
  200. }
  201. }
  202. public class PipeImageSizeData : PipeTransferData
  203. {
  204. public int Width { get; }
  205. public int Height { get; }
  206. public PipeImageSizeData(int width, int height)
  207. {
  208. Width = width;
  209. Height = height;
  210. }
  211. public byte[] ToBytes()
  212. {
  213. byte[] result = new byte[0];
  214. using (var stream = new MemoryStream())
  215. {
  216. WriteInt(Width, stream);
  217. WriteInt(Height, stream);
  218. result = stream.ToArray();
  219. }
  220. return result;
  221. }
  222. public static PipeImageSizeData FromBytes(byte[] bytes)
  223. {
  224. using (var stream = new MemoryStream(bytes))
  225. {
  226. stream.Position = 0;
  227. var width = ReadInt(stream);
  228. var height = ReadInt(stream);
  229. return new PipeImageSizeData(width, height);
  230. }
  231. }
  232. }
  233. public class PipeRemoteImageSizeData : PipeTransferData
  234. {
  235. public string UserId { get; }
  236. public int Width { get; set; }
  237. public int Height { get; set; }
  238. public PipeRemoteImageSizeData(string userId, int width, int height)
  239. {
  240. UserId = userId;
  241. Width = width;
  242. Height = height;
  243. }
  244. public byte[] ToBytes()
  245. {
  246. byte[] result = new byte[0];
  247. using (var stream = new MemoryStream())
  248. {
  249. WriteString(UserId, stream);
  250. WriteInt(Width, stream);
  251. WriteInt(Height, stream);
  252. result = stream.ToArray();
  253. }
  254. return result;
  255. }
  256. public static PipeRemoteImageSizeData FromBytes(byte[] bytes)
  257. {
  258. using (var stream = new MemoryStream(bytes))
  259. {
  260. stream.Position = 0;
  261. var userId = ReadString(stream);
  262. var width = ReadInt(stream);
  263. var height = ReadInt(stream);
  264. return new PipeRemoteImageSizeData(userId, width, height);
  265. }
  266. }
  267. }
  268. }