Example #1
0
 public SprotoMgr()
 {
     this.Protocols    = new Dictionary <string, SprotoProtocol>();
     this.TagProtocols = new Dictionary <UInt16, SprotoProtocol>();
     this.Types        = new Dictionary <string, SprotoType>();
     this.Codec        = new SprotoCodec();
     this.Packer       = new SprotoPacker();
 }
Example #2
0
        public byte[] Unpack(byte[] src, int start, int length, out int size, byte[] dest = null)
        {
            if (null == dest)
            {
                int dest_length = SprotoPacker.NeedBufferSize("unpack", length);
                dest = new byte[dest_length];
            }
            int srcpos  = start;
            int destpos = 0;

            while (srcpos < start + length)
            {
                int  n;
                byte header = src[srcpos++];
                if (0xff == header)
                {
                    n = src[srcpos++];
                    n = (n + 1) * 8;
                    this.memcpy(dest, destpos, src, srcpos, n);
                    srcpos  += n;
                    destpos += n;
                }
                else
                {
                    for (int i = 0; i < 8; i++)
                    {
                        bool notzero = ((header >> i) & 1) == 1 ? true : false;
                        if (notzero)
                        {
                            byte b = src[srcpos++];
                            dest[destpos++] = b;
                        }
                        else
                        {
                            dest[destpos++] = 0;
                        }
                    }
                }
            }
            if (srcpos != start + length)
            {
                SprotoHelper.Error("[SprotoPacker.UnPack] fail");
            }
            size = destpos;
            return(dest);
        }
Example #3
0
        public byte[] Pack(byte[] src, int start, int length, out int size, byte[] dest = null)
        {
            if (null == dest)
            {
                int dest_length = SprotoPacker.NeedBufferSize("pack", length);
                dest = new byte[dest_length];
            }
            int ff_n       = 0;
            int ff_srcpos  = 0;
            int ff_destpos = 0;
            int srcpos     = start;
            int destpos    = 0;

            for (int i = 0; i < length; i += 8)
            {
                int n;
                int padding = i + 8 - length;
                if (padding > 0)
                {
                    byte[] tmp = new byte[8];
                    //this.memcpy(tmp,0,src,srcpos,8-padding);
                    for (int j = 0; j < 8 - padding; j++)
                    {
                        tmp[j] = src[srcpos + j];
                    }
                    for (int j = 0; j < padding; j++)
                    {
                        tmp[7 - j] = 0;
                    }
                    n = this.pack_seg(dest, destpos, tmp, 0, ff_n);
                }
                else
                {
                    n = this.pack_seg(dest, destpos, src, srcpos, ff_n);
                }
                if (10 == n)
                {
                    ff_srcpos  = srcpos;
                    ff_destpos = destpos;
                    ff_n       = 1;
                }
                else if (8 == n && ff_n > 0)
                {
                    ff_n++;
                    if (ff_n == 256)
                    {
                        this.write_ff(dest, ff_destpos, src, ff_srcpos, ff_n * 8);
                        ff_n = 0;
                    }
                }
                else
                {
                    if (ff_n > 0)
                    {
                        this.write_ff(dest, ff_destpos, src, ff_srcpos, ff_n * 8);
                        ff_n = 0;
                    }
                }
                srcpos  += 8;
                destpos += n;
            }
            if (ff_n == 1)
            {
                this.write_ff(dest, ff_destpos, src, ff_srcpos, 8);
            }
            else if (ff_n > 1)
            {
                this.write_ff(dest, ff_destpos, src, ff_srcpos, length - (ff_srcpos - start));
            }
            size = destpos;
            return(dest);
        }