public static byte[] EncodeMassage(int id, byte[] data) { byte[] newBuffer = null; //id + data using (AppMemoryStream ms = new AppMemoryStream()) { ms.WriteUShort((ushort)id); ms.Write(data, 0, data.Length); newBuffer = ms.ToArray(); } byte[] newData = null; //newBuffer.length + id + data using (AppMemoryStream ms = new AppMemoryStream()) { ms.WriteUShort((ushort)(newBuffer.Length)); ms.Write(newBuffer, 0, newBuffer.Length); newData = ms.ToArray(); } return(newData); }
public void SendMessage(IMessage obj) { if (!MessageDefine.ContainProtoType(obj.GetType())) { AppDebug.Log("协议不存在:" + obj.GetType().ToString()); return; } Message msg = new Message(); //set header msg.Header = new Header(); //set body { byte[] messageBody = obj.ToByteArray(); byte[] body = new byte[2 + messageBody.Length]; using (AppMemoryStream ms = new AppMemoryStream()) { int id = MessageDefine.GetProtoIdByProtoType(obj.GetType()); ms.WriteUShort((ushort)(id)); ms.Write(messageBody, 0, messageBody.Length); body = ms.ToArray(); } msg.Body = ByteString.CopyFrom(body); } //发送消息 int protoId = MessageDefine.GetProtoIdByProtoType(msg.GetType()); SocketClient.Instance.SendMessage(protoId, msg.ToByteArray()); }
private void SendToService(Header header, IMessage obj) { Player p = UserManager.Instance.GetPlayer(); if (p.UserId == 0) { AppDebug.Log("无法获取玩家信息,请重新登陆!"); return; } header.UserId = p.UserId; header.Token = p.Token; header.TokenExpiredTime = p.TokenExpireTime; Message msg = new Message(); msg.Header = header; {//set body byte[] bodyData = obj.ToByteArray(); byte[] body = new byte[2 + bodyData.Length]; using (AppMemoryStream ms = new AppMemoryStream()) { int id = MessageDefine.GetProtoIdByProtoType(obj.GetType()); ms.WriteUShort((ushort)(id)); ms.Write(bodyData, 0, bodyData.Length); body = ms.ToArray(); } msg.Body = ByteString.CopyFrom(body); } //发送消息 int protoId = MessageDefine.GetProtoIdByProtoType(msg.GetType()); SocketClient.Instance.SendMessage(protoId, msg.ToByteArray()); }