示例#1
0
    internal void SendMessage(int id, Google.Protobuf.IMessage message)
    {
        //将发送过来的消息封装为buffer
        MemoryStream ms = new MemoryStream();

        message.WriteTo(ms);
        byte[] messageBytes = ms.ToArray();

        ByteBuffer bb = new ByteBuffer();

        //主体消息的长度
        bb.WriteInt32(messageBytes.Length);
        bb.WriteInt32(id);
        bb.WriteBytes(messageBytes);
        Byte[] sendData = bb.ToArray();

        //数据的发送
        if (client.Connected)
        {
            if (LogIO)
            {
                string strBuff = "";
                foreach (var c in sendData)
                {
                    strBuff += c.ToString("X2") + " ";
                }
                Debug.Log("Send data:" + strBuff);
            }
            this.client.GetStream().BeginWrite(sendData, 0, sendData.Length, new AsyncCallback(OnWrite), sendData);
        }
        else
        {
            Debug.Log("socket client not been connected");
        }
    }
示例#2
0
文件: Client.cs 项目: mengtest/drog
    /// <summary>
    /// 数据流反序列化成Protobuf类
    /// </summary>
    /// <typeparam name="T">Protobuf类名</typeparam>
    /// <param name="data">byte 数据</param>
    /// <param name="offset">偏移量,从byte数组的哪个位置开始读取</param>
    /// <param name="len">数据长度</param>
    public static T Deserialize <T>(byte[] data, int offset, int len) where T : class, IMessage, new()
    {
        T obj = new T();

        Google.Protobuf.IMessage message = obj.Descriptor.Parser.ParseFrom(data, offset, len);
        return(message as T);
    }
示例#3
0
    /// <summary>
    /// parse input data
    /// </summary>
    /// <param name="gameAction"></param>
    /// <param name="actionParam"></param>
    /// <param name="formater"></param>
    /// <param name="bShowLoading"></param>
    private void SocketRequest(GameAction gameAction, Google.Protobuf.IMessage pbData, IHeadFormater formater, bool bShowLoading)
    {
        if (mSocket == null)
        {
            string   url = NetWriter.GetUrl();
            string[] arr = url.Split(new char[] { ':' });
            if (arr.Length != 2)
            {
                Debug.LogError("Url is error:" + url);
                return;
            }
            int nPort = int.Parse(arr[1]);
            mSocket = new SocketConnect(arr[0], nPort, formater);
        }
        gameAction.Head.MsgId = NetWriter.MsgId - 1;

        SocketPackage package = new SocketPackage();

        package.MsgId        = gameAction.Head.MsgId;
        package.ActionId     = gameAction.ActionId;
        package.ActionRespId = gameAction.Head.ActionRespId;
        package.Action       = gameAction;
        package.HasLoading   = bShowLoading;
        package.SendTime     = DateTime.Now;
        byte[] data = gameAction.Send(pbData);
        NetWriter.resetData();
        if (bShowLoading)
        {
            RequestDelegate(Status.eStartRequest);
        }
        mSocket.Send(data, package);
    }
示例#4
0
 /// <summary>
 /// 将一个数据流转化为一个对象
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="mes"></param>
 /// <param name="data"></param>
 public static void ToMessage(Google.Protobuf.IMessage mes, byte[] mesData)
 {
     Google.Protobuf.MessageExtensions.MergeFrom(mes, mesData);
     //if (GameManager.showNetLog == true)
     //{
     //UnityEngine.Debug.Log($"<color=green>{JsonHelper.ToJson(mes)}</color>");
     //}
 }
示例#5
0
 public void Set(MsgID msgID, uint cmd, Google.Protobuf.IMessage T)
 {
     if (!mNetDict.ContainsKey(msgID))
     {
         mNetDict[msgID] = new Dictionary <uint, Google.Protobuf.IMessage>();
     }
     mNetDict[msgID][cmd] = T;
 }
