RSS

(root)/iphone/tappity : 52 : tappity/source/TappityServer.m

To get this branch, use:
bzr branch /browse/iphone/tappity

« back to all changes in this revision

Viewing changes to tappity/source/TappityServer.m

Dömötör Gulyás
2009-10-31 12:32:32
Revision ID: dognotdog@gmail.com-20091031113232-lvx5g6lwnze2ugpu
moves networking into common class

Show diffs side-by-side

added added

removed removed

15
15
#import <objc/runtime.h>
16
16
 
17
17
 
18
 
static void _setStandardSocketOpts(int socket)
19
 
{
20
 
    int yes = 1;
21
 
    setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof(yes));
22
 
        int timeout = 2000;
23
 
        setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout));
24
 
        setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
25
 
}
26
 
 
27
18
@interface CLLocationManager (TappityServer)
28
19
 
29
20
- (void) setDelegate_tappity: (id) delegate;
133
124
        
134
125
        NSString* name = [[NSBundle mainBundle] bundleIdentifier];
135
126
        
136
 
        [[TappityServer sharedServer] startWithName: name];
 
127
        [[TappityServer sharedServer] startBonjourServerWithName: name protocol: @"_tappity._tcp" port: 1234];
137
128
        
138
129
        Class lmc = NSClassFromString(@"CLLocationManager");
139
130
        if (lmc)
179
170
        return self;
180
171
}
181
172
 
182
 
- (void) acceptThread: (id) info
 
173
- (void) connectionWasEstablished
183
174
{
184
 
        NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
185
 
 
186
 
        @synchronized(self)
187
 
        {
188
 
                [self retain];
189
 
        }
190
 
 
191
 
        while ([self threadActive: info])
192
 
        {
193
 
 
194
 
                struct timeval tv;
195
 
                fd_set readfds;
196
 
                fd_set writefds;
197
 
                fd_set errorfds;
198
 
                int maxSocket = -1;
199
 
 
200
 
                tv.tv_sec = 1;
201
 
                tv.tv_usec = 0;
202
 
 
203
 
                FD_ZERO(&readfds);
204
 
                FD_ZERO(&writefds);
205
 
                FD_ZERO(&errorfds);
206
 
 
207
 
                FD_SET(listenSocket, &readfds);
208
 
                FD_SET(listenSocket, &errorfds);
209
 
                maxSocket = MAX(maxSocket, listenSocket);
210
 
                
211
 
                if (maxSocket < 0)
212
 
                {
213
 
                        printf("No sockets, sleeping for a bit...\n");
214
 
                        usleep(1000000);
215
 
                        continue;
216
 
                }
217
 
                
218
 
                //NSLog(@"listening for connection...");
219
 
 
220
 
                if (select(maxSocket+1, &readfds, &writefds, &errorfds, &tv) < 0)
221
 
                {
222
 
                        perror("select");
223
 
                        break;
224
 
                }
225
 
 
226
 
                if (FD_ISSET(listenSocket, &readfds))
227
 
                {
228
 
                        NSLog(@"Accepting connection...");
229
 
                        // accept
230
 
                        socklen_t       sinSize = sizeof(struct sockaddr_in);
231
 
                        struct sockaddr_in      peerAddress;
232
 
                        int newSocket = accept(listenSocket, (struct sockaddr *)&peerAddress, &sinSize);
233
 
                        
234
 
                        _setStandardSocketOpts(newSocket);
235
 
 
236
 
                        
237
 
                        if (newSocket == -1)
238
 
                        {
239
 
                                if (errno == EWOULDBLOCK)
240
 
                                {
241
 
                                }
242
 
                                else
243
 
                                {
244
 
                                        printf("Error accepting connection.\n");
245
 
                                        break;
246
 
                                }
247
 
                        }
248
 
                        
249
 
                        if (commsSocket)
250
 
                                close(commsSocket);
251
 
 
252
 
                        // add socket to sockets list
253
 
                        commsSocket = newSocket;
254
 
                        
255
 
                        
256
 
                        [netService stop];
257
 
                        [netService release];
258
 
                        netService = nil;
259
 
 
260
 
                        if (!sendLock)
261
 
                                sendLock = [[NSCondition alloc] init];
262
 
                        
263
 
                        [self runThreadWithSelector: @selector(sendingThread:)];
264
 
 
265
 
                        [self runThreadWithSelector: @selector(receivingThread:)];
266
 
                        
267
 
                        [self performSelectorOnMainThread: @selector(startScreenieTimer) withObject: nil waitUntilDone: NO];
268
 
 
269
 
                        //[self runThreadWithSelector: @selector(screenieThread:)];
270
 
                        
271
 
                        
272
 
                        NSLog(@"Accepted connection.");
273
 
                }
274
 
 
275
 
        }
276
 
 
277
 
        @synchronized(self)
278
 
        {
279
 
                [self release];
280
 
        }
281
 
 
282
 
        [info release];
283
 
        [pool drain];
 
175
        [self performSelectorOnMainThread: @selector(startScreenieTimer) withObject: nil waitUntilDone: NO];
284
176
}
285
177
 
