Skip to content

Commit 7090957

Browse files
committed
fix: add timeout when awaiting notification response
All socket connections were changed and now are started in ssl mode. This changes the way messages are sent and received from sockets. As a result, `timeout` option is not anymore respected when receiving messages from socket. However, the messages now are received only via methods from mobile device lib. As there isn't a built-in method in mobile device that can work with timeout, we implemented timeout mechanism using threads. The `await_notification_response` operation is started in separate thread and after the provided timeout, the started thread is terminated.
1 parent e221d70 commit 7090957

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

IOSDeviceLib/Declarations.h

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ struct AwaitNotificationResponseInfo
3131
std::string response_command_type;
3232
std::string response_property_name;
3333
SOCKET socket;
34-
int timeout;
3534
};
3635

3736
struct DeviceApplication {

IOSDeviceLib/IOSDeviceLib.cpp

+31-5
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,30 @@ void install_application(std::string install_path, std::string device_identifier
631631
cleanup_file_resources(device_identifier);
632632
}
633633

634+
void setTimeout(std::function<void()> operation, int timeout) {
635+
std::thread([=]() {
636+
std::this_thread::sleep_for(std::chrono::milliseconds(timeout * 1000));
637+
operation();
638+
}).detach();
639+
}
640+
641+
void* handleThreadOperationEntry(void *data) {
642+
auto operation = *(std::function<void ()> *)(data);
643+
operation();
644+
return nullptr;
645+
}
646+
647+
void perform_detached_operation_with_timeout(std::string device_identifier, std::string method_id, int timeout, std::function<void ()> operation) {
648+
pthread_t thread;
649+
pthread_create(&thread, nullptr, handleThreadOperationEntry, &operation);
650+
651+
setTimeout([=]() {
652+
pthread_cancel(thread);
653+
pthread_detach(thread);
654+
print_error("Operation timeouted.", device_identifier, method_id);
655+
}, timeout);
656+
}
657+
634658
void perform_detached_operation(void(*operation)(std::string, std::string), std::string arg, std::string method_id)
635659
{
636660
std::thread([operation, arg, method_id]() { operation(arg, method_id); }).detach();
@@ -1179,8 +1203,7 @@ void await_notification_response(std::string device_identifier, AwaitNotificatio
11791203
ServiceConnRef connection = serviceConnections[(int)await_notification_response_info.socket];
11801204
std::string invalid_connection_error_message = "Invalid socket: " + std::to_string(await_notification_response_info.socket);
11811205
PRINT_ERROR_AND_RETURN_IF_FAILED_RESULT(connection == nullptr, invalid_connection_error_message.c_str(), device_identifier, method_id);
1182-
1183-
ServiceInfo currentNotificationProxy = devices[device_identifier].services[kNotificationProxy];
1206+
11841207
std::map<std::string, boost::any> response = receive_con_message(connection);
11851208
if (response.size())
11861209
{
@@ -1195,7 +1218,7 @@ void await_notification_response(std::string device_identifier, AwaitNotificatio
11951218
}
11961219
else
11971220
{
1198-
print_error("ObserveNotification timeout.", device_identifier, method_id);
1221+
print_error("ObserveNotification failed.", device_identifier, method_id);
11991222
}
12001223
}
12011224

@@ -1527,8 +1550,11 @@ int main()
15271550
SOCKET socket = arg.value(kSocket, 0);
15281551
int timeout = arg.value(kTimeout, -1);
15291552

1530-
AwaitNotificationResponseInfo await_notification_response_info({ response_command_type, response_key_name, socket, timeout });
1531-
std::thread([=]() { await_notification_response(device_identifier, await_notification_response_info, method_id); }).detach();
1553+
AwaitNotificationResponseInfo await_notification_response_info({ response_command_type, response_key_name, socket });
1554+
1555+
perform_detached_operation_with_timeout(device_identifier, method_id, timeout, [=]() {
1556+
await_notification_response(device_identifier, await_notification_response_info, method_id);
1557+
});
15321558
}
15331559
}
15341560
else if (method_name == "start")

0 commit comments

Comments
 (0)