示例#6
0
 private byte[] Serialize(Google.Protobuf.IMessage msg)
 {
     using (MemoryStream sndms = new MemoryStream())
     {
         msg.WriteTo(sndms);
         return(sndms.ToArray());
     }
 }
示例#7
0
    public byte[] Send(Google.Protobuf.IMessage pbData)
    {
        NetWriter writer = NetWriter.Instance;

        SetActionHead(writer, pbData);

        writer.SetBodyData(PackCodec.Serialize(pbData));
        return(writer.PostData());
    }
示例#8
0
 public Google.Protobuf.IMessage Query(MsgID msgID, uint cmd)
 {
     Google.Protobuf.IMessage T = null;
     if (!mNetDict.ContainsKey(msgID))
     {
         mNetDict[msgID] = new Dictionary <uint, Google.Protobuf.IMessage>();
     }
     mNetDict[msgID].TryGetValue(cmd, out T);
     return(T);
 }
示例#9
0
 /// <summary>
 /// Convert class ByteString
 /// </summary>
 /// <param name="msg"></param>
 /// <returns></returns>
 public static Google.Protobuf.ByteString Marshal(Google.Protobuf.IMessage msg)
 {
     using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
     {
         var s = new Google.Protobuf.CodedOutputStream(ms);
         msg.WriteTo(s);
         s.Flush();
         var str = Google.Protobuf.ByteString.CopyFrom(ms.ToArray());
         return(str);
     }
 }
示例#10
0
        /// <summary>
        /// Convert class ByteString
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static byte[] MarshalByte(Google.Protobuf.IMessage msg)
        {
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                var s = new Google.Protobuf.CodedOutputStream(ms);
                msg.WriteTo(s);
                s.Flush();

                return(ms.ToArray());
            }
        }
示例#11
0
        static void WriteMessage(object value, CodedOutputStream stream)
        {
            var savedCachedBuffer = cachedBuffer;
            var savedCachedStream = cachedStream;

            cachedBuffer = new MemoryStream();
            cachedStream = new CodedOutputStream(cachedBuffer);
            Google.Protobuf.IMessage message = ((Service.Messages.IMessage)value).ToProtobufMessage();
            cachedBuffer = savedCachedBuffer;
            cachedStream = savedCachedStream;
            message.WriteTo(stream);
        }
示例#12
0
 //public bool Add(MsgID msgID, uint cmd, Google.Protobuf.IMessage T)
 //{
 //    //Google.Protobuf.IMessage 之间的相加
 //    return true;
 //}
 public Google.Protobuf.IMessage Del(MsgID msgID, uint cmd)
 {
     Google.Protobuf.IMessage T = null;
     if (mNetDict.ContainsKey(msgID))
     {
         if (mNetDict[msgID].ContainsKey(cmd))
         {
             T = mNetDict[msgID][cmd];
             mNetDict[msgID].Remove(cmd);
         }
     }
     return(T);
 }
示例#13
0
        //记录操作信息
        private static void RecordInfo(this FiveStarRoom fiveStarRoom, int opcode, Google.Protobuf.IMessage iMessage)
        {
            //只有房卡才记录
            if (fiveStarRoom.RoomType != RoomType.RoomCard)
            {
                return;
            }
            MiltaryRecordData miltaryRecordData = ComponentFactory.Create <MiltaryRecordData>();

            miltaryRecordData.Opcode = opcode;
            miltaryRecordData.Data   = new ByteString(ProtobufHelper.ToBytes(iMessage));;
            fiveStarRoom.CurrParticularMiltaryRecordData.MiltaryRecordDatas.Add(miltaryRecordData);
        }
示例#14
0
    protected virtual void SetActionHead(NetWriter writer, Google.Protobuf.IMessage pbData)
    {
        //writer.writeInt32("actionId", ActionId);
        byte[]     bodyBuffer = PackCodec.Serialize(pbData);
        ByteBuffer headBuffer = new ByteBuffer();

        headBuffer.WriteInt(this.ActionId);
        headBuffer.WriteInt(bodyBuffer.Length);
        headBuffer.WriteInt(Head.MsgId);
        headBuffer.WriteInt(0); //body_check;
        headBuffer.WriteInt(0); //head_check;

        writer.SetHeadBuffer(headBuffer.ToBytes());
    }
