15
15
#import <objc/runtime.h>
18
static void _setStandardSocketOpts(int socket)
21
setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof(yes));
23
setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout));
24
setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
27
18
@interface CLLocationManager (TappityServer)
29
20
- (void) setDelegate_tappity: (id) delegate;
134
125
NSString* name = [[NSBundle mainBundle] bundleIdentifier];
136
[[TappityServer sharedServer] startWithName: name];
127
[[TappityServer sharedServer] startBonjourServerWithName: name protocol: @"_tappity._tcp" port: 1234];
138
129
Class lmc = NSClassFromString(@"CLLocationManager");
182
- (void) acceptThread: (id) info
173
- (void) connectionWasEstablished
184
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
191
while ([self threadActive: info])
207
FD_SET(listenSocket, &readfds);
208
FD_SET(listenSocket, &errorfds);
209
maxSocket = MAX(maxSocket, listenSocket);
213
printf("No sockets, sleeping for a bit...\n");
218
//NSLog(@"listening for connection...");
220
if (select(maxSocket+1, &readfds, &writefds, &errorfds, &tv) < 0)
226
if (FD_ISSET(listenSocket, &readfds))
228
NSLog(@"Accepting connection...");
230
socklen_t sinSize = sizeof(struct sockaddr_in);
231
struct sockaddr_in peerAddress;
232
int newSocket = accept(listenSocket, (struct sockaddr *)&peerAddress, &sinSize);
234
_setStandardSocketOpts(newSocket);
239
if (errno == EWOULDBLOCK)
244
printf("Error accepting connection.\n");
252
// add socket to sockets list
253
commsSocket = newSocket;
257
[netService release];
261
sendLock = [[NSCondition alloc] init];
263
[self runThreadWithSelector: @selector(sendingThread:)];
265
[self runThreadWithSelector: @selector(receivingThread:)];
267
[self performSelectorOnMainThread: @selector(startScreenieTimer) withObject: nil waitUntilDone: NO];
269
//[self runThreadWithSelector: @selector(screenieThread:)];
272
NSLog(@"Accepted connection.");
175
[self performSelectorOnMainThread: @selector(startScreenieTimer) withObject: nil waitUntilDone: NO];
286
178
- (void) startScreenieTimer
295
187
[self runThreadWithSelector: @selector(imageConversionThread:)];
298
- (BOOL) enableBonjourWithDomain:(NSString*)domain applicationProtocol:(NSString*)protocol name:(NSString*)name
301
domain = @""; //Will use default Bonjour registration doamins, typically just ".local"
303
name = @""; //Will use default Bonjour name, e.g. the name assigned to the device in iTunes
305
assert([protocol length] && listenSocket);
307
NSLog(@"tappity port: %d", ntohs(myAddress.sin_port));
309
netService = [[NSNetService alloc] initWithDomain: domain type: protocol name: name port: ntohs(myAddress.sin_port)];
310
if(netService == nil)
313
[netService setDelegate:self];
314
// [netService scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
315
[netService publish];
320
- (void) startListening
322
myAddress.sin_family = AF_INET; // host byte order
323
myAddress.sin_port = htons(1234); // short, network byte order, any port
324
myAddress.sin_addr.s_addr = htonl(INADDR_ANY); // auto-fill with my IP
326
listenSocket = socket(PF_INET, SOCK_STREAM, 0);
327
assert(listenSocket != -1);
329
_setStandardSocketOpts(listenSocket);
331
int err = bind(self->listenSocket, (struct sockaddr *)&myAddress, sizeof(myAddress));
334
err = listen(self->listenSocket, 1);
337
[self runThreadWithSelector: @selector(acceptThread:)];
339
if([self enableBonjourWithDomain: @"" applicationProtocol: @"_tappity._tcp" name: serviceName])
340
NSLog(@"tappity bounjour advertisments up and running");
344
- (void) startWithName: (NSString*) name
346
NSLog(@"starting tappity server");
348
[serviceName release];
349
serviceName = [name retain];
351
[self startListening];
356
191
static size_t _read_i8(const void* buf, uint8_t* p)
463
298
- (void) dataReceived: (NSDictionary*) dataDict
465
uint32_t messageId = [[dataDict objectForKey: TapIdKey] intValue];
300
uint32_t messageId = [[dataDict objectForKey: SocketMessageIdKey] intValue];
467
NSLog(@"messageid %d", messageId);
302
// NSLog(@"messageid %d", messageId);
469
NSData* tapData = [dataDict objectForKey: TapDataKey];
304
NSData* tapData = [dataDict objectForKey: SocketMessageDataKey];
471
306
// NSDictionary* tapDict = [NSPropertyListSerialization propertyListFromData: tapData mutabilityOption:NSPropertyListImmutable format: NULL errorDescription: NULL];