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