コード例 #1
0
        public async Task SendFileAsync(string path)
        {
            if (!File.Exists(path))
            {
                throw new Exception("File not found"); // TODO: CHANGE FOR CUSTOM IMPLEMENTATION
            }
            // Send file length
            var fileInfo = new FileInfo(path);
            await _networkCommunication.SendLongAsync(fileInfo.Length);

            // Send file chunks
            await using var fileStream = File.OpenRead(path);

            var sent   = 0;
            var buffer = new byte[ProtocolSpecification.FileChunkSize];

            while (fileInfo.Length - sent > ProtocolSpecification.FileChunkSize)
            {
                await fileStream.ReadAsync(buffer, 0, ProtocolSpecification.FileChunkSize);

                await _networkCommunication.SendBytesAsync(buffer);

                sent += ProtocolSpecification.FileChunkSize;
            }

            buffer = new byte[fileInfo.Length - sent];
            await fileStream.ReadAsync(buffer, 0, (int)fileInfo.Length - sent);

            await _networkCommunication.SendBytesAsync(buffer);
        }