Пример #1
0
    private void OnReceivedGameServerMessage(KProtoBuf proto)
    {
        KG2C_GSToPlayer protoData = proto as KG2C_GSToPlayer;
        BinaryReader    reader    = new BinaryReader(new MemoryStream(protoData.data));

        while (reader.BaseStream.Position < protoData.data.Length)
        {
            int  length        = reader.ReadUInt16();
            long beginPosition = reader.BaseStream.Position;
            long endPosition   = beginPosition + length;
            int  protocolID    = reader.ReadUInt16();

            reader.BaseStream.Seek(beginPosition, SeekOrigin.Begin);
            ProcessInfo processInfo = m_GameServerProtocolMapping.GetProcessInfo(protocolID);
            if (processInfo == null)
            {
                Leyoutech.Utility.DebugUtility.LogError(KConstants.LOG_TAG
                                                        , string.Format("GameServer消息没有被处理: protocolID:{0}({1})"
                                                                        , protocolID
                                                                        , (KS2C_Protocol)protocolID));
                return;
            }

            try
            {
                processInfo.protocolData.UnPack(reader);
                LogDetail("Receive from GameServer", processInfo.protocolData, processInfo.protocolID, ((KS2C_Protocol)processInfo.protocolID).ToString(), length);
                processInfo.HandleProtocol();
            }
            catch (Exception e)
            {
                reader.BaseStream.Seek(beginPosition, SeekOrigin.Begin);
                StringBuilder builder = Leyoutech.Utility.StringUtility.AllocStringBuilderCache();
                for (int iByte = 0; iByte < length; iByte++)
                {
                    byte iterByte = reader.ReadByte();
                    builder.AppendFormat("{0:X2}", iterByte);
                }
                Leyoutech.Utility.DebugUtility.LogError(KConstants.LOG_TAG
                                                        , string.Format("处理协议:{0}({1})异常 Buff:({2}) Exception:\n{3}"
                                                                        , protocolID
                                                                        , (KS2C_Protocol)protocolID
                                                                        , Leyoutech.Utility.StringUtility.ReleaseStringBuilderCacheAndReturnString()
                                                                        , e.ToString()));
            }
            finally
            {
                reader.BaseStream.Seek(endPosition, SeekOrigin.Begin);
            }
        }
    }
Пример #2
0
    public void OnResiveMessage(OldPl message)
    {
        MemoryStream memoryStream  = new MemoryStream(message.Body.ToByteArray());
        BinaryReader binaryReader  = new BinaryReader(memoryStream);
        int          packageLength = binaryReader.ReadUInt16();
        int          protocolID    = binaryReader.ReadUInt16();
        long         beginPosition = memoryStream.Seek(-PROTOCOLID_LENGTH, SeekOrigin.Current);

        if (message.Body.Length < packageLength)
        {
            Leyoutech.Utility.DebugUtility.LogError(KConstants.LOG_TAG
                                                    , string.Format("消息长度错误: protocolID:{0}({1})"
                                                                    , protocolID
                                                                    , (KS2C_Protocol)protocolID));
            return;
        }

        ProcessInfo processInfo = m_GatewayProtocolMapping.GetProcessInfo(protocolID);

        if (processInfo == null)
        {
            Leyoutech.Utility.DebugUtility.LogError(KConstants.LOG_TAG
                                                    , string.Format("Gateway消息没有被处理: protocolID:{0}({1})"
                                                                    , protocolID
                                                                    , (KS2C_Protocol)protocolID));
            return;
        }

        processInfo.protocolData.UnPack(binaryReader);

        try
        {
            processInfo.HandleProtocol();
        }
        catch (Exception e)
        {
            Leyoutech.Utility.DebugUtility.LogError(KConstants.LOG_TAG
                                                    , string.Format("处理协议:{0}({1})异常 Exception:\n{2}"
                                                                    , protocolID
                                                                    , (KS2C_Protocol)protocolID
                                                                    , e.ToString()));
        }
    }