public void Send(byte flag, IMessage message) { ushort opcode = Game.OpcodeTypeComponent.GetOpcode(message.GetType()); byte[] bytes = ProtobufHelper.ToBytes(message); session.Send(flag, opcode, bytes); }
public static void Send(this Session session, IMessage message) { ushort opcode = Hotfix.Scene.GetComponent <OpcodeTypeComponent>().GetOpcode(message.GetType()); byte[] bytes = ProtobufHelper.ToBytes(message); session.Send(opcode, bytes); }
public static void Send(this Session session, IMessage message) { ushort opcode = GetOpcode(message.GetType()); byte[] bytes = ProtobufHelper.ToBytes(message); session.Send(opcode, bytes); }
/// <summary> /// 创建并且发送报文 /// </summary> /// <param name="messageID"></param> /// <param name="message"></param> /// <returns></returns> public static BufferEntity CreateAndSendPackage(int messageID, IMessage message) { JsonHelper.Log(messageID, message); //Debug.Log($"报文ID:{messageID}\n包体{JsonHelper.SerializeObject(message)}"); BufferEntity buffer = new BufferEntity(USocket.local.endPoint, USocket.local.sessionID, 0, 0, MessageType.Login.GetHashCode(), messageID, ProtobufHelper.ToBytes(message)); USocket.local.Send(buffer); return(buffer); }
public void SendMessage <T>(ushort opcode, T message, byte channelID = 0) { byte[] protoBytes = ProtobufHelper.ToBytes(message); var neworkBytes = new byte[sizeof(ushort) + protoBytes.Length]; var opcodeBytes = BitConverter.GetBytes(opcode); opcodeBytes.CopyTo(neworkBytes, 0); protoBytes.CopyTo(neworkBytes, sizeof(ushort)); this.eSocket.WriteAsync(neworkBytes, channelID); }
public void FromBytes(int iteration) { #pragma warning disable CS0618 var binaryBytes = BinaryHelper.ToBytes(_testModel); #pragma warning restore CS0618 var dataContractBytes = DataContractHelper.ToBytes <TestModel>(_testModel); var jilBytes = JilHelper.ToBytes <TestModel>(_testModel); var messagePackBytes = MessagePackHelper.ToBytes <TestModel>(_testModel); var msgPackBytes = MsgPackHelper.ToBytes <TestModel>(_testModel); var newtonsoftJsonBytes = NewtonsoftJsonHelper.ToBytes <TestModel>(_testModel); var protobufBytes = ProtobufHelper.ToBytes <TestModel>(_testModel); var sharpYamlBytes = SharpYamlHelper.ToBytes <TestModel>(_testModel); var systemTextJsonBytes = SystemTextJsonHelper.ToBytes <TestModel>(_testModel); var utf8JsonBytes = Utf8JsonHelper.ToBytes <TestModel>(_testModel); var xmlBytes = XmlHelper.ToBytes <TestModel>(_testModel); var yamlDotNetBytes = YamlDotNetHelper.ToBytes(_testModel); var zeroFormatterBytes = ZeroFormatterHelper.ToBytes <TestModel>(_testModel); Console.WriteLine("FromBytes go!"); Runner.Initialize(); Console.WriteLine(Runner.Time("BinaryHelper FromBytes", iteration, #pragma warning disable CS0618 () => BinaryHelper.FromBytes <TestModel>(binaryBytes))); #pragma warning restore CS0618 Console.WriteLine(Runner.Time("DataContractHelper FromBytes", iteration, () => DataContractHelper.FromBytes <TestModel>(dataContractBytes))); Console.WriteLine(Runner.Time("JilHelper FromBytes", iteration, () => JilHelper.FromBytes <TestModel>(jilBytes))); Console.WriteLine(Runner.Time("MessagePackHelper FromBytes", iteration, () => MessagePackHelper.FromBytes <TestModel>(messagePackBytes))); Console.WriteLine(Runner.Time("MsgPackHelper FromBytes", iteration, () => MsgPackHelper.FromBytes <TestModel>(msgPackBytes))); Console.WriteLine(Runner.Time("NewtonsoftJsonHelper FromBytes", iteration, () => NewtonsoftJsonHelper.FromBytes <TestModel>(newtonsoftJsonBytes))); Console.WriteLine(Runner.Time("ProtobufHelper FromBytes", iteration, () => ProtobufHelper.FromBytes <TestModel>(protobufBytes))); Console.WriteLine(Runner.Time("SharpYamlHelper FromBytes", iteration, () => SharpYamlHelper.FromBytes <TestModel>(sharpYamlBytes))); Console.WriteLine(Runner.Time("SystemTextJsonHelper FromBytes", iteration, () => SystemTextJsonHelper.FromBytes <TestModel>(systemTextJsonBytes))); Console.WriteLine(Runner.Time("Utf8JsonHelper FromBytes", iteration, () => Utf8JsonHelper.FromBytes <TestModel>(utf8JsonBytes))); Console.WriteLine(Runner.Time("XmlHelper FromBytes", iteration, () => XmlHelper.FromBytes <TestModel>(xmlBytes))); Console.WriteLine(Runner.Time("YamlDotNetHelper FromBytes", iteration, () => YamlDotNetHelper.FromBytes <TestModel>(yamlDotNetBytes))); Console.WriteLine(Runner.Time("ZeroFormatterHelper FromBytes", iteration, () => ZeroFormatterHelper.FromBytes <TestModel>(zeroFormatterBytes))); Console.WriteLine("\r\nFromBytes complete!\r\n"); }
public static async Task <IResponse> Call(this Session session, IRequest request) { byte[] bytes = ProtobufHelper.ToBytes(request); ushort opcode = GetOpcode(request.GetType()); PacketInfo packetInfo = await session.Call(opcode, bytes); ushort responseOpcode = packetInfo.Opcode; Type t = GetMessageType(responseOpcode); object aa = ProtobufHelper.FromBytes(t, packetInfo.Bytes, packetInfo.Index, packetInfo.Length); IResponse response = (IResponse)aa; return(response); }
/// <summary> /// 创建并且发送报文 /// </summary> /// <param name="uClient"></param> /// <param name="messageID"></param> /// <param name="message"></param> /// <returns></returns> public static BufferEntity CreqateAndSendPackage(UClient uClient, int messageID, IMessage message) { //如果处于连接状态 if (uClient.isConnect) { //打印protobuf 按json格式 Debug.Log(messageID, message); BufferEntity bufferEntity = new BufferEntity(uClient.endPoint, uClient.session, 0, 0, MessageType.Login.GetHashCode(), messageID, ProtobufHelper.ToBytes(message)); uClient.Send(bufferEntity); return(bufferEntity); } return(null); }
public void Save(string file) { AllTreeData allTreeData = new AllTreeData(); foreach (var value in this.treeViewModelsDict.Values) { List <TreeNodeData> list = value.GetDatas(); allTreeData.TreeNodeDatas.AddRange(list); } using (Stream stream = new FileStream(file, FileMode.Create, FileAccess.Write)) { byte[] bytes = ProtobufHelper.ToBytes(allTreeData); stream.Write(bytes, 0, bytes.Length); } }
public async void SendMessage <T>(ushort opcode, T message, byte channelID = 0) { byte[] protoBytes = ProtobufHelper.ToBytes(message); var neworkBytes = new byte[sizeof(int) + sizeof(ushort) + protoBytes.Length]; int totalSize = sizeof(ushort) + protoBytes.Length; var totalSizeBytes = BitConverter.GetBytes(totalSize); totalSizeBytes.CopyTo(neworkBytes, 0); var opcodeBytes = BitConverter.GetBytes(opcode); opcodeBytes.CopyTo(neworkBytes, sizeof(int)); protoBytes.CopyTo(neworkBytes, sizeof(int) + sizeof(ushort)); await this.networkStream.WriteAsync(neworkBytes, 0, neworkBytes.Length); }
public void ToBytes(int iteration) { Console.WriteLine("ToBytes go!"); Runner.Initialize(); Console.WriteLine(Runner.Time("BinaryHelper ToBytes", iteration, #pragma warning disable CS0618 () => BinaryHelper.ToBytes(_testModel))); #pragma warning restore CS0618 Console.WriteLine(Runner.Time("DataContractHelper ToBytes", iteration, () => DataContractHelper.ToBytes(_testModel))); Console.WriteLine(Runner.Time("JilHelper ToBytes", iteration, () => JilHelper.ToBytes(_testModel))); Console.WriteLine(Runner.Time("MessagePackHelper ToBytes", iteration, () => MessagePackHelper.ToBytes(_testModel))); Console.WriteLine(Runner.Time("MsgPackHelper ToBytes", iteration, () => MsgPackHelper.ToBytes(_testModel))); Console.WriteLine(Runner.Time("NewtonsoftJsonHelper ToBytes", iteration, () => NewtonsoftJsonHelper.ToBytes(_testModel))); Console.WriteLine(Runner.Time("ProtobufHelper ToBytes", iteration, () => ProtobufHelper.ToBytes(_testModel))); Console.WriteLine(Runner.Time("SharpYamlHelper ToBytes", iteration, () => SharpYamlHelper.ToBytes(_testModel))); Console.WriteLine(Runner.Time("SystemTextJsonHelper ToBytes", iteration, () => SystemTextJsonHelper.ToBytes(_testModel))); Console.WriteLine(Runner.Time("Utf8JsonHelper ToBytes", iteration, () => Utf8JsonHelper.ToBytes(_testModel))); Console.WriteLine(Runner.Time("XmlHelper ToBytes", iteration, () => XmlHelper.ToBytes(_testModel))); Console.WriteLine(Runner.Time("YamlDotNetHelper ToBytes", iteration, () => YamlDotNetHelper.ToBytes(_testModel))); Console.WriteLine(Runner.Time("ZeroFormatterHelper ToBytes", iteration, () => ZeroFormatterHelper.ToBytes(_testModel))); Console.WriteLine("\r\nToBytes complete!\r\n"); }
public byte[] SerializeToByteArray(object obj) { return(ProtobufHelper.ToBytes(obj)); }
public object Clone() { byte[] bytes = ProtobufHelper.ToBytes(this); return(ProtobufHelper.FromBytes(this.GetType(), bytes, 0, bytes.Length)); }
public static byte[] ToBytes(this object?value) => ProtobufHelper.ToBytes(value);
public static byte[] ToBytes <TValue>(this TValue?value) => ProtobufHelper.ToBytes(value);