Skip to content

Commit

Permalink
Completed most obj-c methods - getPeersByState and sendData left
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Owen committed Mar 6, 2012
1 parent e976560 commit 7a3639d
Show file tree
Hide file tree
Showing 6 changed files with 450 additions and 113 deletions.
38 changes: 18 additions & 20 deletions as/src/com/jamieowen/ane/ios/p2p/GKSession.as
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ package com.jamieowen.ane.ios.p2p {
//--//////////////////////////////////////////////////
//--//// WORKING WITH CONNECTED PEERS

public function get disconnectTimeout():uint
public function get disconnectTimeout():Number
{
return _p2p.context.call( "gkSession_get_disconnectTimeout") as uint;
return _p2p.context.call( "gkSession_get_disconnectTimeout") as Number;
}


public function set disconnectTimeout( $timeout:uint ):void
public function set disconnectTimeout( $timeout:Number ):void
{
_p2p.context.call( "gkSession_set_disconnectTimeout", $timeout );
}
Expand Down Expand Up @@ -96,9 +96,9 @@ package com.jamieowen.ane.ios.p2p {
//--//////////////////////////////////////////////////
//--//// OBTAINING INFORMATION ABOUT PEERS

public function peersWithConnectionState():Vector.<String>
public function peersWithConnectionState( $state:uint = GKPeerConnectionState.AVAILABLE ):Vector.<String>
{
return _p2p.context.call( "gkSession_peersWithConnectionState") as Vector.<String>;
return _p2p.context.call( "gkSession_peersWithConnectionState", $state ) as Vector.<String>;
}


Expand All @@ -110,7 +110,7 @@ package com.jamieowen.ane.ios.p2p {
//--//////////////////////////////////////////////////
//--//// CONNECTING TO A REMOTE PEER

public function connectToPeer($peerID:String, $timeout:uint):void
public function connectToPeer($peerID:String, $timeout:Number):void
{
_p2p.context.call( "gkSession_connectToPeer", $peerID, $timeout );
}
Expand Down Expand Up @@ -197,15 +197,6 @@ package com.jamieowen.ane.ios.p2p {
_p2p = null;
}

/**
* The native extension stores the last NSError object for the last method call here. If an error has been thrown it will be here for the duration of method call.
* @return An object with "code" and "message" properties populated with contents from NSError.code and NSError.localizedDescription respectively.
*/
private function getLastNSError():Object
{
return _p2p.context.call( "gkSession_getLastNSError" );
}

//--//////////////////////////////////////////////////
//--//// EVENT HANDLERS

Expand All @@ -227,16 +218,23 @@ package com.jamieowen.ane.ios.p2p {
break;

case GKSessionErrorEvent.CONNECTION_WITH_PEER_FAILED :
peerID = $event.level;
error = getLastNSError();
if( error ) dispatchEvent( new GKSessionErrorEvent(GKSessionErrorEvent.CONNECTION_WITH_PEER_FAILED, error["code"], error["message"]));
// parse error
args = $event.level.split("{&}");
peerID = args[0];
error = {};
error["code"] = args[1];
error["message"] = args[2];

dispatchEvent( new GKSessionErrorEvent(GKSessionErrorEvent.CONNECTION_WITH_PEER_FAILED, error["code"], error["message"],peerID));
break;

case GKSessionErrorEvent.DID_FAIL_WITH_ERROR :
error = getLastNSError();
if( error ) dispatchEvent( new GKSessionErrorEvent(GKSessionErrorEvent.DID_FAIL_WITH_ERROR, error["code"], error["message"]));
args = $event.level.split("{&}");
error = {};
error["code"] = args[0];
error["message"] = args[1];

dispatchEvent( new GKSessionErrorEvent(GKSessionErrorEvent.DID_FAIL_WITH_ERROR, error["code"], error["message"]));
break;

case GKSessionEvent.DID_RECEIVE_CONNECTION_REQUEST_FROM_PEER :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
C90A9F551506368B00B74242 /* AirNativeiOS-GKImpl.h in Headers */ = {isa = PBXBuildFile; fileRef = C90A9F531506368B00B74242 /* AirNativeiOS-GKImpl.h */; };
C90A9F561506368B00B74242 /* AirNativeiOS-GKImpl.m in Sources */ = {isa = PBXBuildFile; fileRef = C90A9F541506368B00B74242 /* AirNativeiOS-GKImpl.m */; };
C90A9F591506626400B74242 /* AirNativeiOS-AirStatusEventCodes.h in Headers */ = {isa = PBXBuildFile; fileRef = C90A9F581506626400B74242 /* AirNativeiOS-AirStatusEventCodes.h */; };
C99E61C215050204005ABF52 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C99E61C115050204005ABF52 /* Foundation.framework */; };
C99E61C815050204005ABF52 /* AirNativeiOS-GameKitP2P.m in Sources */ = {isa = PBXBuildFile; fileRef = C99E61C715050204005ABF52 /* AirNativeiOS-GameKitP2P.m */; };
C99E61CF1505023E005ABF52 /* FlashRuntimeExtensions.h in Frameworks */ = {isa = PBXBuildFile; fileRef = C99E61CE1505023E005ABF52 /* FlashRuntimeExtensions.h */; };
Expand All @@ -18,11 +19,12 @@
/* Begin PBXFileReference section */
C90A9F531506368B00B74242 /* AirNativeiOS-GKImpl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "AirNativeiOS-GKImpl.h"; sourceTree = "<group>"; };
C90A9F541506368B00B74242 /* AirNativeiOS-GKImpl.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "AirNativeiOS-GKImpl.m"; sourceTree = "<group>"; };
C90A9F581506626400B74242 /* AirNativeiOS-AirStatusEventCodes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "AirNativeiOS-AirStatusEventCodes.h"; sourceTree = "<group>"; };
C99E61BE15050204005ABF52 /* libAirNativeiOS-GameKitP2P.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libAirNativeiOS-GameKitP2P.a"; sourceTree = BUILT_PRODUCTS_DIR; };
C99E61C115050204005ABF52 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
C99E61C515050204005ABF52 /* AirNativeiOS-GameKitP2P-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "AirNativeiOS-GameKitP2P-Prefix.pch"; sourceTree = "<group>"; };
C99E61C715050204005ABF52 /* AirNativeiOS-GameKitP2P.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "AirNativeiOS-GameKitP2P.m"; sourceTree = "<group>"; };
C99E61CE1505023E005ABF52 /* FlashRuntimeExtensions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = FlashRuntimeExtensions.h; path = "../../../../../libraries/sdks/air3-2_p6_sdk_mac_022712/include/FlashRuntimeExtensions.h"; sourceTree = "<group>"; };
C99E61CE1505023E005ABF52 /* FlashRuntimeExtensions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = FlashRuntimeExtensions.h; path = "../../../../../../libraries/sdks/air3-2_p6_sdk_mac_022712/include/FlashRuntimeExtensions.h"; sourceTree = "<group>"; };
C99E61D015053A22005ABF52 /* GameKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GameKit.framework; path = System/Library/Frameworks/GameKit.framework; sourceTree = SDKROOT; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -61,7 +63,6 @@
C99E61C015050204005ABF52 /* Frameworks */ = {
isa = PBXGroup;
children = (
C99E61CE1505023E005ABF52 /* FlashRuntimeExtensions.h */,
C99E61C115050204005ABF52 /* Foundation.framework */,
);
name = Frameworks;
Expand All @@ -74,13 +75,15 @@
C99E61C415050204005ABF52 /* Supporting Files */,
C90A9F531506368B00B74242 /* AirNativeiOS-GKImpl.h */,
C90A9F541506368B00B74242 /* AirNativeiOS-GKImpl.m */,
C90A9F581506626400B74242 /* AirNativeiOS-AirStatusEventCodes.h */,
);
path = "AirNativeiOS-GameKitP2P";
sourceTree = "<group>";
};
C99E61C415050204005ABF52 /* Supporting Files */ = {
isa = PBXGroup;
children = (
C99E61CE1505023E005ABF52 /* FlashRuntimeExtensions.h */,
C99E61C515050204005ABF52 /* AirNativeiOS-GameKitP2P-Prefix.pch */,
);
name = "Supporting Files";
Expand All @@ -94,6 +97,7 @@
buildActionMask = 2147483647;
files = (
C90A9F551506368B00B74242 /* AirNativeiOS-GKImpl.h in Headers */,
C90A9F591506626400B74242 /* AirNativeiOS-AirStatusEventCodes.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// AirNativeiOS-AirStatusEventCodes.h
// AirNativeiOS-GameKitP2P
//
// Created by Jamie Owen on 06/03/2012.
// Copyright 2012 __MyCompanyName__. All rights reserved.
//

#ifndef AirNativeiOS_GameKitP2P_AirNativeiOS_AirStatusEventCodes_h
#define AirNativeiOS_GameKitP2P_AirNativeiOS_AirStatusEventCodes_h

// GKSessionDelegate
#define airStatusEvent_gkSessionConnectionWithPeerFailed "gkSessionConnectionWithPeerFailed"
#define airStatusEvent_gkSessionDidFailWithError "gkSessionDidFailWithError"
#define airStatusEvent_gkSessionChangeState "gkSessionChangeState"
#define airStatusEvent_gkSessionDidReceiveConnectionRequestFromPeer "gkSessionDidReceiveConnectionRequestFromPeer"

// GKSession ReceiveData
#define airStatusEvent_gkSessionDataReceived "gkSessionDataReceived"

// GKPeerPickerControllerDelegate
#define airStatusEvent_gkPeerPickerDidConnectPeer "gkPeerPickerDidConnectPeer"
#define airStatusEvent_gkPeerPickerControllerDidCancel "gkPeerPickerControllerDidCancel"

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,28 @@

#import <Foundation/Foundation.h>
#import <GameKit/Gamekit.h>
#import "FlashRuntimeExtensions.h"

@interface AirNativeiOS_GKImpl : NSObject <GKSessionDelegate, GKPeerPickerControllerDelegate>
{
GKSession* currentSession;
GKPeerPickerController* currentPicker;

FREContext* airContext;
}

@property(retain) GKSession* currentSession;
@property(retain) GKPeerPickerController* currentPicker;
@property FREContext* airContext;

-(id) initWithContext:(FREContext)context;

-(void) createSession;
-(void) createPicker;

-(void) disposeSession;
-(void) disposePicker;

-(BOOL) isSessionOK;
-(BOOL) isPickerOK;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,41 @@
//

#import "AirNativeiOS-GKImpl.h"
#import "AirNativeiOS-AirStatusEventCodes.h"

@implementation AirNativeiOS_GKImpl

@synthesize currentSession;
@synthesize currentPicker;
@synthesize airContext;

-(id) initWithContext:(FREContext)context
{
self = [super init];
if( self )
{
airContext = context;
}
return self;
}

-(void) dealloc
{
[self disposeSession];
[self disposePicker];

self.airContext = nil;
}

-(BOOL) isSessionOK
{
return currentSession != nil;
}

-(BOOL) isPickerOK
{
return currentPicker != nil;
}

// creates a new picker object and releases any current session - existing pickers are dismissed/released also
// once the picker has selected a peer - the currentSession is set and the picker removed
Expand Down Expand Up @@ -56,7 +86,7 @@ -(void)disposeSession
//-////////////////////////////////////////////////////////////
//-// GKPeerPickerControllerDelegate methods

// handles the connection by disposing of the picker and saving the current session
// handles the connection and stores the session
-(void)peerPickerController:(GKPeerPickerController *)picker
didConnectPeer:(NSString *)peerID
toSession:(GKSession *)session
Expand All @@ -65,9 +95,10 @@ -(void)peerPickerController:(GKPeerPickerController *)picker
session.delegate = self;
[session setDataReceiveHandler:self withContext:nil];

[self disposePicker];

// dispatch event to AIR extension context
FREDispatchStatusEventAsync( airContext, airStatusEvent_gkPeerPickerDidConnectPeer, (uint8_t*) [peerID UTF8String] );

// the AIR side should dispose the picker now
}

-(void)peerPickerController:(GKPeerPickerController *)picker
Expand All @@ -78,9 +109,10 @@ -(void)peerPickerController:(GKPeerPickerController *)picker

-(void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker
{
[self disposePicker];

// dispatch event to AIR extension context
FREDispatchStatusEventAsync( airContext, airStatusEvent_gkPeerPickerControllerDidCancel, "" );

// the AIR side should dispose the picker now.
}

//-////////////////////////////////////////////////////////////
Expand All @@ -91,27 +123,85 @@ -(void) receiveData:(NSData *)data
inSession:(GKSession *)session
context:(void *)context
{
// combine the peerID and the data into one string. with "{&}" to seperate.

NSString* dataStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSString* message = [dataStr stringByAppendingString:@"{&}"];
message = [message stringByAppendingString:peer];

// dispatch event to AIR extension context.
FREDispatchStatusEventAsync( airContext, airStatusEvent_gkSessionDataReceived, (uint8_t*)[message UTF8String] );
}


//-////////////////////////////////////////////////////////////
//-// GKSessionDelgate methods


- (id)init
-(void) session:(GKSession *)session
peer:(NSString *)peerID
didChangeState:(GKPeerConnectionState)state
{
self = [super init];
if (self) {
// Initialization code here.
NSString* message = peerID;

/**
Actionscript GKPeerConnectionState values:
public static const AVAILABLE:uint = 0;
public static const CONNECTED:uint = 1;
public static const CONNECTING:uint = 2;
public static const DISCONNECTED:uint = 3;
public static const UNAVAILABLE:uint = 4;
**/
// append the state to the message - with the code defined in the actionscript class GKPeerConnectionState
switch(state)
{
case GKPeerStateAvailable:
message = [message stringByAppendingString:@"0"];
break;
case GKPeerStateConnected:
message = [message stringByAppendingString:@"1"];
break;
case GKPeerStateConnecting:
message = [message stringByAppendingString:@"2"];
break;
case GKPeerStateDisconnected:
message = [message stringByAppendingString:@"3"];
break;
case GKPeerStateUnavailable:
message = [message stringByAppendingString:@"4"];
break;
}

return self;
// dispatch event to AIR extension context.]
FREDispatchStatusEventAsync(airContext,airStatusEvent_gkSessionDataReceived, (uint8_t*) [message UTF8String] );
}

-(void) dealloc
-(void)session:(GKSession *)session connectionWithPeerFailed:(NSString *)peerID withError:(NSError *)error
{
// encode error details in string.
NSMutableString* message = [[NSMutableString alloc] initWithString:peerID];
[ message appendString:@"{&}" ];
[ message appendString:[NSString stringWithFormat:@"%d",error.code] ];
[ message appendString:@"{&}" ];
[ message appendString:error.description ];

// dispatch event to AIR extension context.
FREDispatchStatusEventAsync(airContext,airStatusEvent_gkSessionDidReceiveConnectionRequestFromPeer, (uint8_t*) [message UTF8String] );
}

-(void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID
{
// dispatch event to AIR extension context.
FREDispatchStatusEventAsync(airContext,airStatusEvent_gkSessionDidReceiveConnectionRequestFromPeer, (uint8_t*) [peerID UTF8String] );
}

-(void)session:(GKSession *)session didFailWithError:(NSError *)error
{
NSMutableString* message = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:@"%d",error.code]];
[ message appendString:@"{&}" ];
[ message appendString:error.description ];

// dispatch event to AIR extension context.
FREDispatchStatusEventAsync(airContext,airStatusEvent_gkSessionDidReceiveConnectionRequestFromPeer, (uint8_t*) [message UTF8String] );
}

@end
Loading

0 comments on commit 7a3639d

Please sign in to comment.