Ejemplo n.º 1
0
CACFLocalMessagePort::CACFLocalMessagePort(CFStringRef inName, CFMessagePortCallBack inPortCallBack, CFMessagePortInvalidationCallBack inInvalidationCallBack, void* inUserData)
:
	mMessagePort(NULL),
	mRunLoopSource(NULL)
{
	//	create the CFMessagePort
	CFMessagePortContext theContext = { 0, inUserData, NULL, NULL, NULL };
	mMessagePort = CFMessagePortCreateLocal(NULL, inName, inPortCallBack, &theContext, NULL);
	if(mMessagePort != NULL)
	{
		//	add the invalidation callback, if any
		if(inInvalidationCallBack != NULL)
		{
			CFMessagePortSetInvalidationCallBack(mMessagePort, inInvalidationCallBack);
		}
	
		//	get the run loop source
		mRunLoopSource = CFMessagePortCreateRunLoopSource(NULL, mMessagePort, NULL);
		if(mRunLoopSource == NULL)
		{
			CFRelease(mMessagePort);
			mMessagePort = NULL;
			DebugMessage("CACFLocalMessagePort::CACFLocalMessagePort: couldn't create the CFRunLoopSource");
			throw CAException('what');
		}
	}
}
OSStatus BIMInitializeMessageReceiving( void )
{
    OSStatus			result;
    CFStringRef			portName;
    CFMessagePortContext	context;
    CFMessagePortRef		port;
    CFRunLoopRef		runLoop;
    CFRunLoopSourceRef		source;
    ProcessSerialNumber		processSerialNumber;

    result = noErr;
    port = NULL;
    source = NULL;

    //  We need a unique port name on which to listen for messages. We use our process serial
    //  number to create a unique port name of the form "com.apple.dts.BIMServerxxxxxx" where
    //  xxxxxx is the process serial number of our host application. We will listen for messages
    //  on this port.

    result = GetCurrentProcess( &processSerialNumber );
    if( result == noErr )
        portName = BIMCreatePortName( &processSerialNumber );
    
    //  Create a port on which we will listen for messages.
    
    if( result == noErr ) {
        context.version = 0;
        context.info = NULL;
        context.retain = NULL;
        context.release = NULL;
        context.copyDescription = NULL;
        port = CFMessagePortCreateLocal( NULL, portName, BIMMessagePortCallBack, &context, NULL );
        if( port == NULL )
            result = -1;
    }
        
    //  Set up the port with the run loop.

    if( result == noErr ) {
        runLoop = CFRunLoopGetCurrent();
        if (runLoop == NULL)
            result = -2;
    }
    if( result == noErr ) {
        source = CFMessagePortCreateRunLoopSource( NULL, port, 0 );
        if( source == NULL )
            result = -3;
    }
    if( result == noErr )
        CFRunLoopAddSource( runLoop, source, kCFRunLoopCommonModes );
    
    //	Release everything.
    
    if( source )
        CFRelease( source );
    if( port )
        CFRelease( port );
    return result;
}
SC_MachMessagePort::SC_MachMessagePort(struct World *inWorld, CFStringRef serverPortName, CFStringRef replyPortName)
    :   SC_CmdPort(inWorld), mServerPort(NULL), mReplyPort(NULL)
{
    CFMessagePortContext context = { 0, this, NULL, NULL, NULL };
    mServerPort = CFMessagePortCreateLocal(NULL, serverPortName, &messagePortCallBack, &context, NULL);
    if (replyPortName) mReplyPort = CFMessagePortCreateRemote(NULL, replyPortName);
    Start();
}
Ejemplo n.º 4
0
 //
 // open_mach_port()
 //
 // Opens the mach communication port.
 //
 void Monitor::open_mach_port() {
     // XXX_PCB:  this is quite skanky, using CF from here. we expect to be called at the earliest when a GC thread is registered.
     CFStringRef format = CFStringCreateWithCString(NULL, "com.apple.auto.%d", kCFStringEncodingUTF8);
     CFStringRef name = CFStringCreateWithFormat(NULL, NULL, format, getpid());
     CFRelease(format);
     CFMessagePortContext context = { 0, this, NULL, NULL, NULL };
     _request_port = CFMessagePortCreateLocal(NULL, name, receive_request, &context, NULL);
     CFRelease(name);
     CFRunLoopSourceRef source = CFMessagePortCreateRunLoopSource(NULL, _request_port, 0);
     CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes);
     CFRelease(source);
     CFRunLoopRun();
 }
