From d8f583cb795f81d55335141a277626c0ec247d1f Mon Sep 17 00:00:00 2001 From: rowanbeentje Date: Mon, 9 Mar 2009 23:32:06 +0000 Subject: Additional error checking, centralised connection keep-alive interval retrieval, and support for disabling keep-alive by setting the interval to 0. --- Source/CMMCPConnection.h | 1 + Source/CMMCPConnection.m | 42 ++++++++++++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/Source/CMMCPConnection.h b/Source/CMMCPConnection.h index 584b8056..e6479eb1 100644 --- a/Source/CMMCPConnection.h +++ b/Source/CMMCPConnection.h @@ -71,6 +71,7 @@ - (void) setDelegate:(id)object; - (NSTimeZone *) timeZone; - (BOOL) pingConnection; +- (double) keepAliveInterval; - (void) startKeepAliveTimerResettingState:(BOOL)resetState; - (void) stopKeepAliveTimer; - (void) keepAlive:(NSTimer *)theTimer; diff --git a/Source/CMMCPConnection.m b/Source/CMMCPConnection.m index 14cd6ba7..109ab280 100644 --- a/Source/CMMCPConnection.m +++ b/Source/CMMCPConnection.m @@ -68,7 +68,9 @@ static void forcePingTimeout(int signalNumber); connectionSocket = nil; keepAliveTimer = nil; lastKeepAliveSuccess = nil; - [NSBundle loadNibNamed:@"ConnectionErrorDialog" owner:self]; + if (![NSBundle loadNibNamed:@"ConnectionErrorDialog" owner:self]) { + NSLog(@"Connection error dialog could not be loaded; connection failure handling will not function correctly."); + } } @@ -567,11 +569,28 @@ static void forcePingTimeout(int signalNumber) longjmp(pingTimeoutJumpLocation, 1); } + +/* + * Returns the keepalive interval, or 0 if keepalive should be disabled. + * Sequel Pro draws this from the preferences with a default of 60 secs. + */ +- (double) keepAliveInterval +{ + double interval; + + interval = [[[NSUserDefaults standardUserDefaults] objectForKey:@"keepAliveInterval"] doubleValue]; + if (!interval) interval = 0; + + return interval; +} + /* * Restarts a keepalive to fire in the future. */ - (void) startKeepAliveTimerResettingState:(BOOL)resetState { + double interval; + if (keepAliveTimer) [self stopKeepAliveTimer]; if (!mConnected) return; @@ -579,14 +598,17 @@ static void forcePingTimeout(int signalNumber) [lastKeepAliveSuccess release]; lastKeepAliveSuccess = nil; } - - keepAliveTimer = [NSTimer - scheduledTimerWithTimeInterval:[[[NSUserDefaults standardUserDefaults] objectForKey:@"keepAliveInterval"] doubleValue] - target:self - selector:@selector(keepAlive:) - userInfo:nil - repeats:NO]; - [keepAliveTimer retain]; + + interval = [self keepAliveInterval]; + if (interval) { + keepAliveTimer = [NSTimer + scheduledTimerWithTimeInterval:interval + target:self + selector:@selector(keepAlive:) + userInfo:nil + repeats:NO]; + [keepAliveTimer retain]; + } } /* @@ -612,7 +634,7 @@ static void forcePingTimeout(int signalNumber) // cut but mysql doesn't pick up on the fact - see comment for pingConnection above. The same // forced-timeout approach cannot be used here on a background thread. // When the connection is disconnected in code, these 5 "hanging" threads are automatically cleaned. - if (lastKeepAliveSuccess && [lastKeepAliveSuccess timeIntervalSinceNow] < -5 * [[[NSUserDefaults standardUserDefaults] objectForKey:@"keepAliveInterval"] doubleValue]) return; + if (lastKeepAliveSuccess && [lastKeepAliveSuccess timeIntervalSinceNow] < -5 * [self keepAliveInterval]) return; [NSThread detachNewThreadSelector:@selector(threadedKeepAlive) toTarget:self withObject:nil]; [self startKeepAliveTimerResettingState:NO]; -- cgit v1.2.3