-
Notifications
You must be signed in to change notification settings - Fork 0
/
PCBonjourServer.m
152 lines (120 loc) · 4.24 KB
/
PCBonjourServer.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// PCBonjourServer.m
// PCBonjourServerTests
//
// Created by Patrick Perini on 5/24/12.
// Licensing information available in README.md
//
#import "PCBonjourServer.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define randint(min, max) (arc4random() % ((max + 1) - min)) + min
@interface PCBonjourServer ()
- (void)fileHandleDidAcceptConnection: (NSNotification *)notification;
- (void)fileHandleDidCompleteReading: (NSNotification *)notification;
@end
@implementation PCBonjourServer
#pragma mark - Accessors and Mutators
@synthesize delegate;
- (NSString *)serviceName
{
return [netService name];
}
- (NSString *)description
{
return [NSString stringWithFormat: @"Server: %@", [netService description]];
}
#pragma mark - Licecycle Managers
- (id)initWithServiceNamed:(NSString *)serviceName delegate:(id<PCBonjourServerDelegate>)aDelegate
{
self = [super init];
if (!self)
return nil;
remoteHandles = [NSMutableSet set];
delegate = aDelegate;
// Initialize Net Service
NSString *type = [NSString stringWithFormat: @"_%@._tcp", [serviceName lowercaseString]];
do
{
netService = [[NSNetService alloc] initWithDomain: @"local."
type: type
name: serviceName
port: randint(10000, 40000)];
} while ([netService port] == -1);
// Initialize Socket
int socketDescriptor = socket(AF_INET, SOCK_STREAM, 0);
if (socketDescriptor == -1)
{
close(socketDescriptor);
return nil;
}
struct sockaddr_in socketAddress;
socketAddress.sin_family = AF_INET;
socketAddress.sin_port = htons([netService port]);
socketAddress.sin_addr.s_addr = htonl(INADDR_ANY);
int bound = bind(socketDescriptor, (struct sockaddr *) &socketAddress, sizeof(socketAddress));
if (bound == -1)
{
close(socketDescriptor);
return nil;
}
int listening = listen(socketDescriptor, INT_MAX);
if (listening == -1)
{
close(socketDescriptor);
return nil;
}
socketHandle = [[NSFileHandle alloc] initWithFileDescriptor: socketDescriptor
closeOnDealloc: YES];
// Add Responders
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(fileHandleDidAcceptConnection:)
name: NSFileHandleConnectionAcceptedNotification
object: nil];
return self;
}
- (void)dealloc
{
[self stop];
netService = nil;
socketHandle = nil;
[[NSNotificationCenter defaultCenter] removeObserver: self];
}
#pragma mark - Controllers
- (void)start
{
[netService publish];
[socketHandle acceptConnectionInBackgroundAndNotify];
}
- (void)stop
{
[netService stop];
}
#pragma mark - Responders
- (void)fileHandleDidAcceptConnection:(NSNotification *)notification
{
[socketHandle acceptConnectionInBackgroundAndNotify];
// Setup Remote Handle
NSFileHandle *remoteHandle = [[notification userInfo] objectForKey: NSFileHandleNotificationFileHandleItem];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(fileHandleDidCompleteReading:)
name: NSFileHandleReadCompletionNotification
object: remoteHandle];
[remoteHandle readInBackgroundAndNotify];
[remoteHandles addObject: remoteHandle];
}
- (void)fileHandleDidCompleteReading:(NSNotification *)notification
{
NSFileHandle *remoteHandle = [notification object];
[remoteHandle readInBackgroundAndNotify];
// Handle Incoming and Response Data
NSData *incomingData = [[notification userInfo] objectForKey: NSFileHandleNotificationDataItem];
if (delegate)
{
NSData *outgoingData = [delegate bonjourServer: self
responseForData: incomingData];
[remoteHandle writeData: outgoingData];
}
}
@end