Exemple #1
0
    /// <summary>
    /// 在刚好接收完一个完整的协议的时候调用,构造一个完整的协议包
    /// </summary>
    public void CreatePacketFromBuffer()
    {
        try
        {
            int bodyLen = BodyLength;

            int    protoCode = ProtoCode;
            object decoder   = ProtoMapper.GetProtoDecoder(protoCode);
            if (decoder != null)
            {
                byte[] dataBytes = new byte[bodyLen - ProtoCodeLength]; //协议体等于总的协议体长度减去协议号字节
                Array.Copy(protobuffBytes, Header_Length + ProtoCodeLength, dataBytes, 0, dataBytes.Length);
                Debug.Log("Content :" + SocketController.ByteArrayToStr(dataBytes));
                System.IO.MemoryStream stream_output = new System.IO.MemoryStream(dataBytes);

                RuntimeTypeModel.Default.Deserialize(stream_output, decoder, decoder.GetType());

                ProtoPacketQueueManager.AddPacket(decoder);
                //用队列在帧更新的时候处理协议,是因为有可能是在多线程下通信,而有些操作是只能在主线程执行
            }
        }
        catch (System.Exception ex)
        {
            Debug.LogError("协议包创建出错:" + ex.Message + ex.StackTrace);
        }
    }
Exemple #2
0
    /// <summary>
    /// 在刚好接收完一个完整的协议的时候调用,构造一个完整的协议包
    /// </summary>
    public void CreatePacketFromBuffer()
    {
        byte[] packetBytes = new byte[BodyLength];
        //从缓存里获取属于这个协议的内容
        Array.Copy(protobuffBytes, Header_Length, packetBytes, 0, packetBytes.Length);
        byte[] protoCodeBytes = new byte[4];
        Array.Copy(packetBytes, 0, protoCodeBytes, 0, protoCodeBytes.Length); //协议的前面四位为协议号
        int           protoCode = BitConverter.ToInt32(protoCodeBytes, 0);
        ProtoBase_S2C decoder   = ProtoMapper.GetProtoDecoder(protoCode);

        if (decoder != null)
        {
            decoder.Decode(packetBytes);
            ProtoPacketQueueManager.instance.AddPacket(decoder);
        }
    }