Exemplo n.º 1
0
        private byte[] CreatePackages(byte[] bytes)
        {
            var left   = bytes.Length;
            var index  = 0;
            var memory = new List <byte>();

            while (left > 0)
            {
                var lenght = Math.Min(PackageSettings.MaxSettingsLenght(), left);
                left -= lenght;
                var hasMore = left > 0;

                var settings      = new PackageSettings((ushort)lenght, hasMore);
                var settingsBytes = BitConverter.GetBytes(settings.Settings);
                memory.AddRange(settingsBytes);

                memory.AddRange(SubArray(bytes, index, lenght));
                index += lenght;
            }

            return(memory.ToArray());
        }
Exemplo n.º 2
0
        private void WriteToBuffer(byte[] bytes, int index, int count)
        {
            if (_receiveBuffer == null)
            {
                _receiveBuffer = new List <byte>();
                var settingsUshort = BitConverter.ToUInt16(bytes, index);
                index += 2;
                count -= 2;
                var settings = new PackageSettings(settingsUshort);
                _receivePackageLeft = settings.Lenght;
                _receiveHasMore     = settings.HasMore;
            }

            if (count <= _receivePackageLeft)
            {
                _receiveBuffer.AddRange(SubArray(bytes, index, count));
                _receivePackageLeft -= (ushort)count;
                count = 0;
            }
            else
            {
                _receiveBuffer.AddRange(SubArray(bytes, index, _receivePackageLeft));
                index += _receivePackageLeft;
                count -= _receivePackageLeft;
                _receivePackageLeft = 0;
            }

            if (_receivePackageLeft == 0 && !_receiveHasMore)
            {
                CallReceived();
            }

            if (count > 0)
            {
                WriteToBuffer(bytes, index, count);
            }
        }