Respond to CR.

pull/1/head
Matthew Chen 7 years ago
parent dc36ae134d
commit 0a41684374

@ -24,7 +24,7 @@ typedef void (^TSSocketMessageFailure)(NSInteger statusCode, NSError *error);
@interface TSSocketManager : NSObject <SRWebSocketDelegate> @interface TSSocketManager : NSObject <SRWebSocketDelegate>
@property (atomic, readonly) SocketManagerState state; @property (nonatomic, readonly) SocketManagerState state;
@property (atomic, readonly) BOOL canMakeRequests; @property (atomic, readonly) BOOL canMakeRequests;
+ (instancetype)sharedManager; + (instancetype)sharedManager;

@ -44,11 +44,13 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
@interface TSSocketMessage : NSObject @interface TSSocketMessage : NSObject
@property (nonatomic) UInt64 requestId; @property (nonatomic, readonly) UInt64 requestId;
@property (nonatomic, nullable) TSSocketMessageSuccess success; @property (nonatomic, nullable) TSSocketMessageSuccess success;
@property (nonatomic, nullable) TSSocketMessageFailure failure; @property (nonatomic, nullable) TSSocketMessageFailure failure;
@property (nonatomic) BOOL hasCompleted; @property (nonatomic) BOOL hasCompleted;
@property (nonatomic) OWSBackgroundTask *backgroundTask; @property (nonatomic, readonly) OWSBackgroundTask *backgroundTask;
- (instancetype)init NS_UNAVAILABLE;
@end @end
@ -56,6 +58,23 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
@implementation TSSocketMessage @implementation TSSocketMessage
- (instancetype)initWithRequestId:(UInt64)requestId
success:(TSSocketMessageSuccess)success
failure:(TSSocketMessageFailure)failure
{
if (self = [super init]) {
OWSAssert(success);
OWSAssert(failure);
_requestId = requestId;
_success = success;
_failure = failure;
_backgroundTask = [OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__];
}
return self;
}
- (void)didSucceedWithResponseObject:(id _Nullable)responseObject - (void)didSucceedWithResponseObject:(id _Nullable)responseObject
{ {
@synchronized(self) @synchronized(self)
@ -139,8 +158,8 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
// websocket's actual state, so we're defensive and distrustful of // websocket's actual state, so we're defensive and distrustful of
// this property. // this property.
// //
// We only ever mutate this state on the main thread. // We only ever access this state on the main thread.
@property (atomic) SocketManagerState state; @property (nonatomic) SocketManagerState state;
#pragma mark - #pragma mark -
@ -170,14 +189,14 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
// This property should only be accessed while synchronized on the socket manager. // This property should only be accessed while synchronized on the socket manager.
@property (nonatomic, readonly) NSMutableDictionary<NSNumber *, TSSocketMessage *> *socketMessageMap; @property (nonatomic, readonly) NSMutableDictionary<NSNumber *, TSSocketMessage *> *socketMessageMap;
@property (atomic) BOOL canMakeRequests;
@end @end
#pragma mark - #pragma mark -
@implementation TSSocketManager @implementation TSSocketManager
@synthesize state = _state;
- (instancetype)init - (instancetype)init
{ {
self = [super init]; self = [super init];
@ -308,19 +327,13 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
{ {
OWSAssertIsOnMainThread(); OWSAssertIsOnMainThread();
SocketManagerState oldState;
@synchronized(self)
{
oldState = _state;
}
// If this state update is redundant, verify that // If this state update is redundant, verify that
// class state and socket state are aligned. // class state and socket state are aligned.
// //
// Note: it's not safe to check the socket's readyState here as // Note: it's not safe to check the socket's readyState here as
// it may have been just updated on another thread. If so, // it may have been just updated on another thread. If so,
// we'll learn of that state change soon. // we'll learn of that state change soon.
if (oldState == state) { if (_state == state) {
switch (state) { switch (state) {
case SocketManagerStateClosed: case SocketManagerStateClosed:
OWSAssert(!self.websocket); OWSAssert(!self.websocket);
@ -337,7 +350,7 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
DDLogWarn(@"%@ Socket state: %@ -> %@", DDLogWarn(@"%@ Socket state: %@ -> %@",
self.logTag, self.logTag,
[self stringFromSocketManagerState:oldState], [self stringFromSocketManagerState:_state],
[self stringFromSocketManagerState:state]); [self stringFromSocketManagerState:state]);
// If this state update is _not_ redundant, // If this state update is _not_ redundant,
@ -381,37 +394,21 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
// [SRWebSocket open] could hypothetically call a delegate method (e.g. if // [SRWebSocket open] could hypothetically call a delegate method (e.g. if
// the socket failed immediately for some reason), so we update the state // the socket failed immediately for some reason), so we update the state
// _before_ calling it, not after. // _before_ calling it, not after.
@synchronized(self) _state = state;
{ _canMakeRequests = state == SocketManagerStateOpen;
_state = state;
}
[socket open]; [socket open];
[self failAllPendingSocketMessagesIfNecessary]; [self failAllPendingSocketMessagesIfNecessary];
return; return;
} }
} }
@synchronized(self) _state = state;
{ _canMakeRequests = state == SocketManagerStateOpen;
_state = state;
}
[self failAllPendingSocketMessagesIfNecessary]; [self failAllPendingSocketMessagesIfNecessary];
[self notifyStatusChange]; [self notifyStatusChange];
} }
- (SocketManagerState)state
{
@synchronized(self)
{
return _state;
}
}
- (BOOL)canMakeRequests
{
return self.state == SocketManagerStateOpen;
}
- (void)notifyStatusChange - (void)notifyStatusChange
{ {
[[NSNotificationCenter defaultCenter] postNotificationNameAsync:kNSNotification_SocketManagerStateDidChange [[NSNotificationCenter defaultCenter] postNotificationNameAsync:kNSNotification_SocketManagerStateDidChange
@ -451,15 +448,11 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
OWSAssert(success); OWSAssert(success);
OWSAssert(failure); OWSAssert(failure);
TSSocketMessage *socketMessage = [TSSocketMessage new]; TSSocketMessage *socketMessage =
socketMessage.success = success; [[TSSocketMessage alloc] initWithRequestId:[Cryptography randomUInt64] success:success failure:failure];
socketMessage.failure = failure;
socketMessage.backgroundTask = [OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__];
@synchronized(self) @synchronized(self)
{ {
socketMessage.requestId = [Cryptography randomUInt64];
self.socketMessageMap[@(socketMessage.requestId)] = socketMessage; self.socketMessageMap[@(socketMessage.requestId)] = socketMessage;
} }
@ -524,9 +517,9 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
requestPath, requestPath,
jsonData.length); jsonData.length);
// Ensure requests "timeout" after 10 seconds. const int64_t kSocketTimeoutSeconds = 10;
__weak TSSocketMessage *weakSocketMessage = socketMessage; __weak TSSocketMessage *weakSocketMessage = socketMessage;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(30 * NSEC_PER_SEC)), dispatch_after(dispatch_time(DISPATCH_TIME_NOW, kSocketTimeoutSeconds * NSEC_PER_SEC),
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{ ^{
[weakSocketMessage didFailBeforeSending]; [weakSocketMessage didFailBeforeSending];

Loading…
Cancel
Save