public static async Task ReceiveFileWithStreams(long fileSize, string fileName, StreamCommunication streamCommunication)
        {
            long fileParts   = ProtocolHelpers.CalculateFileParts(fileSize);
            long offset      = 0;
            long currentPart = 1;

            while (fileSize > offset)
            {
                byte[] data;
                if (currentPart == fileParts)
                {
                    var lastPartSize = (int)(fileSize - offset);
                    data = await streamCommunication.ReadAsync(lastPartSize);

                    offset += lastPartSize;
                }
                else
                {
                    data = await streamCommunication.ReadAsync(ProtocolConstants.MaxPacketSize);

                    offset += ProtocolConstants.MaxPacketSize;
                }

                FileStreamHandler.Write(fileName, data);
                currentPart++;
            }
        }
Example #2
0
        public static async Task <MessageResponse> RecieveMessageCommand(StreamCommunication streamCommunication)
        {
            var response        = ConversionHandler.ConvertBytesToShort(await streamCommunication.ReadAsync(ProtocolConstants.ShortTypeLength));
            var responseCommand = ConversionHandler.ConvertBytesToShort(await streamCommunication.ReadAsync(ProtocolConstants.ShortTypeLength));
            var responseMessage = ConversionHandler.ConvertBytesToString(await streamCommunication.ReadAsync(ProtocolConstants.ResponseMessageLength));

            return(new MessageResponse()
            {
                Message = responseMessage,
                responseCommands = (ProtocolConstants.ResponseCommands)responseCommand
            });
        }