|
21 | 21 | #include "Printing.h"
|
22 | 22 | #include "CommonFunctions.h"
|
23 | 23 | #include "ServerHelper.h"
|
| 24 | +#include "SetTimeout.h" |
24 | 25 |
|
25 | 26 | #ifdef _WIN32
|
26 | 27 | #pragma region Dll_Variable_Definitions
|
@@ -444,6 +445,7 @@ ServiceInfo start_secure_service(std::string device_identifier, const char* serv
|
444 | 445 | serviceInfoResult.socket = socket;
|
445 | 446 | serviceInfoResult.connection = connection;
|
446 | 447 | serviceInfoResult.connection_id = nextServiceConnectionId;
|
| 448 | + serviceInfoResult.service_name = service_name; |
447 | 449 | serviceConnections[nextServiceConnectionId] = connection;
|
448 | 450 | nextServiceConnectionId++;
|
449 | 451 |
|
@@ -640,6 +642,53 @@ void perform_detached_operation(void(*operation)(std::string, std::string, std::
|
640 | 642 | std::thread([operation, first_arg, device_identifier, method_id]() { operation(first_arg, device_identifier, method_id); }).detach();
|
641 | 643 | }
|
642 | 644 |
|
| 645 | +std::mutex clean_con_resources_mutex; |
| 646 | +void clean_con_resources(void* data) |
| 647 | +{ |
| 648 | + clean_con_resources_mutex.lock(); |
| 649 | + ConnectionMessageData* connectionMessageData = reinterpret_cast<ConnectionMessageData*>(data); |
| 650 | + ServiceConnRef conn = connectionMessageData->conn; |
| 651 | + devices[connectionMessageData->device_identifier].services.erase(connectionMessageData->service_name.c_str()); |
| 652 | + AMDServiceConnectionInvalidate(conn); |
| 653 | + clean_con_resources_mutex.unlock(); |
| 654 | +} |
| 655 | + |
| 656 | +std::mutex receive_con_message_mutex; |
| 657 | +std::map<std::string, boost::any> receive_con_message(ConnectionMessageData data) |
| 658 | +{ |
| 659 | + receive_con_message_mutex.lock(); |
| 660 | + |
| 661 | + ServiceConnRef conn = data.conn; |
| 662 | + TimeoutOutputData timeoutOutputData = setTimeout(data.timeout, &data, clean_con_resources); |
| 663 | + |
| 664 | + std::map<std::string, boost::any> dict; |
| 665 | + char *buffer = new char[4]; |
| 666 | + int bytes_read = AMDServiceConnectionReceive(conn, buffer, 4); |
| 667 | + if (bytes_read > 0) |
| 668 | + { |
| 669 | + unsigned long res = ntohl(*((unsigned long*)buffer)); |
| 670 | + delete[] buffer; |
| 671 | + buffer = new char[res]; |
| 672 | + bytes_read = AMDServiceConnectionReceive(conn, buffer, res); |
| 673 | + if (bytes_read > 0) |
| 674 | + { |
| 675 | + Plist::readPlist(buffer, res, dict); |
| 676 | + clearTimeout(timeoutOutputData); |
| 677 | + } |
| 678 | + } else { |
| 679 | + clearTimeout(timeoutOutputData); |
| 680 | + } |
| 681 | + |
| 682 | + delete[] buffer; |
| 683 | + receive_con_message_mutex.unlock(); |
| 684 | + return dict; |
| 685 | +} |
| 686 | + |
| 687 | +long send_con_message(ServiceConnRef serviceConnection, CFDictionaryRef message) |
| 688 | +{ |
| 689 | + return AMDServiceConnectionSendMessage(serviceConnection, message, kCFPropertyListXMLFormat_v1_0); |
| 690 | +} |
| 691 | + |
643 | 692 | void read_dir(AFCConnectionRef afc_conn_p, const char* dir, json &files, std::stringstream &errors, std::string method_id, std::string device_identifier)
|
644 | 693 | {
|
645 | 694 | char *dir_ent;
|
@@ -1003,7 +1052,8 @@ void get_application_infos(std::string device_identifier, std::string method_id)
|
1003 | 1052 | std::vector<json> livesync_app_infos;
|
1004 | 1053 | while (true)
|
1005 | 1054 | {
|
1006 |
| - std::map<std::string, boost::any> dict = receive_con_message(serviceInfo.connection, device_identifier, method_id, 0); |
| 1055 | + ConnectionMessageData connectionMessageData = { serviceInfo.connection, device_identifier, method_id, kInstallationProxy, 10 }; |
| 1056 | + std::map<std::string, boost::any> dict = receive_con_message(connectionMessageData); |
1007 | 1057 | PRINT_ERROR_AND_RETURN_IF_FAILED_RESULT(dict.count(kErrorKey), boost::any_cast<std::string>(dict[kErrorKey]).c_str(), device_identifier, method_id);
|
1008 | 1058 | if (dict.empty() || (dict.count(kStatusKey) && has_complete_status(dict)))
|
1009 | 1059 | {
|
@@ -1177,7 +1227,8 @@ void await_notification_response(std::string device_identifier, AwaitNotificatio
|
1177 | 1227 | PRINT_ERROR_AND_RETURN_IF_FAILED_RESULT(connection == nullptr, invalid_connection_error_message.c_str(), device_identifier, method_id);
|
1178 | 1228 |
|
1179 | 1229 | ServiceInfo currentNotificationProxy = devices[device_identifier].services[kNotificationProxy];
|
1180 |
| - std::map<std::string, boost::any> response = receive_con_message(connection, device_identifier, method_id, await_notification_response_info.timeout); |
| 1230 | + ConnectionMessageData connectionMessageData = { connection, device_identifier, method_id, kNotificationProxy, await_notification_response_info.timeout }; |
| 1231 | + std::map<std::string, boost::any> response = receive_con_message(connectionMessageData); |
1181 | 1232 | if (response.size())
|
1182 | 1233 | {
|
1183 | 1234 | PRINT_ERROR_AND_RETURN_IF_FAILED_RESULT(response.count(kErrorKey), boost::any_cast<std::string>(response[kErrorKey]).c_str(), device_identifier, method_id);
|
|
0 commit comments