]> git.saurik.com Git - wxWidgets.git/blob - src/osx/core/sockosx.cpp
Fix crash in wxColour ctor from NSColor in wxOSX/Cocoa.
[wxWidgets.git] / src / osx / core / sockosx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: osx/core/gsockosx.cpp
3 // Purpose: wxSocketImpl implementation for OS X
4 // Authors: Brian Victor, Vadim Zeitlin
5 // Created: February 2002
6 // RCS-ID: $Id$
7 // Copyright: (c) 2002 Brian Victor
8 // (c) 2008 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_SOCKETS
15
16 #include "wx/private/socket.h"
17 #include "wx/unix/private/sockunix.h"
18 #include "wx/apptrait.h"
19 #include "wx/link.h"
20
21 #include "wx/osx/core/cfstring.h" // for wxMacWakeUp() only
22
23 #include <CoreFoundation/CoreFoundation.h>
24
25 namespace
26 {
27
28 // ----------------------------------------------------------------------------
29 // global variables
30 // ----------------------------------------------------------------------------
31
32 // Sockets must use the event loop to monitor the events so we store a
33 // reference to the main thread event loop here
34 static CFRunLoopRef gs_mainRunLoop = NULL;
35
36 // ----------------------------------------------------------------------------
37 // Mac-specific socket implementation
38 // ----------------------------------------------------------------------------
39
40 class wxSocketImplMac : public wxSocketImplUnix
41 {
42 public:
43 wxSocketImplMac(wxSocketBase& wxsocket)
44 : wxSocketImplUnix(wxsocket)
45 {
46 m_socket = NULL;
47 m_source = NULL;
48 }
49
50 virtual ~wxSocketImplMac()
51 {
52 wxASSERT_MSG( !m_source && !m_socket, "forgot to call Close()?" );
53 }
54
55 // get the underlying socket: creates it on demand
56 CFSocketRef GetSocket() /* const */
57 {
58 if ( !m_socket )
59 Initialize();
60
61 return m_socket;
62 }
63
64 private:
65 virtual void DoClose()
66 {
67 wxSocketManager * const manager = wxSocketManager::Get();
68 if ( manager )
69 {
70 manager->Uninstall_Callback(this, wxSOCKET_INPUT);
71 manager->Uninstall_Callback(this, wxSOCKET_OUTPUT);
72 }
73
74 // VZ: CFRunLoopRemoveSource() is probably unnecessary as
75 // CFSocketInvalidate() seems to do it internally from reading the
76 // docs, please remove it (and this comment) after testing
77 CFRunLoopRemoveSource(gs_mainRunLoop, m_source, kCFRunLoopCommonModes);
78 CFSocketInvalidate(m_socket);
79
80 CFRelease(m_source);
81 m_source = NULL;
82
83 CFRelease(m_socket);
84 m_socket = NULL;
85 }
86
87 // initialize the data associated with the given socket
88 bool Initialize()
89 {
90 // we need a valid Unix socket to create a CFSocket
91 if ( m_fd < 0 )
92 return false;
93
94 CFSocketContext cont;
95 cont.version = 0; // this currently must be 0
96 cont.info = this; // pointer passed to our callback
97 cont.retain = NULL; // no need to retain/release/copy the
98 cont.release = NULL; // socket pointer, so all callbacks
99 cont.copyDescription = NULL; // can be left NULL
100
101 m_socket = CFSocketCreateWithNative
102 (
103 NULL, // default allocator
104 m_fd,
105 kCFSocketReadCallBack |
106 kCFSocketWriteCallBack |
107 kCFSocketConnectCallBack,
108 SocketCallback,
109 &cont
110 );
111 if ( !m_socket )
112 return false;
113
114 m_source = CFSocketCreateRunLoopSource(NULL, m_socket, 0);
115
116 if ( !m_source )
117 {
118 CFRelease(m_socket);
119 m_socket = NULL;
120
121 return false;
122 }
123
124 CFRunLoopAddSource(gs_mainRunLoop, m_source, kCFRunLoopCommonModes);
125
126 return true;
127 }
128
129 static void SocketCallback(CFSocketRef WXUNUSED(s),
130 CFSocketCallBackType callbackType,
131 CFDataRef WXUNUSED(address),
132 const void* data,
133 void* info)
134 {
135 wxSocketImplMac * const socket = static_cast<wxSocketImplMac *>(info);
136
137 switch (callbackType)
138 {
139 case kCFSocketConnectCallBack:
140 wxASSERT(!socket->IsServer());
141 // KH: If data is non-NULL, the connect failed, do not call Detected_Write,
142 // which will only end up creating a spurious connect event because the
143 // call to getsocketopt SO_ERROR inexplicably returns no error.
144 // The change in behavior cannot be traced to any particular commit or
145 // timeframe so I'm not sure what to think, but after so many hours,
146 // this seems to address the issue and it's time to move on.
147 if (data == NULL)
148 socket->OnWriteWaiting();
149 break;
150
151 case kCFSocketReadCallBack:
152 socket->OnReadWaiting();
153 break;
154
155 case kCFSocketWriteCallBack:
156 socket->OnWriteWaiting();
157 break;
158
159 default:
160 wxFAIL_MSG( "unexpected socket callback" );
161 }
162
163 // receiving a socket event does _not_ make ReceiveNextEvent() (or the
164 // equivalent NSApp:nextEventMatchingMask:untilDate:inMode:dequeue)
165 // return control, i.e. apparently it doesn't count as a real event, so
166 // we need to generate a wake up to return control to the code waiting
167 // for something to happen and process this socket event
168 wxMacWakeUp();
169 }
170
171 CFSocketRef m_socket;
172 CFRunLoopSourceRef m_source;
173
174 wxDECLARE_NO_COPY_CLASS(wxSocketImplMac);
175 };
176
177 } // anonymous namespace
178
179
180 // ----------------------------------------------------------------------------
181 // CoreFoundation implementation of wxSocketManager
182 // ----------------------------------------------------------------------------
183
184 class wxSocketManagerMac : public wxSocketManager
185 {
186 public:
187 virtual bool OnInit();
188 virtual void OnExit();
189
190 virtual wxSocketImpl *CreateSocket(wxSocketBase& wxsocket)
191 {
192 return new wxSocketImplMac(wxsocket);
193 }
194
195 virtual void Install_Callback(wxSocketImpl *socket, wxSocketNotify event);
196 virtual void Uninstall_Callback(wxSocketImpl *socket, wxSocketNotify event);
197
198 private:
199 // return CFSocket callback mask corresponding to the given event (the
200 // socket parameter is needed because some events are interpreted
201 // differently depending on whether they happen on a server or on a client
202 // socket)
203 static int GetCFCallback(wxSocketImpl *socket, wxSocketNotify event);
204 };
205
206 bool wxSocketManagerMac::OnInit()
207 {
208 // No need to store the main loop again
209 if (gs_mainRunLoop != NULL)
210 return true;
211
212 // Get the loop for the main thread so our events will actually fire.
213 // The common socket.cpp code will assert if initialize is called from a
214 // secondary thread, otherwise Mac would have the same problems as MSW
215 gs_mainRunLoop = CFRunLoopGetCurrent();
216 if ( !gs_mainRunLoop )
217 return false;
218
219 CFRetain(gs_mainRunLoop);
220
221 return true;
222 }
223
224 void wxSocketManagerMac::OnExit()
225 {
226 // Release the reference count, and set the reference back to NULL
227 CFRelease(gs_mainRunLoop);
228 gs_mainRunLoop = NULL;
229 }
230
231 /* static */
232 int wxSocketManagerMac::GetCFCallback(wxSocketImpl *socket, wxSocketNotify event)
233 {
234 switch ( event )
235 {
236 case wxSOCKET_CONNECTION:
237 return socket->IsServer() ? kCFSocketReadCallBack
238 : kCFSocketConnectCallBack;
239
240 case wxSOCKET_INPUT:
241 return kCFSocketReadCallBack;
242
243 case wxSOCKET_OUTPUT:
244 return kCFSocketWriteCallBack;
245
246 case wxSOCKET_LOST:
247 wxFAIL_MSG( "unexpected wxSocketNotify" );
248 return 0;
249
250 default:
251 wxFAIL_MSG( "unknown wxSocketNotify" );
252 return 0;
253 }
254 }
255
256 void wxSocketManagerMac::Install_Callback(wxSocketImpl *socket_,
257 wxSocketNotify event)
258 {
259 wxSocketImplMac * const socket = static_cast<wxSocketImplMac *>(socket_);
260
261 CFSocketEnableCallBacks(socket->GetSocket(), GetCFCallback(socket, event));
262 }
263
264 void wxSocketManagerMac::Uninstall_Callback(wxSocketImpl *socket_,
265 wxSocketNotify event)
266 {
267 wxSocketImplMac * const socket = static_cast<wxSocketImplMac *>(socket_);
268
269 CFSocketDisableCallBacks(socket->GetSocket(), GetCFCallback(socket, event));
270 }
271
272 // set the wxBase variable to point to CF wxSocketManager implementation so
273 // that the GUI code in utilsexc_cf.cpp could return it from its traits method
274 //
275 // this is very roundabout but necessary to allow us to have different
276 // behaviours in console and GUI applications while avoiding dependencies of
277 // GUI library on the network one
278 extern WXDLLIMPEXP_BASE wxSocketManager *wxOSXSocketManagerCF;
279
280 static struct OSXManagerSetter
281 {
282 OSXManagerSetter()
283 {
284 static wxSocketManagerMac s_manager;
285 wxOSXSocketManagerCF = &s_manager;
286 }
287 } gs_OSXManagerSetter;
288
289 // see the relative linker macro in socket.cpp
290 wxFORCE_LINK_THIS_MODULE(osxsocket)
291
292 #endif // wxUSE_SOCKETS