]> git.saurik.com Git - wxWidgets.git/blame - src/mac/corefoundation/gsockosx.cpp
Added missing newline for Windows debug output
[wxWidgets.git] / src / mac / corefoundation / gsockosx.cpp
CommitLineData
02dad85e
DE
1/* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
3 * Name: gsockosx.c
4 * Purpose: GSocket: Mac OS X mach-o part
5 * CVSID: $Id$
6 * Mac code by Brian Victor, February 2002. Email comments to bhv1@psu.edu
7 * ------------------------------------------------------------------------- */
8
9#include "wx/setup.h"
10
11#if wxUSE_SOCKETS
12
13#include <stdlib.h>
14#include "wx/gsocket.h"
15#include "wx/unix/gsockunx.h"
16
17#include <CoreFoundation/CoreFoundation.h>
18
19#define ALL_CALLBACK_TYPES (kCFSocketReadCallBack | kCFSocketWriteCallBack | kCFSocketConnectCallBack)
20
21struct MacGSocketData
22{
23 CFSocketRef socket;
24 CFRunLoopSourceRef source;
25};
26
7725dc7c
KH
27// Sockets must use the event loop on the main thread
28// We will store the main loop's reference when Initialize is called
29static CFRunLoopRef s_mainRunLoop = NULL;
30
02dad85e
DE
31void Mac_Socket_Callback(CFSocketRef s, CFSocketCallBackType callbackType,
32 CFDataRef address, const void* data, void* info)
33{
34 GSocket* socket = (GSocket*)info;
35 struct MacGSocketData* macdata;
36 macdata = (struct MacGSocketData*)socket->m_gui_dependent;
37 if (!macdata) return;
38 switch (callbackType)
39 {
40 case kCFSocketConnectCallBack:
41 assert(!socket->m_server);
0a647691 42 socket->Detected_Write();
02dad85e
DE
43 break;
44 case kCFSocketReadCallBack:
0a647691 45 socket->Detected_Read();
02dad85e
DE
46 break;
47 case kCFSocketWriteCallBack:
0a647691 48 socket->Detected_Write();
02dad85e
DE
49 break;
50 default:
51 break; /* We shouldn't get here. */
52 }
53}
54
55struct MacGSocketData* _GSocket_Get_Mac_Socket(GSocket *socket)
56{
57 /* If socket is already created, returns a pointer to the data */
58 /* Otherwise, creates socket and returns the pointer */
59 CFSocketContext cont;
60 struct MacGSocketData* data = (struct MacGSocketData*)socket->m_gui_dependent;
61
62 if (data && data->source) return data;
63
64 /* CFSocket has not been created, create it: */
65 if (socket->m_fd < 0 || !data) return NULL;
66 cont.version = 0; cont.retain = NULL;
67 cont.release = NULL; cont.copyDescription = NULL;
68 cont.info = socket;
69
70 CFSocketRef cf = CFSocketCreateWithNative(NULL, socket->m_fd,
71 ALL_CALLBACK_TYPES, Mac_Socket_Callback, &cont);
72 CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(NULL, cf, 0);
73 assert(source);
74 socket->m_gui_dependent = (char*)data;
75
76 /* Keep the source and the socket around. */
77 data->source = source;
78 data->socket = cf;
79
80 return data;
81}
82
0a647691
DE
83bool GSocketGUIFunctionsTableConcrete::CanUseEventLoop()
84{ return true; }
85
86bool GSocketGUIFunctionsTableConcrete::OnInit(void)
02dad85e 87{
7725dc7c
KH
88 // No need to store the main loop again
89 if (s_mainRunLoop != NULL)
90 return true;
91
92 // Get the loop for the main thread so our events will actually fire.
93 // The common socket.cpp code will assert if initialize is called from a
94 // secondary thread, otherwise Mac would have the same problems as MSW
95 s_mainRunLoop = CFRunLoopGetCurrent();
96 CFRetain(s_mainRunLoop);
97
0a647691 98 return true;
02dad85e
DE
99}
100
0a647691 101void GSocketGUIFunctionsTableConcrete::OnExit(void)
02dad85e 102{
7725dc7c
KH
103 // Release the reference count, and set the reference back to NULL
104 CFRelease(s_mainRunLoop);
105 s_mainRunLoop = NULL;
02dad85e
DE
106}
107
0a647691 108bool GSocketGUIFunctionsTableConcrete::Init_Socket(GSocket *socket)
02dad85e 109{
facd6764 110 struct MacGSocketData *data = (struct MacGSocketData *)malloc(sizeof(struct MacGSocketData));
02dad85e
DE
111 if (data)
112 {
113 socket->m_gui_dependent = (char*)data;
114 data->socket = NULL;
115 data->source = NULL;
116 return 1;
117 }
118 return 0;
119}
120
0a647691 121void GSocketGUIFunctionsTableConcrete::Destroy_Socket(GSocket *socket)
02dad85e
DE
122{
123 struct MacGSocketData *data = (struct MacGSocketData*)(socket->m_gui_dependent);
124 if (data)
125 {
ee9a28ab
SC
126 if ( data->socket )
127 CFRelease(data->socket);
02dad85e
DE
128 free(data);
129 }
130}
131
0a647691 132void GSocketGUIFunctionsTableConcrete::Install_Callback(GSocket *socket, GSocketEvent event)
02dad85e
DE
133{
134 int c;
135 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
136 if (!data) return;
137 switch (event)
138 {
139 case GSOCK_CONNECTION:
140 if(socket->m_server)
141 c = kCFSocketReadCallBack;
142 else
143 c = kCFSocketConnectCallBack;
144 break;
145 case GSOCK_LOST:
146 case GSOCK_INPUT:
147 c = kCFSocketReadCallBack;
148 break;
149 case GSOCK_OUTPUT:
150 c = kCFSocketWriteCallBack;
151 break;
152 default:
153 c = 0;
154 }
155 CFSocketEnableCallBacks(data->socket, c);
156}
157
0a647691 158void GSocketGUIFunctionsTableConcrete::Uninstall_Callback(GSocket *socket, GSocketEvent event)
02dad85e
DE
159{
160 int c;
161 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
162 if (!data) return;
163 switch (event)
164 {
165 case GSOCK_CONNECTION:
166 if(socket->m_server)
167 c = kCFSocketReadCallBack;
168 else
169 c = kCFSocketConnectCallBack;
170 break;
171 case GSOCK_LOST:
172 case GSOCK_INPUT:
173 c = kCFSocketReadCallBack;
174 break;
175 case GSOCK_OUTPUT:
176 c = kCFSocketWriteCallBack;
177 break;
178 default:
179 c = 0;
180 }
181 CFSocketDisableCallBacks(data->socket, c);
182}
183
0a647691 184void GSocketGUIFunctionsTableConcrete::Enable_Events(GSocket *socket)
02dad85e
DE
185{
186 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
187 if (!data) return;
188
7725dc7c 189 CFRunLoopAddSource(s_mainRunLoop, data->source, kCFRunLoopCommonModes);
02dad85e
DE
190}
191
0a647691 192void GSocketGUIFunctionsTableConcrete::Disable_Events(GSocket *socket)
02dad85e
DE
193{
194 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
195 if (!data) return;
196
197 /* CFSocketInvalidate does CFRunLoopRemoveSource anyway */
7725dc7c 198 CFRunLoopRemoveSource(s_mainRunLoop, data->source, kCFRunLoopCommonModes);
02dad85e
DE
199 CFSocketInvalidate(data->socket);
200}
201
202#endif // wxUSE_SOCKETS