30
30
NSString* TapLocationKey = @"tapLocation";
33
@interface Tappity (Private)
34
- (void) dataReceived: (NSData*) data;
37
33
@implementation Tappity
39
- (BOOL) threadActive: (id) key
41
return [[activeThreads objectForKey: key] boolValue];
44
- (void) threadWillExit: (id) key
48
[activeThreads removeObjectForKey: key];
50
if (![activeThreads count])
60
- (void) receivingThread: (id) info
62
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
69
while ([self threadActive: info])
90
FD_SET(socket, &readfds);
91
FD_SET(socket, &errorfds);
92
maxSocket = MAX(maxSocket, socket);
96
//printf("No sockets, sleeping for a bit...\n");
101
if (select(maxSocket+1, &readfds, &writefds, &errorfds, &tv) < 0)
107
if (FD_ISSET(socket, &readfds))
109
//NSLog(@"receiving...");
110
if (!expectedMessageSize)
112
uint32_t header[2] = {0,0};
113
int actuallyRead = 0;
115
actuallyRead = recv(socket, header, 8, MSG_PEEK);
117
if (actuallyRead == 8)
119
actuallyRead = recv(socket, header, 8, 0);
120
expectedMessageSize = ntohl(header[1]);
121
currentMessageId = ntohl(header[0]);
122
currentData = [[NSMutableData alloc] initWithLength: expectedMessageSize];
124
else if (actuallyRead == -1)
126
//close(commsSocket);
127
//self->commsSocket = 0;
128
if (errno != ETIMEDOUT)
130
printf("Connection dropped with error.\n");
134
else if (actuallyRead == 0)
136
printf("remote socket closed.\n");
142
size_t readAmount = expectedMessageSize - currentlyRead;
143
int actuallyRead = 0;
145
actuallyRead = recv(socket, [currentData mutableBytes] + currentlyRead, readAmount, 0);
146
if (actuallyRead == -1)
148
printf("Connection dropped with error.\n");
151
else if (actuallyRead == 0)
153
printf("remote socket closed.\n");
157
currentlyRead += actuallyRead;
159
if (currentlyRead == expectedMessageSize)
163
expectedMessageSize = 0;
165
if (!receivedPackets)
166
receivedPackets = [[NSMutableArray alloc] init];
167
[receivedPackets addObject: currentData];
168
[currentData release];
173
if (receiveDataOnMainThread)
174
[self performSelectorOnMainThread: @selector(dataReceived:) withObject: [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: currentMessageId], TapIdKey, currentData, TapDataKey, nil] waitUntilDone: NO];
176
[self dataReceived: [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: currentMessageId], TapIdKey, currentData, TapDataKey, nil]];
186
[self threadWillExit: info];
198
- (void) runThreadWithSelector: (SEL) selector
203
activeThreads = [[NSMutableDictionary alloc] init];
204
id number = [NSNumber numberWithInt: threadIds++];
206
[activeThreads setObject: [NSNumber numberWithBool: YES] forKey: number];
207
[NSThread detachNewThreadSelector: selector toTarget: self withObject: [number retain]];
211
- (void) sendData: (NSData*) tapData withIdentifier: (NSInteger) messageId
216
sendQueue = [[NSMutableArray alloc] init];
218
[sendQueue addObject: [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInteger: messageId], TapIdKey, tapData, TapDataKey, nil]];
226
- (void) sendingThread: (id) info
228
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
235
while ([self threadActive: info])
238
while (![sendQueue count])
243
NSDictionary* dict = [[sendQueue objectAtIndex: 0] retain];
244
NSData* data = [dict objectForKey: TapDataKey];
245
uint32_t messageId = [[dict objectForKey: TapIdKey] intValue];
246
[sendQueue removeObjectAtIndex: 0];
250
size_t sizeToSend = 8;
252
uint32_t header[2] = {htonl(messageId), htonl([data length])};
258
socket = commsSocket;
261
while (dataSent < sizeToSend)
263
if ((err = send(socket, header + dataSent, sizeToSend - dataSent, 0)) == -1)
274
sizeToSend = [data length];
276
while (dataSent < sizeToSend)
278
if ((err = send(socket, [data bytes] + dataSent, sizeToSend - dataSent, 0)) == -1)
296
[self threadWillExit: info];
37
if (!(self = [super init]))