X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/163c36827d874f82148a3ab0342521fc33ac1e25..ce7208d49d5ce2ca1dc0b3b83f14f1d04f29c4bf:/src/mac/corefoundation/gsockosx.cpp diff --git a/src/mac/corefoundation/gsockosx.cpp b/src/mac/corefoundation/gsockosx.cpp index f9c8028a34..ac4da2300e 100644 --- a/src/mac/corefoundation/gsockosx.cpp +++ b/src/mac/corefoundation/gsockosx.cpp @@ -1,12 +1,12 @@ /* ------------------------------------------------------------------------- * Project: GSocket (Generic Socket) for WX - * Name: gsockosx.c + * Name: src/mac/corefoundation/gsockosx.c * Purpose: GSocket: Mac OS X mach-o part * CVSID: $Id$ * Mac code by Brian Victor, February 2002. Email comments to bhv1@psu.edu * ------------------------------------------------------------------------- */ -#include "wx/setup.h" +#include "wx/wxprec.h" #if wxUSE_SOCKETS @@ -24,6 +24,10 @@ struct MacGSocketData CFRunLoopSourceRef source; }; +// Sockets must use the event loop on the main thread +// We will store the main loop's reference when Initialize is called +static CFRunLoopRef s_mainRunLoop = NULL; + void Mac_Socket_Callback(CFSocketRef s, CFSocketCallBackType callbackType, CFDataRef address, const void* data, void* info) { @@ -35,7 +39,14 @@ void Mac_Socket_Callback(CFSocketRef s, CFSocketCallBackType callbackType, { case kCFSocketConnectCallBack: assert(!socket->m_server); - socket->Detected_Write(); + // KH: If data is non-NULL, the connect failed, do not call Detected_Write, + // which will only end up creating a spurious connect event because the + // call to getsocketopt SO_ERROR inexplicably returns no error. + // The change in behavior cannot be traced to any particular commit or + // timeframe so I'm not sure what to think, but after so many hours, + // this seems to address the issue and it's time to move on. + if (data == NULL) + socket->Detected_Write(); break; case kCFSocketReadCallBack: socket->Detected_Read(); @@ -64,7 +75,7 @@ struct MacGSocketData* _GSocket_Get_Mac_Socket(GSocket *socket) cont.info = socket; CFSocketRef cf = CFSocketCreateWithNative(NULL, socket->m_fd, - ALL_CALLBACK_TYPES, Mac_Socket_Callback, &cont); + ALL_CALLBACK_TYPES, Mac_Socket_Callback, &cont); CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(NULL, cf, 0); assert(source); socket->m_gui_dependent = (char*)data; @@ -81,11 +92,24 @@ bool GSocketGUIFunctionsTableConcrete::CanUseEventLoop() bool GSocketGUIFunctionsTableConcrete::OnInit(void) { + // No need to store the main loop again + if (s_mainRunLoop != NULL) + return true; + + // Get the loop for the main thread so our events will actually fire. + // The common socket.cpp code will assert if initialize is called from a + // secondary thread, otherwise Mac would have the same problems as MSW + s_mainRunLoop = CFRunLoopGetCurrent(); + CFRetain(s_mainRunLoop); + return true; } void GSocketGUIFunctionsTableConcrete::OnExit(void) { + // Release the reference count, and set the reference back to NULL + CFRelease(s_mainRunLoop); + s_mainRunLoop = NULL; } bool GSocketGUIFunctionsTableConcrete::Init_Socket(GSocket *socket) @@ -106,7 +130,10 @@ void GSocketGUIFunctionsTableConcrete::Destroy_Socket(GSocket *socket) struct MacGSocketData *data = (struct MacGSocketData*)(socket->m_gui_dependent); if (data) { - CFRelease(data->socket); + if ( data->source ) + CFRelease(data->source); + if ( data->socket ) + CFRelease(data->socket); free(data); } } @@ -168,7 +195,7 @@ void GSocketGUIFunctionsTableConcrete::Enable_Events(GSocket *socket) struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket); if (!data) return; - CFRunLoopAddSource(CFRunLoopGetCurrent(), data->source, kCFRunLoopCommonModes); + CFRunLoopAddSource(s_mainRunLoop, data->source, kCFRunLoopCommonModes); } void GSocketGUIFunctionsTableConcrete::Disable_Events(GSocket *socket) @@ -177,7 +204,7 @@ void GSocketGUIFunctionsTableConcrete::Disable_Events(GSocket *socket) if (!data) return; /* CFSocketInvalidate does CFRunLoopRemoveSource anyway */ - CFRunLoopRemoveSource(CFRunLoopGetCurrent(), data->source, kCFRunLoopCommonModes); + CFRunLoopRemoveSource(s_mainRunLoop, data->source, kCFRunLoopCommonModes); CFSocketInvalidate(data->socket); }