XMPPReconnect.m 18 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
#import "XMPPReconnect.h"
#import "XMPPStream.h"
#import "XMPPLogging.h"
#import "NSXMLElement+XMPP.h"

#if ! __has_feature(objc_arc)
#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif

#define IMPOSSIBLE_REACHABILITY_FLAGS 0xFFFFFFFF

// Log levels: off, error, warn, info, verbose
#if DEBUG
  static const int xmppLogLevel = XMPP_LOG_LEVEL_WARN;
#else
  static const int xmppLogLevel = XMPP_LOG_LEVEL_WARN;
#endif

enum XMPPReconnectFlags
{
	kShouldReconnect   = 1 << 0,  // If set, disconnection was accidental, and autoReconnect may be used
	kMultipleChanges   = 1 << 1,  // If set, there have been reachability changes during a connection attempt
	kManuallyStarted   = 1 << 2,  // If set, we were started manually via manualStart method
	kQueryingDelegates = 1 << 3,  // If set, we are awaiting response(s) from the delegate(s)
};

enum XMPPReconnectConfig
{
	kAutoReconnect     = 1 << 0,  // If set, automatically attempts to reconnect after a disconnection
};

#if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_5 && !TARGET_OS_IPHONE
// SCNetworkConnectionFlags was renamed to SCNetworkReachabilityFlags in 10.6
typedef SCNetworkConnectionFlags SCNetworkReachabilityFlags;
#endif

@interface XMPPReconnect (PrivateAPI)

- (void)setupReconnectTimer;
- (void)teardownReconnectTimer;

- (void)setupNetworkMonitoring;
- (void)teardownNetworkMonitoring;

- (void)maybeAttemptReconnect;
- (void)maybeAttemptReconnectWithTicket:(int)ticket;
- (void)maybeAttemptReconnectWithReachabilityFlags:(SCNetworkReachabilityFlags)reachabilityFlags;

@end

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

@implementation XMPPReconnect

@dynamic    autoReconnect;
@synthesize reconnectDelay;
@synthesize reconnectTimerInterval;

- (id)init
{
	return [self initWithDispatchQueue:NULL];
}

- (id)initWithDispatchQueue:(dispatch_queue_t)queue
{
	if ((self = [super initWithDispatchQueue:queue]))
	{
		flags = 0;
		config = kAutoReconnect;
		
		reconnectDelay = DEFAULT_XMPP_RECONNECT_DELAY;
		reconnectTimerInterval = DEFAULT_XMPP_RECONNECT_TIMER_INTERVAL;
		
		reconnectTicket = 0;
		
		previousReachabilityFlags = IMPOSSIBLE_REACHABILITY_FLAGS;
	}
	return self;
}

