void SocketStreamHandle::scheduleStreams() { ASSERT(m_readStream); ASSERT(m_writeStream); CFStreamClientContext clientContext = { 0, this, 0, 0, copyCFStreamDescription }; // FIXME: Pass specific events we're interested in instead of -1. CFReadStreamSetClient(m_readStream.get(), static_cast<CFOptionFlags>(-1), readStreamCallback, &clientContext); CFWriteStreamSetClient(m_writeStream.get(), static_cast<CFOptionFlags>(-1), writeStreamCallback, &clientContext); #if PLATFORM(WIN) CFReadStreamScheduleWithRunLoop(m_readStream.get(), loaderRunLoop(), kCFRunLoopDefaultMode); CFWriteStreamScheduleWithRunLoop(m_writeStream.get(), loaderRunLoop(), kCFRunLoopDefaultMode); #else CFReadStreamScheduleWithRunLoop(m_readStream.get(), CFRunLoopGetCurrent(), kCFRunLoopCommonModes); CFWriteStreamScheduleWithRunLoop(m_writeStream.get(), CFRunLoopGetCurrent(), kCFRunLoopCommonModes); #endif CFReadStreamOpen(m_readStream.get()); CFWriteStreamOpen(m_writeStream.get()); #ifndef BUILDING_ON_TIGER if (m_pacRunLoopSource) removePACRunLoopSource(); #endif m_connectingSubstate = WaitingForConnect; }
/* MyStreamInfoDestroy destroys a MyStreamInfo 'object', cleaning up any resources that it owns. */ static void MyStreamInfoDestroy(MyStreamInfo * info) { assert(info != NULL); if (info->readStream) { CFReadStreamUnscheduleFromRunLoop(info->readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); (void) CFReadStreamSetClient(info->readStream, kCFStreamEventNone, NULL, NULL); /* CFReadStreamClose terminates the stream. */ CFReadStreamClose(info->readStream); CFRelease(info->readStream); } if (info->writeStream) { CFWriteStreamUnscheduleFromRunLoop(info->writeStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); (void) CFWriteStreamSetClient(info->writeStream, kCFStreamEventNone, NULL, NULL); /* CFWriteStreamClose terminates the stream. */ CFWriteStreamClose(info->writeStream); CFRelease(info->writeStream); } if (info->proxyDict) { CFRelease(info->proxyDict); // see discussion of <rdar://problem/3745574> below } free(info); }
static void CleanupAfterWAAN(MyStreamInfoPtr myInfoPtr) { assert(myInfoPtr != NULL); if (myInfoPtr->isClientSet) { CFWriteStreamSetClient(myInfoPtr->wStreamRef, 0, NULL, NULL); CFWriteStreamUnscheduleFromRunLoop(myInfoPtr->wStreamRef, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); myInfoPtr->isClientSet = FALSE; } }
/* extern */ void _CFTypeInvalidate(CFTypeRef obj) { CFTypeID t = CFGetTypeID(obj); /* Invalidate according to type of object. */ if (t == CFRunLoopSourceGetTypeID()) { CFRunLoopSourceInvalidate((CFRunLoopSourceRef)obj); } else if (t == CFMachPortGetTypeID()) { CFMachPortInvalidate((CFMachPortRef)obj); } else if (t == CFSocketGetTypeID()) { CFSocketInvalidate((CFSocketRef)obj); } /* For scheduled types of objects, it is invalidated by setting the client to NULL. */ else if (t == CFReadStreamGetTypeID()) { CFReadStreamSetClient((CFReadStreamRef)obj, kCFStreamEventNone, NULL, NULL); } else if (t == CFWriteStreamGetTypeID()) { CFWriteStreamSetClient((CFWriteStreamRef)obj, kCFStreamEventNone, NULL, NULL); } else if (t == CFHostGetTypeID()) { CFHostSetClient((CFHostRef)obj, NULL, NULL); } else if (t == SCNetworkReachabilityGetTypeID()) { SCNetworkReachabilitySetCallback((SCNetworkReachabilityRef)obj, NULL, NULL); } else if (t == CFRunLoopTimerGetTypeID()) { CFRunLoopTimerInvalidate((CFRunLoopTimerRef)obj); } else if (t == CFNetServiceGetTypeID()) { CFNetServiceSetClient((CFNetServiceRef)obj, NULL, NULL); } else if (t == CFNetServiceBrowserGetTypeID()) { CFNetServiceBrowserInvalidate((CFNetServiceBrowserRef)obj); } else if (t == CFNetServiceMonitorGetTypeID()) { CFNetServiceMonitorInvalidate((CFNetServiceMonitorRef)obj); } else if (t == SCNetworkReachabilityGetTypeID()) { SCNetworkConnectionStop((SCNetworkConnectionRef)obj, FALSE); } }
static void cfstream_data_close(struct mailstream_cfstream_data * cfstream_data) { if (cfstream_data->writeStream != NULL) { CFWriteStreamSetClient(cfstream_data->writeStream, kCFStreamEventNone, NULL, NULL); CFWriteStreamClose(cfstream_data->writeStream); CFRelease(cfstream_data->writeStream); cfstream_data->writeStream = NULL; } if (cfstream_data->readStream != NULL) { CFReadStreamSetClient(cfstream_data->readStream, kCFStreamEventNone, NULL, NULL); CFReadStreamClose(cfstream_data->readStream); CFRelease(cfstream_data->readStream); cfstream_data->readStream = NULL; } }
/* extern */ void HttpContextClose(HttpContextRef context) { CFRunLoopRef runLoop = CFRunLoopGetCurrent(); // Check if the read stream exists. if (context->_inStream) { // Unschedule, close, and release it. CFReadStreamSetClient(context->_inStream, 0, NULL, NULL); CFReadStreamUnscheduleFromRunLoop(context->_inStream, runLoop, kCFRunLoopCommonModes); CFReadStreamClose(context->_inStream); CFRelease(context->_inStream); // Remove the reference. context->_inStream = NULL; } // Check if the write stream exists. if (context->_outStream) { // Unschedule, close, and release it. CFWriteStreamSetClient(context->_outStream, 0, NULL, NULL); CFWriteStreamUnscheduleFromRunLoop(context->_outStream, runLoop, kCFRunLoopCommonModes); CFWriteStreamClose(context->_outStream); CFRelease(context->_outStream); // Remove the reference. context->_outStream = NULL; } // Get rid of the timer, if it still exists if (context->_timer != NULL) { CFRunLoopTimerInvalidate(context->_timer); CFRelease(context->_timer); context->_timer = NULL; } }
extern MyInfoRef StartWWAN(ConnectClientCallBack clientCB, void *refCon) { char host[] = kTestHost; int portNum = kTestPort; CFDataRef addressData; MyStreamInfoPtr myInfoPtr; CFStreamClientContext ctxt = {0, NULL, NULL, NULL, NULL}; Boolean errorOccurred = FALSE; myInfoPtr = malloc(sizeof(MyStreamInfo)); if (!myInfoPtr) { return NULL; } // init the allocated memory memset(myInfoPtr, 0, sizeof(MyStreamInfo)); myInfoPtr->clientCB = clientCB; myInfoPtr->refCon = refCon; ctxt.info = myInfoPtr; // Check for a dotted-quad address, if so skip any host lookups in_addr_t addr = inet_addr(host); if (addr != INADDR_NONE) { // Create the streams from numberical host struct sockaddr_in sin; memset(&sin, 0, sizeof(sin)); sin.sin_len= sizeof(sin); sin.sin_family = AF_INET; sin.sin_addr.s_addr = addr; sin.sin_port = htons(portNum); addressData = CFDataCreate(NULL, (UInt8 *)&sin, sizeof(sin)); CFSocketSignature sig = { AF_INET, SOCK_STREAM, IPPROTO_TCP, addressData }; // Create the streams. CFStreamCreatePairWithPeerSocketSignature(kCFAllocatorDefault, &sig, &(myInfoPtr->rStreamRef), &(myInfoPtr->wStreamRef)); CFRelease(addressData); } else { // Create the streams from ascii host name CFStringRef hostStr = CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, host, kCFStringEncodingUTF8, kCFAllocatorNull); CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, hostStr, portNum, &(myInfoPtr->rStreamRef), &(myInfoPtr->wStreamRef)); //hostStr = NULL; if(hostStr) { CFRelease(hostStr); } } myInfoPtr->isConnected = FALSE; myInfoPtr->isStreamInitd = TRUE; myInfoPtr->isClientSet = FALSE; // Inform the streams to kill the socket when it is done with it. // This effects the write stream too since the pair shares the // one socket. CFWriteStreamSetProperty(myInfoPtr->wStreamRef, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue); // set up the client if (!CFWriteStreamSetClient(myInfoPtr->wStreamRef, kCFStreamEventOpenCompleted | kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered, MyCFWriteStreamClientCallBack, &ctxt)) { printf("CFWriteStreamSetClient failed\n"); errorOccurred = TRUE; } else myInfoPtr->isClientSet = TRUE; if (!errorOccurred) { // schedule the stream CFWriteStreamScheduleWithRunLoop(myInfoPtr->wStreamRef, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); // Try to open the stream. if (!CFWriteStreamOpen(myInfoPtr->wStreamRef)) { printf("CFWriteStreamOpen failed\n"); errorOccurred = TRUE; } } if (!errorOccurred) { // everything worked so far, so run the runloop - when the callback gets called, it will stop the run loop printf("CFWriteStreamOpen returned with no error - calling CFRunLoopRun\n"); CFRunLoopRun(); if (myInfoPtr->errorOccurred) errorOccurred = TRUE; printf("after CFRunLoopRun - returning\n"); } if (errorOccurred) { myInfoPtr->isConnected = FALSE; CleanupAfterWAAN(myInfoPtr); CloseStreams(myInfoPtr); if (myInfoPtr->isStreamInitd) { if(myInfoPtr->rStreamRef) CFRelease(myInfoPtr->rStreamRef); if(myInfoPtr->wStreamRef) CFRelease(myInfoPtr->wStreamRef); myInfoPtr->isStreamInitd = FALSE; } free(myInfoPtr); return NULL; } return (MyInfoRef)myInfoPtr; }
mailstream_low * mailstream_low_cfstream_open_voip_timeout(const char * hostname, int16_t port, int voip_enabled, time_t timeout) { #if HAVE_CFNETWORK mailstream_low * s; struct mailstream_cfstream_data * cfstream_data; CFReadStreamRef readStream; CFWriteStreamRef writeStream; CFStringRef hostString; CFOptionFlags readFlags; CFOptionFlags writeFlags; int r; hostString = CFStringCreateWithCString(NULL, hostname, kCFStringEncodingUTF8); CFStreamCreatePairWithSocketToHost(NULL, hostString, port, &readStream, &writeStream); CFRelease(hostString); #if TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR if (voip_enabled) { CFReadStreamSetProperty(readStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP); CFWriteStreamSetProperty(writeStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP); } #endif cfstream_data = cfstream_data_new(readStream, writeStream); s = mailstream_low_new(cfstream_data, mailstream_cfstream_driver); mailstream_low_set_timeout(s, timeout); //fprintf(stderr, "open %s %i -> %p\n", hostname, port, s); /* setup streams */ cfstream_data->streamContext.info = s; readFlags = kCFStreamEventOpenCompleted | kCFStreamEventHasBytesAvailable | kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered; writeFlags = kCFStreamEventOpenCompleted | kCFStreamEventCanAcceptBytes | kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered; CFReadStreamSetClient(cfstream_data->readStream, readFlags, readStreamCallback, &cfstream_data->streamContext); CFWriteStreamSetClient(cfstream_data->writeStream, writeFlags, writeStreamCallback, &cfstream_data->streamContext); CFRelease(readStream); CFRelease(writeStream); readStream = NULL; writeStream = NULL; /* setup cancel */ cfstream_data->cancelContext.info = s; cfstream_data->cancelContext.perform = cancelPerform; cfstream_data->cancelSource = CFRunLoopSourceCreate(NULL, 0, &cfstream_data->cancelContext); r = low_open(s); if (r < 0) { mailstream_low_cfstream_close(s); return NULL; } return s; #else return NULL; #endif }
int tnet_transport_wrap(tnet_transport_t *transport, int index) { transport_context_t *context = transport->context; transport_socket_t *sock = context->sockets[index]; // If the socket is already wrapped in a CFSocket then return. if (sock->cf_socket || sock->cf_read_stream) { return 1; } // Put a reference to the transport context const CFSocketContext socket_context = { 0, transport, NULL, NULL, NULL }; if (TNET_SOCKET_TYPE_IS_DGRAM(sock->type)) { // Create a CFSocket from the native socket and register for Read events sock->cf_socket = CFSocketCreateWithNative(kCFAllocatorDefault, sock->fd, kCFSocketReadCallBack, &__CFSocketCallBack, &socket_context); // Don't close the socket if the CFSocket is invalidated CFOptionFlags flags = CFSocketGetSocketFlags(sock->cf_socket); flags = flags & ~kCFSocketCloseOnInvalidate; CFSocketSetSocketFlags(sock->cf_socket, flags); // Create a new RunLoopSource and register it with the main thread RunLoop sock->cf_run_loop_source = CFSocketCreateRunLoopSource(kCFAllocatorDefault, sock->cf_socket, 0); CFRunLoopAddSource(context->cf_run_loop, sock->cf_run_loop_source, kCFRunLoopDefaultMode); CFRelease(sock->cf_run_loop_source); } else if (TNET_SOCKET_TYPE_IS_STREAM(sock->type)) { // Create a pair of streams (read/write) from the socket CFStreamCreatePairWithSocket(kCFAllocatorDefault, sock->fd, &sock->cf_read_stream, &sock->cf_write_stream); // Don't close underlying socket CFReadStreamSetProperty(sock->cf_read_stream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanFalse); CFWriteStreamSetProperty(sock->cf_write_stream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanFalse); if (TNET_SOCKET_TYPE_IS_SECURE(sock->type)) { CFMutableDictionaryRef settings = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFDictionaryAddValue(settings, kCFStreamSSLAllowsExpiredCertificates, kCFBooleanTrue); CFDictionaryAddValue(settings, kCFStreamSSLAllowsAnyRoot, kCFBooleanTrue); CFDictionaryAddValue(settings, kCFStreamSSLValidatesCertificateChain, kCFBooleanFalse); CFDictionaryAddValue(settings, kCFStreamSSLPeerName, kCFNull); // Set the SSL settings CFReadStreamSetProperty(sock->cf_read_stream, kCFStreamPropertySocketSecurityLevel, kCFStreamSocketSecurityLevelNegotiatedSSL); CFReadStreamSetProperty(sock->cf_read_stream, kCFStreamPropertySSLSettings, settings); CFWriteStreamSetProperty(sock->cf_write_stream, kCFStreamPropertySocketSecurityLevel, kCFStreamSocketSecurityLevelNegotiatedSSL); CFWriteStreamSetProperty(sock->cf_write_stream, kCFStreamPropertySSLSettings, settings); CFRelease(settings); } #if __IPHONE_4_0 // Mark the stream for VoIP usage CFReadStreamSetProperty(sock->cf_read_stream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP); CFWriteStreamSetProperty(sock->cf_write_stream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP); #endif // Setup a context for the streams CFStreamClientContext streamContext = { 0, transport, NULL, NULL, NULL }; // Set the client callback for the stream CFReadStreamSetClient(sock->cf_read_stream, kCFStreamEventOpenCompleted | kCFStreamEventHasBytesAvailable | kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered, &__CFReadStreamClientCallBack, &streamContext); CFWriteStreamSetClient(sock->cf_write_stream, kCFStreamEventOpenCompleted | kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered, &__CFWriteStreamClientCallBack, &streamContext); // Enroll streams in the run-loop CFReadStreamScheduleWithRunLoop(sock->cf_read_stream, context->cf_run_loop, kCFRunLoopDefaultMode); CFWriteStreamScheduleWithRunLoop(sock->cf_write_stream, context->cf_run_loop, kCFRunLoopDefaultMode); // Release references CFRelease(sock->cf_read_stream); CFRelease(sock->cf_write_stream); CFReadStreamOpen(sock->cf_read_stream); CFWriteStreamOpen(sock->cf_write_stream); } return 0; }
/* extern */ Boolean HttpContextOpen(HttpContextRef context) { do { Boolean didSet; CFRunLoopRef runLoop = CFRunLoopGetCurrent(); CFStreamClientContext streamCtxt = {0, context, (void*(*)(void*))&HttpContextRetain, (void(*)(void*))&HttpContextRelease, NULL}; CFRunLoopTimerContext timerCtxt = {0, context, (const void*(*)(const void*))&HttpContextRetain, (void(*)(const void*))&HttpContextRelease, NULL}; // Set the client on the read stream. didSet = CFReadStreamSetClient(context->_inStream, kReadEvents, (CFReadStreamClientCallBack)&_ReadStreamCallBack, &streamCtxt); // Fail if unable to set the client. if (!didSet) break; // Set the client on the write stream. didSet = CFWriteStreamSetClient(context->_outStream, kWriteEvents, (CFWriteStreamClientCallBack)&_WriteStreamCallBack, &streamCtxt); // Fail if unable to set the client. if (!didSet) break; // Schedule the streams on the current run loop and default mode. CFReadStreamScheduleWithRunLoop(context->_inStream, runLoop, kCFRunLoopCommonModes); CFWriteStreamScheduleWithRunLoop(context->_outStream, runLoop, kCFRunLoopCommonModes); // Open the stream for reading. if (!CFReadStreamOpen(context->_inStream)) break; // Open the stream for writing. if (!CFWriteStreamOpen(context->_outStream)) break; // Create the timeout timer context->_timer = CFRunLoopTimerCreate(CFGetAllocator(context->_inStream), CFAbsoluteTimeGetCurrent() + kTimeOutInSeconds, 0, // interval 0, // flags 0, // order (CFRunLoopTimerCallBack)_TimerCallBack, &timerCtxt); // Fail if unable to create the timer. if (context->_timer == NULL) break; CFRunLoopAddTimer(runLoop, context->_timer, kCFRunLoopCommonModes); return TRUE; } while (0); // Something failed, so clean up. HttpContextClose(context); return FALSE; }
/* MySimpleUpload implements the upload command. It sets up a MyStreamInfo 'object' with the read stream being a file stream of the file to upload and the write stream being an FTP stream of the destination file. It then returns, and the real work happens asynchronously in the runloop. The function returns true if the stream setup succeeded, and false if it failed. */ static Boolean MySimpleUpload(CFStringRef uploadDirectory, CFURLRef fileURL, CFStringRef username, CFStringRef password) { CFWriteStreamRef writeStream; CFReadStreamRef readStream; CFStreamClientContext context = { 0, NULL, NULL, NULL, NULL }; CFURLRef uploadURL, destinationURL; CFStringRef fileName; Boolean success = true; MyStreamInfo *streamInfo; assert(uploadDirectory != NULL); assert(fileURL != NULL); assert( (username != NULL) || (password == NULL) ); /* Create a CFURL from the upload directory string */ destinationURL = CFURLCreateWithString(kCFAllocatorDefault, uploadDirectory, NULL); assert(destinationURL != NULL); /* Copy the end of the file path and use it as the file name. */ fileName = CFURLCopyLastPathComponent(fileURL); assert(fileName != NULL); /* Create the destination URL by taking the upload directory and appending the file name. */ uploadURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault, destinationURL, fileName, false); assert(uploadURL != NULL); CFRelease(destinationURL); CFRelease(fileName); /* Create a CFReadStream from the local file being uploaded. */ readStream = CFReadStreamCreateWithFile(kCFAllocatorDefault, fileURL); assert(readStream != NULL); /* Create an FTP write stream for uploading operation to a FTP URL. If the URL specifies a directory, the open will be followed by a close event/state and the directory will have been created. Intermediary directory structure is not created. */ writeStream = CFWriteStreamCreateWithFTPURL(kCFAllocatorDefault, uploadURL); assert(writeStream != NULL); CFRelease(uploadURL); /* Initialize our MyStreamInfo structure, which we use to store some information about the stream. */ MyStreamInfoCreate(&streamInfo, readStream, writeStream); context.info = (void *)streamInfo; /* CFReadStreamOpen will return success/failure. Opening a stream causes it to reserve all the system resources it requires. If the stream can open non-blocking, this will always return TRUE; listen to the run loop source to find out when the open completes and whether it was successful. */ success = CFReadStreamOpen(readStream); if (success) { /* CFWriteStreamSetClient registers a callback to hear about interesting events that occur on a stream. */ success = CFWriteStreamSetClient(writeStream, kNetworkEvents, MyUploadCallBack, &context); if (success) { /* Schedule a run loop on which the client can be notified about stream events. The client callback will be triggered via the run loop. It's the caller's responsibility to ensure that the run loop is running. */ CFWriteStreamScheduleWithRunLoop(writeStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); MyCFStreamSetUsernamePassword(writeStream, username, password); MyCFStreamSetFTPProxy(writeStream, &streamInfo->proxyDict); /* CFWriteStreamOpen will return success/failure. Opening a stream causes it to reserve all the system resources it requires. If the stream can open non-blocking, this will always return TRUE; listen to the run loop source to find out when the open completes and whether it was successful. */ success = CFWriteStreamOpen(writeStream); if (success == false) { fprintf(stderr, "CFWriteStreamOpen failed\n"); MyStreamInfoDestroy(streamInfo); } } else { fprintf(stderr, "CFWriteStreamSetClient failed\n"); MyStreamInfoDestroy(streamInfo); } } else { fprintf(stderr, "CFReadStreamOpen failed\n"); MyStreamInfoDestroy(streamInfo); } return success; }