286
178
- (void) startScreenieTimer
295
187
        [self runThreadWithSelector: @selector(imageConversionThread:)];
296
188
}
297
189
 
298
 
- (BOOL) enableBonjourWithDomain:(NSString*)domain applicationProtocol:(NSString*)protocol name:(NSString*)name
299
 
{
300
 
        if(![domain length])
301
 
                domain = @""; //Will use default Bonjour registration doamins, typically just ".local"
302
 
        if(![name length])
303
 
                name = @""; //Will use default Bonjour name, e.g. the name assigned to the device in iTunes
304
 
        
305
 
        assert([protocol length] && listenSocket);
306
 
        
307
 
        NSLog(@"tappity port: %d", ntohs(myAddress.sin_port));
308
 
 
309
 
        netService = [[NSNetService alloc] initWithDomain: domain type: protocol name: name port: ntohs(myAddress.sin_port)];
310
 
        if(netService == nil)
311
 
                return NO;
312
 
        
313
 
        [netService setDelegate:self];
314
 
//      [netService scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
315
 
        [netService publish];
316
 
        
317
 
        return YES;
318
 
}
319
 
 
320
 
- (void) startListening
321
 
{
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
325
 
 
326
 
        listenSocket = socket(PF_INET, SOCK_STREAM, 0);
327
 
        assert(listenSocket != -1);
328
 
 
329
 
        _setStandardSocketOpts(listenSocket);
330
 
 
331
 
        int err = bind(self->listenSocket, (struct sockaddr *)&myAddress, sizeof(myAddress));
332
 
        assert(-1 != err);
333
 
        
334
 
        err = listen(self->listenSocket, 1);
335
 
        assert(-1 != err);
336
 
 
337
 
        [self runThreadWithSelector: @selector(acceptThread:)];
338
 
        
339
 
        if([self enableBonjourWithDomain: @"" applicationProtocol: @"_tappity._tcp" name: serviceName])
340
 
                NSLog(@"tappity bounjour advertisments up and running");
341
 
 
342
 
}
343
 
 
344
 
- (void) startWithName: (NSString*) name
345
 
{
346
 
        NSLog(@"starting tappity server");
347
 
        
348
 
        [serviceName release];
349
 
        serviceName = [name retain];
350
 
        
351
 
        [self startListening];
352
 
 
353
 
}
354
 
 
355
190
/*
356
191
static size_t _read_i8(const void* buf, uint8_t* p)
357
192
{
462
297
 
463
298
- (void) dataReceived: (NSDictionary*) dataDict
464
299
{
465
 
        uint32_t messageId = [[dataDict objectForKey: TapIdKey] intValue];
 
300
        uint32_t messageId = [[dataDict objectForKey: SocketMessageIdKey] intValue];
466
301
        
467
 
        NSLog(@"messageid %d", messageId);
 
302
//      NSLog(@"messageid %d", messageId);
468
303
 
469
 
        NSData* tapData = [dataDict objectForKey: TapDataKey];
 
304
        NSData* tapData = [dataDict objectForKey: SocketMessageDataKey];
470
305
        
471
306
//      NSDictionary* tapDict = [NSPropertyListSerialization propertyListFromData: tapData mutabilityOption:NSPropertyListImmutable format: NULL errorDescription: NULL];
472
307
        assert(tapData);
809
644
 
810
645
- (void) dealloc
811
646
{
812
 
        [serviceName release];
813
647
        [captureTimer invalidate];
814
648
        [capturedImagesCondition release];
815
649
        [capturedImagesQueue release];
816
650
 
817
 
        [netService stop];
818
 
        [netService release];
819
 
 
820
 
        [activeThreads release];
821
 
 
822
651
        [super dealloc];
823
652
}
824
653
 

Loggerhead 1.17 is a web-based interface for Bazaar branches