]>
Commit | Line | Data |
---|---|---|
56d8adc0 | 1 | ///////////////////////////////////////////////////////////////////////////// |
7fb0a11d | 2 | // Name: src/common/socket.cpp |
f4ada568 | 3 | // Purpose: Socket handler classes |
56d8adc0 | 4 | // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia |
f4ada568 | 5 | // Created: April 1997 |
d3ea6527 | 6 | // Copyright: (C) 1999-1997, Guilhem Lavaux |
51fe4b60 VZ |
7 | // (C) 1999-2000, Guillermo Rodriguez Garcia |
8 | // (C) 2008 Vadim Zeitlin | |
f4ada568 | 9 | // RCS_ID: $Id$ |
7fb0a11d | 10 | // License: wxWindows licence |
56d8adc0 GRG |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
bffc1eaa GRG |
13 | // ========================================================================== |
14 | // Declarations | |
15 | // ========================================================================== | |
16 | ||
fcc6dddd JS |
17 | // For compilers that support precompilation, includes "wx.h". |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
8e3f3880 | 21 | #pragma hdrstop |
fcc6dddd JS |
22 | #endif |
23 | ||
35a4dab7 GL |
24 | #if wxUSE_SOCKETS |
25 | ||
df91131c WS |
26 | #include "wx/socket.h" |
27 | ||
8e3f3880 WS |
28 | #ifndef WX_PRECOMP |
29 | #include "wx/object.h" | |
df91131c | 30 | #include "wx/string.h" |
88a7a4e1 | 31 | #include "wx/intl.h" |
e4db172a | 32 | #include "wx/log.h" |
d5da0ce7 | 33 | #include "wx/event.h" |
670f9935 | 34 | #include "wx/app.h" |
de6185e2 | 35 | #include "wx/utils.h" |
c0badb70 | 36 | #include "wx/timer.h" |
02761f6c | 37 | #include "wx/module.h" |
8e3f3880 WS |
38 | #endif |
39 | ||
e2478fde | 40 | #include "wx/apptrait.h" |
56d8adc0 | 41 | #include "wx/sckaddr.h" |
4252569b | 42 | #include "wx/stopwatch.h" |
204abcd4 | 43 | #include "wx/thread.h" |
2804f77d | 44 | #include "wx/evtloop.h" |
54e757fc | 45 | #include "wx/link.h" |
02564412 | 46 | |
40e7c0b9 | 47 | #include "wx/private/fd.h" |
60913641 | 48 | #include "wx/private/socket.h" |
3b4183d8 | 49 | |
14372de8 VZ |
50 | #ifdef __UNIX__ |
51 | #include <errno.h> | |
52 | #endif | |
53 | ||
54 | // we use MSG_NOSIGNAL to avoid getting SIGPIPE when sending data to a remote | |
55 | // host which closed the connection if it is available, otherwise we rely on | |
56 | // SO_NOSIGPIPE existency | |
57 | // | |
58 | // this should cover all the current Unix systems (Windows never sends any | |
59 | // signals anyhow) but if we find one that has neither we should explicitly | |
60 | // ignore SIGPIPE for it | |
66034aba JJ |
61 | // OpenVMS has neither MSG_NOSIGNAL nor SO_NOSIGPIPE. However the socket sample |
62 | // seems to work. Not sure if problems will show up on OpenVMS using sockets. | |
14372de8 VZ |
63 | #ifdef MSG_NOSIGNAL |
64 | #define wxSOCKET_MSG_NOSIGNAL MSG_NOSIGNAL | |
65 | #else // MSG_NOSIGNAL not available (BSD including OS X) | |
04b2ab1a | 66 | // next best possibility is to use SO_NOSIGPIPE socket option, this covers |
335a0bc3 VZ |
67 | // BSD systems (including OS X) -- but if we don't have it neither (AIX and |
68 | // old HP-UX do not), we have to fall back to the old way of simply | |
69 | // disabling SIGPIPE temporarily, so define a class to do it in a safe way | |
04b2ab1a | 70 | #if defined(__UNIX__) && !defined(SO_NOSIGPIPE) |
a645ea88 | 71 | extern "C" { typedef void (*wxSigHandler_t)(int); } |
04b2ab1a VZ |
72 | namespace |
73 | { | |
74 | class IgnoreSignal | |
75 | { | |
76 | public: | |
77 | // ctor disables the given signal | |
78 | IgnoreSignal(int sig) | |
79 | : m_handler(signal(sig, SIG_IGN)), | |
80 | m_sig(sig) | |
81 | { | |
82 | } | |
83 | ||
84 | // dtor restores the old handler | |
85 | ~IgnoreSignal() | |
86 | { | |
87 | signal(m_sig, m_handler); | |
88 | } | |
89 | ||
90 | private: | |
a645ea88 | 91 | const wxSigHandler_t m_handler; |
04b2ab1a VZ |
92 | const int m_sig; |
93 | ||
94 | wxDECLARE_NO_COPY_CLASS(IgnoreSignal); | |
95 | }; | |
96 | } // anonymous namespace | |
335a0bc3 VZ |
97 | |
98 | #define wxNEEDS_IGNORE_SIGPIPE | |
99 | #endif // Unix without SO_NOSIGPIPE | |
14372de8 VZ |
100 | |
101 | #define wxSOCKET_MSG_NOSIGNAL 0 | |
102 | #endif | |
103 | ||
34fdf762 VS |
104 | // DLL options compatibility check: |
105 | #include "wx/build.h" | |
106 | WX_CHECK_BUILD_OPTIONS("wxNet") | |
107 | ||
bffc1eaa GRG |
108 | // -------------------------------------------------------------------------- |
109 | // macros and constants | |
110 | // -------------------------------------------------------------------------- | |
ef57d866 | 111 | |
3c778901 VZ |
112 | // event |
113 | wxDEFINE_EVENT(wxEVT_SOCKET, wxSocketEvent); | |
114 | ||
dc5c1114 GRG |
115 | // discard buffer |
116 | #define MAX_DISCARD_SIZE (10 * 1024) | |
96db102a | 117 | |
007c77ab RL |
118 | #define wxTRACE_Socket _T("wxSocket") |
119 | ||
ef57d866 | 120 | // -------------------------------------------------------------------------- |
bffc1eaa | 121 | // wxWin macros |
ef57d866 | 122 | // -------------------------------------------------------------------------- |
81b92e17 | 123 | |
a737331d GL |
124 | IMPLEMENT_CLASS(wxSocketBase, wxObject) |
125 | IMPLEMENT_CLASS(wxSocketServer, wxSocketBase) | |
126 | IMPLEMENT_CLASS(wxSocketClient, wxSocketBase) | |
dc5c1114 | 127 | IMPLEMENT_CLASS(wxDatagramSocket, wxSocketBase) |
a737331d | 128 | IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent, wxEvent) |
f4ada568 | 129 | |
00414faf VZ |
130 | // ---------------------------------------------------------------------------- |
131 | // private functions | |
132 | // ---------------------------------------------------------------------------- | |
133 | ||
134 | namespace | |
135 | { | |
136 | ||
137 | void SetTimeValFromMS(timeval& tv, unsigned long ms) | |
138 | { | |
139 | tv.tv_sec = (ms / 1000); | |
140 | tv.tv_usec = (ms % 1000) * 1000; | |
141 | } | |
142 | ||
143 | } // anonymous namespace | |
144 | ||
bffc1eaa GRG |
145 | // -------------------------------------------------------------------------- |
146 | // private classes | |
147 | // -------------------------------------------------------------------------- | |
148 | ||
56d8adc0 GRG |
149 | class wxSocketState : public wxObject |
150 | { | |
a324a7bc | 151 | public: |
c2116a35 VZ |
152 | wxSocketFlags m_flags; |
153 | wxSocketEventFlags m_eventmask; | |
154 | bool m_notify; | |
155 | void *m_clientData; | |
a324a7bc GL |
156 | |
157 | public: | |
c2116a35 | 158 | wxSocketState() : wxObject() {} |
22f3361e | 159 | |
c0c133e1 | 160 | wxDECLARE_NO_COPY_CLASS(wxSocketState); |
a324a7bc GL |
161 | }; |
162 | ||
365b8793 VZ |
163 | // wxSocketWaitModeChanger: temporarily change the socket flags affecting its |
164 | // wait mode | |
165 | class wxSocketWaitModeChanger | |
166 | { | |
167 | public: | |
168 | // temporarily set the flags to include the flag value which may be either | |
169 | // wxSOCKET_NOWAIT or wxSOCKET_WAITALL | |
170 | wxSocketWaitModeChanger(wxSocketBase *socket, int flag) | |
171 | : m_socket(socket), | |
172 | m_oldflags(socket->GetFlags()) | |
173 | ||
174 | { | |
175 | wxASSERT_MSG( flag == wxSOCKET_WAITALL || flag == wxSOCKET_NOWAIT, | |
176 | "not a wait flag" ); | |
177 | ||
178 | // preserve wxSOCKET_BLOCK value when switching to wxSOCKET_WAITALL | |
179 | // mode but not when switching to wxSOCKET_NOWAIT as the latter is | |
180 | // incompatible with wxSOCKET_BLOCK | |
181 | if ( flag != wxSOCKET_NOWAIT ) | |
182 | flag |= m_oldflags & wxSOCKET_BLOCK; | |
183 | ||
184 | socket->SetFlags(flag); | |
185 | } | |
186 | ||
187 | ~wxSocketWaitModeChanger() | |
188 | { | |
189 | m_socket->SetFlags(m_oldflags); | |
190 | } | |
191 | ||
192 | private: | |
193 | wxSocketBase * const m_socket; | |
194 | const int m_oldflags; | |
195 | ||
c0c133e1 | 196 | wxDECLARE_NO_COPY_CLASS(wxSocketWaitModeChanger); |
365b8793 VZ |
197 | }; |
198 | ||
199 | // wxSocketRead/WriteGuard are instantiated before starting reading | |
200 | // from/writing to the socket | |
201 | class wxSocketReadGuard | |
202 | { | |
203 | public: | |
204 | wxSocketReadGuard(wxSocketBase *socket) | |
205 | : m_socket(socket) | |
206 | { | |
207 | wxASSERT_MSG( !m_socket->m_reading, "read reentrancy?" ); | |
208 | ||
209 | m_socket->m_reading = true; | |
210 | } | |
211 | ||
212 | ~wxSocketReadGuard() | |
213 | { | |
214 | m_socket->m_reading = false; | |
df21920b VZ |
215 | |
216 | m_socket->m_impl->ReenableEvents(wxSOCKET_INPUT_FLAG); | |
365b8793 VZ |
217 | } |
218 | ||
219 | private: | |
220 | wxSocketBase * const m_socket; | |
221 | ||
c0c133e1 | 222 | wxDECLARE_NO_COPY_CLASS(wxSocketReadGuard); |
365b8793 VZ |
223 | }; |
224 | ||
225 | class wxSocketWriteGuard | |
226 | { | |
227 | public: | |
228 | wxSocketWriteGuard(wxSocketBase *socket) | |
229 | : m_socket(socket) | |
230 | { | |
231 | wxASSERT_MSG( !m_socket->m_writing, "write reentrancy?" ); | |
232 | ||
233 | m_socket->m_writing = true; | |
df21920b VZ |
234 | |
235 | m_socket->m_impl->ReenableEvents(wxSOCKET_OUTPUT_FLAG); | |
365b8793 VZ |
236 | } |
237 | ||
238 | ~wxSocketWriteGuard() | |
239 | { | |
240 | m_socket->m_writing = false; | |
241 | } | |
242 | ||
243 | private: | |
244 | wxSocketBase * const m_socket; | |
245 | ||
c0c133e1 | 246 | wxDECLARE_NO_COPY_CLASS(wxSocketWriteGuard); |
365b8793 VZ |
247 | }; |
248 | ||
2804f77d | 249 | // ============================================================================ |
51fe4b60 | 250 | // wxSocketManager |
2804f77d VZ |
251 | // ============================================================================ |
252 | ||
51fe4b60 | 253 | wxSocketManager *wxSocketManager::ms_manager = NULL; |
2804f77d VZ |
254 | |
255 | /* static */ | |
51fe4b60 | 256 | void wxSocketManager::Set(wxSocketManager *manager) |
2804f77d VZ |
257 | { |
258 | wxASSERT_MSG( !ms_manager, "too late to set manager now" ); | |
259 | ||
260 | ms_manager = manager; | |
261 | } | |
262 | ||
263 | /* static */ | |
51fe4b60 | 264 | void wxSocketManager::Init() |
2804f77d VZ |
265 | { |
266 | wxASSERT_MSG( !ms_manager, "shouldn't be initialized twice" ); | |
267 | ||
268 | /* | |
269 | Details: Initialize() creates a hidden window as a sink for socket | |
270 | events, such as 'read completed'. wxMSW has only one message loop | |
271 | for the main thread. If Initialize is called in a secondary thread, | |
272 | the socket window will be created for the secondary thread, but | |
273 | since there is no message loop on this thread, it will never | |
274 | receive events and all socket operations will time out. | |
275 | BTW, the main thread must not be stopped using sleep or block | |
276 | on a semaphore (a bad idea in any case) or socket operations | |
277 | will time out. | |
278 | ||
279 | On the Mac side, Initialize() stores a pointer to the CFRunLoop for | |
280 | the main thread. Because secondary threads do not have run loops, | |
281 | adding event notifications to the "Current" loop would have no | |
282 | effect at all, events would never fire. | |
283 | */ | |
284 | wxASSERT_MSG( wxIsMainThread(), | |
285 | "sockets must be initialized from the main thread" ); | |
286 | ||
287 | wxAppConsole * const app = wxAppConsole::GetInstance(); | |
288 | wxCHECK_RET( app, "sockets can't be initialized without wxApp" ); | |
289 | ||
290 | ms_manager = app->GetTraits()->GetSocketManager(); | |
291 | } | |
292 | ||
eb97543d | 293 | // ========================================================================== |
51fe4b60 | 294 | // wxSocketImpl |
eb97543d VZ |
295 | // ========================================================================== |
296 | ||
51fe4b60 | 297 | wxSocketImpl::wxSocketImpl(wxSocketBase& wxsocket) |
53a161e1 | 298 | : m_wxsocket(&wxsocket) |
eb97543d VZ |
299 | { |
300 | m_fd = INVALID_SOCKET; | |
51fe4b60 | 301 | m_error = wxSOCKET_NOERROR; |
eb97543d VZ |
302 | m_server = false; |
303 | m_stream = true; | |
53a161e1 VZ |
304 | |
305 | SetTimeout(wxsocket.GetTimeout() * 1000); | |
eb97543d VZ |
306 | |
307 | m_establishing = false; | |
308 | m_reusable = false; | |
309 | m_broadcast = false; | |
310 | m_dobind = true; | |
311 | m_initialRecvBufferSize = -1; | |
312 | m_initialSendBufferSize = -1; | |
eb97543d VZ |
313 | } |
314 | ||
51fe4b60 | 315 | wxSocketImpl::~wxSocketImpl() |
eb97543d | 316 | { |
c9bccf23 | 317 | if ( m_fd != INVALID_SOCKET ) |
eb97543d | 318 | Shutdown(); |
51fe4b60 VZ |
319 | } |
320 | ||
c9bccf23 | 321 | bool wxSocketImpl::PreCreateCheck(const wxSockAddressImpl& addr) |
51fe4b60 VZ |
322 | { |
323 | if ( m_fd != INVALID_SOCKET ) | |
324 | { | |
325 | m_error = wxSOCKET_INVSOCK; | |
326 | return false; | |
327 | } | |
328 | ||
c9bccf23 | 329 | if ( !addr.IsOk() ) |
51fe4b60 VZ |
330 | { |
331 | m_error = wxSOCKET_INVADDR; | |
332 | return false; | |
333 | } | |
334 | ||
335 | return true; | |
336 | } | |
337 | ||
338 | void wxSocketImpl::PostCreation() | |
339 | { | |
340 | // FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option | |
341 | #ifdef SO_NOSIGPIPE | |
342 | EnableSocketOption(SO_NOSIGPIPE); | |
343 | #endif | |
344 | ||
345 | if ( m_reusable ) | |
346 | EnableSocketOption(SO_REUSEADDR); | |
347 | ||
348 | if ( m_broadcast ) | |
349 | { | |
350 | wxASSERT_MSG( !m_stream, "broadcasting is for datagram sockets only" ); | |
351 | ||
352 | EnableSocketOption(SO_BROADCAST); | |
353 | } | |
354 | ||
355 | if ( m_initialRecvBufferSize >= 0 ) | |
356 | SetSocketOption(SO_RCVBUF, m_initialRecvBufferSize); | |
357 | if ( m_initialSendBufferSize >= 0 ) | |
358 | SetSocketOption(SO_SNDBUF, m_initialSendBufferSize); | |
eb97543d | 359 | |
2b036c4b VZ |
360 | // we always put our sockets in unblocked mode and handle blocking |
361 | // ourselves in DoRead/Write() if wxSOCKET_WAITALL is specified | |
51fe4b60 | 362 | UnblockAndRegisterWithEventLoop(); |
eb97543d VZ |
363 | } |
364 | ||
51fe4b60 VZ |
365 | wxSocketError wxSocketImpl::UpdateLocalAddress() |
366 | { | |
7172db18 VZ |
367 | if ( !m_local.IsOk() ) |
368 | { | |
369 | // ensure that we have a valid object using the correct family: correct | |
370 | // being the same one as our peer uses as we have no other way to | |
371 | // determine it | |
372 | m_local.Create(m_peer.GetFamily()); | |
373 | } | |
374 | ||
c9bccf23 VZ |
375 | WX_SOCKLEN_T lenAddr = m_local.GetLen(); |
376 | if ( getsockname(m_fd, m_local.GetWritableAddr(), &lenAddr) != 0 ) | |
51fe4b60 VZ |
377 | { |
378 | Close(); | |
379 | m_error = wxSOCKET_IOERR; | |
380 | return m_error; | |
381 | } | |
382 | ||
51fe4b60 VZ |
383 | return wxSOCKET_NOERROR; |
384 | } | |
385 | ||
386 | wxSocketError wxSocketImpl::CreateServer() | |
387 | { | |
388 | if ( !PreCreateCheck(m_local) ) | |
389 | return m_error; | |
390 | ||
391 | m_server = true; | |
392 | m_stream = true; | |
393 | ||
394 | // do create the socket | |
c9bccf23 | 395 | m_fd = socket(m_local.GetFamily(), SOCK_STREAM, 0); |
51fe4b60 VZ |
396 | |
397 | if ( m_fd == INVALID_SOCKET ) | |
398 | { | |
399 | m_error = wxSOCKET_IOERR; | |
400 | return wxSOCKET_IOERR; | |
401 | } | |
402 | ||
403 | PostCreation(); | |
404 | ||
405 | // and then bind to and listen on it | |
406 | // | |
407 | // FIXME: should we test for m_dobind here? | |
c9bccf23 | 408 | if ( bind(m_fd, m_local.GetAddr(), m_local.GetLen()) != 0 ) |
51fe4b60 VZ |
409 | m_error = wxSOCKET_IOERR; |
410 | ||
411 | if ( IsOk() ) | |
412 | { | |
413 | if ( listen(m_fd, 5) != 0 ) | |
414 | m_error = wxSOCKET_IOERR; | |
415 | } | |
416 | ||
417 | if ( !IsOk() ) | |
418 | { | |
419 | Close(); | |
420 | return m_error; | |
421 | } | |
422 | ||
423 | // finally retrieve the address we effectively bound to | |
424 | return UpdateLocalAddress(); | |
425 | } | |
426 | ||
2b036c4b | 427 | wxSocketError wxSocketImpl::CreateClient(bool wait) |
51fe4b60 VZ |
428 | { |
429 | if ( !PreCreateCheck(m_peer) ) | |
430 | return m_error; | |
431 | ||
c9bccf23 | 432 | m_fd = socket(m_peer.GetFamily(), SOCK_STREAM, 0); |
51fe4b60 VZ |
433 | |
434 | if ( m_fd == INVALID_SOCKET ) | |
435 | { | |
2b036c4b VZ |
436 | m_error = wxSOCKET_IOERR; |
437 | return wxSOCKET_IOERR; | |
51fe4b60 VZ |
438 | } |
439 | ||
440 | PostCreation(); | |
441 | ||
442 | // If a local address has been set, then bind to it before calling connect | |
c9bccf23 | 443 | if ( m_local.IsOk() ) |
51fe4b60 | 444 | { |
c9bccf23 | 445 | if ( bind(m_fd, m_local.GetAddr(), m_local.GetLen()) != 0 ) |
51fe4b60 VZ |
446 | { |
447 | Close(); | |
448 | m_error = wxSOCKET_IOERR; | |
449 | return m_error; | |
450 | } | |
451 | } | |
452 | ||
2b036c4b | 453 | // Do connect now |
c9bccf23 | 454 | int rc = connect(m_fd, m_peer.GetAddr(), m_peer.GetLen()); |
2b036c4b VZ |
455 | if ( rc == SOCKET_ERROR ) |
456 | { | |
457 | wxSocketError err = GetLastError(); | |
458 | if ( err == wxSOCKET_WOULDBLOCK ) | |
459 | { | |
460 | m_establishing = true; | |
461 | ||
462 | // block waiting for connection if we should (otherwise just return | |
463 | // wxSOCKET_WOULDBLOCK to the caller) | |
464 | if ( wait ) | |
465 | { | |
466 | err = SelectWithTimeout(wxSOCKET_CONNECTION_FLAG) | |
467 | ? wxSOCKET_NOERROR | |
468 | : wxSOCKET_TIMEDOUT; | |
469 | m_establishing = false; | |
470 | } | |
471 | } | |
472 | ||
473 | m_error = err; | |
474 | } | |
475 | else // connected | |
476 | { | |
477 | m_error = wxSOCKET_NOERROR; | |
478 | } | |
479 | ||
480 | return m_error; | |
51fe4b60 VZ |
481 | } |
482 | ||
483 | ||
484 | wxSocketError wxSocketImpl::CreateUDP() | |
485 | { | |
486 | if ( !PreCreateCheck(m_local) ) | |
487 | return m_error; | |
488 | ||
489 | m_stream = false; | |
490 | m_server = false; | |
491 | ||
c9bccf23 | 492 | m_fd = socket(m_local.GetFamily(), SOCK_DGRAM, 0); |
51fe4b60 VZ |
493 | |
494 | if ( m_fd == INVALID_SOCKET ) | |
495 | { | |
496 | m_error = wxSOCKET_IOERR; | |
497 | return wxSOCKET_IOERR; | |
498 | } | |
499 | ||
500 | PostCreation(); | |
501 | ||
502 | if ( m_dobind ) | |
503 | { | |
c9bccf23 | 504 | if ( bind(m_fd, m_local.GetAddr(), m_local.GetLen()) != 0 ) |
51fe4b60 VZ |
505 | { |
506 | Close(); | |
507 | m_error = wxSOCKET_IOERR; | |
508 | return m_error; | |
509 | } | |
510 | ||
511 | return UpdateLocalAddress(); | |
512 | } | |
513 | ||
514 | return wxSOCKET_NOERROR; | |
515 | } | |
516 | ||
2b036c4b VZ |
517 | wxSocketImpl *wxSocketImpl::Accept(wxSocketBase& wxsocket) |
518 | { | |
c9bccf23 | 519 | wxSockAddressStorage from; |
2b036c4b | 520 | WX_SOCKLEN_T fromlen = sizeof(from); |
c9bccf23 | 521 | const SOCKET fd = accept(m_fd, &from.addr, &fromlen); |
2b036c4b | 522 | |
df21920b VZ |
523 | // accepting is similar to reading in the sense that it resets "ready for |
524 | // read" flag on the socket | |
525 | ReenableEvents(wxSOCKET_INPUT_FLAG); | |
526 | ||
2b036c4b VZ |
527 | if ( fd == INVALID_SOCKET ) |
528 | return NULL; | |
529 | ||
530 | wxSocketImpl * const sock = Create(wxsocket); | |
531 | sock->m_fd = fd; | |
c9bccf23 | 532 | sock->m_peer = wxSockAddressImpl(from.addr, fromlen); |
2b036c4b VZ |
533 | |
534 | sock->UnblockAndRegisterWithEventLoop(); | |
535 | ||
536 | return sock; | |
537 | } | |
538 | ||
51fe4b60 VZ |
539 | |
540 | void wxSocketImpl::Close() | |
f0fbbe23 VZ |
541 | { |
542 | if ( m_fd != INVALID_SOCKET ) | |
543 | { | |
51fe4b60 | 544 | DoClose(); |
f0fbbe23 VZ |
545 | m_fd = INVALID_SOCKET; |
546 | } | |
547 | } | |
548 | ||
51fe4b60 | 549 | void wxSocketImpl::Shutdown() |
eb97543d VZ |
550 | { |
551 | if ( m_fd != INVALID_SOCKET ) | |
552 | { | |
553 | shutdown(m_fd, 1 /* SD_SEND */); | |
554 | Close(); | |
555 | } | |
eb97543d VZ |
556 | } |
557 | ||
51fe4b60 | 558 | /* |
53a161e1 VZ |
559 | * Sets the timeout for blocking calls. Time is expressed in |
560 | * milliseconds. | |
561 | */ | |
51fe4b60 | 562 | void wxSocketImpl::SetTimeout(unsigned long millis) |
53a161e1 | 563 | { |
00414faf | 564 | SetTimeValFromMS(m_timeout, millis); |
53a161e1 VZ |
565 | } |
566 | ||
51fe4b60 | 567 | void wxSocketImpl::NotifyOnStateChange(wxSocketNotify event) |
53a161e1 | 568 | { |
51fe4b60 | 569 | m_wxsocket->OnRequest(event); |
53a161e1 VZ |
570 | } |
571 | ||
02564412 | 572 | /* Address handling */ |
c9bccf23 | 573 | wxSocketError wxSocketImpl::SetLocal(const wxSockAddressImpl& local) |
02564412 | 574 | { |
c9bccf23 VZ |
575 | /* the socket must be initialized, or it must be a server */ |
576 | if (m_fd != INVALID_SOCKET && !m_server) | |
577 | { | |
578 | m_error = wxSOCKET_INVSOCK; | |
579 | return wxSOCKET_INVSOCK; | |
580 | } | |
02564412 | 581 | |
c9bccf23 VZ |
582 | if ( !local.IsOk() ) |
583 | { | |
584 | m_error = wxSOCKET_INVADDR; | |
585 | return wxSOCKET_INVADDR; | |
586 | } | |
02564412 | 587 | |
c9bccf23 | 588 | m_local = local; |
02564412 | 589 | |
c9bccf23 | 590 | return wxSOCKET_NOERROR; |
02564412 VZ |
591 | } |
592 | ||
c9bccf23 | 593 | wxSocketError wxSocketImpl::SetPeer(const wxSockAddressImpl& peer) |
02564412 | 594 | { |
c9bccf23 VZ |
595 | if ( !peer.IsOk() ) |
596 | { | |
597 | m_error = wxSOCKET_INVADDR; | |
598 | return wxSOCKET_INVADDR; | |
599 | } | |
02564412 | 600 | |
c9bccf23 | 601 | m_peer = peer; |
02564412 | 602 | |
c9bccf23 | 603 | return wxSOCKET_NOERROR; |
02564412 VZ |
604 | } |
605 | ||
c9bccf23 | 606 | const wxSockAddressImpl& wxSocketImpl::GetLocal() |
02564412 | 607 | { |
c9bccf23 VZ |
608 | if ( !m_local.IsOk() ) |
609 | UpdateLocalAddress(); | |
02564412 | 610 | |
c9bccf23 | 611 | return m_local; |
02564412 VZ |
612 | } |
613 | ||
14372de8 VZ |
614 | // ---------------------------------------------------------------------------- |
615 | // wxSocketImpl IO | |
616 | // ---------------------------------------------------------------------------- | |
617 | ||
618 | // this macro wraps the given expression (normally a syscall) in a loop which | |
619 | // ignores any interruptions, i.e. reevaluates it again if it failed and errno | |
620 | // is EINTR | |
621 | #ifdef __UNIX__ | |
622 | #define DO_WHILE_EINTR( rc, syscall ) \ | |
623 | do { \ | |
624 | rc = (syscall); \ | |
625 | } \ | |
626 | while ( rc == -1 && errno == EINTR ) | |
627 | #else | |
628 | #define DO_WHILE_EINTR( rc, syscall ) rc = (syscall) | |
629 | #endif | |
630 | ||
631 | int wxSocketImpl::RecvStream(void *buffer, int size) | |
632 | { | |
633 | int ret; | |
634 | DO_WHILE_EINTR( ret, recv(m_fd, static_cast<char *>(buffer), size, 0) ); | |
635 | ||
636 | if ( !ret ) | |
637 | { | |
638 | // receiving 0 bytes for a TCP socket indicates that the connection was | |
639 | // closed by peer so shut down our end as well (for UDP sockets empty | |
640 | // datagrams are also possible) | |
641 | m_establishing = false; | |
642 | NotifyOnStateChange(wxSOCKET_LOST); | |
643 | ||
644 | Shutdown(); | |
645 | ||
646 | // do not return an error in this case however | |
647 | } | |
648 | ||
649 | return ret; | |
650 | } | |
651 | ||
652 | int wxSocketImpl::SendStream(const void *buffer, int size) | |
653 | { | |
335a0bc3 | 654 | #ifdef wxNEEDS_IGNORE_SIGPIPE |
04b2ab1a VZ |
655 | IgnoreSignal ignore(SIGPIPE); |
656 | #endif | |
657 | ||
14372de8 VZ |
658 | int ret; |
659 | DO_WHILE_EINTR( ret, send(m_fd, static_cast<const char *>(buffer), size, | |
660 | wxSOCKET_MSG_NOSIGNAL) ); | |
661 | ||
662 | return ret; | |
663 | } | |
664 | ||
665 | int wxSocketImpl::RecvDgram(void *buffer, int size) | |
666 | { | |
c9bccf23 | 667 | wxSockAddressStorage from; |
14372de8 VZ |
668 | WX_SOCKLEN_T fromlen = sizeof(from); |
669 | ||
670 | int ret; | |
671 | DO_WHILE_EINTR( ret, recvfrom(m_fd, static_cast<char *>(buffer), size, | |
c9bccf23 | 672 | 0, &from.addr, &fromlen) ); |
14372de8 VZ |
673 | |
674 | if ( ret == SOCKET_ERROR ) | |
675 | return SOCKET_ERROR; | |
676 | ||
c9bccf23 VZ |
677 | m_peer = wxSockAddressImpl(from.addr, fromlen); |
678 | if ( !m_peer.IsOk() ) | |
14372de8 | 679 | return -1; |
14372de8 VZ |
680 | |
681 | return ret; | |
682 | } | |
683 | ||
684 | int wxSocketImpl::SendDgram(const void *buffer, int size) | |
685 | { | |
c9bccf23 | 686 | if ( !m_peer.IsOk() ) |
14372de8 VZ |
687 | { |
688 | m_error = wxSOCKET_INVADDR; | |
689 | return -1; | |
690 | } | |
691 | ||
14372de8 VZ |
692 | int ret; |
693 | DO_WHILE_EINTR( ret, sendto(m_fd, static_cast<const char *>(buffer), size, | |
c9bccf23 | 694 | 0, m_peer.GetAddr(), m_peer.GetLen()) ); |
14372de8 VZ |
695 | |
696 | return ret; | |
697 | } | |
698 | ||
699 | int wxSocketImpl::Read(void *buffer, int size) | |
700 | { | |
701 | // server sockets can't be used for IO, only to accept new connections | |
702 | if ( m_fd == INVALID_SOCKET || m_server ) | |
703 | { | |
704 | m_error = wxSOCKET_INVSOCK; | |
705 | return -1; | |
706 | } | |
707 | ||
708 | int ret = m_stream ? RecvStream(buffer, size) | |
709 | : RecvDgram(buffer, size); | |
710 | ||
711 | m_error = ret == SOCKET_ERROR ? GetLastError() : wxSOCKET_NOERROR; | |
712 | ||
713 | return ret; | |
714 | } | |
715 | ||
716 | int wxSocketImpl::Write(const void *buffer, int size) | |
717 | { | |
718 | if ( m_fd == INVALID_SOCKET || m_server ) | |
719 | { | |
720 | m_error = wxSOCKET_INVSOCK; | |
721 | return -1; | |
722 | } | |
723 | ||
724 | int ret = m_stream ? SendStream(buffer, size) | |
725 | : SendDgram(buffer, size); | |
726 | ||
727 | m_error = ret == SOCKET_ERROR ? GetLastError() : wxSOCKET_NOERROR; | |
728 | ||
729 | return ret; | |
730 | } | |
731 | ||
ef57d866 GRG |
732 | // ========================================================================== |
733 | // wxSocketBase | |
734 | // ========================================================================== | |
735 | ||
6c0d0845 VZ |
736 | // -------------------------------------------------------------------------- |
737 | // Initialization and shutdown | |
738 | // -------------------------------------------------------------------------- | |
739 | ||
740 | // FIXME-MT: all this is MT-unsafe, of course, we should protect all accesses | |
741 | // to m_countInit with a crit section | |
742 | size_t wxSocketBase::m_countInit = 0; | |
743 | ||
744 | bool wxSocketBase::IsInitialized() | |
745 | { | |
746 | return m_countInit > 0; | |
747 | } | |
748 | ||
749 | bool wxSocketBase::Initialize() | |
750 | { | |
751 | if ( !m_countInit++ ) | |
752 | { | |
51fe4b60 VZ |
753 | wxSocketManager * const manager = wxSocketManager::Get(); |
754 | if ( !manager || !manager->OnInit() ) | |
6c0d0845 VZ |
755 | { |
756 | m_countInit--; | |
757 | ||
d775fa82 | 758 | return false; |
6c0d0845 VZ |
759 | } |
760 | } | |
761 | ||
d775fa82 | 762 | return true; |
6c0d0845 VZ |
763 | } |
764 | ||
765 | void wxSocketBase::Shutdown() | |
766 | { | |
767 | // we should be initialized | |
2804f77d | 768 | wxASSERT_MSG( m_countInit > 0, _T("extra call to Shutdown()") ); |
8d7eaf91 | 769 | if ( --m_countInit == 0 ) |
6c0d0845 | 770 | { |
51fe4b60 VZ |
771 | wxSocketManager * const manager = wxSocketManager::Get(); |
772 | wxCHECK_RET( manager, "should have a socket manager" ); | |
773 | ||
774 | manager->OnExit(); | |
6c0d0845 VZ |
775 | } |
776 | } | |
777 | ||
ef57d866 GRG |
778 | // -------------------------------------------------------------------------- |
779 | // Ctor and dtor | |
780 | // -------------------------------------------------------------------------- | |
56d8adc0 | 781 | |
71622a7a | 782 | void wxSocketBase::Init() |
f4ada568 | 783 | { |
365b8793 | 784 | m_impl = NULL; |
c2116a35 VZ |
785 | m_type = wxSOCKET_UNINIT; |
786 | ||
787 | // state | |
788 | m_flags = 0; | |
789 | m_connected = | |
790 | m_establishing = | |
791 | m_reading = | |
792 | m_writing = | |
c2116a35 VZ |
793 | m_closed = false; |
794 | m_lcount = 0; | |
795 | m_timeout = 600; | |
796 | m_beingDeleted = false; | |
797 | ||
798 | // pushback buffer | |
799 | m_unread = NULL; | |
800 | m_unrd_size = 0; | |
801 | m_unrd_cur = 0; | |
802 | ||
803 | // events | |
804 | m_id = wxID_ANY; | |
805 | m_handler = NULL; | |
806 | m_clientData = NULL; | |
807 | m_notify = false; | |
5c1193e0 VZ |
808 | m_eventmask = |
809 | m_eventsgot = 0; | |
c2116a35 VZ |
810 | |
811 | if ( !IsInitialized() ) | |
812 | { | |
813 | // this Initialize() will be undone by wxSocketModule::OnExit(), all | |
814 | // the other calls to it should be matched by a call to Shutdown() | |
54e757fc FM |
815 | if (!Initialize()) |
816 | wxLogError("Cannot initialize wxSocketBase"); | |
c2116a35 | 817 | } |
f4ada568 GL |
818 | } |
819 | ||
f187448d GRG |
820 | wxSocketBase::wxSocketBase() |
821 | { | |
c2116a35 | 822 | Init(); |
f187448d | 823 | } |
71622a7a GRG |
824 | |
825 | wxSocketBase::wxSocketBase(wxSocketFlags flags, wxSocketType type) | |
f4ada568 | 826 | { |
c2116a35 | 827 | Init(); |
71622a7a | 828 | |
cc0972a2 VZ |
829 | SetFlags(flags); |
830 | ||
831 | m_type = type; | |
f4ada568 GL |
832 | } |
833 | ||
f4ada568 GL |
834 | wxSocketBase::~wxSocketBase() |
835 | { | |
c2116a35 VZ |
836 | // Just in case the app called Destroy() *and* then deleted the socket |
837 | // immediately: don't leave dangling pointers. | |
838 | wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL; | |
839 | if ( traits ) | |
840 | traits->RemoveFromPendingDelete(this); | |
f4ada568 | 841 | |
c2116a35 VZ |
842 | // Shutdown and close the socket |
843 | if (!m_beingDeleted) | |
844 | Close(); | |
9181a383 | 845 | |
51fe4b60 VZ |
846 | // Destroy the implementation object |
847 | delete m_impl; | |
c2116a35 VZ |
848 | |
849 | // Free the pushback buffer | |
850 | if (m_unread) | |
851 | free(m_unread); | |
f4ada568 GL |
852 | } |
853 | ||
ef57d866 GRG |
854 | bool wxSocketBase::Destroy() |
855 | { | |
c2116a35 VZ |
856 | // Delayed destruction: the socket will be deleted during the next idle |
857 | // loop iteration. This ensures that all pending events have been | |
858 | // processed. | |
859 | m_beingDeleted = true; | |
860 | ||
861 | // Shutdown and close the socket | |
862 | Close(); | |
863 | ||
22185a1f | 864 | // Suppress events from now on |
c2116a35 VZ |
865 | Notify(false); |
866 | ||
867 | // schedule this object for deletion | |
868 | wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL; | |
869 | if ( traits ) | |
870 | { | |
871 | // let the traits object decide what to do with us | |
872 | traits->ScheduleForDestroy(this); | |
873 | } | |
874 | else // no app or no traits | |
875 | { | |
876 | // in wxBase we might have no app object at all, don't leak memory | |
877 | delete this; | |
878 | } | |
879 | ||
880 | return true; | |
ef57d866 | 881 | } |
9181a383 | 882 | |
51fe4b60 | 883 | // ---------------------------------------------------------------------------- |
64b1cea0 | 884 | // simple accessors |
51fe4b60 VZ |
885 | // ---------------------------------------------------------------------------- |
886 | ||
64b1cea0 VZ |
887 | void wxSocketBase::SetError(wxSocketError error) |
888 | { | |
889 | m_impl->m_error = error; | |
890 | } | |
891 | ||
51fe4b60 VZ |
892 | wxSocketError wxSocketBase::LastError() const |
893 | { | |
894 | return m_impl->GetError(); | |
895 | } | |
896 | ||
ef57d866 | 897 | // -------------------------------------------------------------------------- |
bffc1eaa | 898 | // Basic IO calls |
ef57d866 GRG |
899 | // -------------------------------------------------------------------------- |
900 | ||
64b1cea0 | 901 | // The following IO operations update m_lcount: |
ef57d866 | 902 | // {Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard} |
f4ada568 GL |
903 | bool wxSocketBase::Close() |
904 | { | |
c2116a35 VZ |
905 | // Interrupt pending waits |
906 | InterruptWait(); | |
907 | ||
b67397a7 | 908 | ShutdownOutput(); |
c2116a35 VZ |
909 | |
910 | m_connected = false; | |
911 | m_establishing = false; | |
912 | return true; | |
f4ada568 GL |
913 | } |
914 | ||
b67397a7 VZ |
915 | void wxSocketBase::ShutdownOutput() |
916 | { | |
917 | if ( m_impl ) | |
918 | m_impl->Shutdown(); | |
919 | } | |
920 | ||
f187448d | 921 | wxSocketBase& wxSocketBase::Read(void* buffer, wxUint32 nbytes) |
f4ada568 | 922 | { |
365b8793 | 923 | wxSocketReadGuard read(this); |
a324a7bc | 924 | |
cc0972a2 | 925 | m_lcount = DoRead(buffer, nbytes); |
17aa2bec | 926 | |
c2116a35 | 927 | return *this; |
96db102a GRG |
928 | } |
929 | ||
cc0972a2 | 930 | wxUint32 wxSocketBase::DoRead(void* buffer_, wxUint32 nbytes) |
96db102a | 931 | { |
f5395600 VZ |
932 | wxCHECK_MSG( m_impl, 0, "socket must be valid" ); |
933 | ||
cc0972a2 VZ |
934 | // We use pointer arithmetic here which doesn't work with void pointers. |
935 | char *buffer = static_cast<char *>(buffer_); | |
f5395600 | 936 | wxCHECK_MSG( buffer, 0, "NULL buffer" ); |
c2116a35 | 937 | |
cc0972a2 VZ |
938 | // Try the push back buffer first, even before checking whether the socket |
939 | // is valid to allow reading previously pushed back data from an already | |
940 | // closed socket. | |
941 | wxUint32 total = GetPushback(buffer, nbytes, false); | |
c2116a35 | 942 | nbytes -= total; |
cc0972a2 | 943 | buffer += total; |
c2116a35 | 944 | |
f5395600 | 945 | while ( nbytes ) |
cc0972a2 | 946 | { |
f5395600 VZ |
947 | // our socket is non-blocking so Read() will return immediately if |
948 | // there is nothing to read yet and it's more efficient to try it first | |
6f405b31 VZ |
949 | // before entering DoWait() which is going to start dispatching GUI |
950 | // events and, even more importantly, we must do this under Windows | |
f5395600 VZ |
951 | // where we're not going to get notifications about socket being ready |
952 | // for reading before we read all the existing data from it | |
d34f724d VZ |
953 | const int ret = !m_impl->m_stream || m_connected |
954 | ? m_impl->Read(buffer, nbytes) | |
955 | : 0; | |
64b1cea0 VZ |
956 | if ( ret == -1 ) |
957 | { | |
f5395600 | 958 | if ( m_impl->GetLastError() == wxSOCKET_WOULDBLOCK ) |
c2116a35 | 959 | { |
f5395600 VZ |
960 | // if we don't want to wait, just return immediately |
961 | if ( m_flags & wxSOCKET_NOWAIT ) | |
962 | break; | |
c2116a35 | 963 | |
6f405b31 VZ |
964 | // otherwise wait until the socket becomes ready for reading or |
965 | // an error occurs on it | |
ebbf7407 | 966 | if ( !DoWaitWithTimeout(wxSOCKET_INPUT_FLAG) ) |
f5395600 VZ |
967 | { |
968 | // and exit if the timeout elapsed before it did | |
969 | SetError(wxSOCKET_TIMEDOUT); | |
970 | break; | |
971 | } | |
c2116a35 | 972 | |
f5395600 VZ |
973 | // retry reading |
974 | continue; | |
975 | } | |
976 | else // "real" error | |
977 | { | |
978 | SetError(wxSOCKET_IOERR); | |
c2116a35 | 979 | break; |
f5395600 VZ |
980 | } |
981 | } | |
982 | else if ( ret == 0 ) | |
983 | { | |
984 | // for connection-oriented (e.g. TCP) sockets we can only read | |
985 | // 0 bytes if the other end has been closed, and for connectionless | |
986 | // ones (UDP) this flag doesn't make sense anyhow so we can set it | |
987 | // to true too without doing any harm | |
988 | m_closed = true; | |
989 | ||
990 | // we're not going to read anything else and so if we haven't read | |
991 | // anything (or not everything in wxSOCKET_WAITALL case) already, | |
992 | // signal an error | |
993 | if ( (m_flags & wxSOCKET_WAITALL) || !total ) | |
994 | SetError(wxSOCKET_IOERR); | |
995 | break; | |
996 | } | |
c2116a35 | 997 | |
f5395600 | 998 | total += ret; |
c2116a35 | 999 | |
f5395600 VZ |
1000 | // if we are happy to read something and not the entire nbytes bytes, |
1001 | // then we're done | |
1002 | if ( !(m_flags & wxSOCKET_WAITALL) ) | |
1003 | break; | |
64b1cea0 | 1004 | |
f5395600 VZ |
1005 | nbytes -= ret; |
1006 | buffer += ret; | |
c2116a35 | 1007 | } |
96db102a | 1008 | |
96db102a | 1009 | return total; |
c2116a35 | 1010 | } |
f4ada568 | 1011 | |
c2116a35 VZ |
1012 | wxSocketBase& wxSocketBase::ReadMsg(void* buffer, wxUint32 nbytes) |
1013 | { | |
c2116a35 VZ |
1014 | struct |
1015 | { | |
1016 | unsigned char sig[4]; | |
1017 | unsigned char len[4]; | |
1018 | } msg; | |
1019 | ||
365b8793 | 1020 | wxSocketReadGuard read(this); |
c2116a35 | 1021 | |
365b8793 | 1022 | wxSocketWaitModeChanger changeFlags(this, wxSOCKET_WAITALL); |
c9157492 | 1023 | |
64b1cea0 VZ |
1024 | bool ok = false; |
1025 | if ( DoRead(&msg, sizeof(msg)) == sizeof(msg) ) | |
56d8adc0 | 1026 | { |
64b1cea0 VZ |
1027 | wxUint32 sig = (wxUint32)msg.sig[0]; |
1028 | sig |= (wxUint32)(msg.sig[1] << 8); | |
1029 | sig |= (wxUint32)(msg.sig[2] << 16); | |
1030 | sig |= (wxUint32)(msg.sig[3] << 24); | |
f4ada568 | 1031 | |
64b1cea0 VZ |
1032 | if ( sig == 0xfeeddead ) |
1033 | { | |
1034 | wxUint32 len = (wxUint32)msg.len[0]; | |
1035 | len |= (wxUint32)(msg.len[1] << 8); | |
1036 | len |= (wxUint32)(msg.len[2] << 16); | |
1037 | len |= (wxUint32)(msg.len[3] << 24); | |
f4ada568 | 1038 | |
64b1cea0 VZ |
1039 | wxUint32 len2; |
1040 | if (len > nbytes) | |
1041 | { | |
1042 | len2 = len - nbytes; | |
1043 | len = nbytes; | |
1044 | } | |
1045 | else | |
1046 | len2 = 0; | |
c2116a35 | 1047 | |
64b1cea0 VZ |
1048 | // Don't attempt to read if the msg was zero bytes long. |
1049 | m_lcount = len ? DoRead(buffer, len) : 0; | |
c2116a35 | 1050 | |
64b1cea0 VZ |
1051 | if ( len2 ) |
1052 | { | |
1053 | char discard_buffer[MAX_DISCARD_SIZE]; | |
1054 | long discard_len; | |
1055 | ||
1056 | // NOTE: discarded bytes don't add to m_lcount. | |
1057 | do | |
1058 | { | |
1059 | discard_len = len2 > MAX_DISCARD_SIZE | |
1060 | ? MAX_DISCARD_SIZE | |
1061 | : len2; | |
1062 | discard_len = DoRead(discard_buffer, (wxUint32)discard_len); | |
1063 | len2 -= (wxUint32)discard_len; | |
1064 | } | |
1065 | while ((discard_len > 0) && len2); | |
1066 | } | |
17aa2bec | 1067 | |
64b1cea0 VZ |
1068 | if ( !len2 && DoRead(&msg, sizeof(msg)) == sizeof(msg) ) |
1069 | { | |
1070 | sig = (wxUint32)msg.sig[0]; | |
1071 | sig |= (wxUint32)(msg.sig[1] << 8); | |
1072 | sig |= (wxUint32)(msg.sig[2] << 16); | |
1073 | sig |= (wxUint32)(msg.sig[3] << 24); | |
c2116a35 | 1074 | |
64b1cea0 VZ |
1075 | if ( sig == 0xdeadfeed ) |
1076 | ok = true; | |
1077 | } | |
c2116a35 | 1078 | } |
c2116a35 | 1079 | } |
062c4861 | 1080 | |
64b1cea0 VZ |
1081 | if ( !ok ) |
1082 | SetError(wxSOCKET_IOERR); | |
96db102a | 1083 | |
c2116a35 | 1084 | return *this; |
062c4861 GL |
1085 | } |
1086 | ||
f187448d | 1087 | wxSocketBase& wxSocketBase::Peek(void* buffer, wxUint32 nbytes) |
f4ada568 | 1088 | { |
365b8793 | 1089 | wxSocketReadGuard read(this); |
96db102a | 1090 | |
cc0972a2 | 1091 | m_lcount = DoRead(buffer, nbytes); |
96db102a | 1092 | |
365b8793 | 1093 | Pushback(buffer, m_lcount); |
f4ada568 | 1094 | |
c2116a35 | 1095 | return *this; |
f4ada568 GL |
1096 | } |
1097 | ||
f187448d | 1098 | wxSocketBase& wxSocketBase::Write(const void *buffer, wxUint32 nbytes) |
f4ada568 | 1099 | { |
365b8793 | 1100 | wxSocketWriteGuard write(this); |
56d8adc0 | 1101 | |
cc0972a2 | 1102 | m_lcount = DoWrite(buffer, nbytes); |
96db102a | 1103 | |
c2116a35 | 1104 | return *this; |
96db102a GRG |
1105 | } |
1106 | ||
cc0972a2 | 1107 | // This function is a mirror image of DoRead() except that it doesn't use the |
f5395600 VZ |
1108 | // push back buffer and doesn't treat 0 return value specially (normally this |
1109 | // shouldn't happen at all here), so please see comments there for explanations | |
cc0972a2 | 1110 | wxUint32 wxSocketBase::DoWrite(const void *buffer_, wxUint32 nbytes) |
96db102a | 1111 | { |
f5395600 VZ |
1112 | wxCHECK_MSG( m_impl, 0, "socket must be valid" ); |
1113 | ||
cc0972a2 | 1114 | const char *buffer = static_cast<const char *>(buffer_); |
f5395600 | 1115 | wxCHECK_MSG( buffer, 0, "NULL buffer" ); |
c2116a35 | 1116 | |
cc0972a2 | 1117 | wxUint32 total = 0; |
f5395600 | 1118 | while ( nbytes ) |
cc0972a2 | 1119 | { |
d34f724d | 1120 | if ( m_impl->m_stream && !m_connected ) |
f1763270 VZ |
1121 | { |
1122 | if ( (m_flags & wxSOCKET_WAITALL) || !total ) | |
1123 | SetError(wxSOCKET_IOERR); | |
1124 | break; | |
1125 | } | |
1126 | ||
51fe4b60 | 1127 | const int ret = m_impl->Write(buffer, nbytes); |
64b1cea0 VZ |
1128 | if ( ret == -1 ) |
1129 | { | |
f5395600 | 1130 | if ( m_impl->GetLastError() == wxSOCKET_WOULDBLOCK ) |
c2116a35 | 1131 | { |
f5395600 VZ |
1132 | if ( m_flags & wxSOCKET_NOWAIT ) |
1133 | break; | |
5c9eff30 | 1134 | |
ebbf7407 | 1135 | if ( !DoWaitWithTimeout(wxSOCKET_OUTPUT_FLAG) ) |
f5395600 VZ |
1136 | { |
1137 | SetError(wxSOCKET_TIMEDOUT); | |
1138 | break; | |
1139 | } | |
a324a7bc | 1140 | |
f5395600 VZ |
1141 | continue; |
1142 | } | |
1143 | else // "real" error | |
1144 | { | |
1145 | SetError(wxSOCKET_IOERR); | |
c2116a35 | 1146 | break; |
f5395600 VZ |
1147 | } |
1148 | } | |
c9157492 | 1149 | |
f5395600 | 1150 | total += ret; |
c9157492 | 1151 | |
f5395600 VZ |
1152 | if ( !(m_flags & wxSOCKET_WAITALL) ) |
1153 | break; | |
64b1cea0 | 1154 | |
f5395600 VZ |
1155 | nbytes -= ret; |
1156 | buffer += ret; | |
81b92e17 | 1157 | } |
a324a7bc | 1158 | |
c2116a35 | 1159 | return total; |
f4ada568 GL |
1160 | } |
1161 | ||
f187448d | 1162 | wxSocketBase& wxSocketBase::WriteMsg(const void *buffer, wxUint32 nbytes) |
062c4861 | 1163 | { |
c2116a35 VZ |
1164 | struct |
1165 | { | |
1166 | unsigned char sig[4]; | |
1167 | unsigned char len[4]; | |
1168 | } msg; | |
062c4861 | 1169 | |
365b8793 | 1170 | wxSocketWriteGuard write(this); |
96db102a | 1171 | |
365b8793 | 1172 | wxSocketWaitModeChanger changeFlags(this, wxSOCKET_WAITALL); |
96db102a | 1173 | |
c2116a35 VZ |
1174 | msg.sig[0] = (unsigned char) 0xad; |
1175 | msg.sig[1] = (unsigned char) 0xde; | |
1176 | msg.sig[2] = (unsigned char) 0xed; | |
1177 | msg.sig[3] = (unsigned char) 0xfe; | |
062c4861 | 1178 | |
c2116a35 VZ |
1179 | msg.len[0] = (unsigned char) (nbytes & 0xff); |
1180 | msg.len[1] = (unsigned char) ((nbytes >> 8) & 0xff); | |
1181 | msg.len[2] = (unsigned char) ((nbytes >> 16) & 0xff); | |
1182 | msg.len[3] = (unsigned char) ((nbytes >> 24) & 0xff); | |
17aa2bec | 1183 | |
64b1cea0 VZ |
1184 | bool ok = false; |
1185 | if ( DoWrite(&msg, sizeof(msg)) == sizeof(msg) ) | |
1186 | { | |
1187 | m_lcount = DoWrite(buffer, nbytes); | |
1188 | if ( m_lcount == nbytes ) | |
1189 | { | |
1190 | msg.sig[0] = (unsigned char) 0xed; | |
1191 | msg.sig[1] = (unsigned char) 0xfe; | |
1192 | msg.sig[2] = (unsigned char) 0xad; | |
1193 | msg.sig[3] = (unsigned char) 0xde; | |
1194 | msg.len[0] = | |
1195 | msg.len[1] = | |
1196 | msg.len[2] = | |
1197 | msg.len[3] = (char) 0; | |
1198 | ||
1199 | if ( DoWrite(&msg, sizeof(msg)) == sizeof(msg)) | |
1200 | ok = true; | |
1201 | } | |
1202 | } | |
17aa2bec | 1203 | |
64b1cea0 VZ |
1204 | if ( !ok ) |
1205 | SetError(wxSOCKET_IOERR); | |
96db102a | 1206 | |
c2116a35 | 1207 | return *this; |
062c4861 GL |
1208 | } |
1209 | ||
f187448d | 1210 | wxSocketBase& wxSocketBase::Unread(const void *buffer, wxUint32 nbytes) |
f4ada568 | 1211 | { |
c2116a35 VZ |
1212 | if (nbytes != 0) |
1213 | Pushback(buffer, nbytes); | |
f4ada568 | 1214 | |
64b1cea0 | 1215 | SetError(wxSOCKET_NOERROR); |
c2116a35 | 1216 | m_lcount = nbytes; |
f4ada568 | 1217 | |
c2116a35 | 1218 | return *this; |
a324a7bc GL |
1219 | } |
1220 | ||
96db102a | 1221 | wxSocketBase& wxSocketBase::Discard() |
f4ada568 | 1222 | { |
c2116a35 VZ |
1223 | char *buffer = new char[MAX_DISCARD_SIZE]; |
1224 | wxUint32 ret; | |
1225 | wxUint32 total = 0; | |
f4ada568 | 1226 | |
365b8793 | 1227 | wxSocketReadGuard read(this); |
96db102a | 1228 | |
365b8793 | 1229 | wxSocketWaitModeChanger changeFlags(this, wxSOCKET_NOWAIT); |
384b4373 | 1230 | |
c2116a35 VZ |
1231 | do |
1232 | { | |
cc0972a2 | 1233 | ret = DoRead(buffer, MAX_DISCARD_SIZE); |
c2116a35 VZ |
1234 | total += ret; |
1235 | } | |
1236 | while (ret == MAX_DISCARD_SIZE); | |
f4ada568 | 1237 | |
c2116a35 VZ |
1238 | delete[] buffer; |
1239 | m_lcount = total; | |
64b1cea0 | 1240 | SetError(wxSOCKET_NOERROR); |
17aa2bec | 1241 | |
c2116a35 | 1242 | return *this; |
f4ada568 GL |
1243 | } |
1244 | ||
ef57d866 GRG |
1245 | // -------------------------------------------------------------------------- |
1246 | // Wait functions | |
1247 | // -------------------------------------------------------------------------- | |
f4ada568 | 1248 | |
51fe4b60 | 1249 | /* |
00414faf VZ |
1250 | This function will check for the events specified in the flags parameter, |
1251 | and it will return a mask indicating which operations can be performed. | |
f0db5d75 | 1252 | */ |
00414faf | 1253 | wxSocketEventFlags wxSocketImpl::Select(wxSocketEventFlags flags, |
2b036c4b | 1254 | const timeval *timeout) |
f0db5d75 | 1255 | { |
5e9238f9 VZ |
1256 | if ( m_fd == INVALID_SOCKET ) |
1257 | return (wxSOCKET_LOST_FLAG & flags); | |
f0db5d75 | 1258 | |
5e9238f9 VZ |
1259 | struct timeval tv; |
1260 | if ( timeout ) | |
1261 | tv = *timeout; | |
1262 | else | |
1263 | tv.tv_sec = tv.tv_usec = 0; | |
f0db5d75 | 1264 | |
5e9238f9 VZ |
1265 | // prepare the FD sets, passing NULL for the one(s) we don't use |
1266 | fd_set | |
1267 | readfds, *preadfds = NULL, | |
1268 | writefds, *pwritefds = NULL, | |
1269 | exceptfds; // always want to know about errors | |
f0db5d75 | 1270 | |
5e9238f9 VZ |
1271 | if ( flags & wxSOCKET_INPUT_FLAG ) |
1272 | { | |
1273 | preadfds = &readfds; | |
1274 | wxFD_ZERO(preadfds); | |
1275 | wxFD_SET(m_fd, preadfds); | |
1276 | } | |
f0db5d75 | 1277 | |
5e9238f9 VZ |
1278 | // when using non-blocking connect() the socket becomes connected |
1279 | // (successfully or not) when it becomes writable | |
1280 | if ( flags & (wxSOCKET_OUTPUT_FLAG | wxSOCKET_CONNECTION_FLAG) ) | |
1281 | { | |
1282 | pwritefds = &writefds; | |
1283 | wxFD_ZERO(pwritefds); | |
1284 | wxFD_SET(m_fd, pwritefds); | |
1285 | } | |
f0db5d75 | 1286 | |
5e9238f9 VZ |
1287 | wxFD_ZERO(&exceptfds); |
1288 | wxFD_SET(m_fd, &exceptfds); | |
f0db5d75 | 1289 | |
5e9238f9 VZ |
1290 | const int rc = select(m_fd + 1, preadfds, pwritefds, &exceptfds, &tv); |
1291 | ||
1292 | // check for errors first | |
1293 | if ( rc == -1 || wxFD_ISSET(m_fd, &exceptfds) ) | |
f0db5d75 | 1294 | { |
5e9238f9 VZ |
1295 | m_establishing = false; |
1296 | ||
1297 | return wxSOCKET_LOST_FLAG & flags; | |
f0db5d75 | 1298 | } |
f0db5d75 | 1299 | |
5e9238f9 VZ |
1300 | if ( rc == 0 ) |
1301 | return 0; | |
f0db5d75 | 1302 | |
5e9238f9 | 1303 | wxASSERT_MSG( rc == 1, "unexpected select() return value" ); |
f0db5d75 | 1304 | |
5e9238f9 VZ |
1305 | wxSocketEventFlags detected = 0; |
1306 | if ( preadfds && wxFD_ISSET(m_fd, preadfds) ) | |
1307 | detected |= wxSOCKET_INPUT_FLAG; | |
1308 | ||
1309 | if ( pwritefds && wxFD_ISSET(m_fd, pwritefds) ) | |
f0db5d75 | 1310 | { |
5e9238f9 VZ |
1311 | // check for the case of non-blocking connect() |
1312 | if ( m_establishing && !m_server ) | |
1313 | { | |
1314 | int error; | |
1315 | SOCKOPTLEN_T len = sizeof(error); | |
1316 | m_establishing = false; | |
1317 | getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len); | |
1318 | ||
1319 | if ( error ) | |
1320 | detected = wxSOCKET_LOST_FLAG; | |
1321 | else | |
1322 | detected |= wxSOCKET_CONNECTION_FLAG; | |
1323 | } | |
1324 | else // not called to get non-blocking connect() status | |
1325 | { | |
1326 | detected |= wxSOCKET_OUTPUT_FLAG; | |
1327 | } | |
f0db5d75 | 1328 | } |
f0db5d75 | 1329 | |
5e9238f9 | 1330 | return detected & flags; |
f0db5d75 VZ |
1331 | } |
1332 | ||
ebbf7407 | 1333 | int |
60ee0172 | 1334 | wxSocketBase::DoWait(long seconds, long milliseconds, wxSocketEventFlags flags) |
6f405b31 VZ |
1335 | { |
1336 | // Use either the provided timeout or the default timeout value associated | |
1337 | // with this socket. | |
1338 | // | |
1339 | // TODO: allow waiting forever, see #9443 | |
1340 | const long timeout = seconds == -1 ? m_timeout * 1000 | |
1341 | : seconds * 1000 + milliseconds; | |
1342 | ||
1343 | return DoWait(timeout, flags); | |
1344 | } | |
1345 | ||
ebbf7407 | 1346 | int |
6f405b31 | 1347 | wxSocketBase::DoWait(long timeout, wxSocketEventFlags flags) |
375abe3d | 1348 | { |
ebbf7407 | 1349 | wxCHECK_MSG( m_impl, -1, "can't wait on invalid socket" ); |
375abe3d | 1350 | |
9588640d VZ |
1351 | // we're never going to become ready if we're not connected (any more) |
1352 | if ( !m_connected && !m_establishing ) | |
ebbf7407 | 1353 | return -1; |
9588640d | 1354 | |
60ee0172 | 1355 | // This can be set to true from Interrupt() to exit this function a.s.a.p. |
c2116a35 | 1356 | m_interrupt = false; |
81b92e17 | 1357 | |
af2fd961 | 1358 | |
60ee0172 VZ |
1359 | const wxMilliClock_t timeEnd = wxGetLocalTimeMillis() + timeout; |
1360 | ||
1361 | // Get the active event loop which we'll use for the message dispatching | |
2b036c4b VZ |
1362 | // when running in the main thread unless this was explicitly disabled by |
1363 | // setting wxSOCKET_BLOCK flag | |
60ee0172 | 1364 | wxEventLoopBase *eventLoop; |
2b036c4b | 1365 | if ( !(m_flags & wxSOCKET_BLOCK) && wxIsMainThread() ) |
60ee0172 VZ |
1366 | { |
1367 | eventLoop = wxEventLoop::GetActive(); | |
60ee0172 VZ |
1368 | } |
1369 | else // in worker thread | |
af2fd961 | 1370 | { |
60ee0172 VZ |
1371 | // We never dispatch messages from threads other than the main one. |
1372 | eventLoop = NULL; | |
af2fd961 GRG |
1373 | } |
1374 | ||
5ab2f257 VZ |
1375 | // Make sure the events we're interested in are enabled before waiting for |
1376 | // them: this is really necessary here as otherwise this could happen: | |
1377 | // 1. DoRead(wxSOCKET_WAITALL) is called | |
1378 | // 2. There is nothing to read so DoWait(wxSOCKET_INPUT_FLAG) is called | |
1379 | // 3. Some, but not all data appears, wxSocketImplUnix::OnReadWaiting() | |
1380 | // is called and wxSOCKET_INPUT_FLAG events are disabled in it | |
1381 | // 4. Because of wxSOCKET_WAITALL we call DoWait() again but the events | |
1382 | // are still disabled and we block forever | |
1383 | // | |
1384 | // More elegant solution would be nice but for now simply re-enabling the | |
1385 | // events here will do | |
1386 | m_impl->ReenableEvents(flags & (wxSOCKET_INPUT_FLAG | wxSOCKET_OUTPUT_FLAG)); | |
1387 | ||
1388 | ||
00414faf VZ |
1389 | // Wait until we receive the event we're waiting for or the timeout expires |
1390 | // (but note that we always execute the loop at least once, even if timeout | |
1391 | // is 0 as this is used for polling) | |
ebbf7407 VZ |
1392 | int rc = 0; |
1393 | for ( bool firstTime = true; !m_interrupt; firstTime = false ) | |
af2fd961 | 1394 | { |
00414faf VZ |
1395 | long timeLeft = wxMilliClockToLong(timeEnd - wxGetLocalTimeMillis()); |
1396 | if ( timeLeft < 0 ) | |
1397 | { | |
1398 | if ( !firstTime ) | |
1399 | break; // timed out | |
1400 | ||
1401 | timeLeft = 0; | |
1402 | } | |
1403 | ||
2f1c8faf VZ |
1404 | wxSocketEventFlags events; |
1405 | if ( eventLoop ) | |
1406 | { | |
00414faf VZ |
1407 | // reset them before starting to wait |
1408 | m_eventsgot = 0; | |
1409 | ||
1410 | eventLoop->DispatchTimeout(timeLeft); | |
2f1c8faf VZ |
1411 | |
1412 | events = m_eventsgot; | |
1413 | } | |
00414faf VZ |
1414 | else // no event loop or waiting in another thread |
1415 | { | |
1416 | // as explained below, we should always check for wxSOCKET_LOST_FLAG | |
2b036c4b VZ |
1417 | timeval tv; |
1418 | SetTimeValFromMS(tv, timeLeft); | |
1419 | events = m_impl->Select(flags | wxSOCKET_LOST_FLAG, &tv); | |
00414faf VZ |
1420 | } |
1421 | ||
1422 | // always check for wxSOCKET_LOST_FLAG, even if flags doesn't include | |
1423 | // it, as continuing to wait for anything else after getting it is | |
1424 | // pointless | |
1425 | if ( events & wxSOCKET_LOST_FLAG ) | |
2f1c8faf | 1426 | { |
00414faf VZ |
1427 | m_connected = false; |
1428 | m_establishing = false; | |
ebbf7407 | 1429 | rc = -1; |
00414faf | 1430 | break; |
2f1c8faf | 1431 | } |
60ee0172 | 1432 | |
00414faf VZ |
1433 | // otherwise mask out the bits we're not interested in |
1434 | events &= flags; | |
1435 | ||
60ee0172 | 1436 | // Incoming connection (server) or connection established (client)? |
2f1c8faf | 1437 | if ( events & wxSOCKET_CONNECTION_FLAG ) |
c2116a35 VZ |
1438 | { |
1439 | m_connected = true; | |
1440 | m_establishing = false; | |
ebbf7407 | 1441 | rc = true; |
c2116a35 VZ |
1442 | break; |
1443 | } | |
c0043a50 | 1444 | |
60ee0172 | 1445 | // Data available or output buffer ready? |
2f1c8faf | 1446 | if ( (events & wxSOCKET_INPUT_FLAG) || (events & wxSOCKET_OUTPUT_FLAG) ) |
b8d1915d | 1447 | { |
ebbf7407 | 1448 | rc = true; |
c2116a35 | 1449 | break; |
b8d1915d | 1450 | } |
f01bca89 | 1451 | } |
f4ada568 | 1452 | |
ebbf7407 | 1453 | return rc; |
f4ada568 | 1454 | } |
f4ada568 | 1455 | |
a737331d | 1456 | bool wxSocketBase::Wait(long seconds, long milliseconds) |
f4ada568 | 1457 | { |
60ee0172 | 1458 | return DoWait(seconds, milliseconds, |
ebbf7407 VZ |
1459 | wxSOCKET_INPUT_FLAG | |
1460 | wxSOCKET_OUTPUT_FLAG | | |
1461 | wxSOCKET_CONNECTION_FLAG) != 0; | |
f4ada568 | 1462 | } |
f4ada568 | 1463 | |
a737331d | 1464 | bool wxSocketBase::WaitForRead(long seconds, long milliseconds) |
f4ada568 | 1465 | { |
60ee0172 VZ |
1466 | // Check pushback buffer before entering DoWait |
1467 | if ( m_unread ) | |
c2116a35 | 1468 | return true; |
96db102a | 1469 | |
6f405b31 VZ |
1470 | // Check if the socket is not already ready for input, if it is, there is |
1471 | // no need to start waiting for it (worse, we'll actually never get a | |
1472 | // notification about the socket becoming ready if it is already under | |
1473 | // Windows) | |
1474 | if ( m_impl->Select(wxSOCKET_INPUT_FLAG) ) | |
1475 | return true; | |
1476 | ||
ebbf7407 | 1477 | return DoWait(seconds, milliseconds, wxSOCKET_INPUT_FLAG) != 0; |
f4ada568 GL |
1478 | } |
1479 | ||
bfa7bf7d | 1480 | |
a737331d | 1481 | bool wxSocketBase::WaitForWrite(long seconds, long milliseconds) |
f4ada568 | 1482 | { |
6f405b31 VZ |
1483 | if ( m_impl->Select(wxSOCKET_OUTPUT_FLAG) ) |
1484 | return true; | |
1485 | ||
ebbf7407 | 1486 | return DoWait(seconds, milliseconds, wxSOCKET_OUTPUT_FLAG) != 0; |
a737331d | 1487 | } |
f4ada568 | 1488 | |
a737331d GL |
1489 | bool wxSocketBase::WaitForLost(long seconds, long milliseconds) |
1490 | { | |
ebbf7407 | 1491 | return DoWait(seconds, milliseconds, wxSOCKET_LOST_FLAG) == -1; |
f4ada568 | 1492 | } |
a737331d | 1493 | |
ef57d866 GRG |
1494 | // -------------------------------------------------------------------------- |
1495 | // Miscellaneous | |
1496 | // -------------------------------------------------------------------------- | |
1497 | ||
1498 | // | |
1499 | // Get local or peer address | |
1500 | // | |
1501 | ||
c9bccf23 | 1502 | bool wxSocketBase::GetPeer(wxSockAddress& addr) const |
ef57d866 | 1503 | { |
c9bccf23 | 1504 | wxCHECK_MSG( m_impl, false, "invalid socket" ); |
ef57d866 | 1505 | |
c9bccf23 VZ |
1506 | const wxSockAddressImpl& peer = m_impl->GetPeer(); |
1507 | if ( !peer.IsOk() ) | |
c2116a35 | 1508 | return false; |
007c77ab | 1509 | |
c9bccf23 | 1510 | addr.SetAddress(peer); |
ef57d866 | 1511 | |
c2116a35 | 1512 | return true; |
ef57d866 GRG |
1513 | } |
1514 | ||
c9bccf23 | 1515 | bool wxSocketBase::GetLocal(wxSockAddress& addr) const |
ef57d866 | 1516 | { |
c9bccf23 | 1517 | wxCHECK_MSG( m_impl, false, "invalid socket" ); |
ef57d866 | 1518 | |
c9bccf23 VZ |
1519 | const wxSockAddressImpl& local = m_impl->GetLocal(); |
1520 | if ( !local.IsOk() ) | |
7fb0a11d | 1521 | return false; |
ef57d866 | 1522 | |
c9bccf23 | 1523 | addr.SetAddress(local); |
ef57d866 | 1524 | |
7fb0a11d | 1525 | return true; |
ef57d866 GRG |
1526 | } |
1527 | ||
1528 | // | |
1529 | // Save and restore socket state | |
1530 | // | |
1531 | ||
1532 | void wxSocketBase::SaveState() | |
1533 | { | |
7fb0a11d | 1534 | wxSocketState *state; |
ef57d866 | 1535 | |
7fb0a11d | 1536 | state = new wxSocketState(); |
ef57d866 | 1537 | |
7fb0a11d WS |
1538 | state->m_flags = m_flags; |
1539 | state->m_notify = m_notify; | |
1540 | state->m_eventmask = m_eventmask; | |
1541 | state->m_clientData = m_clientData; | |
ef57d866 | 1542 | |
7fb0a11d | 1543 | m_states.Append(state); |
ef57d866 GRG |
1544 | } |
1545 | ||
1546 | void wxSocketBase::RestoreState() | |
1547 | { | |
7fb0a11d WS |
1548 | wxList::compatibility_iterator node; |
1549 | wxSocketState *state; | |
ef57d866 | 1550 | |
7fb0a11d WS |
1551 | node = m_states.GetLast(); |
1552 | if (!node) | |
1553 | return; | |
ef57d866 | 1554 | |
7fb0a11d | 1555 | state = (wxSocketState *)node->GetData(); |
ef57d866 | 1556 | |
7fb0a11d WS |
1557 | m_flags = state->m_flags; |
1558 | m_notify = state->m_notify; | |
1559 | m_eventmask = state->m_eventmask; | |
1560 | m_clientData = state->m_clientData; | |
37340c48 | 1561 | |
7fb0a11d WS |
1562 | m_states.Erase(node); |
1563 | delete state; | |
ef57d866 GRG |
1564 | } |
1565 | ||
1566 | // | |
1567 | // Timeout and flags | |
1568 | // | |
1569 | ||
17aa2bec GRG |
1570 | void wxSocketBase::SetTimeout(long seconds) |
1571 | { | |
7fb0a11d | 1572 | m_timeout = seconds; |
17aa2bec | 1573 | |
51fe4b60 VZ |
1574 | if (m_impl) |
1575 | m_impl->SetTimeout(m_timeout * 1000); | |
17aa2bec GRG |
1576 | } |
1577 | ||
71622a7a | 1578 | void wxSocketBase::SetFlags(wxSocketFlags flags) |
f4ada568 | 1579 | { |
cc0972a2 VZ |
1580 | // Do some sanity checking on the flags used: not all values can be used |
1581 | // together. | |
1582 | wxASSERT_MSG( !(flags & wxSOCKET_NOWAIT) || | |
1583 | !(flags & (wxSOCKET_WAITALL | wxSOCKET_BLOCK)), | |
1584 | "Using wxSOCKET_WAITALL or wxSOCKET_BLOCK with " | |
1585 | "wxSOCKET_NOWAIT doesn't make sense" ); | |
1586 | ||
7fb0a11d | 1587 | m_flags = flags; |
f4ada568 | 1588 | } |
f0a56ab0 | 1589 | |
f187448d | 1590 | |
ef57d866 | 1591 | // -------------------------------------------------------------------------- |
f187448d | 1592 | // Event handling |
ef57d866 | 1593 | // -------------------------------------------------------------------------- |
a324a7bc | 1594 | |
bffc1eaa | 1595 | void wxSocketBase::OnRequest(wxSocketNotify notification) |
f4ada568 | 1596 | { |
c2116a35 | 1597 | wxSocketEventFlags flag = 0; |
5c1193e0 | 1598 | switch ( notification ) |
c2116a35 | 1599 | { |
5c1193e0 VZ |
1600 | case wxSOCKET_INPUT: |
1601 | flag = wxSOCKET_INPUT_FLAG; | |
1602 | break; | |
1603 | ||
1604 | case wxSOCKET_OUTPUT: | |
1605 | flag = wxSOCKET_OUTPUT_FLAG; | |
1606 | break; | |
1607 | ||
1608 | case wxSOCKET_CONNECTION: | |
1609 | flag = wxSOCKET_CONNECTION_FLAG; | |
40a983ad VZ |
1610 | |
1611 | // we're now successfully connected | |
1612 | m_connected = true; | |
609aa390 VZ |
1613 | m_establishing = false; |
1614 | ||
1615 | // error was previously set to wxSOCKET_WOULDBLOCK, but this is not | |
1616 | // the case any longer | |
1617 | SetError(wxSOCKET_NOERROR); | |
5c1193e0 VZ |
1618 | break; |
1619 | ||
1620 | case wxSOCKET_LOST: | |
1621 | flag = wxSOCKET_LOST_FLAG; | |
40a983ad VZ |
1622 | |
1623 | // if we lost the connection the socket is now closed | |
1624 | m_closed = true; | |
5c1193e0 VZ |
1625 | break; |
1626 | ||
c2116a35 | 1627 | default: |
5c1193e0 | 1628 | wxFAIL_MSG( "unknown wxSocket notification" ); |
c2116a35 VZ |
1629 | } |
1630 | ||
5c1193e0 VZ |
1631 | // remember the events which were generated for this socket, we're going to |
1632 | // use this in DoWait() | |
1633 | m_eventsgot |= flag; | |
1634 | ||
1635 | // send the wx event if enabled and we're interested in it | |
1636 | if ( m_notify && (m_eventmask & flag) && m_handler ) | |
c2116a35 | 1637 | { |
0ed00ab5 VZ |
1638 | // don't generate the events when we're inside DoWait() called from our |
1639 | // own code as we are going to consume the data that has just become | |
1640 | // available ourselves and the user code won't see it at all | |
1641 | if ( (notification == wxSOCKET_INPUT && m_reading) || | |
1642 | (notification == wxSOCKET_OUTPUT && m_writing) ) | |
1643 | { | |
1644 | return; | |
1645 | } | |
1646 | ||
5c1193e0 VZ |
1647 | wxSocketEvent event(m_id); |
1648 | event.m_event = notification; | |
1649 | event.m_clientData = m_clientData; | |
1650 | event.SetEventObject(this); | |
c2116a35 | 1651 | |
5c1193e0 | 1652 | m_handler->AddPendingEvent(event); |
bffc1eaa | 1653 | } |
f4ada568 GL |
1654 | } |
1655 | ||
f187448d GRG |
1656 | void wxSocketBase::Notify(bool notify) |
1657 | { | |
7fb0a11d | 1658 | m_notify = notify; |
f187448d GRG |
1659 | } |
1660 | ||
1661 | void wxSocketBase::SetNotify(wxSocketEventFlags flags) | |
1662 | { | |
7fb0a11d | 1663 | m_eventmask = flags; |
f187448d GRG |
1664 | } |
1665 | ||
71622a7a | 1666 | void wxSocketBase::SetEventHandler(wxEvtHandler& handler, int id) |
f4ada568 | 1667 | { |
7fb0a11d WS |
1668 | m_handler = &handler; |
1669 | m_id = id; | |
f4ada568 GL |
1670 | } |
1671 | ||
ef57d866 GRG |
1672 | // -------------------------------------------------------------------------- |
1673 | // Pushback buffer | |
1674 | // -------------------------------------------------------------------------- | |
db131261 | 1675 | |
f187448d | 1676 | void wxSocketBase::Pushback(const void *buffer, wxUint32 size) |
f4ada568 | 1677 | { |
c2116a35 | 1678 | if (!size) return; |
dc5c1114 | 1679 | |
c2116a35 VZ |
1680 | if (m_unread == NULL) |
1681 | m_unread = malloc(size); | |
1682 | else | |
1683 | { | |
1684 | void *tmp; | |
f4ada568 | 1685 | |
c2116a35 VZ |
1686 | tmp = malloc(m_unrd_size + size); |
1687 | memcpy((char *)tmp + size, m_unread, m_unrd_size); | |
1688 | free(m_unread); | |
a324a7bc | 1689 | |
c2116a35 VZ |
1690 | m_unread = tmp; |
1691 | } | |
31528cd3 | 1692 | |
c2116a35 | 1693 | m_unrd_size += size; |
a324a7bc | 1694 | |
c2116a35 | 1695 | memcpy(m_unread, buffer, size); |
f4ada568 GL |
1696 | } |
1697 | ||
f187448d | 1698 | wxUint32 wxSocketBase::GetPushback(void *buffer, wxUint32 size, bool peek) |
f4ada568 | 1699 | { |
cc0972a2 VZ |
1700 | wxCHECK_MSG( buffer, 0, "NULL buffer" ); |
1701 | ||
c2116a35 VZ |
1702 | if (!m_unrd_size) |
1703 | return 0; | |
f4ada568 | 1704 | |
c2116a35 VZ |
1705 | if (size > (m_unrd_size-m_unrd_cur)) |
1706 | size = m_unrd_size-m_unrd_cur; | |
96db102a | 1707 | |
c2116a35 | 1708 | memcpy(buffer, (char *)m_unread + m_unrd_cur, size); |
f4ada568 | 1709 | |
c2116a35 | 1710 | if (!peek) |
dc5c1114 | 1711 | { |
c2116a35 VZ |
1712 | m_unrd_cur += size; |
1713 | if (m_unrd_size == m_unrd_cur) | |
1714 | { | |
1715 | free(m_unread); | |
1716 | m_unread = NULL; | |
1717 | m_unrd_size = 0; | |
1718 | m_unrd_cur = 0; | |
1719 | } | |
f4ada568 | 1720 | } |
f4ada568 | 1721 | |
c2116a35 | 1722 | return size; |
f4ada568 GL |
1723 | } |
1724 | ||
f187448d | 1725 | |
ef57d866 | 1726 | // ========================================================================== |
c3e646b4 | 1727 | // wxSocketServer |
ef57d866 | 1728 | // ========================================================================== |
f4ada568 | 1729 | |
ef57d866 GRG |
1730 | // -------------------------------------------------------------------------- |
1731 | // Ctor | |
1732 | // -------------------------------------------------------------------------- | |
56d8adc0 | 1733 | |
c9bccf23 | 1734 | wxSocketServer::wxSocketServer(const wxSockAddress& addr, |
71622a7a GRG |
1735 | wxSocketFlags flags) |
1736 | : wxSocketBase(flags, wxSOCKET_SERVER) | |
f4ada568 | 1737 | { |
007c77ab | 1738 | wxLogTrace( wxTRACE_Socket, _T("Opening wxSocketServer") ); |
f4ada568 | 1739 | |
51fe4b60 | 1740 | m_impl = wxSocketImpl::Create(*this); |
384b4373 | 1741 | |
51fe4b60 | 1742 | if (!m_impl) |
007c77ab | 1743 | { |
51fe4b60 | 1744 | wxLogTrace( wxTRACE_Socket, _T("*** Failed to create m_impl") ); |
007c77ab RL |
1745 | return; |
1746 | } | |
a737331d | 1747 | |
2804f77d | 1748 | // Setup the socket as server |
c9bccf23 | 1749 | m_impl->SetLocal(addr.GetAddress()); |
d775fa82 | 1750 | |
74c481d1 | 1751 | if (GetFlags() & wxSOCKET_REUSEADDR) { |
51fe4b60 | 1752 | m_impl->SetReusable(); |
74c481d1 | 1753 | } |
60edcf45 | 1754 | if (GetFlags() & wxSOCKET_BROADCAST) { |
51fe4b60 | 1755 | m_impl->SetBroadcast(); |
60edcf45 VZ |
1756 | } |
1757 | if (GetFlags() & wxSOCKET_NOBIND) { | |
51fe4b60 | 1758 | m_impl->DontDoBind(); |
60edcf45 | 1759 | } |
74c481d1 | 1760 | |
51fe4b60 | 1761 | if (m_impl->CreateServer() != wxSOCKET_NOERROR) |
007c77ab | 1762 | { |
51fe4b60 VZ |
1763 | delete m_impl; |
1764 | m_impl = NULL; | |
007c77ab | 1765 | |
51fe4b60 | 1766 | wxLogTrace( wxTRACE_Socket, _T("*** CreateServer() failed") ); |
007c77ab RL |
1767 | return; |
1768 | } | |
1769 | ||
51fe4b60 | 1770 | wxLogTrace( wxTRACE_Socket, _T("wxSocketServer on fd %d"), m_impl->m_fd ); |
f4ada568 GL |
1771 | } |
1772 | ||
ef57d866 GRG |
1773 | // -------------------------------------------------------------------------- |
1774 | // Accept | |
1775 | // -------------------------------------------------------------------------- | |
8c14576d | 1776 | |
d80d1aba | 1777 | bool wxSocketServer::AcceptWith(wxSocketBase& sock, bool wait) |
f4ada568 | 1778 | { |
2b036c4b VZ |
1779 | if ( !m_impl || (m_impl->m_fd == INVALID_SOCKET) || !m_impl->IsServer() ) |
1780 | { | |
1781 | wxFAIL_MSG( "can only be called for a valid server socket" ); | |
1782 | ||
64b1cea0 | 1783 | SetError(wxSOCKET_INVSOCK); |
2b036c4b | 1784 | |
c2116a35 | 1785 | return false; |
2b036c4b VZ |
1786 | } |
1787 | ||
1788 | if ( wait ) | |
1789 | { | |
1790 | // wait until we get a connection | |
1791 | if ( !m_impl->SelectWithTimeout(wxSOCKET_INPUT_FLAG) ) | |
1792 | { | |
64b1cea0 | 1793 | SetError(wxSOCKET_TIMEDOUT); |
2b036c4b VZ |
1794 | |
1795 | return false; | |
1796 | } | |
1797 | } | |
17aa2bec | 1798 | |
2b036c4b | 1799 | sock.m_impl = m_impl->Accept(sock); |
d80d1aba | 1800 | |
51fe4b60 | 1801 | if ( !sock.m_impl ) |
2b036c4b | 1802 | { |
64b1cea0 | 1803 | SetError(m_impl->GetLastError()); |
2b036c4b | 1804 | |
c2116a35 | 1805 | return false; |
2b036c4b | 1806 | } |
d80d1aba | 1807 | |
c2116a35 | 1808 | sock.m_type = wxSOCKET_BASE; |
c2116a35 | 1809 | sock.m_connected = true; |
f4ada568 | 1810 | |
c2116a35 | 1811 | return true; |
f4ada568 GL |
1812 | } |
1813 | ||
d80d1aba | 1814 | wxSocketBase *wxSocketServer::Accept(bool wait) |
f4ada568 | 1815 | { |
c2116a35 | 1816 | wxSocketBase* sock = new wxSocketBase(); |
f4ada568 | 1817 | |
c2116a35 | 1818 | sock->SetFlags(m_flags); |
f4ada568 | 1819 | |
c2116a35 VZ |
1820 | if (!AcceptWith(*sock, wait)) |
1821 | { | |
1822 | sock->Destroy(); | |
1823 | sock = NULL; | |
1824 | } | |
f4ada568 | 1825 | |
c2116a35 | 1826 | return sock; |
f4ada568 GL |
1827 | } |
1828 | ||
17aa2bec | 1829 | bool wxSocketServer::WaitForAccept(long seconds, long milliseconds) |
d80d1aba | 1830 | { |
ebbf7407 | 1831 | return DoWait(seconds, milliseconds, wxSOCKET_CONNECTION_FLAG) == 1; |
d80d1aba GRG |
1832 | } |
1833 | ||
bfa7bf7d VZ |
1834 | bool wxSocketBase::GetOption(int level, int optname, void *optval, int *optlen) |
1835 | { | |
51fe4b60 | 1836 | wxASSERT_MSG( m_impl, _T("Socket not initialised") ); |
11734f8a | 1837 | |
735ac2bc | 1838 | SOCKOPTLEN_T lenreal = *optlen; |
51fe4b60 VZ |
1839 | if ( getsockopt(m_impl->m_fd, level, optname, |
1840 | static_cast<char *>(optval), &lenreal) != 0 ) | |
d775fa82 | 1841 | return false; |
51fe4b60 VZ |
1842 | |
1843 | *optlen = lenreal; | |
1844 | ||
d775fa82 | 1845 | return true; |
bfa7bf7d VZ |
1846 | } |
1847 | ||
51fe4b60 VZ |
1848 | bool |
1849 | wxSocketBase::SetOption(int level, int optname, const void *optval, int optlen) | |
bfa7bf7d | 1850 | { |
51fe4b60 | 1851 | wxASSERT_MSG( m_impl, _T("Socket not initialised") ); |
8e3f3880 | 1852 | |
51fe4b60 VZ |
1853 | return setsockopt(m_impl->m_fd, level, optname, |
1854 | static_cast<const char *>(optval), optlen) == 0; | |
bfa7bf7d VZ |
1855 | } |
1856 | ||
72ac4e88 | 1857 | bool wxSocketBase::SetLocal(const wxIPV4address& local) |
33d925b0 | 1858 | { |
c9bccf23 | 1859 | m_localAddress = local; |
33d925b0 | 1860 | |
c9bccf23 | 1861 | return true; |
33d925b0 KH |
1862 | } |
1863 | ||
ef57d866 | 1864 | // ========================================================================== |
8c14576d | 1865 | // wxSocketClient |
ef57d866 | 1866 | // ========================================================================== |
f4ada568 | 1867 | |
ef57d866 GRG |
1868 | // -------------------------------------------------------------------------- |
1869 | // Ctor and dtor | |
1870 | // -------------------------------------------------------------------------- | |
56d8adc0 | 1871 | |
71622a7a GRG |
1872 | wxSocketClient::wxSocketClient(wxSocketFlags flags) |
1873 | : wxSocketBase(flags, wxSOCKET_CLIENT) | |
f4ada568 | 1874 | { |
089b23d0 VZ |
1875 | m_initialRecvBufferSize = |
1876 | m_initialSendBufferSize = -1; | |
f4ada568 GL |
1877 | } |
1878 | ||
ef57d866 GRG |
1879 | // -------------------------------------------------------------------------- |
1880 | // Connect | |
1881 | // -------------------------------------------------------------------------- | |
dc5c1114 | 1882 | |
2b036c4b | 1883 | bool wxSocketClient::DoConnect(const wxSockAddress& remote, |
72ac4e88 VZ |
1884 | const wxSockAddress* local, |
1885 | bool wait) | |
f4ada568 | 1886 | { |
2b036c4b | 1887 | if ( m_impl ) |
c2116a35 | 1888 | { |
2b036c4b | 1889 | // Shutdown and destroy the old socket |
c2116a35 | 1890 | Close(); |
51fe4b60 | 1891 | delete m_impl; |
c2116a35 | 1892 | } |
a324a7bc | 1893 | |
c2116a35 VZ |
1894 | m_connected = false; |
1895 | m_establishing = false; | |
384b4373 | 1896 | |
2b036c4b VZ |
1897 | // Create and set up the new one |
1898 | m_impl = wxSocketImpl::Create(*this); | |
1899 | if ( !m_impl ) | |
c2116a35 | 1900 | return false; |
384b4373 | 1901 | |
c2116a35 VZ |
1902 | // Reuse makes sense for clients too, if we are trying to rebind to the same port |
1903 | if (GetFlags() & wxSOCKET_REUSEADDR) | |
51fe4b60 | 1904 | m_impl->SetReusable(); |
c2116a35 | 1905 | if (GetFlags() & wxSOCKET_BROADCAST) |
51fe4b60 | 1906 | m_impl->SetBroadcast(); |
c2116a35 | 1907 | if (GetFlags() & wxSOCKET_NOBIND) |
51fe4b60 | 1908 | m_impl->DontDoBind(); |
33d925b0 | 1909 | |
2b036c4b VZ |
1910 | // Bind to the local IP address and port, when provided or if one had been |
1911 | // set before | |
c9bccf23 | 1912 | if ( !local && m_localAddress.GetAddress().IsOk() ) |
c2116a35 | 1913 | local = &m_localAddress; |
c2116a35 | 1914 | |
2b036c4b VZ |
1915 | if ( local ) |
1916 | m_impl->SetLocal(local->GetAddress()); | |
33d925b0 | 1917 | |
51fe4b60 | 1918 | m_impl->SetInitialSocketBuffers(m_initialRecvBufferSize, m_initialSendBufferSize); |
8c029a5b | 1919 | |
2b036c4b VZ |
1920 | m_impl->SetPeer(remote.GetAddress()); |
1921 | ||
1922 | // Finally do create the socket and connect to the peer | |
1923 | const wxSocketError err = m_impl->CreateClient(wait); | |
791b24c4 | 1924 | |
2b036c4b | 1925 | if ( err != wxSOCKET_NOERROR ) |
c2116a35 | 1926 | { |
2b036c4b VZ |
1927 | if ( err == wxSOCKET_WOULDBLOCK ) |
1928 | { | |
1929 | wxASSERT_MSG( !wait, "shouldn't get this for blocking connect" ); | |
1930 | ||
c2116a35 | 1931 | m_establishing = true; |
2b036c4b | 1932 | } |
56d8adc0 | 1933 | |
c2116a35 VZ |
1934 | return false; |
1935 | } | |
9111db68 | 1936 | |
c2116a35 VZ |
1937 | m_connected = true; |
1938 | return true; | |
f4ada568 GL |
1939 | } |
1940 | ||
2b036c4b | 1941 | bool wxSocketClient::Connect(const wxSockAddress& remote, bool wait) |
33d925b0 | 1942 | { |
2b036c4b | 1943 | return DoConnect(remote, NULL, wait); |
33d925b0 KH |
1944 | } |
1945 | ||
2b036c4b | 1946 | bool wxSocketClient::Connect(const wxSockAddress& remote, |
72ac4e88 VZ |
1947 | const wxSockAddress& local, |
1948 | bool wait) | |
33d925b0 | 1949 | { |
2b036c4b | 1950 | return DoConnect(remote, &local, wait); |
33d925b0 KH |
1951 | } |
1952 | ||
d80d1aba | 1953 | bool wxSocketClient::WaitOnConnect(long seconds, long milliseconds) |
f4ada568 | 1954 | { |
60ee0172 VZ |
1955 | if ( m_connected ) |
1956 | { | |
1957 | // this happens if the initial attempt to connect succeeded without | |
1958 | // blocking | |
7fb0a11d | 1959 | return true; |
60ee0172 | 1960 | } |
d80d1aba | 1961 | |
51fe4b60 | 1962 | wxCHECK_MSG( m_establishing && m_impl, false, |
60ee0172 | 1963 | "No connection establishment attempt in progress" ); |
d80d1aba | 1964 | |
ebbf7407 VZ |
1965 | // notice that we return true even if DoWait() returned -1, i.e. if an |
1966 | // error occurred and connection was lost: this is intentional as we should | |
1967 | // return false only if timeout expired without anything happening | |
1968 | return DoWait(seconds, milliseconds, wxSOCKET_CONNECTION_FLAG) != 0; | |
f4ada568 GL |
1969 | } |
1970 | ||
ef57d866 | 1971 | // ========================================================================== |
dc5c1114 | 1972 | // wxDatagramSocket |
ef57d866 | 1973 | // ========================================================================== |
dc5c1114 | 1974 | |
fbfb8bcc | 1975 | wxDatagramSocket::wxDatagramSocket( const wxSockAddress& addr, |
71622a7a GRG |
1976 | wxSocketFlags flags ) |
1977 | : wxSocketBase( flags, wxSOCKET_DATAGRAM ) | |
dc5c1114 | 1978 | { |
7fb0a11d | 1979 | // Create the socket |
51fe4b60 | 1980 | m_impl = wxSocketImpl::Create(*this); |
dc5c1114 | 1981 | |
51fe4b60 | 1982 | if (!m_impl) |
7fb0a11d | 1983 | return; |
53a161e1 | 1984 | |
7fb0a11d | 1985 | // Setup the socket as non connection oriented |
51fe4b60 | 1986 | m_impl->SetLocal(addr.GetAddress()); |
28bf2f3c VZ |
1987 | if (flags & wxSOCKET_REUSEADDR) |
1988 | { | |
51fe4b60 | 1989 | m_impl->SetReusable(); |
28bf2f3c | 1990 | } |
60edcf45 VZ |
1991 | if (GetFlags() & wxSOCKET_BROADCAST) |
1992 | { | |
51fe4b60 | 1993 | m_impl->SetBroadcast(); |
60edcf45 VZ |
1994 | } |
1995 | if (GetFlags() & wxSOCKET_NOBIND) | |
1996 | { | |
51fe4b60 | 1997 | m_impl->DontDoBind(); |
60edcf45 | 1998 | } |
51fe4b60 VZ |
1999 | |
2000 | if ( m_impl->CreateUDP() != wxSOCKET_NOERROR ) | |
7fb0a11d | 2001 | { |
51fe4b60 VZ |
2002 | delete m_impl; |
2003 | m_impl = NULL; | |
7fb0a11d WS |
2004 | return; |
2005 | } | |
dc5c1114 | 2006 | |
7fb0a11d WS |
2007 | // Initialize all stuff |
2008 | m_connected = false; | |
2009 | m_establishing = false; | |
dc5c1114 GRG |
2010 | } |
2011 | ||
2012 | wxDatagramSocket& wxDatagramSocket::RecvFrom( wxSockAddress& addr, | |
f187448d | 2013 | void* buf, |
dc5c1114 GRG |
2014 | wxUint32 nBytes ) |
2015 | { | |
2016 | Read(buf, nBytes); | |
2017 | GetPeer(addr); | |
2018 | return (*this); | |
2019 | } | |
2020 | ||
fbfb8bcc | 2021 | wxDatagramSocket& wxDatagramSocket::SendTo( const wxSockAddress& addr, |
f187448d | 2022 | const void* buf, |
dc5c1114 GRG |
2023 | wxUint32 nBytes ) |
2024 | { | |
51fe4b60 | 2025 | wxASSERT_MSG( m_impl, _T("Socket not initialised") ); |
8e3f3880 | 2026 | |
51fe4b60 | 2027 | m_impl->SetPeer(addr.GetAddress()); |
dc5c1114 GRG |
2028 | Write(buf, nBytes); |
2029 | return (*this); | |
2030 | } | |
2031 | ||
ef57d866 | 2032 | // ========================================================================== |
a58d5df4 | 2033 | // wxSocketModule |
ef57d866 | 2034 | // ========================================================================== |
dc5c1114 | 2035 | |
ed4c6c69 | 2036 | class wxSocketModule : public wxModule |
dc5c1114 | 2037 | { |
dc5c1114 | 2038 | public: |
6c0d0845 VZ |
2039 | virtual bool OnInit() |
2040 | { | |
51fe4b60 VZ |
2041 | // wxSocketBase will call Initialize() itself only if sockets are |
2042 | // really used, don't do it from here | |
d775fa82 | 2043 | return true; |
6c0d0845 VZ |
2044 | } |
2045 | ||
2046 | virtual void OnExit() | |
2047 | { | |
2048 | if ( wxSocketBase::IsInitialized() ) | |
2049 | wxSocketBase::Shutdown(); | |
2050 | } | |
2051 | ||
2052 | private: | |
2053 | DECLARE_DYNAMIC_CLASS(wxSocketModule) | |
a58d5df4 GL |
2054 | }; |
2055 | ||
2056 | IMPLEMENT_DYNAMIC_CLASS(wxSocketModule, wxModule) | |
2057 | ||
4f61df82 | 2058 | #if defined(wxUSE_SELECT_DISPATCHER) && wxUSE_SELECT_DISPATCHER |
54e757fc FM |
2059 | // NOTE: we need to force linking against socketiohandler.cpp otherwise in |
2060 | // static builds of wxWidgets the ManagerSetter::ManagerSetter ctor | |
2061 | // contained there wouldn't be ever called | |
2062 | wxFORCE_LINK_MODULE( socketiohandler ) | |
4f61df82 | 2063 | #endif |
54e757fc | 2064 | |
c2116a35 | 2065 | #endif // wxUSE_SOCKETS |