public void Write(byte[] l, int tag)
        {
            int nLen = 0;

            if (l != null)
            {
                nLen = l.Length;
            }
            reserve(8 + nLen);
            writeHead((byte)TarsStructType.SIMPLE_LIST, tag);
            writeHead((byte)TarsStructType.BYTE, 0);
            Write(nLen, 0);

            try
            {
                if (l != null)
                {
                    bw.Write(l);
                }
            }
            catch (Exception e)
            {
                QTrace.Trace(e.Message);
            }
        }
示例#2
0
        public void Write(byte[] l, int tag)
        {
            int nLen = 0;

            if (l != null)
            {
                nLen = l.Length;
            }
            reserve(8 + nLen);
            writeHead((byte)JceStructType.SIMPLE_LIST, tag);
            writeHead((byte)JceStructType.BYTE, 0);
            Write(nLen, 0);

            try
            {
                //using (BinaryWriter bw = new BinaryWriter(ms))
                if (l != null)
                {
                    bw.Write(l);
                }
            }
            catch (Exception e)
            {
                QTrace.Trace(e.Message);
            }
        }
        public void Write(byte[] array, int tag)
        {
            int nLen = 0;

            if (array != null)
            {
                nLen = array.Length;
            }
            Reserve(8 + nLen);
            WriteHead((byte)TarsStructType.SIMPLE_LIST, tag);
            WriteHead((byte)TarsStructType.BYTE, 0);
            Write(nLen, 0);

            try
            {
                if (array != null)
                {
                    writer.Write(array);
                }
            }
            catch (Exception ex)
            {
                QTrace.Trace(ex.Message);
            }
        }
 public void writeByteString(string s, int tag)
 {
     reserve(10 + s.Length);
     byte[] by = HexUtil.hexStr2Bytes(s);
     if (by.Length > 255)
     {
         writeHead((byte)TarsStructType.STRING4, tag);
         try
         {
             {
                 bw.Write(ByteConverter.ReverseEndian(by.Length));
                 bw.Write(by);
             }
         }
         catch (Exception e)
         {
             QTrace.Trace(e.Message);
         }
     }
     else
     {
         writeHead((byte)TarsStructType.STRING1, tag);
         try
         {
             {
                 bw.Write((byte)by.Length);
                 bw.Write(by);
             }
         }
         catch (Exception e)
         {
             QTrace.Trace(e.Message);
         }
     }
 }
        public void Write(string str, int tag, bool IsLocalString = false)
        {
            byte[] bytes;
            try
            {
                bytes = ByteConverter.String2Bytes(str, IsLocalString);
                if (bytes == null)
                {
                    bytes = new byte[0];
                }
            }
            catch (Exception ex)
            {
                Tup.QTrace.Trace(this + " write s Exception" + ex.Message);
                return;
            }

            if (bytes != null)
            {
                Reserve(10 + bytes.Length);
            }
            if (bytes != null && bytes.Length > 255)
            {
                WriteHead((byte)TarsStructType.STRING4, tag);
                try
                {
                    {
                        writer.Write(ByteConverter.ReverseEndian(bytes.Length));
                        writer.Write(bytes);
                    }
                }
                catch (Exception ex)
                {
                    QTrace.Trace(ex.Message);
                }
            }
            else
            {
                WriteHead((byte)TarsStructType.STRING1, tag);
                try
                {
                    {
                        if (bytes != null)
                        {
                            writer.Write((byte)bytes.Length);
                            writer.Write(bytes);
                        }
                        else
                        {
                            writer.Write((byte)0);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Tup.QTrace.Trace(this + " write s(2) Exception" + ex.Message);
                }
            }
        }
 public void Write(float n, int tag)
 {
     reserve(6);
     writeHead((byte)TarsStructType.FLOAT, tag);
     try
     {
         {
             bw.Write(ByteConverter.ReverseEndian(n));
         }
     }
     catch (Exception e)
     {
         QTrace.Trace(e.Message);
     }
 }
 public void Write(double n, int tag)
 {
     reserve(10);
     writeHead((byte)TarsStructType.DOUBLE, tag);
     try
     {
         {
             bw.Write(ByteConverter.ReverseEndian(n));
         }
     }
     catch (Exception e)
     {
         QTrace.Trace(e.Message);
     }
 }
示例#8
0
 public void Write(double n, int tag)
 {
     reserve(10);
     writeHead((byte)JceStructType.DOUBLE, tag);
     try
     {
         //using (BinaryWriter bw = new BinaryWriter(ms))
         {
             bw.Write(n);
         }
     }
     catch (Exception e)
     {
         QTrace.Trace(e.Message);
     }
 }
 public void Write(byte n, int tag)
 {
     Reserve(3);
     if (n == 0)
     {
         WriteHead((byte)TarsStructType.ZERO_TAG, tag);
     }
     else
     {
         WriteHead((byte)TarsStructType.BYTE, tag);
         try
         {
             {
                 writer.Write(n);
             }
         }
         catch (Exception ex)
         {
             QTrace.Trace(ex.Message);
         }
     }
 }
示例#10
0
 public void Write(long n, int tag)
 {
     reserve(10);
     if (n >= int.MinValue && n <= int.MaxValue)
     {
         Write((int)n, tag);
     }
     else
     {
         writeHead((byte)TarsStructType.LONG, tag);
         try
         {
             {
                 bw.Write(ByteConverter.ReverseEndian(n));
             }
         }
         catch (Exception e)
         {
             QTrace.Trace(e.Message);
         }
     }
 }
示例#11
0
 public void Write(short n, int tag)
 {
     reserve(4);
     if (n >= -128 && n <= 127)
     {
         Write((byte)n, tag);
     }
     else
     {
         writeHead((byte)TarsStructType.SHORT, tag);
         try
         {
             {
                 bw.Write(ByteConverter.ReverseEndian(n));
             }
         }
         catch (Exception e)
         {
             QTrace.Trace(this + " Write: " + e.Message);
         }
     }
 }
示例#12
0
 public void Write(byte b, int tag)
 {
     reserve(3);
     if (b == 0)
     {
         writeHead((byte)TarsStructType.ZERO_TAG, tag);
     }
     else
     {
         writeHead((byte)TarsStructType.BYTE, tag);
         try
         {
             {
                 bw.Write(b);
             }
         }
         catch (Exception e)
         {
             QTrace.Trace(e.Message);
         }
     }
 }
示例#13
0
        // 写入头信息
        public void writeHead(byte type, int tag)
        {
            if (tag < 15)
            {
                byte b = (byte)((tag << 4) | type);

                try
                {
                    //using (BinaryWriter bw = new BinaryWriter(ms))
                    {
                        bw.Write(b);
                    }
                }
                catch (Exception e)
                {
                    QTrace.Trace(e.Message);
                }
            }
            else if (tag < 256)
            {
                try
                {
                    byte b = (byte)((15 << 4) | type);
                    //using (BinaryWriter bw = new BinaryWriter(ms))
                    {
                        bw.Write(b);
                        bw.Write((byte)tag);
                    }
                }
                catch (Exception e)
                {
                    QTrace.Trace(this + " writeHead: " + e.Message);
                }
            }
            else
            {
                throw new JceEncodeException("tag is too large: " + tag);
            }
        }
示例#14
0
 public void writeStringByte(string s, int tag)
 {
     byte[] by = HexUtil.hexStr2Bytes(s);
     reserve(10 + by.Length);    // 预分配足够的缓冲区
     if (by.Length > 255)
     {
         // 长度大于255,为String4类型
         writeHead((byte)JceStructType.STRING4, tag);
         try
         {
             //using (BinaryWriter bw = new BinaryWriter(ms))
             {
                 bw.Write(ByteConverter.ReverseEndian(by.Length));
                 bw.Write(by);
             }
         }
         catch (Exception e)
         {
             QTrace.Trace(e.Message);
         }
     }
     else
     {
         // 长度小于255,位String1类型
         writeHead((byte)JceStructType.STRING1, tag);
         try
         {
             //using (BinaryWriter bw = new BinaryWriter(ms))
             {
                 bw.Write((byte)by.Length);
                 bw.Write(by);
             }
         }
         catch (Exception e)
         {
             QTrace.Trace(e.Message);
         }
     }
 }
示例#15
0
 public void Write(byte b, int tag)
 {
     reserve(3);
     if (b == 0)
     {
         writeHead((byte)JceStructType.ZERO_TAG, tag);
     }
     else
     {
         writeHead((byte)JceStructType.BYTE, tag);
         try
         {
             //using (BinaryWriter bw = new BinaryWriter(ms))
             {
                 bw.Write(b);
             }
         }
         catch (Exception e)
         {
             QTrace.Trace(e.Message);
         }
     }
 }
        public void Write(int n, int tag)
        {
            Reserve(6);

            if (n >= short.MinValue && n <= short.MaxValue)
            {
                Write((short)n, tag);
            }
            else
            {
                WriteHead((byte)TarsStructType.INT, tag);
                try
                {
                    {
                        writer.Write(ByteConverter.ReverseEndian(n));
                    }
                }
                catch (Exception ex)
                {
                    QTrace.Trace(ex.Message);
                }
            }
        }
示例#17
0
 public void Write(short n, int tag)
 {
     reserve(4);
     if (n >= -128 && n <= 127)// (n >= byte.MinValue && n <= byte.MaxValue)
     {
         Write((byte)n, tag);
     }
     else
     {
         writeHead((byte)JceStructType.SHORT, tag);
         try
         {
             //using (BinaryWriter bw = new BinaryWriter(ms))
             {
                 bw.Write(ByteConverter.ReverseEndian(n));
             }
         }
         catch (Exception e)
         {
             QTrace.Trace(this + " Write: " + e.Message);
         }
     }
 }
示例#18
0
        // 跳到指定的tag的数据之前
        public bool skipToTag(int tag)
        {
            try
            {
                HeadData hd = new HeadData();
                while (true)
                {
                    int len = peakHead(hd);
                    if (tag <= hd.tag || hd.type == (byte)TarsStructType.STRUCT_END)
                    {
                        return(tag == hd.tag);
                    }

                    skip(len);
                    skipField(hd.type);
                }
            }
            catch (TarsDecodeException e)
            {
                QTrace.Trace(e.Message);
            }
            return(false);
        }
示例#19
0
        // 写入头信息
        public void writeHead(byte type, int tag)
        {
            if (tag < 15)
            {
                byte b = (byte)((tag << 4) | type);

                try
                {
                    {
                        bw.Write(b);
                    }
                }
                catch (Exception e)
                {
                    QTrace.Trace(e.Message);
                }
            }
            else if (tag < 256)
            {
                try
                {
                    byte b = (byte)((15 << 4) | type);
                    {
                        bw.Write(b);
                        bw.Write((byte)tag);
                    }
                }
                catch (Exception e)
                {
                    QTrace.Trace(this + " writeHead: " + e.Message);
                }
            }
            else
            {
                throw new TarsEncodeException("tag is too large: " + tag);
            }
        }
 public void WriteStringByte(string str, int tag)
 {
     byte[] by = HexUtil.hexStr2Bytes(str);
     Reserve(10 + by.Length);
     if (by.Length > 255)
     {
         // Length greater than 255, which is a String4 type.
         WriteHead((byte)TarsStructType.STRING4, tag);
         try
         {
             {
                 writer.Write(ByteConverter.ReverseEndian(by.Length));
                 writer.Write(by);
             }
         }
         catch (Exception ex)
         {
             QTrace.Trace(ex.Message);
         }
     }
     else
     {
         // Length less than 255, bit String1 type.
         WriteHead((byte)TarsStructType.STRING1, tag);
         try
         {
             {
                 writer.Write((byte)by.Length);
                 writer.Write(by);
             }
         }
         catch (Exception ex)
         {
             QTrace.Trace(ex.Message);
         }
     }
 }
示例#21
0
        public void Write(int n, int tag)
        {
            reserve(6);

            if (n >= short.MinValue && n <= short.MaxValue)
            {
                Write((short)n, tag);
            }
            else
            {
                writeHead((byte)JceStructType.INT, tag);
                try
                {
                    //using (BinaryWriter bw = new BinaryWriter(ms))
                    {
                        bw.Write(ByteConverter.ReverseEndian(n));
                    }
                }
                catch (Exception e)
                {
                    QTrace.Trace(e.Message);
                }
            }
        }
        //
        // Jump to the data of the specified tag before.
        //
        public bool SkipToTag(int tag)
        {
            try
            {
                HeadData head = new HeadData();
                while (true)
                {
                    int len = PeekHead(head);
                    if (tag <= head.tag || head.type == (byte)TarsStructType.STRUCT_END)
                    {
                        /* Modified by shines77, 2018-12-17 20:00 */
                        return((head.type == (byte)TarsStructType.STRUCT_END) ? false : (tag == head.tag));
                    }

                    Skip(len);
                    SkipField(head.type);
                }
            }
            catch (TarsDecodeException ex)
            {
                QTrace.Trace(ex.Message);
            }
            return(false);
        }
示例#23
0
        public byte[] Read(byte[] l, int tag, bool isRequire)
        {
            byte[] lr = null;
            if (skipToTag(tag))
            {
                HeadData hd = new HeadData();
                readHead(hd);
                switch (hd.type)
                {
                case (byte)TarsStructType.SIMPLE_LIST:
                {
                    HeadData hh = new HeadData();
                    readHead(hh);
                    if (hh.type != (byte)TarsStructType.BYTE)
                    {
                        throw new TarsDecodeException("type mismatch, tag: " + tag + ", type: " + hd.type + ", " + hh.type);
                    }

                    int size = Read(0, 0, true);
                    if (size < 0)
                    {
                        throw new TarsDecodeException("invalid size, tag: " + tag + ", type: " + hd.type + ", " + hh.type + ", size: " + size);
                    }

                    lr = new byte[size];


                    try
                    {
                        lr = br.ReadBytes(size);
                    }
                    catch (Exception e)
                    {
                        QTrace.Trace(e.Message);
                        return(null);
                    }
                    break;
                }

                case (byte)TarsStructType.LIST:
                {
                    int size = Read(0, 0, true);
                    if (size < 0)
                    {
                        throw new TarsDecodeException("size invalid: " + size);
                    }
                    lr = new byte[size];
                    for (int i = 0; i < size; ++i)
                    {
                        lr[i] = Read(lr[0], 0, true);
                    }
                    break;
                }

                default:
                    throw new TarsDecodeException("type mismatch.");
                }
            }
            else if (isRequire)
            {
                throw new TarsDecodeException("require field not exist.");
            }
            return(lr);
        }
示例#24
0
        ////@SuppressWarnings("unchecked")
        private Array readArrayImpl <T>(T mt, int tag, bool isRequire)
        {
            if (skipToTag(tag))
            {
                HeadData hd = new HeadData();
                readHead(hd);
                switch (hd.type)
                {
                case (byte)TarsStructType.LIST:
                {
                    int size = Read(0, 0, true);
                    if (size < 0)
                    {
                        throw new TarsDecodeException("size invalid: " + size);
                    }

                    Array lr = Array.CreateInstance(mt.GetType(), size);
                    for (int i = 0; i < size; ++i)
                    {
                        T t = (T)Read(mt, 0, true);
                        lr.SetValue(t, i);
                    }
                    return(lr);
                }

                case (byte)TarsStructType.SIMPLE_LIST:
                {
                    HeadData hh = new HeadData();
                    readHead(hh);
                    if (hh.type == (byte)TarsStructType.ZERO_TAG)
                    {
                        return(Array.CreateInstance(mt.GetType(), 0));
                    }
                    if (hh.type != (byte)TarsStructType.BYTE)
                    {
                        throw new TarsDecodeException("type mismatch, tag: " + tag + ", type: " + hd.type + ", " + hh.type);
                    }
                    int size = Read(0, 0, true);
                    if (size < 0)
                    {
                        throw new TarsDecodeException("invalid size, tag: " + tag + ", type: " + hd.type + ", size: " + size);
                    }

                    T[] lr = new T[size];

                    try
                    {
                        byte[] lrtmp = br.ReadBytes(size);
                        for (int i = 0; i < lrtmp.Length; i++)
                        {
                            object obj = lrtmp[i];
                            lr[i] = (T)obj;
                        }

                        return(lr);
                    }
                    catch (Exception e)
                    {
                        QTrace.Trace(e.Message);
                        return(null);
                    }
                }

                default:
                {
                    throw new TarsDecodeException("type mismatch.");
                }
                }
            }
            else if (isRequire)
            {
                throw new TarsDecodeException("require field not exist.");
            }
            return(null);
        }
示例#25
0
 public void Write(string s, int tag)
 {
     byte[] by;
     try
     {
         //Encoding en = Encoding.GetEncoding(sServerEncoding);
         //by = en.GetBytes(s);
         by = ByteConverter.String2Bytes(s, false);
         if (by == null)
         {
             by = new byte[0];
         }
     }
     catch (Exception e)
     {
         Wup.QTrace.Trace(this + " write s Exception" + e.Message);
         return;
     }
     if (by != null)
     {
         reserve(10 + by.Length);
     }
     if (by != null && by.Length > 255)
     {
         writeHead((byte)JceStructType.STRING4, tag);
         try
         {
             //using (BinaryWriter bw = new BinaryWriter(ms))
             {
                 bw.Write(ByteConverter.ReverseEndian(by.Length));
                 bw.Write(by);
             }
         }
         catch (Exception e)
         {
             QTrace.Trace(e.Message);
         }
     }
     else
     {
         writeHead((byte)JceStructType.STRING1, tag);
         try
         {
             //using (BinaryWriter bw = new BinaryWriter(ms))
             {
                 if (by != null)
                 {
                     bw.Write((byte)by.Length);
                     bw.Write(by);
                 }
                 else
                 {
                     bw.Write((byte)0);
                 }
             }
         }
         catch (Exception e)
         {
             Wup.QTrace.Trace(this + " write s(2) Exception" + e.Message);
         }
     }
 }
示例#26
0
        public void TestMapList()
        {
            UniPacket client = new UniPacket();

            client.ServantName = "ServantName";
            client.FuncName    = "FuncName";
            client.RequestId   = 0;

            Dictionary <string, int> dict1 = new Dictionary <string, int>();

            dict1.Add("s1", 2341);

            List <int> list1 = new List <int>();

            for (int il = 0; il < 10; il++)
            {
                list1.Add(il);
            }

            Dictionary <string, List <int> > dictList = new Dictionary <string, List <int> >();

            for (int idl = 0; idl < 3; idl++)
            {
                List <int> listdict = new List <int>();

                for (int il = 0; il < 10; il++)
                {
                    listdict.Add(il + idl);
                }
                dictList.Add("key " + idl, listdict);
            }


            Dictionary <string, Dictionary <string, int> > dictdict = new Dictionary <string, Dictionary <string, int> >();

            for (int idl = 0; idl < 3; idl++)
            {
                Dictionary <string, int> dd = new Dictionary <string, int>();

                for (int il = 0; il < 10; il++)
                {
                    dd.Add("sub key " + il + idl, il + idl);
                }
                dictdict.Add("key " + idl, dd);
            }

            byte[] abtye = new byte[10];
            for (byte ib = 1; ib < abtye.Length; ib++)
            {
                abtye[ib] = ib;
            }

            client.Put("array", abtye);
            client.Put("list", list1);
            client.Put("emptylist", new List <byte>());
            client.Put("emptylistint", new List <int>());
            client.Put("emptylistTars", new List <TestFriendlInfo>());
            client.Put("emptyMapintstring", new Dictionary <int, string>());
            client.Put("emptyMapintTars", new Dictionary <int, TestFriendlInfo>());
            client.Put("map", dict1);
            client.Put("maplist", dictList);
            client.Put("mapmap", dictdict);

            byte[] buffer = client.Encode();

            UniPacket de = new UniPacket();

            de.Decode(buffer);

            abtye = de.Get <byte[]>("array");

            Dictionary <string, int> dict10 = new Dictionary <string, int>();

            dict10 = de.Get <Dictionary <string, int> >("map");
            QTrace.Trace(dict10);

            List <int> list10 = new List <int>();

            list10 = de.Get <List <int> >("list");
            QTrace.Trace(list10);

            List <byte> emptylist = new List <byte>();

            emptylist = de.Get <List <byte> >("emptylist");
            QTrace.Trace(emptylist);

            List <int> emptylistint = new List <int>();

            emptylistint = de.Get <List <int> >("emptylistint");
            QTrace.Trace(emptylistint);

            List <TestFriendlInfo> emptylistTars = de.Get <List <TestFriendlInfo> >("emptylistTars");

            QTrace.Trace(emptylistTars);

            Dictionary <int, string> emptyMapintstring = de.Get <Dictionary <int, string> >("emptyMapintstring");

            QTrace.Trace(emptyMapintstring);

            Dictionary <int, TestFriendlInfo> emptyMapintTars = de.Get <Dictionary <int, TestFriendlInfo> >("emptyMapintTars");

            QTrace.Trace(emptyMapintTars);

            dictList = (Dictionary <string, List <int> >)de.Get <Dictionary <string, List <int> > >("maplist");
            foreach (string key in dictList.Keys)
            {
                QTrace.Trace(key);
                QTrace.Trace(dictList[key]);
            }

            dictdict = (Dictionary <string, Dictionary <string, int> >)de.Get <Dictionary <string, Dictionary <string, int> > >("mapmap");
            QTrace.Trace("Map Map");
            foreach (string key in dictdict.Keys)
            {
                QTrace.Trace(key);
                Dictionary <string, int> subdict = dictdict[key];
                QTrace.Trace(subdict);
            }
        }