Esempio n. 1
0
 void HandleFileBytesReceived(object sender, List <byte> fileBytes)
 {
     ReceivedFileBytes?.Invoke(sender, fileBytes);
 }
Esempio n. 2
0
        async Task <Result <byte[]> > ReceiveRequestBytesAsync(int totalBytes)
        {
            EventOccurred?.Invoke(this,
                                  new ServerEvent {
                EventType = EventType.ReceiveRequestBytesStarted
            });

            int currentRequestBytesReceived;
            var socketReadCount = 0;
            var bytesReceived   = 0;
            var bytesRemaining  = totalBytes;
            var requestBytes    = new List <byte>();
            var unreadByteCount = 0;

            if (_unreadBytes.Count > 0)
            {
                currentRequestBytesReceived = Math.Min(totalBytes, _unreadBytes.Count);
                var unreadRequestBytes = new byte[currentRequestBytesReceived];

                _unreadBytes.CopyTo(0, unreadRequestBytes, 0, currentRequestBytesReceived);
                requestBytes.AddRange(unreadRequestBytes.ToList());

                bytesReceived  += currentRequestBytesReceived;
                bytesRemaining -= currentRequestBytesReceived;

                EventOccurred?.Invoke(this, new ServerEvent
                {
                    EventType             = EventType.CopySavedBytesToRequestData,
                    UnreadBytesCount      = _unreadBytes.Count,
                    RequestLengthInBytes  = totalBytes,
                    RequestBytesRemaining = bytesRemaining
                });

                if (_unreadBytes.Count > totalBytes)
                {
                    var fileByteCount = _unreadBytes.Count - totalBytes;
                    var fileBytes     = new byte[fileByteCount];
                    _unreadBytes.CopyTo(totalBytes, fileBytes, 0, fileByteCount);
                    _unreadBytes = fileBytes.ToList();
                }
                else
                {
                    _unreadBytes = new List <byte>();
                }
            }

            currentRequestBytesReceived = 0;
            while (bytesRemaining > 0)
            {
                var readFromSocket =
                    await _socket.ReceiveWithTimeoutAsync(
                        _buffer,
                        0,
                        _bufferSize,
                        SocketFlags.None,
                        _timeoutMs)
                    .ConfigureAwait(false);

                if (readFromSocket.Failure)
                {
                    return(Result.Fail <byte[]>(readFromSocket.Error));
                }

                var lastBytesReceivedCount = readFromSocket.Value;
                currentRequestBytesReceived = Math.Min(bytesRemaining, lastBytesReceivedCount);

                var requestBytesFromSocket = new byte[currentRequestBytesReceived];
                _buffer.ToList().CopyTo(0, requestBytesFromSocket, 0, currentRequestBytesReceived);
                requestBytes.AddRange(requestBytesFromSocket.ToList());

                socketReadCount++;
                unreadByteCount = lastBytesReceivedCount - currentRequestBytesReceived;
                bytesReceived  += currentRequestBytesReceived;
                bytesRemaining -= currentRequestBytesReceived;

                SocketEventOccurred?.Invoke(this, new ServerEvent
                {
                    EventType                   = EventType.ReceivedRequestBytesFromSocket,
                    SocketReadCount             = socketReadCount,
                    BytesReceivedCount          = lastBytesReceivedCount,
                    CurrentRequestBytesReceived = currentRequestBytesReceived,
                    TotalRequestBytesReceived   = bytesReceived,
                    RequestLengthInBytes        = totalBytes,
                    RequestBytesRemaining       = bytesRemaining,
                    UnreadBytesCount            = unreadByteCount
                });
            }

            EventOccurred?.Invoke(this, new ServerEvent
            {
                EventType    = EventType.ReceiveRequestBytesComplete,
                RequestBytes = requestBytes.ToArray()
            });

            if (unreadByteCount == 0)
            {
                return(Result.Ok(requestBytes.ToArray()));
            }

            var unreadBytes = new byte[unreadByteCount];

            _buffer.ToList().CopyTo(currentRequestBytesReceived, unreadBytes, 0, unreadByteCount);
            _unreadBytes = unreadBytes.ToList();

            EventOccurred?.Invoke(this, new ServerEvent
            {
                EventType         = EventType.SaveUnreadBytesAfterAllRequestBytesReceived,
                ExpectedByteCount = currentRequestBytesReceived,
                UnreadBytesCount  = unreadByteCount
            });

            ReceivedFileBytes?.Invoke(this, _unreadBytes);
            return(Result.Ok(requestBytes.ToArray()));
        }