- (void)dealloc
{
	dispatch_block_t block = ^{
		[self teardownReconnectTimer];
		[self teardownNetworkMonitoring];
	};
	
	if (dispatch_get_specific(moduleQueueTag))
		block();
	else
		dispatch_sync(moduleQueue, block);
	
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Configuration and Flags
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

- (BOOL)autoReconnect
{
	__block BOOL result = NO;
	
	dispatch_block_t block = ^{
		result = (config & kAutoReconnect) ? YES : NO;
	};
	
	if (dispatch_get_specific(moduleQueueTag))
		block();
	else
		dispatch_sync(moduleQueue, block);
	
	return result;
}

- (void)setAutoReconnect:(BOOL)flag
{
	dispatch_block_t block = ^{
		if (flag)
			config |= kAutoReconnect;
		else
			config &= ~kAutoReconnect;
	};
	
	if (dispatch_get_specific(moduleQueueTag))
		block();
	else
		dispatch_async(moduleQueue, block);
}

- (BOOL)shouldReconnect
{
	NSAssert(dispatch_get_specific(moduleQueueTag), @"Invoked private method outside moduleQueue");
	
	return (flags & kShouldReconnect) ? YES : NO;
}

- (void)setShouldReconnect:(BOOL)flag
{
	NSAssert(dispatch_get_specific(moduleQueueTag), @"Invoked private method outside moduleQueue");
	
	if (flag)
		flags |= kShouldReconnect;
	else
		flags &= ~kShouldReconnect;
}

- (BOOL)multipleReachabilityChanges
{
	NSAssert(dispatch_get_specific(moduleQueueTag), @"Invoked private method outside moduleQueue");
	
	return (flags & kMultipleChanges) ? YES : NO;
}

- (void)setMultipleReachabilityChanges:(BOOL)flag
{
	NSAssert(dispatch_get_specific(moduleQueueTag), @"Invoked private method outside moduleQueue");
	
	if (flag)
		flags |= kMultipleChanges;
	else
		flags &= ~kMultipleChanges;
}

- (BOOL)manuallyStarted
{
	NSAssert(dispatch_get_specific(moduleQueueTag), @"Invoked private method outside moduleQueue");
	
	return (flags & kManuallyStarted) ? YES : NO;
}

- (void)setManuallyStarted:(BOOL)flag
{
	NSAssert(dispatch_get_specific(moduleQueueTag), @"Invoked private method outside moduleQueue");
	
	if (flag)
		flags |= kManuallyStarted;
	else
		flags &= ~kManuallyStarted;
}

- (BOOL)queryingDelegates
{
	NSAssert(dispatch_get_specific(moduleQueueTag), @"Invoked private method outside moduleQueue");
	
	return (flags & kQueryingDelegates) ? YES : NO;
}

- (void)setQueryingDelegates:(BOOL)flag
{
	NSAssert(dispatch_get_specific(moduleQueueTag), @"Invoked private method outside moduleQueue");
	
	if (flag)
		flags |= kQueryingDelegates;
	else
		flags &= ~kQueryingDelegates;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Manual Manipulation
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

- (void)manualStart
{
	dispatch_block_t block = ^{ @autoreleasepool {
		
		if ([xmppStream isDisconnected] && [self manuallyStarted] == NO)
		{
			[self setManuallyStarted:YES];
			
			[self setupReconnectTimer];
			[self setupNetworkMonitoring];
		}
	}};
	
	if (dispatch_get_specific(moduleQueueTag))
		block();
	else
		dispatch_async(moduleQueue, block);
}

- (void)stop
{
	dispatch_block_t block = ^{ @autoreleasepool {
		
		// Clear all flags to disable any further reconnect attemts regardless of the state we're in.
		
		flags = 0;
		
		// Stop any planned reconnect attempts and stop monitoring the network.
		
		reconnectTicket++;
		
		[self teardownReconnectTimer];
		[self teardownNetworkMonitoring];
		
	}};
	
	if (dispatch_get_specific(moduleQueueTag))
		block();
	else
		dispatch_async(moduleQueue, block);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark XMPPStream Delegate
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

- (void)xmppStreamDidConnect:(XMPPStream *)sender
{
	// This method is executed on our moduleQueue.
	
	// The stream is up so we can stop our reconnect attempts now.
	// 
	// We essentially want to do the same thing as the stop method with one exception:
	// We do not want to clear the shouldReconnect flag.
	// 
	// Remember the shouldReconnect flag gets set upon authentication.
	// A combination of this flag and the autoReconnect flag controls the auto reconnect mechanism.
	// 
	// It is possible for us to get accidentally disconnected after
	// the stream opens but prior to authentication completing.
	// If this happens we still want to abide by the previous shouldReconnect setting.
	
	[self setMultipleReachabilityChanges:NO];
	[self setManuallyStarted:NO];
	
	reconnectTicket++;
	
	[self teardownReconnectTimer];
	[self teardownNetworkMonitoring];
}

- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
	// This method is executed on our moduleQueue.
	
	// We're now connected and properly authenticated.
	// Should we get accidentally disconnected we should automatically reconnect (if autoReconnect is set).
	[self setShouldReconnect:YES];
}

- (void)xmppStream:(XMPPStream *)sender didReceiveError:(NSXMLElement *)element
{
	// This method is executed on our moduleQueue.
	
	// <stream:error>
	//   <conflict xmlns="urn:ietf:params:xml:ns:xmpp-streams"/>
	//   <text xmlns="urn:ietf:params:xml:ns:xmpp-streams" xml:lang="">Replaced by new connection</text>
	// </stream:error>
	// 
	// If our connection ever gets replaced, we shouldn't attempt a reconnect,
	// because the user has logged in on another device.
	// If we still applied the reconnect logic,
	// the two devices may get into an infinite loop of kicking each other off the system.
	
	NSString *elementName = [element name];
	
	if ([elementName isEqualToString:@"stream:error"] || [elementName isEqualToString:@"error"])
	{
		NSXMLElement *conflict = [element elementForName:@"conflict" xmlns:@"urn:ietf:params:xml:ns:xmpp-streams"];
		if (conflict)
		{
			[self setShouldReconnect:NO];
		}
	}
}

- (void)xmppStreamWasToldToDisconnect:(XMPPStream *)sender
{
	// This method is executed on our moduleQueue.
	
	// We should not automatically attempt to reconnect when the connection closes.
	[self stop];
}

- (void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error
{
	// This method is executed on our moduleQueue.
	
	if ([self autoReconnect] && [self shouldReconnect])
	{
		[self setupReconnectTimer];
		[self setupNetworkMonitoring];
		
		SCNetworkReachabilityFlags reachabilityFlags = 0;
		SCNetworkReachabilityGetFlags(reachability, &reachabilityFlags);
		
		[multicastDelegate xmppReconnect:self didDetectAccidentalDisconnect:reachabilityFlags];
	}
	
	if ([self multipleReachabilityChanges])
	{
		// While the previous connection attempt was in progress, the reachability of the xmpp host changed.
		// This means that while the previous attempt failed, an attempt now might succeed.
		
		int ticket = ++reconnectTicket;
		
		dispatch_time_t tt = dispatch_time(DISPATCH_TIME_NOW, (0.1 * NSEC_PER_SEC));
		dispatch_after(tt, moduleQueue, ^{ @autoreleasepool {
			
			[self maybeAttemptReconnectWithTicket:ticket];
			
		}});
		
		// Note: We delay the method call.
		// This allows the other delegates to be notified of the closed stream prior to our reconnect attempt.
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Reachability
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

static void XMPPReconnectReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void *info)
{
	@autoreleasepool {
	
		XMPPReconnect *instance = (__bridge XMPPReconnect *)info;
		[instance maybeAttemptReconnectWithReachabilityFlags:flags];
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Logic
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

- (void)setupReconnectTimer
{
	NSAssert(dispatch_get_specific(moduleQueueTag) , @"Invoked on incorrect queue");
	
	if (reconnectTimer == NULL)
	{
		if ((reconnectDelay <= 0.0) && (reconnectTimerInterval <= 0.0))
		{
			// All timed reconnect attempts are disabled
			return;
		}
		
		reconnectTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, moduleQueue);
		
		dispatch_source_set_event_handler(reconnectTimer, ^{ @autoreleasepool {
			
			[self maybeAttemptReconnect];
			
		}});
		
		#if !OS_OBJECT_USE_OBJC
		dispatch_source_t theReconnectTimer = reconnectTimer;
		
		dispatch_source_set_cancel_handler(reconnectTimer, ^{
			XMPPLogVerbose(@"dispatch_release(reconnectTimer)");
			dispatch_release(theReconnectTimer);
		});
		#endif
		
		dispatch_time_t startTime;
		if (reconnectDelay > 0.0)
			startTime = dispatch_time(DISPATCH_TIME_NOW, (reconnectDelay * NSEC_PER_SEC));
		else
			startTime = dispatch_time(DISPATCH_TIME_NOW, (reconnectTimerInterval * NSEC_PER_SEC));
		
		uint64_t intervalTime;
		if (reconnectTimerInterval > 0.0)
			intervalTime = reconnectTimerInterval * NSEC_PER_SEC;
		else
			intervalTime = 0.0;
		
		dispatch_source_set_timer(reconnectTimer, startTime, intervalTime, 0.25);
		dispatch_resume(reconnectTimer);
	}
}

- (void)teardownReconnectTimer
{
	NSAssert(dispatch_get_specific(moduleQueueTag) , @"Invoked on incorrect queue");
	
	if (reconnectTimer)
	{
		dispatch_source_cancel(reconnectTimer);
		reconnectTimer = NULL;
	}
}

- (void)setupNetworkMonitoring
{
	NSAssert(dispatch_get_specific(moduleQueueTag) , @"Invoked on incorrect queue");
	
	if (reachability == NULL)
	{
		NSString *domain = xmppStream.hostName;
		if (domain == nil)
		{
			domain = @"apple.com";
		}
		
		reachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [domain UTF8String]);
		
		if (reachability)
		{
			SCNetworkReachabilityContext context = {0, (__bridge void *)(self), NULL, NULL, NULL};
			SCNetworkReachabilitySetCallback(reachability, XMPPReconnectReachabilityCallback, &context);
			
			if (moduleQueue)
			{
                SCNetworkReachabilitySetDispatchQueue(reachability,moduleQueue);
			}
			else
			{
                XMPPLogWarn(@"%@: %@ - No xmpp moduleQueue!", THIS_FILE, THIS_METHOD);
			}
		}
	}
}

- (void)teardownNetworkMonitoring
{
	NSAssert(dispatch_get_specific(moduleQueueTag) , @"Invoked on incorrect queue");
	
	if (reachability)
	{
		if (moduleQueue)
		{
            SCNetworkReachabilitySetDispatchQueue(reachability,NULL);
		}
		else
		{
			XMPPLogWarn(@"%@: %@ - No xmpp moduleQueue!", THIS_FILE, THIS_METHOD);
		}
		
		SCNetworkReachabilitySetCallback(reachability, NULL, NULL);
		CFRelease(reachability);
		reachability = NULL;
	}
}

/**
 * This method may be invoked by the reconnectTimer.
 * 
 * During auto reconnection it is invoked reconnectDelay seconds after an accidental disconnection.
 * After that, it is then invoked every reconnectTimerInterval seconds.
 * 
 * This handles disconnections that were not the result of an internet connectivity issue.
**/
- (void)maybeAttemptReconnect
{
	NSAssert(dispatch_get_specific(moduleQueueTag) , @"Invoked on incorrect queue");
	
	if (reachability)
	{
		SCNetworkReachabilityFlags reachabilityFlags;
		if (SCNetworkReachabilityGetFlags(reachability, &reachabilityFlags))
		{
			[self maybeAttemptReconnectWithReachabilityFlags:reachabilityFlags];
		}
	}
}

/**
 * This method is invoked (after a short delay) if the reachability changed while
 * a reconnection attempt was in progress.
**/
- (void)maybeAttemptReconnectWithTicket:(int)ticket
{
	NSAssert(dispatch_get_specific(moduleQueueTag) , @"Invoked on incorrect queue");
	
	if (ticket != reconnectTicket)
	{
		// The dispatched task was cancelled.
		return;
	}
	
	if (reachability)
	{
		SCNetworkReachabilityFlags reachabilityFlags;
		if (SCNetworkReachabilityGetFlags(reachability, &reachabilityFlags))
		{
			[self maybeAttemptReconnectWithReachabilityFlags:reachabilityFlags];
		}
	}
}

- (void)maybeAttemptReconnectWithReachabilityFlags:(SCNetworkReachabilityFlags)reachabilityFlags
{
	if (!dispatch_get_specific(moduleQueueTag))
	{
		dispatch_async(moduleQueue, ^{ @autoreleasepool {
			
			[self maybeAttemptReconnectWithReachabilityFlags:reachabilityFlags];
			
		}});
		
		return;
	}
	
	if (([self manuallyStarted]) || ([self autoReconnect] && [self shouldReconnect])) 
	{
		if ([xmppStream isDisconnected] && ([self queryingDelegates] == NO))
		{
			// The xmpp stream is disconnected, and is not attempting reconnection
			
			// Delegate rules:
			// 
			// If ALL of the delegates return YES, then the result is YES.
			// If ANY of the delegates return NO, then the result is NO.
			// If there are no delegates, the default answer is YES.
			
			GCDMulticastDelegateEnumerator *delegateEnumerator = [multicastDelegate delegateEnumerator];
			
			id del;
			dispatch_queue_t dq;
			
			SEL selector = @selector(xmppReconnect:shouldAttemptAutoReconnect:);
			
			NSUInteger delegateCount = [delegateEnumerator countForSelector:selector];
			
			dispatch_semaphore_t delSemaphore = dispatch_semaphore_create(0);
			dispatch_group_t delGroup = dispatch_group_create();
			
			while ([delegateEnumerator getNextDelegate:&del delegateQueue:&dq forSelector:selector])
			{
				dispatch_group_async(delGroup, dq, ^{ @autoreleasepool {
					
					if (![del xmppReconnect:self shouldAttemptAutoReconnect:reachabilityFlags])
					{
						dispatch_semaphore_signal(delSemaphore);
					}
				}});
			}
			
			[self setQueryingDelegates:YES];
			
			dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
			dispatch_async(concurrentQueue, ^{ @autoreleasepool {
				
				dispatch_group_wait(delGroup, DISPATCH_TIME_FOREVER);
				
				// What was the delegate response?
				
				BOOL shouldAttemptReconnect;
				if (delegateCount == 0)
				{
					shouldAttemptReconnect = YES;
				}
				else
				{
					shouldAttemptReconnect = (dispatch_semaphore_wait(delSemaphore, DISPATCH_TIME_NOW) != 0);
				}
				
				dispatch_async(moduleQueue, ^{ @autoreleasepool {
					
					[self setQueryingDelegates:NO];
					
					if (shouldAttemptReconnect)
					{
						[self setMultipleReachabilityChanges:NO];
						previousReachabilityFlags = reachabilityFlags;
						
                        if (self.usesOldSchoolSecureConnect)
                        {
                            [xmppStream oldSchoolSecureConnectWithTimeout:XMPPStreamTimeoutNone error:nil];
                        }
                        else
                        {
                            [xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:nil];
                        }
					}
					else if ([self multipleReachabilityChanges])
					{
						[self setMultipleReachabilityChanges:NO];
						previousReachabilityFlags = IMPOSSIBLE_REACHABILITY_FLAGS;
						
						[self maybeAttemptReconnect];
					}
					else
					{
						previousReachabilityFlags = IMPOSSIBLE_REACHABILITY_FLAGS;
					}
					
				}});
				
				#if !OS_OBJECT_USE_OBJC
				dispatch_release(delSemaphore);
				dispatch_release(delGroup);
				#endif
			}});
			
		}
		else
		{
			// The xmpp stream is already attempting a connection.
			
			if (reachabilityFlags != previousReachabilityFlags)
			{
				// It seems that the reachability of our xmpp host has changed in the middle of either
				// a reconnection attempt or while querying our delegates for permission to attempt reconnect.
				// 
				// This may mean that the current attempt will fail,
				// but an another attempt after the failure will succeed.
				// 
				// We make a note of the multiple changes,
				// and if the current attempt fails, we'll try again after a short delay.
				
				[self setMultipleReachabilityChanges:YES];
			}
		}
	}
}

@end