@@ -35,7 +35,7 @@ napi_value create_response(napi_env env, bool finished)
3535 return result;
3636}
3737
38- void transfer_data (
38+ HRESULT transfer_data (
3939 _In_ CF_CONNECTION_KEY connectionKey,
4040 _In_ LARGE_INTEGER transferKey,
4141 _In_reads_bytes_opt_ (length.QuadPart) LPCVOID transferData,
@@ -56,10 +56,10 @@ void transfer_data(
5656 opParams.TransferData .Offset = startingOffset;
5757 opParams.TransferData .Length = length;
5858
59- winrt::check_hresult ( CfExecute (&opInfo, &opParams) );
59+ return CfExecute (&opInfo, &opParams);
6060}
6161
62- size_t file_incremental_reading (napi_env env, TransferContext &ctx, bool final_step )
62+ size_t file_incremental_reading (napi_env env, TransferContext &ctx)
6363{
6464 std::ifstream file (ctx.tmpPath , std::ios::in | std::ios::binary);
6565
@@ -68,51 +68,47 @@ size_t file_incremental_reading(napi_env env, TransferContext &ctx, bool final_s
6868 throw std::runtime_error (" Failed to open tmp file" );
6969 }
7070
71- file.clear ();
7271 file.seekg (0 , std::ios::end);
7372 size_t newSize = static_cast <size_t >(file.tellg ());
7473 size_t datasizeAvailableUnread = newSize - ctx.lastReadOffset ;
7574
76- try
75+ if (datasizeAvailableUnread > 0 )
7776 {
78- if (datasizeAvailableUnread > 0 )
79- {
80- std::vector<char > buffer (CHUNK_SIZE );
81- file.seekg (ctx.lastReadOffset );
82- file.read (buffer.data (), CHUNK_SIZE );
77+ std::vector<char > buffer (CHUNK_SIZE );
78+ file.seekg (ctx.lastReadOffset );
79+ file.read (buffer.data (), CHUNK_SIZE );
80+
81+ LARGE_INTEGER startingOffset, chunkBufferSize;
82+ startingOffset.QuadPart = ctx.lastReadOffset ;
83+ chunkBufferSize.QuadPart = min (datasizeAvailableUnread, CHUNK_SIZE );
8384
84- LARGE_INTEGER startingOffset, chunkBufferSize;
85- startingOffset.QuadPart = ctx.lastReadOffset ;
86- chunkBufferSize.QuadPart = min (datasizeAvailableUnread, CHUNK_SIZE );
85+ HRESULT hr = transfer_data (
86+ ctx.connectionKey ,
87+ ctx.transferKey ,
88+ buffer.data (),
89+ startingOffset,
90+ chunkBufferSize,
91+ STATUS_SUCCESS );
8792
93+ if (FAILED (hr))
94+ {
8895 transfer_data (
8996 ctx.connectionKey ,
9097 ctx.transferKey ,
91- buffer.data (),
92- startingOffset,
93- chunkBufferSize,
94- STATUS_SUCCESS );
95-
96- ctx.lastReadOffset += chunkBufferSize.QuadPart ;
98+ nullptr ,
99+ ctx.requiredOffset ,
100+ ctx.requiredLength ,
101+ STATUS_UNSUCCESSFUL );
97102
98- UINT64 totalSize = static_cast <UINT64 >(ctx.fileSize .QuadPart );
99- Utilities::ApplyTransferStateToFile (ctx.path , ctx.callbackInfo , totalSize, ctx.lastReadOffset );
103+ winrt::throw_hresult (hr);
100104 }
101- }
102- catch (...)
103- {
104- Logger::getInstance ().log (" Excepción en file_incremental_reading." , LogLevel::ERROR );
105- ctx.loadFinished = true ;
106- transfer_data (
107- ctx.connectionKey ,
108- ctx.transferKey ,
109- nullptr ,
110- ctx.requiredOffset ,
111- ctx.requiredLength ,
112- STATUS_UNSUCCESSFUL );
105+
106+ ctx.lastReadOffset += chunkBufferSize.QuadPart ;
107+
108+ UINT64 totalSize = static_cast <UINT64 >(ctx.fileSize .QuadPart );
109+ Utilities::ApplyTransferStateToFile (ctx.path , ctx.callbackInfo , totalSize, ctx.lastReadOffset );
113110 }
114111
115- file.close ();
116112 ctx.lastSize = newSize;
117113 return ctx.lastReadOffset ;
118114}
@@ -144,7 +140,7 @@ napi_value response_callback_fn_fetch_data(napi_env env, napi_callback_info info
144140
145141 ctx->tmpPath = tmpPath;
146142
147- ctx->lastReadOffset = file_incremental_reading (env, *ctx, false );
143+ ctx->lastReadOffset = file_incremental_reading (env, *ctx);
148144
149145 if (ctx->lastReadOffset == (size_t )ctx->fileSize .QuadPart )
150146 {
0 commit comments