-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkProxy.m
104 lines (88 loc) · 2.84 KB
/
NetworkProxy.m
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
//
// NetworkProxy.m
// DumbestGame
//
// Created by João Caxaria on 9/15/10.
// Copyright 2010 Imaginary Factory. All rights reserved.
//
#import "NetworkProxy.h"
@implementation NetworkProxy
-(id) init
{
icAssert(false, @"Should never call init");
self = [super init];
return self;
}
-(id) initWithSession:(GKSession*) msession
{
self = [super init];
session = [msession retain];
session.delegate = self;
[session setDataReceiveHandler: self withContext: nil];
return self;
}
-(void) setClient:(id<INetworkClient>) mnetworkClient
{
networkClient = [mnetworkClient retain];
}
-(void) sendMessage:(NetworkMessage*) message
{
@try {
NSError* error;
if(![session sendDataToAllPeers:[message getData] withDataMode:GKSendDataUnreliable error:&error])
icDEBUG(@"Could not send data :%@", [error localizedFailureReason]);
}
@catch (NSException * e) {
icDEBUG(@"Caught exception :%@", [e reason]);
}
}
#pragma mark GKSessionDelegate
/* Indicates a state change for the given peer.
*/
- (void)session:(GKSession *)msession peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state
{
switch(state)
{
case GKPeerStateAvailable:// not connected to session, but available for connectToPeer:withTimeout:
break;
case GKPeerStateConnecting: // waiting for accept, or deny response
break;
case GKPeerStateConnected: // connected to the session
break;
case GKPeerStateDisconnected: // disconnected from the session
[msession disconnectPeerFromAllPeers:peerID];
[networkClient networkError];
break;
case GKPeerStateUnavailable: // no longer available
[networkClient networkError];
break;
}
}
- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
{
// Read the bytes in data and perform an application-specific action.
[networkClient messageReceived:[NetworkMessage fromData:data]];
}
/* Indicates a connection request was received from another peer.
Accept by calling -acceptConnectionFromPeer:
Deny by calling -denyConnectionFromPeer:
*/
- (void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID
{
icAssert(false, @"Should never receive connection request");
}
/* Indicates a connection error occurred with a peer, which includes connection request failures, or disconnects due to timeouts.
*/
- (void)session:(GKSession *)session connectionWithPeerFailed:(NSString *)peerID withError:(NSError *)error
{
[networkClient networkError];
icDEBUG(@"Connection With Peer Failed: %s", [[error localizedFailureReason] UTF8String]);
}
/* Indicates an error occurred with the session such as failing to make available.
*/
- (void)session:(GKSession *)session didFailWithError:(NSError *)error
{
[networkClient networkError];
icDEBUG(@"Session did fail with error: %s", [[error localizedFailureReason] UTF8String]);
}
@end