Commit c855c689 authored by Chengzhao Li's avatar Chengzhao Li
Browse files

- add reliable/unreliable mode from central to peripheral

parent 661f4a3e
......@@ -73,21 +73,21 @@
int i = 0;
NSString* localName = [[MultiplayerController instance]localName];
for(NCMCPeerID* pd in playerData) {
if( i < 3 && ![localName isEqualToString:[pd getDisplayName]]) {
if( i < 3 && ![localName isEqualToString:[self stringForMCPeerDisplayName:[pd getDisplayName]]]) {
if (i == 0) {
lbPlayer1.string = [pd getDisplayName];
lbPlayer1.string = [self stringForMCPeerDisplayName:[pd getDisplayName]];
lbPlayer1.color = [CCColor whiteColor];
btnSendTo1.enabled = YES;
}
if (i == 1) {
lbPlayer2.string = [pd getDisplayName];
lbPlayer2.string = [self stringForMCPeerDisplayName:[pd getDisplayName]];
lbPlayer2.color = [CCColor whiteColor];
btnSendTo2.enabled = YES;
}
if (i == 2) {
lbPlayer3.string = [pd getDisplayName];
lbPlayer3.string = [self stringForMCPeerDisplayName:[pd getDisplayName]];
lbPlayer3.color = [CCColor whiteColor];
btnSendTo3.enabled = YES;
}
......@@ -109,7 +109,7 @@
NSString* msg = tfMsg.string;
if (msg.length > 0) {
NSData* data = [msg dataUsingEncoding:NSUTF8StringEncoding];
[[MultiplayerController instance] sendData:data to:lbPlayer1.string];
[[MultiplayerController instance] sendData:data to:lbPlayer1.string withMode:NCMCSessionSendDataUnreliable];
}
}
......@@ -118,7 +118,7 @@
NSString* msg = tfMsg.string;
if (msg.length > 0) {
NSData* data = [msg dataUsingEncoding:NSUTF8StringEncoding];
[[MultiplayerController instance] sendData:data to:lbPlayer2.string];
[[MultiplayerController instance] sendData:data to:lbPlayer2.string withMode:NCMCSessionSendDataUnreliable];
}
}
......@@ -127,7 +127,7 @@
NSString* msg = tfMsg.string;
if (msg.length > 0) {
NSData* data = [msg dataUsingEncoding:NSUTF8StringEncoding];
[[MultiplayerController instance] sendData:data to:lbPlayer3.string];
[[MultiplayerController instance] sendData:data to:lbPlayer3.string withMode:NCMCSessionSendDataUnreliable];
}
}
......@@ -146,5 +146,13 @@
[self setPlayerList];
}
- (NSString*) stringForMCPeerDisplayName:(NSString*)displayName {
if([displayName length] > 12) {
NSString* realDisplayName = [displayName substringFromIndex:12];
return realDisplayName;
}
return @"Unknown Player";
}
@end
#import "LobbyScene.h"
#import "MultiplayerController.h"
#import "NCMultipeerConnectiviy/NCMCPeerID.h"
#import "Parameters.h"
@implementation LobbyScene
......@@ -98,22 +97,22 @@
for(NCMCPeerID* pd in playerData) {
if( i < 4) {
if (i == 0) {
lbPlayer1.string = [pd getDisplayName];
lbPlayer1.string = [self stringForMCPeerDisplayName:[pd getDisplayName]];
lbPlayer1.color = [CCColor whiteColor];
}
if (i == 1) {
lbPlayer2.string = [pd getDisplayName];
lbPlayer2.string = [self stringForMCPeerDisplayName:[pd getDisplayName]];
lbPlayer2.color = [CCColor whiteColor];
}
if (i == 2) {
lbPlayer3.string = [pd getDisplayName];
lbPlayer3.string = [self stringForMCPeerDisplayName:[pd getDisplayName]];
lbPlayer3.color = [CCColor whiteColor];
}
if (i == 3) {
lbPlayer4.string = [pd getDisplayName];
lbPlayer4.string = [self stringForMCPeerDisplayName:[pd getDisplayName]];
lbPlayer4.color = [CCColor whiteColor];
}
}
......@@ -133,4 +132,13 @@
}
- (NSString*) stringForMCPeerDisplayName:(NSString*)displayName {
if([displayName length] > 12) {
NSString* realDisplayName = [displayName substringFromIndex:12];
return realDisplayName;
}
return @"Unknown Player";
}
@end
......@@ -7,9 +7,9 @@
//
#import <Foundation/Foundation.h>
#import "NCMCSession.h"
#import "NCMCCentralService.h"
#import "NCMCPeripheralService.h"
#import "NCMultipeerConnectiviy/NCMCSession.h"
#import "NCMultipeerConnectiviy/NCMCCentralService.h"
#import "NCMultipeerConnectiviy/NCMCPeripheralService.h"
@interface MultiplayerController : NSObject<NCMCSessionDelegate, NCMCCentralServiceDelegate, NCMCPeripheralServiceDelegate, UIAlertViewDelegate>
......@@ -30,7 +30,7 @@
-(void) startHost;
-(void) startClient;
-(void)sendData:(NSData*)data to:(NSString*)name;
-(void)sendData:(NSData*)data to:(NSString*)name withMode:(NCMCSessionSendDataMode)mode;
-(void)gotoChatRoom;
@end
......@@ -7,7 +7,6 @@
//
#import "MultiplayerController.h"
#import "NCMCPeerID.h"
#import "NCMCAlertView.h"
#import "Parameters.h"
#import "LobbyScene.h"
......@@ -33,7 +32,11 @@ static MultiplayerController *_sharedMultiplayerController = nil;
-(void)initializeControllerForNewMatch
{
NCMCPeerID *peer = [[NCMCPeerID alloc]initWithDisplayName:self.localName];
NSString* uid = [NSUUID UUID].UUIDString;
NSString* suid = [uid substringFromIndex:24];
NSString* displayName = [NSString stringWithFormat:@"%@%@",suid, self.localName];
NCMCPeerID *peer = [[NCMCPeerID alloc]initWithDisplayName:displayName];
self.currentSession = [[NCMCSession alloc]initWithPeer:peer andServiceID:TRANSFER_SERVICE_UUID];
self.currentSession.delegate = self;
......@@ -101,21 +104,21 @@ static MultiplayerController *_sharedMultiplayerController = nil;
}
}
-(void)sendData:(NSData *)msgData to:(NSString *)name
-(void)sendData:(NSData *)msgData to:(NSString *)name withMode:(NCMCSessionSendDataMode)mode;
{
NCMCPeerID *peer = [self getPeerIDByName:name];
if (peer != nil) {
CCLOG(@"MultiplayerController send data to %@", name);
NSData *data = [self packMessageWithType:MSG_CHAT_MSG andMessage:msgData];
NSArray *targets = @[peer];
[self.currentSession sendData:data toPeers:targets];
[self.currentSession sendData:data toPeers:targets withMode:mode];
}
}
-(NCMCPeerID*)getPeerIDByName:(NSString*)name
{
for (NCMCPeerID *pid in self.currentSessionPlayerIDs) {
if ([[pid getDisplayName] isEqualToString:name]) {
if ([[self stringForMCPeerDisplayName:[pid getDisplayName]] isEqualToString:name]) {
return pid;
}
}
......@@ -139,7 +142,7 @@ static MultiplayerController *_sharedMultiplayerController = nil;
if (isHost) {
NSData *data = [self packMessageWithType:MSG_SERVER_CLIENT_GO_TO_CHAT andMessage:nil];
[self.currentSession sendData:data toPeers:self.currentSessionPlayerIDs];
[self.currentSession sendData:data toPeers:self.currentSessionPlayerIDs withMode:NCMCSessionSendDataReliable];
}
CCScene *chatScene = [CCBReader loadAsScene:@"ChatRoomScene"];
......@@ -180,7 +183,7 @@ static MultiplayerController *_sharedMultiplayerController = nil;
}
case MSG_CHAT_MSG:
{
NSDictionary *userInfo = @{ @"name": [peer getDisplayName],
NSDictionary *userInfo = @{ @"name": [self stringForMCPeerDisplayName:[peer getDisplayName]],
@"message": msgData};
dispatch_async(dispatch_get_main_queue(), ^{
......@@ -193,12 +196,21 @@ static MultiplayerController *_sharedMultiplayerController = nil;
}
}
- (NSString*) stringForMCPeerDisplayName:(NSString*)displayName {
if([displayName length] > 12) {
NSString* realDisplayName = [displayName substringFromIndex:12];
return realDisplayName;
}
return @"Unknown Player";
}
/***********************************************************************/
/* DELEGATE FUNCTIONS */
/***********************************************************************/
-(void)session:(NCMCSession *)session peer:(NCMCPeerID *)peerID didChangeState:(NCMCSessionState)state
{
CCLOG(@"MCSession session peer didChangeState : %@, state : %ld", [peerID getDisplayName], (long)state);
CCLOG(@"MCSession session peer didChangeState : %@, state : %ld", [self stringForMCPeerDisplayName:[peerID getDisplayName]], (long)state);
dispatch_async(dispatch_get_main_queue(), ^{
switch (state) {
......@@ -224,7 +236,7 @@ static MultiplayerController *_sharedMultiplayerController = nil;
}
case NCMCSessionStateNotConnected: {
for (NCMCPeerID *pid in self.currentSessionPlayerIDs) {
if ([[pid getDisplayName] isEqualToString:[peerID getDisplayName]]) {
if ([[self stringForMCPeerDisplayName:[pid getDisplayName]] isEqualToString:[self stringForMCPeerDisplayName:[peerID getDisplayName]]]) {
[self.currentSessionPlayerIDs removeObject:pid];
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:UPDATE_PLAYERLIST_NOTIFICATION
......@@ -250,7 +262,7 @@ static MultiplayerController *_sharedMultiplayerController = nil;
{
dispatch_async(dispatch_get_main_queue(), ^{
if (self.isHost) {
NSString* msg = [NSString stringWithFormat:@"\"%@\" would like to join your game, do you accept?", [peerID getDisplayName]];
NSString* msg = [NSString stringWithFormat:@"\"%@\" would like to join your game, do you accept?", [self stringForMCPeerDisplayName:[peerID getDisplayName]]];
NCMCAlertView *alert = [[NCMCAlertView alloc] initWithTitle:@"Player Request" message:msg delegate:self cancelButtonTitle:@"Decline" otherButtonTitles:@"Accept", nil];
......@@ -276,7 +288,7 @@ static MultiplayerController *_sharedMultiplayerController = nil;
{
dispatch_async(dispatch_get_main_queue(), ^{
if (!self.isHost) {
NSString* msg = [NSString stringWithFormat:@"The game \"%@\" was found,. Would you like to connect?", [peerID getDisplayName]];
NSString* msg = [NSString stringWithFormat:@"The game \"%@\" was found,. Would you like to connect?", [self stringForMCPeerDisplayName:[peerID getDisplayName]]];
NCMCAlertView *alert = [[NCMCAlertView alloc] initWithTitle:@"Game Found" message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Connect", nil];
......
......@@ -32,7 +32,7 @@
-(Boolean)startBrowsing;
-(void)stopBrowsing;
-(void)invitePeer:(NCMCPeerID*)peerID;
-(void)sendCentralData : (NSData*)data toPerihperal:(NSString*)identifier;
-(void)sendCentralData : (NSData*)data toPerihperal:(NSString*)identifier withMode:(NCMCSessionSendDataMode)mode;
-(void)disconnectToPeripheral:(NSString*) identifier;
......
......@@ -11,8 +11,9 @@
@interface NCMCMessageData : NSObject
@property(strong, nonatomic) NSString *deviceUUID;
@property(strong, nonatomic) NSMutableData *data;
@property(assign, nonatomic) Boolean isReliable;
-(instancetype)initWithDeviceUUID:(NSString*)uuid;
-(instancetype)initWithDeviceUUID:(NSString*)uuid andIsReliable:(Boolean) reliable;
-(void)addData:(NSData*)d;
-(void)clearData;
@end
......@@ -11,13 +11,15 @@
@implementation NCMCMessageData
@synthesize deviceUUID;
@synthesize data;
@synthesize isReliable;
-(instancetype)initWithDeviceUUID:(NSString *)uuid
-(instancetype)initWithDeviceUUID:(NSString *)uuid andIsReliable:(Boolean) reliable
{
self = [super init];
if (self) {
self.deviceUUID = uuid;
self.isReliable = reliable;
self.data = [[NSMutableData alloc]init];
}
......
......@@ -12,6 +12,7 @@
@interface NCMCPeripheralInfo : NSObject
@property(strong, nonatomic)CBPeripheral *peripheral;
@property (strong, nonatomic) CBCharacteristic *readCharacteristic;
@property (strong, nonatomic) CBCharacteristic *writeCharacteristic;
@property (strong, nonatomic) CBCharacteristic *writeWithResponseCharacteristic;
@property (strong, nonatomic) CBCharacteristic *writeWithoutResponseCharacteristic;
@property(strong, nonatomic)NSString *name;
@end
......@@ -9,5 +9,5 @@
#import "NCMCPeripheralInfo.h"
@implementation NCMCPeripheralInfo
@synthesize peripheral, name, readCharacteristic, writeCharacteristic;
@synthesize peripheral, name, readCharacteristic, writeWithResponseCharacteristic, writeWithoutResponseCharacteristic;
@end
......@@ -14,6 +14,11 @@ typedef NS_ENUM (NSInteger, NCMCSessionState) {
NCMCSessionStateConnected // connected to the session
};
typedef NS_ENUM (NSInteger, NCMCSessionSendDataMode) {
NCMCSessionSendDataReliable, // guaranteed reliable and in-order delivery
NCMCSessionSendDataUnreliable // sent immediately without queuing, no guaranteed delivery
};
@protocol NCMCSessionDelegate;
NS_ASSUME_NONNULL_BEGIN
......@@ -27,7 +32,7 @@ NS_ASSUME_NONNULL_BEGIN
-(void)disconnect;
-(void)sendData:(NSData *)data toPeers:(NSArray<NCMCPeerID *> *)peerIDs;
-(void)sendData:(NSData *)data toPeers:(NSArray<NCMCPeerID *> *)peerIDs withMode:(NCMCSessionSendDataMode)mode;
-(NSArray<NCMCPeerID*>*)getConnectedPeers;
......
......@@ -44,12 +44,12 @@ typedef enum NCMCSystemMessageType {
[self.connectedDevices removeAllObjects];
}
-(void)sendData:(NSData *)data toPeers:(NSArray<NCMCPeerID *> *)peerIDs
-(void)sendData:(NSData *)data toPeers:(NSArray<NCMCPeerID *> *)peerIDs withMode:(NCMCSessionSendDataMode)mode
{
for (NCMCPeerID* peerID in peerIDs) {
NSData* msg = [self packUserMessage:data withTargetPeerID:peerID];
if ([[NCMCBluetoothLEManager instance]isCentral]) {
[[NCMCBluetoothLEManager instance] sendCentralData:msg toPerihperal:peerID.identifier];
[[NCMCBluetoothLEManager instance] sendCentralData:msg toPerihperal:peerID.identifier withMode:mode];
} else {
[[NCMCBluetoothLEManager instance] sendPeriheralData:msg toCentral: [self getCentralDeviceIdentifier]];
}
......@@ -100,7 +100,7 @@ typedef enum NCMCSystemMessageType {
for (NCMCDeviceInfo *info in enmuerator) {
if (info.uniqueID != 0) {
[[NCMCBluetoothLEManager instance] sendCentralData:sysData toPerihperal:info.identifier];
[[NCMCBluetoothLEManager instance] sendCentralData:sysData toPerihperal:info.identifier withMode:NCMCSessionSendDataReliable];
}
}
}
......@@ -239,7 +239,7 @@ typedef enum NCMCSystemMessageType {
NSData* sysData = [self packSystemMessageWithType:CENTRA_PERIPHERAL_CONNECTION_REQUEST andMessage:centralDeviceData];
[[NCMCBluetoothLEManager instance] sendCentralData:sysData toPerihperal:peerID.identifier];
[[NCMCBluetoothLEManager instance] sendCentralData:sysData toPerihperal:peerID.identifier withMode:NCMCSessionSendDataReliable];
}
void(^myInvitationHandler)(BOOL, NCMCSession*, NCMCPeerID*) = ^(BOOL accept, NCMCSession* session, NCMCPeerID *peerID) {
......@@ -314,14 +314,14 @@ void(^myInvitationHandler)(BOOL, NCMCSession*, NCMCPeerID*) = ^(BOOL accept, NCM
NSData* sysData = [self packSystemMessageWithType:CENTRA_PERIPHERAL_ASSIGN_IDENTIFIER andMessage:deviceData];
[[NCMCBluetoothLEManager instance] sendCentralData:sysData toPerihperal:identifier];
[[NCMCBluetoothLEManager instance] sendCentralData:sysData toPerihperal:identifier withMode:NCMCSessionSendDataReliable];
// update new connected device info to all connected periherals
NSData* sysBroadcastNewDeviceData = [self packSystemMessageWithType:CENTRA_PERIPHERAL_DEVICE_CONNECTED andMessage:deviceData];
for (NSString *key in self.connectedDevices) {
NCMCDeviceInfo* peripheralDeviceInfo = self.connectedDevices[key];
if (peripheralDeviceInfo.uniqueID != 0) {
[[NCMCBluetoothLEManager instance] sendCentralData:sysBroadcastNewDeviceData toPerihperal:peripheralDeviceInfo.identifier];
[[NCMCBluetoothLEManager instance] sendCentralData:sysBroadcastNewDeviceData toPerihperal:peripheralDeviceInfo.identifier withMode:NCMCSessionSendDataReliable];
}
}
......@@ -331,7 +331,7 @@ void(^myInvitationHandler)(BOOL, NCMCSession*, NCMCPeerID*) = ^(BOOL accept, NCM
if (peripheralDeviceInfo.uniqueID != 0) {
NSData* peripheralDeviceData = [self encodeDeviceInfo:peripheralDeviceInfo];
NSData* sysBroadcastData = [self packSystemMessageWithType:CENTRA_PERIPHERAL_DEVICE_CONNECTED andMessage:peripheralDeviceData];
[[NCMCBluetoothLEManager instance] sendCentralData:sysBroadcastData toPerihperal:identifier];
[[NCMCBluetoothLEManager instance] sendCentralData:sysBroadcastData toPerihperal:identifier withMode:NCMCSessionSendDataReliable];
}
}
......@@ -427,7 +427,7 @@ void(^myInvitationHandler)(BOOL, NCMCSession*, NCMCPeerID*) = ^(BOOL accept, NCM
// data from peripheral to peripheral
NCMCDeviceInfo* targetDevice = [self getDeviceInfoByUniqueID:extraInfo];
if (targetDevice != nil) {
[[NCMCBluetoothLEManager instance] sendCentralData:data toPerihperal:targetDevice.identifier];
[[NCMCBluetoothLEManager instance] sendCentralData:data toPerihperal:targetDevice.identifier withMode:NCMCSessionSendDataReliable];
}
}
} else {
......
//
// NCMultipeerConnectivity.h
// NCMultipeerConnectivity
//
// Created by Chengzhao Li on 2016-06-30.
// Copyright © 2016 Chengzhao Li. All rights reserved.
//
#import "NCMCPeerID.h"
#import "NCMCSession.h"
#import "NCMCCentralService.h"
#import "NCMCPeripheralService.h"
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment