-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKCNetServiceSession.m
65 lines (49 loc) · 1.73 KB
/
KCNetServiceSession.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
//
// KCNetServiceSession.m
// Degrees
//
// Created by Kevin Conner on 5/22/12.
// This software is free to use.
//
#import "KCNetServiceSession.h"
#import <arpa/inet.h>
@interface KCNetServiceSession ()
@property (nonatomic, strong) NSNetService *netService;
@end
@implementation KCNetServiceSession
@synthesize netService = _netService;
#pragma mark - KCSession
- (void)disconnect {
[super disconnect];
self.netService.delegate = nil;
self.netService = nil;
}
#pragma mark - Init/dealloc
- (id)initWithNetService:(NSNetService *)service delegate:(id<KCSessionDelegate>)delegate {
self = [super initWithDelegate:delegate];
if (self) {
// The browser could initialize sessions with the same service twice, but you don't want to ask the same NSNetService to resolve twice.
// So just make a copy of the found service and use that.
NSNetService *serviceCopy = [[NSNetService alloc] initWithDomain:service.domain type:service.type name:service.name];
self.netService = serviceCopy;
[self.netService setDelegate:self];
[self.netService resolveWithTimeout:2];
}
return self;
}
- (void)dealloc {
self.netService.delegate = nil;
}
#pragma mark - NSNetServiceDelegate
- (void)netServiceDidResolveAddress:(NSNetService *)sender {
NSInputStream *inputStream = nil;
NSOutputStream *outputStream = nil;
[self.netService getInputStream:&inputStream outputStream:&outputStream];
[self connectWithInputStream:inputStream outputStream:outputStream];
}
- (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict {
self.netService.delegate = nil;
self.netService = nil;
[self.delegate sessionDidNotConnect:self];
}
@end