123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- using System;
- using System.IO;
- using System.Text;
- namespace Vinno.FIS.TRTCClient.Common.Pipe
- {
- public class PipeTransferData
- {
- protected void WriteInt(int value, Stream stream)
- {
- stream.Write(BitConverter.GetBytes(value), 0, sizeof(int));
- }
- protected void WriteString(string value, Stream stream)
- {
- var valueData = Encoding.Unicode.GetBytes(value);
- stream.Write(BitConverter.GetBytes(valueData.Length), 0, sizeof(int));
- stream.Write(valueData, 0, valueData.Length);
- }
- protected void WriteBytes(byte[] value, Stream stream)
- {
- var dataLength = value.Length;
- stream.Write(BitConverter.GetBytes(dataLength), 0, sizeof(int));
- stream.Write(value, 0, dataLength);
- }
- protected void WriteBool(bool value, Stream stream)
- {
- stream.Write(BitConverter.GetBytes(value), 0, sizeof(bool));
- }
- protected static int ReadInt(Stream stream)
- {
- var data = new byte[sizeof(int)];
- stream.Read(data, 0, sizeof(int));
- return BitConverter.ToInt32(data, 0);
- }
- protected static string ReadString(Stream stream)
- {
- var dataLength = ReadInt(stream);
- var data = new byte[dataLength];
- stream.Read(data, 0, dataLength);
- return Encoding.Unicode.GetString(data, 0, dataLength);
- }
- protected static byte[] ReadBytes(Stream stream)
- {
- var dataLength = ReadInt(stream);
- var data = new byte[dataLength];
- stream.Read(data, 0, dataLength);
- return data;
- }
- protected static bool ReadBool(Stream stream)
- {
- var data = new byte[sizeof(bool)];
- stream.Read(data, 0, sizeof(bool));
- return BitConverter.ToBoolean(data, 0);
- }
- }
- public class PipeMessageData : PipeTransferData
- {
- public PipeMessage PipeMessage { get; }
- public byte[] MessageData { get; }
- public PipeMessageData(PipeMessage pipeMessage, byte[] data)
- {
- PipeMessage = pipeMessage;
- MessageData = data;
- }
- public byte[] ToBytes()
- {
- byte[] result = new byte[0];
- using (var stream = new MemoryStream())
- {
- WriteInt((int)PipeMessage, stream);
- WriteBytes(MessageData, stream);
- result = stream.ToArray();
- }
- return result;
- }
- public static PipeMessageData FromBytes(byte[] bytes)
- {
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var pipeMessage = (PipeMessage)ReadInt(stream);
- var messageData = ReadBytes(stream);
- return new PipeMessageData(pipeMessage, messageData);
- }
- }
- }
- public class PipeConnectOtherRoomMessage : PipeTransferData
- {
- public string UserId { get; }
- public int RoomId { get; }
- public PipeConnectOtherRoomMessage(string userId, int roomId)
- {
- UserId = userId;
- RoomId = roomId;
- }
- public byte[] ToBytes()
- {
- byte[] result = new byte[0];
- using (var stream = new MemoryStream())
- {
- WriteString(UserId, stream);
- WriteInt(RoomId, stream);
- result = stream.ToArray();
- }
- return result;
- }
- public static PipeConnectOtherRoomMessage FromBytes(byte[] bytes)
- {
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var userId = ReadString(stream);
- var roomId = ReadInt(stream);
- return new PipeConnectOtherRoomMessage(userId, roomId);
- }
- }
- }
- public class PipeBoolMessage : PipeTransferData
- {
- public bool Value { get; }
- public PipeBoolMessage(bool value)
- {
- Value = value;
- }
- public byte[] ToBytes()
- {
- byte[] result = new byte[0];
- using (var stream = new MemoryStream())
- {
- WriteBool(Value, stream);
- result = stream.ToArray();
- }
- return result;
- }
- public static PipeBoolMessage FromBytes(byte[] bytes)
- {
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var isMute = ReadBool(stream);
- return new PipeBoolMessage(isMute);
- }
- }
- }
- public class PipeRemoteUserLeaveRoomMessage : PipeTransferData
- {
- public string UserId { get; }
- public int Reason { get; }
- public PipeRemoteUserLeaveRoomMessage(string userId, int reason)
- {
- UserId = userId;
- Reason = reason;
- }
- public byte[] ToBytes()
- {
- byte[] result = new byte[0];
- using (var stream = new MemoryStream())
- {
- WriteString(UserId, stream);
- WriteInt(Reason, stream);
- result = stream.ToArray();
- }
- return result;
- }
- public static PipeRemoteUserLeaveRoomMessage FromBytes(byte[] bytes)
- {
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var userId = ReadString(stream);
- var reason = ReadInt(stream);
- return new PipeRemoteUserLeaveRoomMessage(userId, reason);
- }
- }
- }
- public class PipeUserIdMessage : PipeTransferData
- {
- public string UserId { get; }
- public PipeUserIdMessage(string userId)
- {
- UserId = userId;
- }
- public byte[] ToBytes()
- {
- byte[] result = new byte[0];
- using (var stream = new MemoryStream())
- {
- WriteString(UserId, stream);
- result = stream.ToArray();
- }
- return result;
- }
- public static PipeUserIdMessage FromBytes(byte[] bytes)
- {
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var userId = ReadString(stream);
- return new PipeUserIdMessage(userId);
- }
- }
- }
- public class PipeImageSizeData : PipeTransferData
- {
- public int Width { get; }
- public int Height { get; }
- public PipeImageSizeData(int width, int height)
- {
- Width = width;
- Height = height;
- }
- public byte[] ToBytes()
- {
- byte[] result = new byte[0];
- using (var stream = new MemoryStream())
- {
- WriteInt(Width, stream);
- WriteInt(Height, stream);
- result = stream.ToArray();
- }
- return result;
- }
- public static PipeImageSizeData FromBytes(byte[] bytes)
- {
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var width = ReadInt(stream);
- var height = ReadInt(stream);
- return new PipeImageSizeData(width, height);
- }
- }
- }
- public class PipeRemoteImageSizeData : PipeTransferData
- {
- public string UserId { get; }
- public int Width { get; set; }
- public int Height { get; set; }
- public PipeRemoteImageSizeData(string userId, int width, int height)
- {
- UserId = userId;
- Width = width;
- Height = height;
- }
- public byte[] ToBytes()
- {
- byte[] result = new byte[0];
- using (var stream = new MemoryStream())
- {
- WriteString(UserId, stream);
- WriteInt(Width, stream);
- WriteInt(Height, stream);
- result = stream.ToArray();
- }
- return result;
- }
- public static PipeRemoteImageSizeData FromBytes(byte[] bytes)
- {
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var userId = ReadString(stream);
- var width = ReadInt(stream);
- var height = ReadInt(stream);
- return new PipeRemoteImageSizeData(userId, width, height);
- }
- }
- }
- }
|