示例#15
0
    public static byte[] Encode(Google.Protobuf.IMessage msg, bool b)
    {
        using (MemoryStream rawOutput = new MemoryStream())
        {
            CodedOutputStream output = new CodedOutputStream(rawOutput);
            if (b)
            {
                output.WriteRawVarint32((uint)msg.CalculateSize() - 1);
            }
            output.WriteMessage(msg);
            output.Flush();
            byte[] result = rawOutput.ToArray();

            return(result);
        }
    }
示例#16
0
    static public byte[] Serialize(Google.Protobuf.IMessage msg)
    {
        byte[] result = null;

        if (msg != null)
        {
            using (var stream = new MemoryStream())
            {
                msg.WriteTo(stream);
                //Serializer.Serialize<T>(stream, msg);
                result = stream.ToArray();
            }
        }

        return(result);
    }
示例#17
0
    public void Send(int actionId, Action <ActionResult> callback, Google.Protobuf.IMessage pbData, bool bShowLoading = true)
    {
        GameAction gameAction = ActionFactory.Create(actionId);

        if (gameAction == null)
        {
            throw new ArgumentException(string.Format("Not found {0} of GameAction object.", actionId));
        }
        //if (SceneChangeManager.mInstance != null)
        //    SceneChangeManager.mInstance.ShowLoading(true);

        gameAction.Callback += callback;
        if (NetWriter.IsSocket())
        {
            SocketRequest(gameAction, pbData, HeadFormater, bShowLoading);
        }
        else
        {
            //HttpRequest(gameAction, pbData, HeadFormater, bShowLoading);
            Debug.LogError("must be socket!!!");
        }
    }
示例#18
0
 public static string GetProtoMessageTypeName(Google.Protobuf.IMessage message)
 {
     return("proto:" + message.Descriptor.FullName);
 }
示例#19
0
 public static byte[] Encode(System.Object protoObj)
 {
     Google.Protobuf.IMessage message = (Google.Protobuf.IMessage)protoObj;
     return(message.ToByteArray());
 }
