-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWorkerThread.m
More file actions
executable file
·104 lines (81 loc) · 1.79 KB
/
WorkerThread.m
File metadata and controls
executable file
·104 lines (81 loc) · 1.79 KB
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
/*!
@source WorkerThread.m
@project Pusher
@author Alexander Maksimenko
@copyright Copyright (c) 2009 Ripdev. All rights reserved.
*/
#import "WorkerThread.h"
@implementation WorkerThread
-(id) init
{
self = [super init];
_port = nil;
return self;
}
-(void) dealloc
{
[_port release];
[super dealloc];
}
-(void) instanceThreadMethod:(NSDictionary*) dict
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@try
{
_port = [[dict objectForKey:MachPortKey] retain];
[self checkinThread];
[self threadTask:dict];
}
@catch (NSException *ex)
{
Log(@"%@ %@", [ex name], [ex reason]);
}
@finally
{
[pool release];
}
}
-(void) checkinThread
{
NSPort* port = [NSMachPort port];
if(port)
{
[port setDelegate:self];
[[NSRunLoop currentRunLoop] addPort:port forMode:NSDefaultRunLoopMode];
NSPortMessage* msg = [[NSPortMessage alloc] initWithSendPort:_port receivePort:port components:nil];
if(msg)
{
[msg setMsgid:ThreadStartMessage];
[msg sendBeforeDate:[NSDate date]];
}
}
}
-(void) threadTask:(NSDictionary*) dict
{
}
-(void) startSun
{
[self sendMessage:SunStartMessage withData:nil];
}
-(void) stopSun
{
[self sendMessage:SunStopMessage withData:nil];
}
-(void) setText:(NSString*) text
{
[InstanceManager setText:text];
}
-(void) sendMessage:(int) msg withData:(NSData*) data
{
NSPortMessage* portMessage = [[NSPortMessage alloc] initWithSendPort:_port receivePort:nil components:(data == nil ? nil : [NSArray arrayWithObject:data])];
if(msg)
{
[portMessage setMsgid:msg];
BOOL res = FALSE;
while(!res)
res = [portMessage sendBeforeDate:[NSDate date]];
if(!res)
Log(@"Failed to send port message: %d\n", portMessage);
}
}
@end