11#include " stdafx.h"
22#include < Callbacks.h>
3- #include < Logger.h>
43#include < cfapi.h>
54#include < condition_variable>
65#include < iostream>
76#include < mutex>
87#include < filesystem>
8+ #include < TransferContext.h>
99
1010napi_threadsafe_function g_cancel_fetch_data_threadsafe_callback = nullptr ;
1111
12- struct CallbackContext {
13- std::mutex mtx;
14- std::condition_variable cv;
15- bool ready = false ;
16- };
17-
18- struct CancelFetchDataArgs {
19- std::wstring fileIdentityArg;
20- CallbackContext* context;
21-
22- CancelFetchDataArgs (const std::wstring& fileId, CallbackContext* ctx)
23- : fileIdentityArg(fileId), context(ctx) {}
24- };
25-
2612void notify_cancel_fetch_data_call (napi_env env, napi_value js_callback, void *context, void *data)
2713{
28- CancelFetchDataArgs *args = static_cast <CancelFetchDataArgs *>(data);
29- std::u16string u16_fileIdentity (args->fileIdentityArg .begin (), args->fileIdentityArg .end ());
14+ std::wstring *path = static_cast <std::wstring *>(data);
3015
31- napi_value js_string ;
32- napi_create_string_utf16 (env, u16_fileIdentity. c_str (), u16_fileIdentity. size (), &js_string );
16+ napi_value js_path ;
17+ napi_create_string_utf16 (env, ( char16_t *)path-> c_str (), path-> length (), &js_path );
3318
34- napi_value args_to_js_callback_cancel_fetch [1 ] = {js_string };
19+ napi_value args_to_js_callback [1 ] = {js_path };
3520
3621 napi_value undefined;
3722 napi_get_undefined (env, &undefined);
38- napi_value result ;
23+ napi_call_function (env, undefined, js_callback, 1 , args_to_js_callback, nullptr ) ;
3924
40- Logger::getInstance ().log (" Executed to call JS function in cancelFetchCallback." , LogLevel::ERROR);
41- napi_status status = napi_call_function (env, undefined, js_callback, 1 , args_to_js_callback_cancel_fetch, &result);
42- if (status != napi_ok)
43- {
44- fprintf (stderr, " Failed to call JS function.\n " );
45- Logger::getInstance ().log (" Failed to call JS function in cancelFetchCallback." , LogLevel::ERROR);
46- }
25+ delete path;
26+ }
27+
28+ void CALLBACK cancel_fetch_data_callback_wrapper (_In_ CONST CF_CALLBACK_INFO *callbackInfo, _In_ CONST CF_CALLBACK_PARAMETERS *callbackParameters)
29+ {
30+ wprintf (L" ConnectionKey: %lld, TransferKey: %lld\n " , callbackInfo->ConnectionKey , callbackInfo->TransferKey .QuadPart );
31+
32+ auto ctx = GetTransferContext (callbackInfo->TransferKey );
33+
34+ if (!ctx)
35+ return ;
36+
37+ std::wstring *path = new std::wstring (ctx->path );
38+ wprintf (L" Cancel fetch data path: %s\n " , path->c_str ());
4739
4840 {
49- std::lock_guard<std::mutex> lock (args-> context ->mtx );
50- args-> context ->ready = true ;
41+ std::lock_guard<std::mutex> lock (ctx ->mtx );
42+ ctx ->ready = true ;
5143 }
5244
53- args->context ->cv .notify_one ();
54- delete args;
45+ ctx->cv .notify_one ();
46+
47+ napi_call_threadsafe_function (g_cancel_fetch_data_threadsafe_callback, path, napi_tsfn_blocking);
5548}
5649
5750void register_threadsafe_cancel_fetch_data_callback (const std::string &resource_name, napi_env env, InputSyncCallbacks input)
@@ -61,11 +54,11 @@ void register_threadsafe_cancel_fetch_data_callback(const std::string &resource_
6154 napi_value resource_name_value;
6255 napi_create_string_utf16 (env, converted_resource_name.c_str (), NAPI_AUTO_LENGTH, &resource_name_value);
6356
64- napi_threadsafe_function tsfn_cancel_fetch_data;
6557 napi_value cancel_fetch_data_value;
6658 napi_get_reference_value (env, input.cancel_fetch_data_callback_ref , &cancel_fetch_data_value);
6759
68- napi_status status = napi_create_threadsafe_function (
60+ napi_threadsafe_function tsfn_cancel_fetch_data;
61+ napi_create_threadsafe_function (
6962 env,
7063 cancel_fetch_data_value,
7164 NULL ,
@@ -78,46 +71,5 @@ void register_threadsafe_cancel_fetch_data_callback(const std::string &resource_
7871 notify_cancel_fetch_data_call,
7972 &tsfn_cancel_fetch_data);
8073
81- if (status != napi_ok)
82- {
83- napi_throw_error (env, nullptr , " Failed to create cancel fetch data threadsafe function" );
84- return ;
85- }
86-
8774 g_cancel_fetch_data_threadsafe_callback = tsfn_cancel_fetch_data;
88- }
89-
90- void CALLBACK cancel_fetch_data_callback_wrapper (
91- _In_ CONST CF_CALLBACK_INFO *callbackInfo,
92- _In_ CONST CF_CALLBACK_PARAMETERS *callbackParameters)
93- {
94- printf (" cancel_fetch_data_callback_wrapper called\n " );
95-
96- LPCVOID fileIdentity = callbackInfo->FileIdentity ;
97- DWORD fileIdentityLength = callbackInfo->FileIdentityLength ;
98-
99- const wchar_t *wchar_ptr = static_cast <const wchar_t *>(fileIdentity);
100- std::wstring fileIdentityStr (wchar_ptr, fileIdentityLength / sizeof (wchar_t ));
101-
102- if (g_cancel_fetch_data_threadsafe_callback == nullptr )
103- {
104- wprintf (L" Callback fetch_data_callback_wrapper called but g_fetch_data_threadsafe_callback is null\n " );
105- return ;
106- }
107-
108- CallbackContext context;
109- CancelFetchDataArgs *args = new CancelFetchDataArgs (fileIdentityStr, &context);
110-
111- napi_call_threadsafe_function (g_cancel_fetch_data_threadsafe_callback, args, napi_tsfn_blocking);
112-
113- {
114- std::unique_lock<std::mutex> lock (context.mtx );
115- auto timeout = std::chrono::seconds (30 );
116-
117- if (context.cv .wait_for (lock, timeout, [&context] { return context.ready ; })) {
118- wprintf (L" Cancel fetch completed\n " );
119- } else {
120- wprintf (L" Cancel fetch timed out\n " );
121- }
122- }
123- }
75+ }
0 commit comments