コード例 #1
0
        private static List <T> GetList <T>(PacketReader reader, IPacketConverter converter)
        {
            var itm = reader.GetList();
            var len = itm.Count;

            if (len < 1)
            {
                return(new List <T>());
            }
            var lst = new List <T>(len);
            var gen = converter as IPacketConverter <T>;

            try
            {
                if (gen != null)
                {
                    for (int i = 0; i < len; i++)
                    {
                        lst.Add(gen.GetValue(itm[i].element));
                    }
                }
                else
                {
                    for (int i = 0; i < len; i++)
                    {
                        lst.Add((T)converter.GetValue(itm[i].element));
                    }
                }
            }
            catch (Exception ex) when(PacketException.WrapFilter(ex))
            {
                throw PacketException.ConvertError(ex);
            }
            return(lst);
        }
コード例 #2
0
        private static T[] GetArray <T>(PacketReader reader, IPacketConverter converter)
        {
            var itm = reader.GetList();
            var len = itm.Count;

            if (len < 1)
            {
                return(new T[0]);
            }
            var arr = new T[len];
            var gen = converter as IPacketConverter <T>;

            try
            {
                if (gen != null)
                {
                    for (int i = 0; i < len; i++)
                    {
                        arr[i] = gen.GetValue(itm[i].element);
                    }
                }
                else
                {
                    for (int i = 0; i < len; i++)
                    {
                        arr[i] = (T)converter.GetValue(itm[i].element);
                    }
                }
            }
            catch (Exception ex) when(PacketException.WrapFilter(ex))
            {
                throw PacketException.ConvertError(ex);
            }
            return(arr);
        }
コード例 #3
0
 internal static T GetValueWrap <T>(this IPacketConverter <T> converter, Element element)
 {
     try
     {
         return(converter.GetValue(element.buffer, element.offset, element.length));
     }
     catch (Exception ex) when(PacketException.WrapFilter(ex))
     {
         throw PacketException.ConvertError(ex);
     }
 }
コード例 #4
0
 internal static T GetValueWrap <T>(this IPacketConverter <T> converter, byte[] buffer, int offset, int length)
 {
     try
     {
         return(converter.GetValue(buffer, offset, length));
     }
     catch (Exception ex) when(PacketException.WrapFilter(ex))
     {
         throw PacketException.ConvertError(ex);
     }
 }
コード例 #5
0
 internal static object GetValueWrap(this IPacketConverter converter, byte[] buffer, int offset, int length, bool check = false)
 {
     try
     {
         if (check && converter.Length > length)
         {
             throw PacketException.Overflow();
         }
         return(converter.GetValue(buffer, offset, length));
     }
     catch (Exception ex) when(PacketException.WrapFilter(ex))
     {
         throw PacketException.ConvertError(ex);
     }
 }
コード例 #6
0
        internal List <T> ToList <T>(IPacketConverter converter)
        {
            if (length < 1)
            {
                return(new List <T>());
            }
            if (typeof(T) == typeof(byte))
            {
                return(new List <T>((T[])GetByteArray()));
            }
            else if (typeof(T) == typeof(sbyte))
            {
                return(new List <T>((T[])GetSByteArray()));
            }

            var def = converter.Length;
            var sum = Math.DivRem(length, def, out var rem);

            if (rem != 0)
            {
                throw PacketException.Overflow();
            }
            var lst = new List <T>(sum);
            var gen = converter as IPacketConverter <T>;

            try
            {
                if (gen != null)
                {
                    for (int idx = 0; idx < sum; idx++)
                    {
                        lst.Add(gen.GetValue(buffer, offset + idx * def, def));
                    }
                }
                else
                {
                    for (int idx = 0; idx < sum; idx++)
                    {
                        lst.Add((T)converter.GetValue(buffer, offset + idx * def, def));
                    }
                }
            }
            catch (Exception ex) when(PacketException.WrapFilter(ex))
            {
                throw PacketException.ConvertError(ex);
            }
            return(lst);
        }
コード例 #7
0
 internal static byte[] GetBytesWrap <T>(this IPacketConverter <T> converter, T value)
 {
     try
     {
         var buf = converter.GetBytes(value);
         if (buf == null)
         {
             buf = s_empty_bytes;
         }
         var len = converter.Length;
         if (len > 0 && len != buf.Length)
         {
             throw PacketException.ConvertMismatch(len);
         }
         return(buf);
     }
     catch (Exception ex) when(PacketException.WrapFilter(ex))
     {
         throw PacketException.ConvertError(ex);
     }
 }
コード例 #8
0
        internal void ToDictionary <TK, TV>(IPacketConverter indexConverter, IPacketConverter elementConverter, DictionaryAbstract <TK, TV> dictionary)
        {
            if (length == 0)
            {
                return;
            }
            var keygen = indexConverter as IPacketConverter <TK>;
            var valgen = elementConverter as IPacketConverter <TV>;
            var keylen = indexConverter.Length;
            var vallen = elementConverter.Length;
            var max    = Max;
            var idx    = offset;
            var len    = 0;

            try
            {
                while (true)
                {
                    var sub = max - idx;
                    if (sub == 0)
                    {
                        break;
                    }

                    if (keylen > 0)
                    {
                        if (sub < keylen)
                        {
                            goto fail;
                        }
                        else
                        {
                            len = keylen;
                        }
                    }
                    else if (buffer.MoveNext(max, ref idx, out len) == false)
                    {
                        goto fail;
                    }

                    var key = (keygen != null ? keygen.GetValue(buffer, idx, len) : (TK)indexConverter.GetValue(buffer, idx, len));
                    idx += len;
                    sub  = max - idx;

                    if (vallen > 0)
                    {
                        if (sub < vallen)
                        {
                            goto fail;
                        }
                        else
                        {
                            len = vallen;
                        }
                    }
                    else if (buffer.MoveNext(max, ref idx, out len) == false)
                    {
                        goto fail;
                    }

                    var val = (valgen != null ? valgen.GetValue(buffer, idx, len) : (TV)elementConverter.GetValue(buffer, idx, len));
                    idx += len;
                    dictionary.Add(key, val);
                }
            }
            catch (Exception ex) when(PacketException.WrapFilter(ex))
            {
                throw PacketException.ConvertError(ex);
            }

            return;

fail:
            throw PacketException.Overflow();
        }