public async Task SendPartialDataMessageAsync(byte[] bytes)
        {
            var packageSize     = 32000;
            var partialTemplate = new PartialDataMessage
            {
                PhotoId         = Guid.NewGuid(),
                TotalPartsCount = bytes.Length / packageSize + (bytes.Length % packageSize > 0 ? 1 : 0)
            };

            for (var i = 0; i < partialTemplate.TotalPartsCount; i++)
            {
                var packagesSent = i;
                var dst          = new byte[
                    i < partialTemplate.TotalPartsCount - 1
                        ? packageSize
                        : bytes.Length - (packageSize * packagesSent)];
                Array.Copy(bytes, packageSize * packagesSent, dst, 0, dst.Length);

                var dataMessage = new PartialDataMessage
                {
                    PhotoId           = partialTemplate.PhotoId,
                    TotalPartsCount   = partialTemplate.TotalPartsCount,
                    CurrentPartNumber = i,
                    Payload           = dst
                };
                await _hubService.SendPartialDataMessageAsync(dataMessage);
            }
        }
        public virtual void OnPartialDataMessageReceived(PartialDataMessage dataMessage)
        {
            _logger.LogInfo($"Partial data message received: {dataMessage}");

            if (_partialMesages.ContainsKey(dataMessage.PhotoId))
            {
                _partialMesages[dataMessage.PhotoId].Add(dataMessage);
            }
            else
            {
                _partialMesages[dataMessage.PhotoId] = new List <PartialDataMessage>()
                {
                    dataMessage
                };
            }

            var list = _partialMesages[dataMessage.PhotoId];

            _partialMesages[dataMessage.PhotoId] = list.OrderBy(x => x.CurrentPartNumber).ToList();
            list = _partialMesages[dataMessage.PhotoId];

            if (list.Count == dataMessage.TotalPartsCount)
            {
                var bytesCount = list.Sum(x => x.Payload.Length);
                var bytes      = new byte[bytesCount];
                var writeFrom  = 0;
                foreach (var item in list)
                {
                    item.Payload.CopyTo(bytes, writeFrom);
                    writeFrom += item.Payload.Length;
                }

                _partialMesages.Remove(dataMessage.PhotoId);
                OnPartialDataMessageCompleted(bytes, dataMessage.PhotoId.ToString() + ".png");
            }
        }
예제 #3
0
 public async Task SendPartialDataMessageAsync(PartialDataMessage dataMessage)
 {
     await _hubClient.SendAsync(nameof(SendPartialDataMessageAsync), dataMessage, _sessionName);
 }
예제 #4
0
 public override void OnPartialDataMessageReceived(PartialDataMessage dataMessage)
 {
     base.OnPartialDataMessageReceived(dataMessage);
 }