Skip to content

Commit 1110296

Browse files
authored
Merge pull request #57 from telerik/fatme/await-notification
fix: add timeout when awaiting notification response
2 parents e221d70 + 08df456 commit 1110296

File tree

5 files changed

+28
-13
lines changed

5 files changed

+28
-13
lines changed

IOSDeviceLib/Declarations.h

+1
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ extern "C"
267267
{
268268
CFSocketNativeHandle AMDServiceConnectionGetSocket(ServiceConnRef con);
269269
long AMDServiceConnectionReceive(ServiceConnRef, void *, long);
270+
void AMDServiceConnectionInvalidate(ServiceConnRef);
270271
long AMDServiceConnectionSendMessage(ServiceConnRef serviceConnection, CFDictionaryRef message, CFPropertyListFormat format);
271272
unsigned AMDeviceSecureStartService(AMDeviceRef device, CFStringRef service_name, unsigned int *unknown, ServiceConnRef * handle);
272273
unsigned AMDeviceNotificationSubscribe(void(*f)(const DevicePointer*), long, long, long, HANDLE*);

IOSDeviceLib/IOSDeviceLib.cpp

+5-10
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,6 @@ void cleanup_file_resources(const std::string& device_identifier, const std::str
210210
AFCConnectionClose(afc_connection_to_close);
211211
devices[device_identifier].apps_cache.erase(application_identifier);
212212
}
213-
214-
if (devices[device_identifier].services.count(kHouseArrest))
215-
{
216-
devices[device_identifier].services.erase(kHouseArrest);
217-
}
218213
}
219214

220215
void cleanup_file_resources(const std::string& device_identifier)
@@ -982,13 +977,13 @@ void get_application_infos(std::string device_identifier, std::string method_id)
982977

983978
CFStringRef cf_app_type_value = create_CFString("User");
984979
const void *client_opts_values_arr[] = { cf_app_type_value, cf_return_attributes_array };
985-
CFDictionaryRef clinet_opts_dict = CFDictionaryCreate(NULL, client_opts_keys_arr, client_opts_values_arr, 2, NULL, NULL);
980+
CFDictionaryRef client_opts_dict = CFDictionaryCreate(NULL, client_opts_keys_arr, client_opts_values_arr, 2, NULL, NULL);
986981

987982
CFStringRef cf_command_key = create_CFString("Command");
988983
CFStringRef cf_client_options_key = create_CFString("ClientOptions");
989984
const void *keys_arr[] = { cf_command_key, cf_client_options_key };
990985
CFStringRef cf_command_value = create_CFString("Browse");
991-
const void *values_arr[] = { cf_command_value, clinet_opts_dict };
986+
const void *values_arr[] = { cf_command_value, client_opts_dict };
992987
CFDictionaryRef dict_command = CFDictionaryCreate(NULL, keys_arr, values_arr, 2, NULL, NULL);
993988

994989
send_con_message(serviceInfo.connection, dict_command);
@@ -998,7 +993,7 @@ void get_application_infos(std::string device_identifier, std::string method_id)
998993
CFRelease(cf_app_type_key);
999994
CFRelease(cf_return_attrs_key);
1000995
CFRelease(cf_app_type_value);
1001-
CFRelease(clinet_opts_dict);
996+
CFRelease(client_opts_dict);
1002997
CFRelease(cf_command_key);
1003998
CFRelease(cf_client_options_key);
1004999
CFRelease(cf_command_value);
@@ -1007,7 +1002,7 @@ void get_application_infos(std::string device_identifier, std::string method_id)
10071002
std::vector<json> livesync_app_infos;
10081003
while (true)
10091004
{
1010-
std::map<std::string, boost::any> dict = receive_con_message(serviceInfo.connection);
1005+
std::map<std::string, boost::any> dict = receive_con_message(serviceInfo.connection, device_identifier, method_id, 10);
10111006
PRINT_ERROR_AND_RETURN_IF_FAILED_RESULT(dict.count(kErrorKey), boost::any_cast<std::string>(dict[kErrorKey]).c_str(), device_identifier, method_id);
10121007
if (dict.empty() || (dict.count(kStatusKey) && has_complete_status(dict)))
10131008
{
@@ -1181,7 +1176,7 @@ void await_notification_response(std::string device_identifier, AwaitNotificatio
11811176
PRINT_ERROR_AND_RETURN_IF_FAILED_RESULT(connection == nullptr, invalid_connection_error_message.c_str(), device_identifier, method_id);
11821177

11831178
ServiceInfo currentNotificationProxy = devices[device_identifier].services[kNotificationProxy];
1184-
std::map<std::string, boost::any> response = receive_con_message(connection);
1179+
std::map<std::string, boost::any> response = receive_con_message(connection, device_identifier, method_id, await_notification_response_info.timeout);
11851180
if (response.size())
11861181
{
11871182
PRINT_ERROR_AND_RETURN_IF_FAILED_RESULT(response.count(kErrorKey), boost::any_cast<std::string>(response[kErrorKey]).c_str(), device_identifier, method_id);

IOSDeviceLib/SocketHelper.cpp

+20-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,28 @@
77
#include "PlistCpp/Plist.hpp"
88
#include "PlistCpp/PlistDate.hpp"
99

10+
void setTimeout(std::function<void()> operation, int timeout) {
11+
std::thread([=]() {
12+
std::this_thread::sleep_for(std::chrono::milliseconds(timeout * 1000));
13+
operation();
14+
}).detach();
15+
}
16+
1017
std::mutex receive_con_message_mutex;
11-
std::map<std::string, boost::any> receive_con_message(ServiceConnRef con)
18+
std::map<std::string, boost::any> receive_con_message(ServiceConnRef con, std::string device_identifier, std::string method_id, int timeout)
1219
{
1320
receive_con_message_mutex.lock();
21+
22+
bool isSuccessful = false;
23+
24+
if (timeout > 0) {
25+
setTimeout([=]() {
26+
if (!isSuccessful) {
27+
AMDServiceConnectionInvalidate(con);
28+
}
29+
}, timeout);
30+
}
31+
1432
std::map<std::string, boost::any> dict;
1533
char *buffer = new char[4];
1634
int bytes_read = AMDServiceConnectionReceive(con, buffer, 4);
@@ -23,6 +41,7 @@ std::map<std::string, boost::any> receive_con_message(ServiceConnRef con)
2341
if (bytes_read > 0)
2442
{
2543
Plist::readPlist(buffer, res, dict);
44+
isSuccessful = true;
2645
}
2746
}
2847

IOSDeviceLib/SocketHelper.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ LengthEncodedMessage get_message_with_encoded_length(const char* message, long l
4141
int send_message(const char* message, SOCKET socket, long long length = -1);
4242
int send_message(std::string message, SOCKET socket, long long length = -1);
4343
long send_con_message(HANDLE* serviceConnection, CFDictionaryRef message);
44-
std::map<std::string, boost::any> receive_con_message(HANDLE* con);
44+
std::map<std::string, boost::any> receive_con_message(HANDLE* con, std::string device_identifier, std::string method_id, int timeout);
4545
std::map<std::string, boost::any> receive_message(SOCKET socket, int timeout = 5);
4646
std::string receive_message_raw(SOCKET socket, int size = 1000);
4747

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ios-device-lib",
3-
"version": "0.7.0",
3+
"version": "0.7.1",
44
"description": "",
55
"types": "./typings/ios-device-lib.d.ts",
66
"main": "index.js",

0 commit comments

Comments
 (0)