Example #1
0
        /**
         * 对传入的数据进行解码 填充可get的对象
         */

        public new void Decode(byte[] buffer, int Index = 0)
        {
            if (buffer.Length < UniPacketHeadSize)
            {
                throw new ArgumentException("Decode namespace must include size head");
            }

            try
            {
                TarsInputStream _is = new TarsInputStream(buffer, UniPacketHeadSize + Index);
                _is.setServerEncoding(EncodeName);
                //解码出RequestPacket包
                this.ReadFrom(_is);

                //设置tup版本
                _iVer = _package.iVersion;

                _is = new TarsInputStream(_package.sBuffer);
                _is.setServerEncoding(EncodeName);

                if (_package.iVersion == Const.PACKET_TYPE_TUP)
                {
                    _data = (Dictionary <string, Dictionary <string, byte[]> >)_is.readMap <Dictionary <string, Dictionary <string, byte[]> > >(0, false);
                }
                else
                {
                    _new_data = (Dictionary <string, byte[]>)_is.readMap <Dictionary <string, byte[]> >(0, false);
                }
            }
            catch (Exception e)
            {
                QTrace.Trace(this + " Decode Exception: " + e.Message);
                throw (e);
            }
        }
        public override void ReadFrom(TarsInputStream _is)
        {
            try
            {
                iVersion     = (short)_is.Read(iVersion, 1, true);
                cPacketType  = (byte)_is.Read(cPacketType, 2, true);
                iMessageType = (int)_is.Read(iMessageType, 3, true);
                iRequestId   = (int)_is.Read(iRequestId, 4, true);
                sServantName = (string)_is.ReadString(5, true);
                sFuncName    = (string)_is.ReadString(6, true);

                if (cache_sBuffer == null)
                {
                    cache_sBuffer = new byte[] { 0 };
                }
                sBuffer  = (byte[])_is.Read <byte[]>(cache_sBuffer, 7, true);
                iTimeout = (int)_is.Read(iTimeout, 8, true);

                Dictionary <string, string> cache_context = null;
                context = (Dictionary <string, string>)_is.Read <Dictionary <string, string> >(cache_context, 9, true);
                status  = (Dictionary <string, string>)_is.Read <Dictionary <string, string> >(cache_context, 10, true);
            }
            catch (Exception ex)
            {
                QTrace.Trace(this + " ReadFrom Exception: " + ex.Message);
                throw ex;
            }
        }
        /**
         * 获取调用tars的返回值
         */
        public int GetResultCode()
        {
            int result = 0;

            try
            {
                string rcode = _package.status[(Const.STATUS_RESULT_CODE)];
                result = (rcode != null ? int.Parse(rcode) : 0);
            }
            catch (Exception ex)
            {
                QTrace.Trace(ex.Message);
                return(0);
            }
            return(result);
        }
Example #4
0
        static public void Trace(IList list)
        {
            if (list == null)
            {
                return;
            }

            string strTrace = " List: ";

            foreach (object item in list)
            {
                strTrace += item.ToString();
                strTrace += "\r\n";
            }

            QTrace.Trace(strTrace);
        }
Example #5
0
        static public string Trace(byte[] value)
        {
            string strValue = "\r\n";
            int    i        = 0;

            foreach (byte bt in value)
            {
                strValue += string.Format(" {0,02:x}", bt);
                i++;
                if ((i % 16) == 0)
                {
                    strValue += "\n";
                }
            }

            QTrace.Trace(strValue);
            return(strValue);
        }
Example #6
0
        static public void Trace(IDictionary dict)
        {
            if (dict == null)
            {
                return;
            }

            string strTrace = " Dictionary: ";

            foreach (object key in dict.Keys)
            {
                strTrace += key.ToString();
                strTrace += "\t\t";
                strTrace += dict[key].ToString();
                strTrace += "\r\n";
            }

            QTrace.Trace(strTrace);
        }
