Example #1
0
 void AddPack(NetPack p)
 {
     lock (_recv_list)
     {
         _recv_list.Add(p);
     }
 }
Example #2
0
        // 解压 json,  并填充到 p 中
        void DecodeJson(byte[] buff, int buff_size, int offset, NetPack p)
        {
            // 解析字符串
            try
            {
                // 开始计时
                _sw.ResetAndStart();

                // 读入字符串
                var size = ByteUtils.Read7BitEncodedInt(buff, ref offset);
                var flag = buff[offset];
                string str = null;

                // 压缩
                if (flag == 1)
                {
                    Lzf.LZF lzf = new Lzf.LZF();

                    // src data
                    var src_start = offset + 1;     // 跳过标志
                    var src_len = buff_size - src_start;

                    // dst data
                    _decode_buff.Grow(src_len * 20);
                    int dst_Len = lzf.Decompress(buff, src_start, src_len, _decode_buff.Array, _decode_buff.MaxLength);

                    // save
                    if (dst_Len > 0)
                    {
                        str = Encoding.UTF8.GetString(_decode_buff.Array, 0, dst_Len);
                    }
                }
                // 非压缩
                else
                {
                    if (buff.Length < offset + size)
                    {
                        Log.LogError("OnPack error, opcode={0}, buff.Length:{1}, offset:{2}, size:{3}, datas:\n{4}", p.lua_code, buff.Length, offset, size, ByteUtils.FormatBytes(buff));
                        Error("data error!");
                    }
                    else
                    {
                        str = Encoding.UTF8.GetString(buff, offset, size);
                    }
                }

                // json 解压, 放在网络线程解压, 以节省主线程开销
                if (!string.IsNullOrEmpty(str))
                {
                    p.str = str;
                    p.ht = MiniJSON.JsonDecode(str) as Hashtable;
                }

                //
                p.time_decode = (int)_sw.StopAndGetTimeMs();
            }
            catch (Exception e)
            {
                if (!MyExpception.HandleException(e)) throw;
            }
            finally
            {
                _sw.Stop();
            }

            //
            //if (opcode == (int)OpCodes_S2C.M2C_COMMON_GM)
            //{
            //    Log.LogInfo("msgGM:{0}", str);
            //}
            //if (opcode == (int)OpCodes_S2C.M2C_COMMON_ERROR)
            //{
            //    Log.LogError("msgERROR:{0}", str);
            //}
            //else if (opcode == (int)OpCodes_S2C.M2C_COMMON_OK)
            //{
            //    Log.LogInfo("msgOK:{0}", str);
            //}
        }
Example #3
0
        //
        NetPack AddPack(uint lua_code, int packet_id, int enc_type)
        {
            var p = new NetPack();
            p.socket_obj_rcv = this;
            p.time_create = AppTime.unscaledTime;
            p.cpid = GameSocket.GetClientPackId();

            p.sock_name = _name;
            p.lua_code = lua_code;
            p.packet_id = packet_id;
            p.enc_type = enc_type;

            return p;
        }