#include <CoreFoundation/CoreFoundation.h>
+#define ALL_CALLBACK_TYPES (kCFSocketReadCallBack | kCFSocketWriteCallBack | kCFSocketConnectCallBack)
+
struct MacGSocketData
{
CFSocketRef socket;
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);
{
/* 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;
struct MacGSocketData *data = (struct MacGSocketData*)(socket->m_gui_dependent);
if (data)
{
- CFRunLoopRemoveSource(CFRunLoopGetCurrent(), data->source, kCFRunLoopCommonModes);
- CFSocketInvalidate(data->socket);
+ CFRelease(data->socket);
free(data);
}
}
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;
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;
{
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