]> git.saurik.com Git - wxWidgets.git/blobdiff - src/mac/carbon/gsockosx.c
load wsock32.dll dynamically, so that wxCore has no dependency on it
[wxWidgets.git] / src / mac / carbon / gsockosx.c
index c93e4c9e2e3d138732ee53de78bcf748b2d35650..614ed425c8cc2f0cf76088b857278509e764ea8e 100644 (file)
@@ -16,6 +16,8 @@
 
 #include <CoreFoundation/CoreFoundation.h>
 
+#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