示例#20
0
        public static void ValidContractProto(Transaction.Types.Contract contract)
        {
            System.Type type = null;
            Any         contract_parameter = contract.Parameter;

            Google.Protobuf.IMessage src = null;
            Google.Protobuf.IMessage contract_message = null;

            switch (contract.Type)
            {
            case ContractType.AccountCreateContract:
                src = contract_parameter.Unpack <AccountCreateContract>();
                contract_message = Parse <AccountCreateContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.TransferContract:
                src = contract_parameter.Unpack <TransferContract>();
                contract_message = Parse <TransferContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.TransferAssetContract:
                src = contract_parameter.Unpack <TransferAssetContract>();
                contract_message = Parse <TransferAssetContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.VoteAssetContract:
                src = contract_parameter.Unpack <VoteAssetContract>();
                contract_message = Parse <VoteAssetContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.VoteWitnessContract:
                src = contract_parameter.Unpack <VoteWitnessContract>();
                contract_message = Parse <VoteWitnessContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.WitnessCreateContract:
                src = contract_parameter.Unpack <WitnessCreateContract>();
                contract_message = Parse <WitnessCreateContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.AssetIssueContract:
                src = contract_parameter.Unpack <AssetIssueContract>();
                contract_message = Parse <AssetIssueContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.WitnessUpdateContract:
                src = contract_parameter.Unpack <WitnessUpdateContract>();
                contract_message = Parse <WitnessUpdateContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.ParticipateAssetIssueContract:
                src = contract_parameter.Unpack <ParticipateAssetIssueContract>();
                contract_message = Parse <ParticipateAssetIssueContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.AccountUpdateContract:
                src = contract_parameter.Unpack <AccountUpdateContract>();
                contract_message = Parse <AccountUpdateContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.FreezeBalanceContract:
                src = contract_parameter.Unpack <FreezeBalanceContract>();
                contract_message = Parse <FreezeBalanceContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.UnfreezeBalanceContract:
                src = contract_parameter.Unpack <UnfreezeBalanceContract>();
                contract_message = Parse <UnfreezeBalanceContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.UnfreezeAssetContract:
                src = contract_parameter.Unpack <UnfreezeAssetContract>();
                contract_message = Parse <UnfreezeAssetContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.WithdrawBalanceContract:
                src = contract_parameter.Unpack <WithdrawBalanceContract>();
                contract_message = Parse <WithdrawBalanceContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.CreateSmartContract:
                src = contract_parameter.Unpack <CreateSmartContract>();
                contract_message = Parse <CreateSmartContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.TriggerSmartContract:
                src = contract_parameter.Unpack <TriggerSmartContract>();
                contract_message = Parse <TriggerSmartContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.UpdateAssetContract:
                src = contract_parameter.Unpack <UpdateAssetContract>();
                contract_message = Parse <UpdateAssetContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.ProposalCreateContract:
                src = contract_parameter.Unpack <ProposalCreateContract>();
                contract_message = Parse <ProposalCreateContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.ProposalApproveContract:
                src = contract_parameter.Unpack <ProposalApproveContract>();
                contract_message = Parse <ProposalApproveContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.ProposalDeleteContract:
                src = contract_parameter.Unpack <ProposalDeleteContract>();
                contract_message = Parse <ProposalDeleteContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.SetAccountIdContract:
                src = contract_parameter.Unpack <SetAccountIdContract>();
                contract_message = Parse <SetAccountIdContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.UpdateSettingContract:
                src = contract_parameter.Unpack <UpdateSettingContract>();
                contract_message = Parse <UpdateSettingContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.UpdateEnergyLimitContract:
                src = contract_parameter.Unpack <UpdateEnergyLimitContract>();
                contract_message = Parse <UpdateEnergyLimitContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.ClearAbicontract:
                src = contract_parameter.Unpack <ClearABIContract>();
                contract_message = Parse <ClearABIContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.ExchangeCreateContract:
                src = contract_parameter.Unpack <ExchangeCreateContract>();
                contract_message = Parse <ExchangeCreateContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.ExchangeInjectContract:
                src = contract_parameter.Unpack <ExchangeInjectContract>();
                contract_message = Parse <ExchangeInjectContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.ExchangeWithdrawContract:
                src = contract_parameter.Unpack <ExchangeWithdrawContract>();
                contract_message = Parse <ExchangeWithdrawContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.ExchangeTransactionContract:
                src = contract_parameter.Unpack <ExchangeTransactionContract>();
                contract_message = Parse <ExchangeTransactionContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            case ContractType.AccountPermissionUpdateContract:
                src = contract_parameter.Unpack <AccountPermissionUpdateContract>();
                contract_message = Parse <AccountPermissionUpdateContract>(Message.GetCodedInputStream(src.ToByteArray()));
                break;

            default:
                break;
            }

            if (type == null)
            {
                throw new P2pException(P2pException.ErrorType.PROTOBUF_ERROR, P2pException.GetDescription(P2pException.ErrorType.PROTOBUF_ERROR));
            }

            Message.CompareBytes(src.ToByteArray(), contract_message.ToByteArray());
        }
示例#21
0
 public static byte[] SerializePackage(IMessage data)
 {
     Google.Protobuf.IMessage data1 = data as Google.Protobuf.IMessage;
     byte[] stream = data1.ToByteArray();
     return(stream);
 }
示例#22
0
 // support protobuf message
 public Packet AddProtoBuf(Google.Protobuf.IMessage msg)
 {
     SetMessageType(MESSAGE_TYPE.PROTO_BUF);
     return(AddByteArray(msg.ToByteArray()));
 }
示例#23
0
 public static string GetProtoMessageDescription(Google.Protobuf.IMessage message)
 {
     return("");
 }