From ea24b39fbece85e12d74f343e7b6af74d81a1cfa Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Tue, 21 Oct 2014 14:54:10 -0400 Subject: [PATCH] Enhance the connection callback protocol to include other connection listener callbacks exposed by common. Start handling some HTTP request errors. --- Limelight/Network/HttpManager.m | 4 +- Limelight/Stream/Connection.h | 13 ++++-- Limelight/Stream/Connection.m | 21 +++++----- Limelight/Stream/StreamManager.h | 2 +- Limelight/Stream/StreamManager.m | 26 ++++++++---- .../StreamFrameViewController.h | 2 +- .../StreamFrameViewController.m | 42 ++++++++++++++++++- 7 files changed, 83 insertions(+), 27 deletions(-) diff --git a/Limelight/Network/HttpManager.m b/Limelight/Network/HttpManager.m index 2ac68e9f..21121e7f 100644 --- a/Limelight/Network/HttpManager.m +++ b/Limelight/Network/HttpManager.m @@ -38,7 +38,8 @@ static const NSString* PORT = @"47984"; // Check root status_code if (![HttpManager verifyStatus: rootNode]) { - //TODO: handle error + NSLog(@"ERROR: Request returned with failure status"); + return NULL; } // Skip the root node @@ -229,6 +230,7 @@ static const NSString* PORT = @"47984"; - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"connection error: %@", error); + dispatch_semaphore_signal(_requestLock); } @end diff --git a/Limelight/Stream/Connection.h b/Limelight/Stream/Connection.h index e4a97350..05a1384c 100644 --- a/Limelight/Stream/Connection.h +++ b/Limelight/Stream/Connection.h @@ -10,15 +10,22 @@ #import "VideoDecoderRenderer.h" #import "StreamConfiguration.h" -@protocol ConTermCallback +@protocol ConnectionCallbacks -- (void) connectionTerminated; +- (void) connectionStarted; +- (void) connectionTerminated:(long)errorCode; +- (void) stageStarting:(char*)stageName; +- (void) stageComplete:(char*)stageName; +- (void) stageFailed:(char*)stageName withError:(long)errorCode; +- (void) launchFailed; +- (void) displayMessage:(char*)message; +- (void) displayTransientMessage:(char*)message; @end @interface Connection : NSOperation --(id) initWithConfig:(StreamConfiguration*)config renderer:(VideoDecoderRenderer*)myRenderer connectionTerminatedCallback:(id)callback; +-(id) initWithConfig:(StreamConfiguration*)config renderer:(VideoDecoderRenderer*)myRenderer connectionCallbacks:(id)callbacks; -(void) terminate; -(void) main; diff --git a/Limelight/Stream/Connection.m b/Limelight/Stream/Connection.m index 139adc6e..9679260d 100644 --- a/Limelight/Stream/Connection.m +++ b/Limelight/Stream/Connection.m @@ -20,11 +20,10 @@ CONNECTION_LISTENER_CALLBACKS _clCallbacks; DECODER_RENDERER_CALLBACKS _drCallbacks; AUDIO_RENDERER_CALLBACKS _arCallbacks; - } static OpusDecoder *opusDecoder; -static id _callback; +static id _callbacks; #define PCM_BUFFER_SIZE 1024 #define OUTPUT_BUS 0 @@ -243,37 +242,37 @@ void ArDecodeAndPlaySample(char* sampleData, int sampleLength) void ClStageStarting(int stage) { + [_callbacks stageStarting:(char*)LiGetStageName(stage)]; } void ClStageComplete(int stage) { + [_callbacks stageComplete:(char*)LiGetStageName(stage)]; } void ClStageFailed(int stage, long errorCode) { - printf("Stage %d failed: %ld\n", stage, errorCode); + [_callbacks stageFailed:(char*)LiGetStageName(stage) withError:errorCode]; } void ClConnectionStarted(void) { - printf("Connection started\n"); + [_callbacks connectionStarted]; } void ClConnectionTerminated(long errorCode) { - printf("ConnectionTerminated: %ld\n", errorCode); - - [_callback connectionTerminated]; + [_callbacks connectionTerminated: errorCode]; } void ClDisplayMessage(char* message) { - printf("DisplayMessage: %s\n", message); + [_callbacks displayMessage: message]; } void ClDisplayTransientMessage(char* message) { - printf("DisplayTransientMessage: %s\n", message); + [_callbacks displayTransientMessage: message]; } -(void) terminate @@ -286,12 +285,12 @@ void ClDisplayTransientMessage(char* message) }); } --(id) initWithConfig:(StreamConfiguration*)config renderer:(VideoDecoderRenderer*)myRenderer connectionTerminatedCallback:(id)callback +-(id) initWithConfig:(StreamConfiguration*)config renderer:(VideoDecoderRenderer*)myRenderer connectionCallbacks:(id)callbacks { self = [super init]; _host = config.hostAddr; renderer = myRenderer; - _callback = callback; + _callbacks = callbacks; _streamConfig.width = config.width; _streamConfig.height = config.height; _streamConfig.fps = config.frameRate; diff --git a/Limelight/Stream/StreamManager.h b/Limelight/Stream/StreamManager.h index 4f865f2f..d0dd6d37 100644 --- a/Limelight/Stream/StreamManager.h +++ b/Limelight/Stream/StreamManager.h @@ -12,7 +12,7 @@ @interface StreamManager : NSOperation -- (id) initWithConfig:(StreamConfiguration*)config renderView:(UIView*)view connectionTerminatedCallback:(id)callback; +- (id) initWithConfig:(StreamConfiguration*)config renderView:(UIView*)view connectionCallbacks:(id)callback; - (void) stopStream; @end diff --git a/Limelight/Stream/StreamManager.m b/Limelight/Stream/StreamManager.m index 21e4cb72..d8135df1 100644 --- a/Limelight/Stream/StreamManager.m +++ b/Limelight/Stream/StreamManager.m @@ -14,15 +14,15 @@ @implementation StreamManager { StreamConfiguration* _config; UIView* _renderView; - id _callback; + id _callbacks; Connection* _connection; } -- (id) initWithConfig:(StreamConfiguration*)config renderView:(UIView*)view connectionTerminatedCallback:(id)callback { +- (id) initWithConfig:(StreamConfiguration*)config renderView:(UIView*)view connectionCallbacks:(id)callbacks { self = [super init]; _config = config; _renderView = view; - _callback = callback; + _callbacks = callbacks; _config.riKey = [Utils randomBytes:16]; _config.riKeyId = arc4random(); _config.bitRate = 10000; @@ -50,7 +50,7 @@ } VideoDecoderRenderer* renderer = [[VideoDecoderRenderer alloc]initWithView:_renderView]; - _connection = [[Connection alloc] initWithConfig:_config renderer:renderer connectionTerminatedCallback:_callback]; + _connection = [[Connection alloc] initWithConfig:_config renderer:renderer connectionCallbacks:_callbacks]; NSOperationQueue* opQueue = [[NSOperationQueue alloc] init]; [opQueue addOperation:_connection]; } @@ -60,7 +60,7 @@ [_connection terminate]; } -- (void) launchApp:(HttpManager*)hMan { +- (BOOL) launchApp:(HttpManager*)hMan { NSData* launchResp = [hMan executeRequestSynchronously: [hMan newLaunchRequest:@"67339056" width:_config.width @@ -68,14 +68,24 @@ refreshRate:_config.frameRate rikey:[Utils bytesToHex:_config.riKey] rikeyid:_config.riKeyId]]; - [HttpManager getStringFromXML:launchResp tag:@"gamesession"]; + NSString *gameSession = [HttpManager getStringFromXML:launchResp tag:@"gamesession"]; + if (gameSession == NULL || [gameSession isEqualToString:@"0"]) { + return FALSE; + } + + return TRUE; } -- (void) resumeApp:(HttpManager*)hMan { +- (BOOL) resumeApp:(HttpManager*)hMan { NSData* resumeResp = [hMan executeRequestSynchronously: [hMan newResumeRequestWithRiKey:[Utils bytesToHex:_config.riKey] riKeyId:_config.riKeyId]]; - [HttpManager getStringFromXML:resumeResp tag:@"gamesession"]; + NSString *resume = [HttpManager getStringFromXML:resumeResp tag:@"resume"]; + if (resume == NULL || [resume isEqualToString:@"0"]) { + return FALSE; + } + + return TRUE; } @end diff --git a/Limelight/ViewControllers/StreamFrameViewController.h b/Limelight/ViewControllers/StreamFrameViewController.h index adbcd299..07a13742 100644 --- a/Limelight/ViewControllers/StreamFrameViewController.h +++ b/Limelight/ViewControllers/StreamFrameViewController.h @@ -10,6 +10,6 @@ #import -@interface StreamFrameViewController : UIViewController +@interface StreamFrameViewController : UIViewController @end diff --git a/Limelight/ViewControllers/StreamFrameViewController.m b/Limelight/ViewControllers/StreamFrameViewController.m index f8a21034..18758c25 100644 --- a/Limelight/ViewControllers/StreamFrameViewController.m +++ b/Limelight/ViewControllers/StreamFrameViewController.m @@ -29,7 +29,9 @@ _controllerSupport = [[ControllerSupport alloc] init]; - _streamMan = [[StreamManager alloc] initWithConfig:[MainFrameViewController getStreamConfiguration] renderView:self.view connectionTerminatedCallback:self]; + _streamMan = [[StreamManager alloc] initWithConfig:[MainFrameViewController getStreamConfiguration] + renderView:self.view + connectionCallbacks:self]; NSOperationQueue* opQueue = [[NSOperationQueue alloc] init]; [opQueue addOperation:_streamMan]; @@ -44,7 +46,13 @@ [self performSegueWithIdentifier:@"returnToMainFrame" sender:self]; } -- (void)connectionTerminated { +- (void) connectionStarted { + printf("Connection started\n"); +} + +- (void)connectionTerminated:(long)errorCode { + printf("Connection terminated: %ld\n", errorCode); + UIAlertController* conTermAlert = [UIAlertController alertControllerWithTitle:@"Connection Terminated" message:@"The connection terminated unexpectedly" preferredStyle:UIAlertControllerStyleAlert]; [conTermAlert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDestructive handler:^(UIAlertAction* action){ [self performSegueWithIdentifier:@"returnToMainFrame" sender:self]; @@ -54,6 +62,36 @@ [_streamMan stopStream]; } +- (void) stageStarting:(char*)stageName { + printf("Starting %s\n", stageName); +} + +- (void) stageComplete:(char*)stageName { +} + +- (void) stageFailed:(char*)stageName withError:(long)errorCode { + UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Connection Failed" + message:[NSString stringWithFormat:@"%s failed with error %ld", + stageName, errorCode] + preferredStyle:UIAlertControllerStyleAlert]; + [alert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDestructive handler:^(UIAlertAction* action){ + [self performSegueWithIdentifier:@"returnToMainFrame" sender:self]; + }]]; + [self presentViewController:alert animated:YES completion:nil]; +} + +- (void) launchFailed { + +} + +- (void) displayMessage:(char*)message { + printf("Display message: %s\n", message); +} + +- (void) displayTransientMessage:(char*)message { + printf("Display transient message: %s\n", message); +} + - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning];