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
->Detected_Write();
40 case kCFSocketReadCallBack
:
41 socket
->Detected_Read();
43 case kCFSocketWriteCallBack
:
44 socket
->Detected_Write();
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 socket
->m_gui_dependent
= (char*)data
;
72 /* Keep the source and the socket around. */
73 data
->source
= source
;
79 bool GSocketGUIFunctionsTableConcrete::CanUseEventLoop()
82 bool GSocketGUIFunctionsTableConcrete::OnInit(void)
87 void GSocketGUIFunctionsTableConcrete::OnExit(void)
91 bool GSocketGUIFunctionsTableConcrete::Init_Socket(GSocket
*socket
)
93 struct MacGSocketData
*data
= (struct MacGSocketData
*)malloc(sizeof(struct MacGSocketData
));
96 socket
->m_gui_dependent
= (char*)data
;
104 void GSocketGUIFunctionsTableConcrete::Destroy_Socket(GSocket
*socket
)
106 struct MacGSocketData
*data
= (struct MacGSocketData
*)(socket
->m_gui_dependent
);
109 CFRelease(data
->socket
);
114 void GSocketGUIFunctionsTableConcrete::Install_Callback(GSocket
*socket
, GSocketEvent event
)
117 struct MacGSocketData
* data
= _GSocket_Get_Mac_Socket(socket
);
121 case GSOCK_CONNECTION
:
123 c
= kCFSocketReadCallBack
;
125 c
= kCFSocketConnectCallBack
;
129 c
= kCFSocketReadCallBack
;
132 c
= kCFSocketWriteCallBack
;
137 CFSocketEnableCallBacks(data
->socket
, c
);
140 void GSocketGUIFunctionsTableConcrete::Uninstall_Callback(GSocket
*socket
, GSocketEvent event
)
143 struct MacGSocketData
* data
= _GSocket_Get_Mac_Socket(socket
);
147 case GSOCK_CONNECTION
:
149 c
= kCFSocketReadCallBack
;
151 c
= kCFSocketConnectCallBack
;
155 c
= kCFSocketReadCallBack
;
158 c
= kCFSocketWriteCallBack
;
163 CFSocketDisableCallBacks(data
->socket
, c
);
166 void GSocketGUIFunctionsTableConcrete::Enable_Events(GSocket
*socket
)
168 struct MacGSocketData
* data
= _GSocket_Get_Mac_Socket(socket
);
171 CFRunLoopAddSource(CFRunLoopGetCurrent(), data
->source
, kCFRunLoopDefaultMode
);
174 void GSocketGUIFunctionsTableConcrete::Disable_Events(GSocket
*socket
)
176 struct MacGSocketData
* data
= _GSocket_Get_Mac_Socket(socket
);
179 /* CFSocketInvalidate does CFRunLoopRemoveSource anyway */
180 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), data
->source
, kCFRunLoopCommonModes
);
181 CFSocketInvalidate(data
->socket
);
184 #endif // wxUSE_SOCKETS