|
| 1 | +// |
| 2 | +// Copyright © 2023 osy. All rights reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +#import "UTMQemuGuestAgent.h" |
| 18 | +#import "UTMQemuManager-Protected.h" |
| 19 | +#import "qga-qapi-commands.h" |
| 20 | + |
| 21 | +extern NSString *const kUTMErrorDomain; |
| 22 | + |
| 23 | +@interface UTMQemuGuestAgent () |
| 24 | + |
| 25 | +@property (nonatomic) BOOL isGuestAgentResponsive; |
| 26 | +@property (nonatomic, readwrite) BOOL shouldSynchronizeParser; |
| 27 | +@property (nonatomic) dispatch_queue_t guestAgentQueue; |
| 28 | + |
| 29 | +@end |
| 30 | + |
| 31 | +@implementation UTMQemuGuestAgent |
| 32 | + |
| 33 | +- (NSInteger)timeoutSeconds { |
| 34 | + if (self.isGuestAgentResponsive) { |
| 35 | + return 10; |
| 36 | + } else { |
| 37 | + return 1; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +- (instancetype)initWithPort:(CSPort *)port { |
| 42 | + if (self = [super initWithPort:port]) { |
| 43 | + dispatch_queue_attr_t attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, QOS_MIN_RELATIVE_PRIORITY); |
| 44 | + self.guestAgentQueue = dispatch_queue_create("QEMU Guest Agent Server", attr); |
| 45 | + } |
| 46 | + return self; |
| 47 | +} |
| 48 | + |
| 49 | +- (void)jsonStream:(UTMJSONStream *)stream seenError:(NSError *)error { |
| 50 | + self.isGuestAgentResponsive = NO; |
| 51 | + [super jsonStream:stream seenError:error]; |
| 52 | +} |
| 53 | + |
| 54 | +- (void)synchronizeWithCompletion:(void (^ _Nullable)(NSError * _Nullable))completion { |
| 55 | + self.isGuestAgentResponsive = NO; |
| 56 | + dispatch_async(self.guestAgentQueue, ^{ |
| 57 | + Error *qerr = NULL; |
| 58 | + int64_t random = g_random_int(); |
| 59 | + int64_t response = 0; |
| 60 | + self.shouldSynchronizeParser = YES; |
| 61 | + response = qmp_guest_sync_delimited(random, &qerr, (__bridge void *)self); |
| 62 | + self.shouldSynchronizeParser = NO; |
| 63 | + if (qerr) { |
| 64 | + if (completion) { |
| 65 | + completion([self errorForQerror:qerr]); |
| 66 | + } |
| 67 | + return; |
| 68 | + } |
| 69 | + if (response != random) { |
| 70 | + if (completion) { |
| 71 | + completion([NSError errorWithDomain:kUTMErrorDomain code:-1 userInfo:@{NSLocalizedDescriptionKey: NSLocalizedString(@"Mismatched id from guest-sync-delimited.", "UTMQemuGuestAgent")}]); |
| 72 | + } |
| 73 | + return; |
| 74 | + } |
| 75 | + self.isGuestAgentResponsive = YES; |
| 76 | + if (completion) { |
| 77 | + completion(nil); |
| 78 | + } |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +- (void)_withSynchronizeBlock:(NSError * _Nullable (^)(void))block withCompletion:(void (^ _Nullable)(NSError * _Nullable))completion { |
| 83 | + dispatch_async(self.guestAgentQueue, ^{ |
| 84 | + if (!self.isGuestAgentResponsive) { |
| 85 | + [self synchronizeWithCompletion:^(NSError *error) { |
| 86 | + if (error) { |
| 87 | + if (completion) { |
| 88 | + completion(error); |
| 89 | + } |
| 90 | + } else { |
| 91 | + NSError *error = block(); |
| 92 | + if (completion) { |
| 93 | + completion(error); |
| 94 | + } |
| 95 | + } |
| 96 | + }]; |
| 97 | + } else { |
| 98 | + NSError *error = block(); |
| 99 | + if (completion) { |
| 100 | + completion(error); |
| 101 | + } |
| 102 | + } |
| 103 | + }); |
| 104 | +} |
| 105 | + |
| 106 | +@end |
0 commit comments