Example #7
0
        /**
         * 获取一个元素,只能用于tup版本2,如果待获取的数据为tup3,则抛异常
         * @param <T>
         * @param name
         * @return
         * @throws ObjectCreateException
         */
        public T Get <T>(string name)
        {
            if (_iVer == Const.PACKET_TYPE_TUP3)
            {
                throw new Exception("data is encoded by new version, please use getTarsStruct(String name,T proxy)");
            }

            object obj = null;

            if (!_data.ContainsKey(name))
            {
                return((T)obj);
            }
            else if (cachedData.ContainsKey(name))
            {
                if (!cachedData.TryGetValue(name, out obj))
                {
                    obj = null;
                }
                return((T)obj);
            }
            else
            {
                Dictionary <string, byte[]> pair;
                _data.TryGetValue(name, out pair);

                string strBasicType = "";
                string className    = null;
                byte[] data         = new byte[0];

                // 找到和T类型对应的数据data
                foreach (KeyValuePair <string, byte[]> e in pair)
                {
                    className = e.Key;
                    data      = e.Value;

                    if (className == null || className == string.Empty)
                    {
                        continue;
                    }

                    // 比较基本类型
                    strBasicType = BasicClassTypeUtil.CS2UniType(typeof(T).ToString());
                    if (className.Length > 0 && className == strBasicType)
                    {
                        break;
                    }
                    if (strBasicType == "map" && className.Length >= 3 && className.Substring(0, 3).ToLower() == "map")
                    {
                        break;
                    }
                    if (typeof(T).IsArray && className.Length > 3 && className.Substring(0, 4).ToLower() == "list")
                    {
                        break;
                    }
                    if (strBasicType == "list" && className.Length > 3 && className.Substring(0, 4).ToLower() == "list")
                    {
                        break;
                    }
                }

                try
                {
                    object objtmp = GetCacheProxy <T>(className);
                    if (objtmp == null)
                    {
                        return((T)objtmp);
                    }

                    obj = decodeData(data, objtmp);
                    if (obj != null)
                    {
                        SaveDataCache(name, obj);
                    }
                    return((T)obj);
                }
                catch (Exception ex)
                {
                    QTrace.Trace(this + " Get Exception: " + ex.Message);
                    throw new ObjectCreateException(ex);
                }
            }
        }
        /**
         * Get an element, only for tup version 2, if the data to be acquired is version tup3,
         * throw an exception.
         * @param <T>
         * @param name
         * @return
         * @throws ObjectCreateException
         */
        public T Get <T>(string name)
        {
            if (_iVer == Const.TUP_VERSION_3)
            {
                throw new Exception("data is encoded by new version, please use Get(string Name, T DefaultObj)");
            }

            object obj = null;

            if (!_data.ContainsKey(name))
            {
                return((T)obj);
            }
            else if (cachedData.ContainsKey(name))
            {
                if (!cachedData.TryGetValue(name, out obj))
                {
                    obj = null;
                }
                return((T)obj);
            }
            else
            {
                Dictionary <string, byte[]> map;
                _data.TryGetValue(name, out map);

                string strBasicType = "";
                string className    = null;
                byte[] data         = new byte[0];

                // Find the data corresponding to the T type.
                foreach (KeyValuePair <string, byte[]> pair in map)
                {
                    className = pair.Key;
                    data      = pair.Value;

                    if (className == null || className == string.Empty)
                    {
                        continue;
                    }

                    // Comparative basic type.
                    strBasicType = BasicClassTypeUtil.CS2UniType(typeof(T).ToString());
                    if (className.Length > 0 && className == strBasicType)
                    {
                        break;
                    }

                    if (strBasicType == "map" && className.Length >= 3 && className.Substring(0, 3).ToLower() == "map")
                    {
                        break;
                    }

                    if (typeof(T).IsArray && className.Length > 3 && className.Substring(0, 4).ToLower() == "list")
                    {
                        break;
                    }

                    if (strBasicType == "list" && className.Length > 3 && className.Substring(0, 4).ToLower() == "list")
                    {
                        break;
                    }
                }

                try
                {
                    object tmpObj = GetCacheProxy <T>(className);
                    if (tmpObj == null)
                    {
                        return((T)tmpObj);
                    }

                    obj = DecodeData(data, tmpObj);
                    if (obj != null)
                    {
                        SaveDataCache(name, obj);
                    }
                    return((T)obj);
                }
                catch (Exception ex)
                {
                    QTrace.Trace(this + " Get Exception: " + ex.Message);
                    throw new ObjectCreateException(ex);
                }
            }
        }