Ejemplo n.º 5
0
extern GPDuplexClientRef GPDuplexClient_Create() {
	CFRunLoopRef runLoop = CFRunLoopGetCurrent();
	GPDuplexClientRef client = calloc(1, sizeof(GPDuplexClientRef));
	client->refcount = 1;
	client->observers = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
	
	// (1) Obtain the server port.
	client->serverPort = CFMessagePortCreateRemote(NULL, CFSTR("hk.kennytm.GriP.server"));
	if (client->serverPort == NULL) {
		CFShow(CFSTR("GPDuplexClient_Init(): Cannot create server port. Is GriP running?"));
		GPDuplexClient_Release(client);
		return NULL;
	}
	
	// (2) ask the server port for a unique ID.
	CFDataRef pidData = NULL;
	SInt32 errorCode = CFMessagePortSendRequest(client->serverPort, GPMessage_GetClientPortID, NULL, 1, 1, kCFRunLoopDefaultMode, &pidData);
	if (errorCode != kCFMessagePortSuccess || pidData == NULL) {
		CFLog(4, CFSTR("GPDuplexClient_Init(): Cannot obtain a unique client port ID from server. Error code = %d and pidData = %@."), errorCode, pidData);
		if (pidData != NULL)
			CFRelease(pidData);
		GPDuplexClient_Release(client);
		return NULL;
	}
	
	// (3) Create client port from UID.
	const char* clientPortCString = (const char*)CFDataGetBytePtr(pidData);
	CFStringRef clientPortName = CFStringCreateWithCString(NULL, clientPortCString, kCFStringEncodingUTF8);
	CFMessagePortContext clientContext = {0, client, NULL, NULL, NULL};
	Boolean shouldFreeInfo = false;
	client->clientPort = CFMessagePortCreateLocal(NULL, clientPortName, &GPClientCallback, &clientContext, &shouldFreeInfo);
	if (shouldFreeInfo || client->clientPort == NULL) {
		CFLog(4, CFSTR("GPDuplexClient_Init(): Cannot create client port with port name %@."), clientPortName);
		CFRelease(clientPortName);
		CFRelease(pidData);
		GPDuplexClient_Release(client);
		return NULL;
	}
	CFRelease(clientPortName);
	
	// (4) Add client port to run loop.
	client->clientSource = CFMessagePortCreateRunLoopSource(NULL, client->clientPort, 0);
	CFRunLoopAddSource(runLoop, client->clientSource, kCFRunLoopDefaultMode);
	
	CFRelease(pidData);
	
	CFLog(4, CFSTR("create client = %@"), client->clientPort);
	
	return client;
}
Ejemplo n.º 6
0
CACFLocalMessagePort::CACFLocalMessagePort(CFStringRef inName, CFMessagePortCallBack inPortCallBack, CFMessagePortInvalidationCallBack inInvalidationCallBack, void* inUserData)
:
	mMessagePort(NULL),
	mRunLoopSource(NULL),
	mDispatchQueue(NULL)
{
	//	create the CFMessagePort
	CFMessagePortContext theContext = { 0, inUserData, NULL, NULL, NULL };
	mMessagePort = CFMessagePortCreateLocal(NULL, inName, inPortCallBack, &theContext, NULL);
	if(mMessagePort != NULL)
	{
		//	add the invalidation callback, if any
		if(inInvalidationCallBack != NULL)
		{
			CFMessagePortSetInvalidationCallBack(mMessagePort, inInvalidationCallBack);
		}
	}
}
Ejemplo n.º 7
0
void
UniqueApplication::init1()
{
    if (m_alreadyRunning) {
        Q_ASSERT_X( 0, "init1", "You're doing it wrong" );
        return;
    }

#ifdef WIN32
    ::CreateMutexA( NULL, false, m_id ); //extern const char*
    
    // The call fails with ERROR_ACCESS_DENIED if the Mutex was 
    // created in a different users session because of passing
    // NULL for the SECURITY_ATTRIBUTES on Mutex creation);
    //bool success = ::GetLastError() == ERROR_ALREADY_EXISTS || ::GetLastError() == ERROR_ACCESS_DENIED;
#endif
    
#ifdef __APPLE__
    if (m_port == 0)
    {
        CFStringRef cfid = CFStringCreateWithCString( NULL, m_id, kCFStringEncodingISOLatin1 );
        
        CFMessagePortContext context;
        context.version = 0;
        context.info = this;
        context.retain = 0;
        context.release = 0;
        context.copyDescription = 0;
        m_port = CFMessagePortCreateLocal( kCFAllocatorDefault, 
                                           cfid, 
                                           MacCallBack, 
                                           &context, 
                                           0 );
        CFRunLoopRef runloop = CFRunLoopGetCurrent();
        if (m_port && runloop) {
            CFRunLoopSourceRef source = CFMessagePortCreateRunLoopSource( 0, m_port, 0 );
            if (source)
                CFRunLoopAddSource( runloop, source, kCFRunLoopCommonModes );
            CFRelease( source );
        }
        CFRelease( cfid );
    }    
#endif
}
Ejemplo n.º 8
0
Zirk2Port::Zirk2Port() : mMessagePort(NULL), mRunLoopSource(NULL)
{
	Boolean shouldFreeInfo;
	CFMessagePortContext context;
	context.version = 0;
	context.info = this;
	context.retain = NULL;
	context.release = NULL;
	context.copyDescription = NULL;
	
	mMessagePort = CFMessagePortCreateLocal(NULL, NULL, PortManagerCallBack, &context, &shouldFreeInfo);
	if (shouldFreeInfo || (!mMessagePort)) {
		CFShow(CFSTR("Could not create message port"));
		mMessagePort = NULL;
		return;
	}
	
	mRunLoopSource = CFMessagePortCreateRunLoopSource(NULL, mMessagePort, 0);
}
Ejemplo n.º 9
0
static DWORD WINAPI MIDIIn_MessageThread(LPVOID p)
{
    CFMessagePortRef local;
    CFRunLoopSourceRef source;
    Boolean info;

    local = CFMessagePortCreateLocal(kCFAllocatorDefault, MIDIInThreadPortName, &MIDIIn_MessageHandler, NULL, &info);

    source = CFMessagePortCreateRunLoopSource(kCFAllocatorDefault, local, 0);
    CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);

    CFRunLoopRun();

    CFRunLoopSourceInvalidate(source);
    CFRelease(source);
    CFRelease(local);
    CFRelease(MIDIInThreadPortName);
    MIDIInThreadPortName = NULL;

    return 0;
}
Ejemplo n.º 10
0
int ServerMain()
{
	// Reachability
	struct sockaddr_in address;
	bzero(&address, sizeof(address));
	address.sin_len = sizeof(address);
	address.sin_family = AF_INET;

	reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *) &address);

	if(!SCNetworkReachabilitySetCallback(reachability, &ReachabilityCallback, NULL))
		return EXIT_FAILURE;

	if(!SCNetworkReachabilityScheduleWithRunLoop(reachability, CFRunLoopGetCurrent(), kCFRunLoopCommonModes))
		return EXIT_FAILURE;

	{
		SCNetworkReachabilityFlags flags;
		SCNetworkReachabilityGetFlags(reachability, &flags);

		ReachabilityCallback(reachability, flags, NULL);
	}

	// CFMessagePort
	CFMessagePortRef localPort = CFMessagePortCreateLocal(nil, kServerPortName, &IPCCallback, nil, nil);
	CFRunLoopSourceRef runLoopSource = CFMessagePortCreateRunLoopSource(nil, localPort, 0);

	CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);

	CFRunLoopRun();

	CFRunLoopRemoveSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);

	CFRelease(runLoopSource);
	CFRelease(localPort);
	CFRelease(reachability);

	return 0;
}
Ejemplo n.º 11
0
int main() {
  CFMessagePortRef port = CFMessagePortCreateLocal(NULL, CFSTR("test"), NULL, NULL, NULL);
  printf("Port %s\n", (port != NULL ? "successfully created" : "could not be created"));
}