1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/core/gsockosx.cpp
3 // Purpose: wxSocketImpl implementation for OS X
4 // Authors: Brian Victor, Vadim Zeitlin
5 // Created: February 2002
7 // Copyright: (c) 2002 Brian Victor
8 // (c) 2008 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/private/socket.h"
17 #include "wx/unix/private/sockunix.h"
18 #include "wx/apptrait.h"
21 #include "wx/osx/core/cfstring.h" // for wxMacWakeUp() only
23 #include <CoreFoundation/CoreFoundation.h>
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
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
;
36 // ----------------------------------------------------------------------------
37 // Mac-specific socket implementation
38 // ----------------------------------------------------------------------------
40 class wxSocketImplMac
: public wxSocketImplUnix
43 wxSocketImplMac(wxSocketBase
& wxsocket
)
44 : wxSocketImplUnix(wxsocket
)
50 virtual ~wxSocketImplMac()
52 wxASSERT_MSG( !m_source
&& !m_socket
, "forgot to call Close()?" );
55 // get the underlying socket: creates it on demand
56 CFSocketRef
GetSocket() /* const */
65 virtual void DoClose()
67 wxSocketManager
* const manager
= wxSocketManager::Get();
70 manager
->Uninstall_Callback(this, wxSOCKET_INPUT
);
71 manager
->Uninstall_Callback(this, wxSOCKET_OUTPUT
);
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
);
87 // initialize the data associated with the given socket
90 // we need a valid Unix socket to create a CFSocket
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
101 m_socket
= CFSocketCreateWithNative
103 NULL
, // default allocator
105 kCFSocketReadCallBack
|
106 kCFSocketWriteCallBack
|
107 kCFSocketConnectCallBack
,
114 m_source
= CFSocketCreateRunLoopSource(NULL
, m_socket
, 0);
124 CFRunLoopAddSource(gs_mainRunLoop
, m_source
, kCFRunLoopCommonModes
);
129 static void SocketCallback(CFSocketRef
WXUNUSED(s
),
130 CFSocketCallBackType callbackType
,
131 CFDataRef
WXUNUSED(address
),
135 wxSocketImplMac
* const socket
= static_cast<wxSocketImplMac
*>(info
);
137 switch (callbackType
)
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.
148 socket
->OnWriteWaiting();
151 case kCFSocketReadCallBack
:
152 socket
->OnReadWaiting();
155 case kCFSocketWriteCallBack
:
156 socket
->OnWriteWaiting();
160 wxFAIL_MSG( "unexpected socket callback" );
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
171 CFSocketRef m_socket
;
172 CFRunLoopSourceRef m_source
;
174 wxDECLARE_NO_COPY_CLASS(wxSocketImplMac
);
177 } // anonymous namespace
180 // ----------------------------------------------------------------------------
181 // CoreFoundation implementation of wxSocketManager
182 // ----------------------------------------------------------------------------
184 class wxSocketManagerMac
: public wxSocketManager
187 virtual bool OnInit();
188 virtual void OnExit();
190 virtual wxSocketImpl
*CreateSocket(wxSocketBase
& wxsocket
)
192 return new wxSocketImplMac(wxsocket
);
195 virtual void Install_Callback(wxSocketImpl
*socket
, wxSocketNotify event
);
196 virtual void Uninstall_Callback(wxSocketImpl
*socket
, wxSocketNotify event
);
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
203 static int GetCFCallback(wxSocketImpl
*socket
, wxSocketNotify event
);
206 bool wxSocketManagerMac::OnInit()
208 // No need to store the main loop again
209 if (gs_mainRunLoop
!= NULL
)
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
)
219 CFRetain(gs_mainRunLoop
);
224 void wxSocketManagerMac::OnExit()
226 // Release the reference count, and set the reference back to NULL
227 CFRelease(gs_mainRunLoop
);
228 gs_mainRunLoop
= NULL
;
232 int wxSocketManagerMac::GetCFCallback(wxSocketImpl
*socket
, wxSocketNotify event
)
236 case wxSOCKET_CONNECTION
:
237 return socket
->IsServer() ? kCFSocketReadCallBack
238 : kCFSocketConnectCallBack
;
241 return kCFSocketReadCallBack
;
243 case wxSOCKET_OUTPUT
:
244 return kCFSocketWriteCallBack
;
247 wxFAIL_MSG( "unexpected wxSocketNotify" );
251 wxFAIL_MSG( "unknown wxSocketNotify" );
256 void wxSocketManagerMac::Install_Callback(wxSocketImpl
*socket_
,
257 wxSocketNotify event
)
259 wxSocketImplMac
* const socket
= static_cast<wxSocketImplMac
*>(socket_
);
261 CFSocketEnableCallBacks(socket
->GetSocket(), GetCFCallback(socket
, event
));
264 void wxSocketManagerMac::Uninstall_Callback(wxSocketImpl
*socket_
,
265 wxSocketNotify event
)
267 wxSocketImplMac
* const socket
= static_cast<wxSocketImplMac
*>(socket_
);
269 CFSocketDisableCallBacks(socket
->GetSocket(), GetCFCallback(socket
, event
));
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
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
;
280 static struct OSXManagerSetter
284 static wxSocketManagerMac s_manager
;
285 wxOSXSocketManagerCF
= &s_manager
;
287 } gs_OSXManagerSetter
;
289 // see the relative linker macro in socket.cpp
290 wxFORCE_LINK_THIS_MODULE(osxsocket
)
292 #endif // wxUSE_SOCKETS