public static ByteBuf NewPacket(pb.ID id, Google.ProtocolBuffers.IMessage msg = null) { var bufLen = BitConverter.GetBytes((ushort)0); var t = new List <byte>(); byte[] buffData = null; if (null != msg) { using (MemoryStream stream = new MemoryStream()) { //Save the person to a stream msg.WriteTo(stream); buffData = stream.ToArray(); bufLen = BitConverter.GetBytes((ushort)buffData.Length); } } if (BitConverter.IsLittleEndian) { Array.Reverse(bufLen); } t.AddRange(bufLen); t.Add((byte)id); if (null != buffData) { t.AddRange(buffData); } return(new ByteBuf(t.ToArray())); }
public void Send(pb.ID id, Google.ProtocolBuffers.IMessage obj = null) { if (null != client && client.IsRunning() && Connected) { client.Send(PacketWraper.NewPacket(id, obj)); } else { UnityEngine.Debug.LogError("client no connect"); } }