1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
4 * Purpose: GSocket: Mac OS X mach-o part
6 * Mac code by Brian Victor, February 2002. Email comments to bhv1@psu.edu
7 * ------------------------------------------------------------------------- */
14 #include "wx/gsocket.h"
15 #include "wx/unix/gsockunx.h"
17 #include <CoreFoundation/CoreFoundation.h>
19 #define ALL_CALLBACK_TYPES (kCFSocketReadCallBack | kCFSocketWriteCallBack | kCFSocketConnectCallBack)
24 CFRunLoopSourceRef source
;
27 void Mac_Socket_Callback(CFSocketRef s
, CFSocketCallBackType callbackType
,
28 CFDataRef address
, const void* data
, void* info
)
30 GSocket
* socket
= (GSocket
*)info
;
31 struct MacGSocketData
* macdata
;
32 macdata
= (struct MacGSocketData
*)socket
->m_gui_dependent
;
36 case kCFSocketConnectCallBack
:
37 assert(!socket
->m_server
);
38 socket
->m_functions
->Detected_Write(socket
);
40 case kCFSocketReadCallBack
:
41 socket
->m_functions
->Detected_Read(socket
);
43 case kCFSocketWriteCallBack
:
44 socket
->m_functions
->Detected_Write(socket
);
47 break; /* We shouldn't get here. */
51 struct MacGSocketData
* _GSocket_Get_Mac_Socket(GSocket
*socket
)
53 /* If socket is already created, returns a pointer to the data */
54 /* Otherwise, creates socket and returns the pointer */
56 struct MacGSocketData
* data
= (struct MacGSocketData
*)socket
->m_gui_dependent
;
58 if (data
&& data
->source
) return data
;
60 /* CFSocket has not been created, create it: */
61 if (socket
->m_fd
< 0 || !data
) return NULL
;
62 cont
.version
= 0; cont
.retain
= NULL
;
63 cont
.release
= NULL
; cont
.copyDescription
= NULL
;
66 CFSocketRef cf
= CFSocketCreateWithNative(NULL
, socket
->m_fd
,
67 ALL_CALLBACK_TYPES
, Mac_Socket_Callback
, &cont
);
68 CFRunLoopSourceRef source
= CFSocketCreateRunLoopSource(NULL
, cf
, 0);
70 /* Turn off kCFSocketCloseOnInvalidate (NOTE: > 10.2 only!) */
71 /* Another default flag that we don't turn on here is for DataCallBack and
72 also AcceptCallback (which overlap in bits) which we don't use anyway */
73 /* FIXME: For < 10.2 compatibility fix GSocket to call a platform-dependent
74 function to close the socket so that we can just call invalidate and
75 avoid having to set any special flags at all. */
76 CFSocketSetSocketFlags(cf
, kCFSocketAutomaticallyReenableReadCallBack
| kCFSocketAutomaticallyReenableWriteCallBack
);
77 socket
->m_gui_dependent
= (char*)data
;
79 /* Keep the source and the socket around. */
80 data
->source
= source
;
86 int _GSocket_GUI_Init(void)
91 void _GSocket_GUI_Cleanup(void)
95 int _GSocket_GUI_Init_Socket(GSocket
*socket
)
97 struct MacGSocketData
*data
= malloc(sizeof(struct MacGSocketData
));
100 socket
->m_gui_dependent
= (char*)data
;
108 void _GSocket_GUI_Destroy_Socket(GSocket
*socket
)
110 struct MacGSocketData
*data
= (struct MacGSocketData
*)(socket
->m_gui_dependent
);
113 CFSocketInvalidate(data
->socket
);
114 CFRelease(data
->socket
);
119 void _GSocket_Install_Callback(GSocket
*socket
, GSocketEvent event
)
122 struct MacGSocketData
* data
= _GSocket_Get_Mac_Socket(socket
);
126 case GSOCK_CONNECTION
:
128 c
= kCFSocketReadCallBack
;
130 c
= kCFSocketConnectCallBack
;
134 c
= kCFSocketReadCallBack
;
137 c
= kCFSocketWriteCallBack
;
142 CFSocketEnableCallBacks(data
->socket
, c
);
145 void _GSocket_Uninstall_Callback(GSocket
*socket
, GSocketEvent event
)
148 struct MacGSocketData
* data
= _GSocket_Get_Mac_Socket(socket
);
152 case GSOCK_CONNECTION
:
154 c
= kCFSocketReadCallBack
;
156 c
= kCFSocketConnectCallBack
;
160 c
= kCFSocketReadCallBack
;
163 c
= kCFSocketWriteCallBack
;
168 CFSocketDisableCallBacks(data
->socket
, c
);
171 void _GSocket_Enable_Events(GSocket
*socket
)
173 struct MacGSocketData
* data
= _GSocket_Get_Mac_Socket(socket
);
176 CFRunLoopAddSource(CFRunLoopGetCurrent(), data
->source
, kCFRunLoopDefaultMode
);
179 void _GSocket_Disable_Events(GSocket
*socket
)
181 struct MacGSocketData
* data
= _GSocket_Get_Mac_Socket(socket
);
184 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), data
->source
, kCFRunLoopCommonModes
);
187 #endif // wxUSE_SOCKETS