X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e13854762b0a536ea5888afdef2bd6ece072d999..0ebb0e5c842c2a0be09364bda0d14b3281ddec43:/src/mac/gsockosx.c diff --git a/src/mac/gsockosx.c b/src/mac/gsockosx.c index c93e4c9e2e..614ed425c8 100644 --- a/src/mac/gsockosx.c +++ b/src/mac/gsockosx.c @@ -16,6 +16,8 @@ #include +#define ALL_CALLBACK_TYPES (kCFSocketReadCallBack | kCFSocketWriteCallBack | kCFSocketConnectCallBack) + struct MacGSocketData { CFSocketRef socket; @@ -32,7 +34,8 @@ void Mac_Socket_Callback(CFSocketRef s, CFSocketCallBackType callbackType, switch (callbackType) { case kCFSocketConnectCallBack: - socket->m_functions->Detected_Read(socket); + assert(!socket->m_server); + socket->m_functions->Detected_Write(socket); break; case kCFSocketReadCallBack: socket->m_functions->Detected_Read(socket); @@ -49,25 +52,22 @@ struct MacGSocketData* _GSocket_Get_Mac_Socket(GSocket *socket) { /* If socket is already created, returns a pointer to the data */ /* Otherwise, creates socket and returns the pointer */ - CFOptionFlags c; CFSocketContext cont; struct MacGSocketData* data = (struct MacGSocketData*)socket->m_gui_dependent; if (data && data->source) return data; + + /* CFSocket has not been created, create it: */ if (socket->m_fd < 0 || !data) return NULL; cont.version = 0; cont.retain = NULL; cont.release = NULL; cont.copyDescription = NULL; cont.info = socket; - c = kCFSocketReadCallBack | kCFSocketWriteCallBack; - CFSocketRef cf = CFSocketCreateWithNative(NULL, socket->m_fd, c, - Mac_Socket_Callback, &cont); - CFSocketDisableCallBacks(cf, kCFSocketReadCallBack | kCFSocketWriteCallBack); + CFSocketRef cf = CFSocketCreateWithNative(NULL, socket->m_fd, + ALL_CALLBACK_TYPES, Mac_Socket_Callback, &cont); CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(NULL, cf, 0); assert(source); - CFSocketSetSocketFlags(cf, 0); /* Callbacks must be reenabled manually */ socket->m_gui_dependent = (char*)data; - CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode); /* Keep the source and the socket around. */ data->source = source; @@ -103,8 +103,7 @@ void _GSocket_GUI_Destroy_Socket(GSocket *socket) struct MacGSocketData *data = (struct MacGSocketData*)(socket->m_gui_dependent); if (data) { - CFRunLoopRemoveSource(CFRunLoopGetCurrent(), data->source, kCFRunLoopCommonModes); - CFSocketInvalidate(data->socket); + CFRelease(data->socket); free(data); } } @@ -117,8 +116,12 @@ void _GSocket_Install_Callback(GSocket *socket, GSocketEvent event) switch (event) { case GSOCK_CONNECTION: - c = kCFSocketReadCallBack; /* This works, but I don't know why. */ + if(socket->m_server) + c = kCFSocketReadCallBack; + else + c = kCFSocketConnectCallBack; break; + case GSOCK_LOST: case GSOCK_INPUT: c = kCFSocketReadCallBack; break; @@ -139,8 +142,12 @@ void _GSocket_Uninstall_Callback(GSocket *socket, GSocketEvent event) switch (event) { case GSOCK_CONNECTION: - c = kCFSocketConnectCallBack; + if(socket->m_server) + c = kCFSocketReadCallBack; + else + c = kCFSocketConnectCallBack; break; + case GSOCK_LOST: case GSOCK_INPUT: c = kCFSocketReadCallBack; break; @@ -157,14 +164,18 @@ void _GSocket_Enable_Events(GSocket *socket) { struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket); if (!data) return; - CFSocketEnableCallBacks(data->socket, kCFSocketReadCallBack | kCFSocketWriteCallBack); + + CFRunLoopAddSource(CFRunLoopGetCurrent(), data->source, kCFRunLoopDefaultMode); } void _GSocket_Disable_Events(GSocket *socket) { struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket); if (!data) return; - CFSocketDisableCallBacks(data->socket, kCFSocketReadCallBack | kCFSocketWriteCallBack); + + /* CFSocketInvalidate does CFRunLoopRemoveSource anyway */ + CFRunLoopRemoveSource(CFRunLoopGetCurrent(), data->source, kCFRunLoopCommonModes); + CFSocketInvalidate(data->socket); } #endif // wxUSE_SOCKETS