@@ -199,56 +199,58 @@ -(id) initWithProcessName:(NSString*) processName
199199
200200-(void ) serverThreadMain
201201{
202- DDLogInfo (@" Now running IPC server for '%@ ' with thread priority %f ..." , _processName, [NSThread threadPriority ]);
203- // register darwin notification handler for "im.monal.ipc.wakeup:<process name>" which is used to wake up readNextMessage using the NSCondition
204- CFNotificationCenterAddObserver (_darwinNotificationCenterRef, (__bridge void *) self, &darwinNotificationCenterCallback, (__bridge CFNotificationName)[NSString stringWithFormat: @" im.monal.ipc.wakeup:%@ " , _processName], NULL , 0 );
205- CFNotificationCenterAddObserver (_darwinNotificationCenterRef, (__bridge void *) self, &darwinNotificationCenterCallback, (__bridge CFNotificationName)@" im.monal.ipc.wakeup:*" , NULL , 0 );
206- while (![[NSThread currentThread ] isCancelled ])
207- {
208- NSDictionary * message = [self readNextMessage ]; // this will be blocking
209- if (!message)
210- continue ;
211- DDLogDebug (@" Got IPC message: %@ " , message);
212-
213- // use a dedicated serial queue for every IPC receiver to maintain IPC message ordering while not blocking other receivers or this serverThread)
214- NSArray * parts = [message[@" name" ] componentsSeparatedByString: @" ." ];
215- NSString * queueName = [parts objectAtIndex: 0 ];
216- if (!queueName || [parts count ]<2 )
217- queueName = @" _default" ;
218- queueName = [NSString stringWithFormat: @" ipc.queue:%@ " , queueName];
219- if (!_ipcQueues[queueName])
220- _ipcQueues[queueName] = dispatch_queue_create ([queueName cStringUsingEncoding: NSUTF8StringEncoding], dispatch_queue_attr_make_with_qos_class (DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INTERACTIVE, 0 ));
221-
222- // handle all responses (don't trigger a kMonalIncomingIPC for responses)
223- if (message[@" response_to" ] && [message[@" response_to" ] intValue ] > 0 )
202+ @autoreleasepool {
203+ DDLogInfo (@" Now running IPC server for '%@ ' with thread priority %f ..." , _processName, [NSThread threadPriority ]);
204+ // register darwin notification handler for "im.monal.ipc.wakeup:<process name>" which is used to wake up readNextMessage using the NSCondition
205+ CFNotificationCenterAddObserver (_darwinNotificationCenterRef, (__bridge void *) self, &darwinNotificationCenterCallback, (__bridge CFNotificationName)[NSString stringWithFormat: @" im.monal.ipc.wakeup:%@ " , _processName], NULL , 0 );
206+ CFNotificationCenterAddObserver (_darwinNotificationCenterRef, (__bridge void *) self, &darwinNotificationCenterCallback, (__bridge CFNotificationName)@" im.monal.ipc.wakeup:*" , NULL , 0 );
207+ while (![[NSThread currentThread ] isCancelled ])
224208 {
225- // call response handler if one is present (ignore the spurious response otherwise)
226- if (_responseHandlers[message[@" response_to" ]])
209+ NSDictionary * message = [self readNextMessage ]; // this will be blocking
210+ if (!message)
211+ continue ;
212+ DDLogDebug (@" Got IPC message: %@ " , message);
213+
214+ // use a dedicated serial queue for every IPC receiver to maintain IPC message ordering while not blocking other receivers or this serverThread)
215+ NSArray * parts = [message[@" name" ] componentsSeparatedByString: @" ." ];
216+ NSString * queueName = [parts objectAtIndex: 0 ];
217+ if (!queueName || [parts count ]<2 )
218+ queueName = @" _default" ;
219+ queueName = [NSString stringWithFormat: @" ipc.queue:%@ " , queueName];
220+ if (!_ipcQueues[queueName])
221+ _ipcQueues[queueName] = dispatch_queue_create ([queueName cStringUsingEncoding: NSUTF8StringEncoding], dispatch_queue_attr_make_with_qos_class (DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INTERACTIVE, 0 ));
222+
223+ // handle all responses (don't trigger a kMonalIncomingIPC for responses)
224+ if (message[@" response_to" ] && [message[@" response_to" ] intValue ] > 0 )
227225 {
228- IPC_response_handler_t responseHandler = (IPC_response_handler_t)_responseHandlers[message[ @" response_to " ]];
229- if (responseHandler )
226+ // call response handler if one is present (ignore the spurious response otherwise)
227+ if (_responseHandlers[message[ @" response_to " ]] )
230228 {
231- // responses handlers are only valid for the maximum RTT of messages (+ some safety margin)
232- createTimer (MSG_TIMEOUT*2 + 1 , (^{
233- [_responseHandlers removeObjectForKey: message[@" response_to" ]];
234- }));
235- dispatch_async (_ipcQueues[queueName], ^{
236- responseHandler (message);
237- });
229+ IPC_response_handler_t responseHandler = (IPC_response_handler_t)_responseHandlers[message[@" response_to" ]];
230+ if (responseHandler)
231+ {
232+ // responses handlers are only valid for the maximum RTT of messages (+ some safety margin)
233+ createTimer (MSG_TIMEOUT*2 + 1 , (^{
234+ [_responseHandlers removeObjectForKey: message[@" response_to" ]];
235+ }));
236+ dispatch_async (_ipcQueues[queueName], ^{
237+ responseHandler (message);
238+ });
239+ }
238240 }
239241 }
242+ else // publish all non-responses (using the message name as object allows for filtering by ipc message name)
243+ dispatch_async (_ipcQueues[queueName], ^{
244+ [[NSNotificationCenter defaultCenter ] postNotificationName: kMonalIncomingIPC object: message[@" name" ] userInfo: message];
245+ });
246+
247+ DDLogDebug (@" Handled IPC message: %@ " , message);
240248 }
241- else // publish all non-responses (using the message name as object allows for filtering by ipc message name)
242- dispatch_async (_ipcQueues[queueName], ^{
243- [[NSNotificationCenter defaultCenter ] postNotificationName: kMonalIncomingIPC object: message[@" name" ] userInfo: message];
244- });
245-
246- DDLogDebug (@" Handled IPC message: %@ " , message);
249+ // unregister darwin notification handler
250+ CFNotificationCenterRemoveObserver (_darwinNotificationCenterRef, (__bridge void *) self, (__bridge CFNotificationName)[NSString stringWithFormat: @" im.monal.ipc.wakeup:%@ " , _processName], NULL );
251+ CFNotificationCenterRemoveObserver (_darwinNotificationCenterRef, (__bridge void *) self, (__bridge CFNotificationName)@" im.monal.ipc.wakeup:*" , NULL );
252+ DDLogInfo (@" IPC server for '%@ ' now terminated" , _processName);
247253 }
248- // unregister darwin notification handler
249- CFNotificationCenterRemoveObserver (_darwinNotificationCenterRef, (__bridge void *) self, (__bridge CFNotificationName)[NSString stringWithFormat: @" im.monal.ipc.wakeup:%@ " , _processName], NULL );
250- CFNotificationCenterRemoveObserver (_darwinNotificationCenterRef, (__bridge void *) self, (__bridge CFNotificationName)@" im.monal.ipc.wakeup:*" , NULL );
251- DDLogInfo (@" IPC server for '%@ ' now terminated" , _processName);
252254}
253255
254256-(void ) incomingDarwinNotification : (NSString *) name
0 commit comments