Esempio n. 1
0
        private async Task <byte[]> PrepareFramesBytesAsync(byte[] body, IDictionary <string, object> properties)
        {
            if (body == null)
            {
                throw new ArgumentNullException(nameof(body));
            }

            if (properties == null)
            {
                properties = new Dictionary <string, object>();
            }

            bool compressed = GZipHelper.IsGZipBody(body);

            object key = null;

            if (properties.TryGetValue(CompressedKey, out key))
            {
                properties[CompressedKey] = compressed;
            }
            else
            {
                properties.Add(CompressedKey, compressed);
            }

            string props = JsonConvert.SerializeObject(properties);

            byte[] header = Encoding.UTF8.GetBytes($"{props}");

#if DEBUG
            if (properties.TryGetValue("Key", out key))
            {
                int length = body.Length;
                Debug.WriteLine($"=====Key: {key?.ToString()}=====Length: {length}=====");
            }
#endif

            if (!compressed)
            {
                body = await _compressor.CompressAsync(body);
            }

            body = header.Concat(Splitter).Concat(body).ToArray();

            return(body);
        }
Esempio n. 2
0
        private async Task <byte[]> ToBytesAsync(byte[] body, IDictionary <string, object> properties = null)
        {
            if (body == null)
            {
                throw new ArgumentNullException(nameof(body));
            }

            if (properties == null)
            {
                properties = new Dictionary <string, object>();
            }

            bool compressed = GZipHelper.IsGZipBody(body);

            if (properties.TryGetValue(NCSConstants.CompressedKey, out object key))
            {
                properties[NCSConstants.CompressedKey] = compressed;
            }
            else
            {
                properties.Add(NCSConstants.CompressedKey, compressed);
            }

            _headerProvider.Invoke(properties);
            string props = JsonConvert.SerializeObject(properties);

            byte[] header = Encoding.UTF8.GetBytes(props);

            if (!compressed)
            {
                body = await _compressor.CompressAsync(body);
            }

            body = header.Concat(NCSConstants.Splitter).Concat(body).ToArray();
            return(body);
        }