]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: socket.cpp | |
3 | // Purpose: Socket handler classes | |
4 | // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia | |
5 | // Created: April 1997 | |
6 | // Copyright: (C) 1999-1997, Guilhem Lavaux | |
7 | // (C) 2000-1999, Guillermo Rodriguez Garcia | |
8 | // RCS_ID: $Id$ | |
9 | // License: see wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ========================================================================== | |
13 | // Declarations | |
14 | // ========================================================================== | |
15 | ||
16 | #ifdef __GNUG__ | |
17 | #pragma implementation "socket.h" | |
18 | #endif | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #if wxUSE_SOCKETS | |
28 | ||
29 | #include "wx/app.h" | |
30 | #include "wx/apptrait.h" | |
31 | #include "wx/defs.h" | |
32 | #include "wx/object.h" | |
33 | #include "wx/string.h" | |
34 | #include "wx/timer.h" | |
35 | #include "wx/utils.h" | |
36 | #include "wx/module.h" | |
37 | #include "wx/log.h" | |
38 | #include "wx/intl.h" | |
39 | #include "wx/event.h" | |
40 | ||
41 | #include "wx/sckaddr.h" | |
42 | #include "wx/socket.h" | |
43 | ||
44 | // DLL options compatibility check: | |
45 | #include "wx/build.h" | |
46 | WX_CHECK_BUILD_OPTIONS("wxNet") | |
47 | ||
48 | // -------------------------------------------------------------------------- | |
49 | // macros and constants | |
50 | // -------------------------------------------------------------------------- | |
51 | ||
52 | // discard buffer | |
53 | #define MAX_DISCARD_SIZE (10 * 1024) | |
54 | ||
55 | // what to do within waits: we have 2 cases: from the main thread itself we | |
56 | // have to call wxYield() to let the events (including the GUI events and the | |
57 | // low-level (not wxWindows) events from GSocket) be processed. From another | |
58 | // thread it is enough to just call wxThread::Yield() which will give away the | |
59 | // rest of our time slice: the explanation is that the events will be processed | |
60 | // by the main thread anyhow, without calling wxYield(), but we don't want to | |
61 | // eat the CPU time uselessly while sitting in the loop waiting for the data | |
62 | #if wxUSE_THREADS | |
63 | #define PROCESS_EVENTS() \ | |
64 | { \ | |
65 | if ( wxThread::IsMain() ) \ | |
66 | wxYield(); \ | |
67 | else \ | |
68 | wxThread::Yield(); \ | |
69 | } | |
70 | #else // !wxUSE_THREADS | |
71 | #define PROCESS_EVENTS() wxYield() | |
72 | #endif // wxUSE_THREADS/!wxUSE_THREADS | |
73 | ||
74 | #define wxTRACE_Socket _T("wxSocket") | |
75 | ||
76 | // -------------------------------------------------------------------------- | |
77 | // wxWin macros | |
78 | // -------------------------------------------------------------------------- | |
79 | ||
80 | IMPLEMENT_CLASS(wxSocketBase, wxObject) | |
81 | IMPLEMENT_CLASS(wxSocketServer, wxSocketBase) | |
82 | IMPLEMENT_CLASS(wxSocketClient, wxSocketBase) | |
83 | IMPLEMENT_CLASS(wxDatagramSocket, wxSocketBase) | |
84 | IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent, wxEvent) | |
85 | ||
86 | // -------------------------------------------------------------------------- | |
87 | // private classes | |
88 | // -------------------------------------------------------------------------- | |
89 | ||
90 | class wxSocketState : public wxObject | |
91 | { | |
92 | public: | |
93 | wxSocketFlags m_flags; | |
94 | wxSocketEventFlags m_eventmask; | |
95 | bool m_notify; | |
96 | void *m_clientData; | |
97 | #if WXWIN_COMPATIBILITY | |
98 | wxSocketBase::wxSockCbk m_cbk; | |
99 | char *m_cdata; | |
100 | #endif // WXWIN_COMPATIBILITY | |
101 | ||
102 | public: | |
103 | wxSocketState() : wxObject() {} | |
104 | ||
105 | DECLARE_NO_COPY_CLASS(wxSocketState) | |
106 | }; | |
107 | ||
108 | // ========================================================================== | |
109 | // wxSocketBase | |
110 | // ========================================================================== | |
111 | ||
112 | // -------------------------------------------------------------------------- | |
113 | // Initialization and shutdown | |
114 | // -------------------------------------------------------------------------- | |
115 | ||
116 | // FIXME-MT: all this is MT-unsafe, of course, we should protect all accesses | |
117 | // to m_countInit with a crit section | |
118 | size_t wxSocketBase::m_countInit = 0; | |
119 | ||
120 | bool wxSocketBase::IsInitialized() | |
121 | { | |
122 | return m_countInit > 0; | |
123 | } | |
124 | ||
125 | bool wxSocketBase::Initialize() | |
126 | { | |
127 | if ( !m_countInit++ ) | |
128 | { | |
129 | if ( !GSocket_Init() ) | |
130 | { | |
131 | m_countInit--; | |
132 | ||
133 | return FALSE; | |
134 | } | |
135 | } | |
136 | ||
137 | return TRUE; | |
138 | } | |
139 | ||
140 | void wxSocketBase::Shutdown() | |
141 | { | |
142 | // we should be initialized | |
143 | wxASSERT_MSG( m_countInit, _T("extra call to Shutdown()") ); | |
144 | if ( !--m_countInit ) | |
145 | { | |
146 | GSocket_Cleanup(); | |
147 | } | |
148 | } | |
149 | ||
150 | // -------------------------------------------------------------------------- | |
151 | // Ctor and dtor | |
152 | // -------------------------------------------------------------------------- | |
153 | ||
154 | void wxSocketBase::Init() | |
155 | { | |
156 | m_socket = NULL; | |
157 | m_type = wxSOCKET_UNINIT; | |
158 | ||
159 | // state | |
160 | m_flags = 0; | |
161 | m_connected = | |
162 | m_establishing = | |
163 | m_reading = | |
164 | m_writing = | |
165 | m_error = FALSE; | |
166 | m_lcount = 0; | |
167 | m_timeout = 600; | |
168 | m_beingDeleted = FALSE; | |
169 | ||
170 | // pushback buffer | |
171 | m_unread = NULL; | |
172 | m_unrd_size = 0; | |
173 | m_unrd_cur = 0; | |
174 | ||
175 | // events | |
176 | m_id = -1; | |
177 | m_handler = NULL; | |
178 | m_clientData = NULL; | |
179 | m_notify = FALSE; | |
180 | m_eventmask = 0; | |
181 | #if WXWIN_COMPATIBILITY | |
182 | m_cbk = NULL; | |
183 | m_cdata = NULL; | |
184 | #endif // WXWIN_COMPATIBILITY | |
185 | ||
186 | if ( !IsInitialized() ) | |
187 | { | |
188 | // this Initialize() will be undone by wxSocketModule::OnExit(), all the | |
189 | // other calls to it should be matched by a call to Shutdown() | |
190 | Initialize(); | |
191 | } | |
192 | } | |
193 | ||
194 | wxSocketBase::wxSocketBase() | |
195 | { | |
196 | Init(); | |
197 | } | |
198 | ||
199 | wxSocketBase::wxSocketBase(wxSocketFlags flags, wxSocketType type) | |
200 | { | |
201 | Init(); | |
202 | ||
203 | m_flags = flags; | |
204 | m_type = type; | |
205 | } | |
206 | ||
207 | wxSocketBase::~wxSocketBase() | |
208 | { | |
209 | // Just in case the app called Destroy() *and* then deleted | |
210 | // the socket immediately: don't leave dangling pointers. | |
211 | wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL; | |
212 | if ( traits ) | |
213 | traits->RemoveFromPendingDelete(this); | |
214 | ||
215 | // Shutdown and close the socket | |
216 | if (!m_beingDeleted) | |
217 | Close(); | |
218 | ||
219 | // Destroy the GSocket object | |
220 | if (m_socket) | |
221 | GSocket_destroy(m_socket); | |
222 | ||
223 | // Free the pushback buffer | |
224 | if (m_unread) | |
225 | free(m_unread); | |
226 | } | |
227 | ||
228 | bool wxSocketBase::Destroy() | |
229 | { | |
230 | // Delayed destruction: the socket will be deleted during the next | |
231 | // idle loop iteration. This ensures that all pending events have | |
232 | // been processed. | |
233 | m_beingDeleted = TRUE; | |
234 | ||
235 | // Shutdown and close the socket | |
236 | Close(); | |
237 | ||
238 | // Supress events from now on | |
239 | Notify(FALSE); | |
240 | ||
241 | // schedule this object for deletion | |
242 | wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL; | |
243 | if ( traits ) | |
244 | { | |
245 | // let the traits object decide what to do with us | |
246 | traits->ScheduleForDestroy(this); | |
247 | } | |
248 | else // no app or no traits | |
249 | { | |
250 | // in wxBase we might have no app object at all, don't leak memory | |
251 | delete this; | |
252 | } | |
253 | ||
254 | return TRUE; | |
255 | } | |
256 | ||
257 | // -------------------------------------------------------------------------- | |
258 | // Basic IO calls | |
259 | // -------------------------------------------------------------------------- | |
260 | ||
261 | // The following IO operations update m_error and m_lcount: | |
262 | // {Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard} | |
263 | // | |
264 | // TODO: Should Connect, Accept and AcceptWith update m_error? | |
265 | ||
266 | bool wxSocketBase::Close() | |
267 | { | |
268 | // Interrupt pending waits | |
269 | InterruptWait(); | |
270 | ||
271 | if (m_socket) | |
272 | { | |
273 | // Disable callbacks | |
274 | GSocket_UnsetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG | | |
275 | GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG); | |
276 | ||
277 | // Shutdown the connection | |
278 | GSocket_Shutdown(m_socket); | |
279 | } | |
280 | ||
281 | m_connected = FALSE; | |
282 | m_establishing = FALSE; | |
283 | return TRUE; | |
284 | } | |
285 | ||
286 | wxSocketBase& wxSocketBase::Read(void* buffer, wxUint32 nbytes) | |
287 | { | |
288 | // Mask read events | |
289 | m_reading = TRUE; | |
290 | ||
291 | m_lcount = _Read(buffer, nbytes); | |
292 | ||
293 | // If in wxSOCKET_WAITALL mode, all bytes should have been read. | |
294 | if (m_flags & wxSOCKET_WAITALL) | |
295 | m_error = (m_lcount != nbytes); | |
296 | else | |
297 | m_error = (m_lcount == 0); | |
298 | ||
299 | // Allow read events from now on | |
300 | m_reading = FALSE; | |
301 | ||
302 | return *this; | |
303 | } | |
304 | ||
305 | wxUint32 wxSocketBase::_Read(void* buffer, wxUint32 nbytes) | |
306 | { | |
307 | int total; | |
308 | int ret = 1; | |
309 | ||
310 | // Try the pushback buffer first | |
311 | total = GetPushback(buffer, nbytes, FALSE); | |
312 | nbytes -= total; | |
313 | buffer = (char *)buffer + total; | |
314 | ||
315 | // Return now in one of the following cases: | |
316 | // - the socket is invalid, | |
317 | // - we got all the data, | |
318 | // - we got *some* data and we are not using wxSOCKET_WAITALL. | |
319 | if ( !m_socket || | |
320 | !nbytes || | |
321 | ((total != 0) && !(m_flags & wxSOCKET_WAITALL)) ) | |
322 | return total; | |
323 | ||
324 | // Possible combinations (they are checked in this order) | |
325 | // wxSOCKET_NOWAIT | |
326 | // wxSOCKET_WAITALL (with or without wxSOCKET_BLOCK) | |
327 | // wxSOCKET_BLOCK | |
328 | // wxSOCKET_NONE | |
329 | // | |
330 | if (m_flags & wxSOCKET_NOWAIT) | |
331 | { | |
332 | GSocket_SetNonBlocking(m_socket, 1); | |
333 | ret = GSocket_Read(m_socket, (char *)buffer, nbytes); | |
334 | GSocket_SetNonBlocking(m_socket, 0); | |
335 | ||
336 | if (ret > 0) | |
337 | total += ret; | |
338 | } | |
339 | else | |
340 | { | |
341 | bool more = TRUE; | |
342 | ||
343 | while (more) | |
344 | { | |
345 | if ( !(m_flags & wxSOCKET_BLOCK) && !WaitForRead() ) | |
346 | break; | |
347 | ||
348 | ret = GSocket_Read(m_socket, (char *)buffer, nbytes); | |
349 | ||
350 | if (ret > 0) | |
351 | { | |
352 | total += ret; | |
353 | nbytes -= ret; | |
354 | buffer = (char *)buffer + ret; | |
355 | } | |
356 | ||
357 | // If we got here and wxSOCKET_WAITALL is not set, we can leave | |
358 | // now. Otherwise, wait until we recv all the data or until there | |
359 | // is an error. | |
360 | // | |
361 | more = (ret > 0 && nbytes > 0 && (m_flags & wxSOCKET_WAITALL)); | |
362 | } | |
363 | } | |
364 | ||
365 | return total; | |
366 | } | |
367 | ||
368 | wxSocketBase& wxSocketBase::ReadMsg(void* buffer, wxUint32 nbytes) | |
369 | { | |
370 | wxUint32 len, len2, sig, total; | |
371 | bool error; | |
372 | int old_flags; | |
373 | struct | |
374 | { | |
375 | unsigned char sig[4]; | |
376 | unsigned char len[4]; | |
377 | } msg; | |
378 | ||
379 | // Mask read events | |
380 | m_reading = TRUE; | |
381 | ||
382 | total = 0; | |
383 | error = TRUE; | |
384 | old_flags = m_flags; | |
385 | SetFlags((m_flags & wxSOCKET_BLOCK) | wxSOCKET_WAITALL); | |
386 | ||
387 | if (_Read(&msg, sizeof(msg)) != sizeof(msg)) | |
388 | goto exit; | |
389 | ||
390 | sig = (wxUint32)msg.sig[0]; | |
391 | sig |= (wxUint32)(msg.sig[1] << 8); | |
392 | sig |= (wxUint32)(msg.sig[2] << 16); | |
393 | sig |= (wxUint32)(msg.sig[3] << 24); | |
394 | ||
395 | if (sig != 0xfeeddead) | |
396 | { | |
397 | wxLogWarning(_("wxSocket: invalid signature in ReadMsg.")); | |
398 | goto exit; | |
399 | } | |
400 | ||
401 | len = (wxUint32)msg.len[0]; | |
402 | len |= (wxUint32)(msg.len[1] << 8); | |
403 | len |= (wxUint32)(msg.len[2] << 16); | |
404 | len |= (wxUint32)(msg.len[3] << 24); | |
405 | ||
406 | if (len > nbytes) | |
407 | { | |
408 | len2 = len - nbytes; | |
409 | len = nbytes; | |
410 | } | |
411 | else | |
412 | len2 = 0; | |
413 | ||
414 | // Don't attemp to read if the msg was zero bytes long. | |
415 | if (len) | |
416 | { | |
417 | total = _Read(buffer, len); | |
418 | ||
419 | if (total != len) | |
420 | goto exit; | |
421 | } | |
422 | if (len2) | |
423 | { | |
424 | char *discard_buffer = new char[MAX_DISCARD_SIZE]; | |
425 | long discard_len; | |
426 | ||
427 | // NOTE: discarded bytes don't add to m_lcount. | |
428 | do | |
429 | { | |
430 | discard_len = ((len2 > MAX_DISCARD_SIZE)? MAX_DISCARD_SIZE : len2); | |
431 | discard_len = _Read(discard_buffer, (wxUint32)discard_len); | |
432 | len2 -= (wxUint32)discard_len; | |
433 | } | |
434 | while ((discard_len > 0) && len2); | |
435 | ||
436 | delete [] discard_buffer; | |
437 | ||
438 | if (len2 != 0) | |
439 | goto exit; | |
440 | } | |
441 | if (_Read(&msg, sizeof(msg)) != sizeof(msg)) | |
442 | goto exit; | |
443 | ||
444 | sig = (wxUint32)msg.sig[0]; | |
445 | sig |= (wxUint32)(msg.sig[1] << 8); | |
446 | sig |= (wxUint32)(msg.sig[2] << 16); | |
447 | sig |= (wxUint32)(msg.sig[3] << 24); | |
448 | ||
449 | if (sig != 0xdeadfeed) | |
450 | { | |
451 | wxLogWarning(_("wxSocket: invalid signature in ReadMsg.")); | |
452 | goto exit; | |
453 | } | |
454 | ||
455 | // everything was OK | |
456 | error = FALSE; | |
457 | ||
458 | exit: | |
459 | m_error = error; | |
460 | m_lcount = total; | |
461 | m_reading = FALSE; | |
462 | SetFlags(old_flags); | |
463 | ||
464 | return *this; | |
465 | } | |
466 | ||
467 | wxSocketBase& wxSocketBase::Peek(void* buffer, wxUint32 nbytes) | |
468 | { | |
469 | // Mask read events | |
470 | m_reading = TRUE; | |
471 | ||
472 | m_lcount = _Read(buffer, nbytes); | |
473 | Pushback(buffer, m_lcount); | |
474 | ||
475 | // If in wxSOCKET_WAITALL mode, all bytes should have been read. | |
476 | if (m_flags & wxSOCKET_WAITALL) | |
477 | m_error = (m_lcount != nbytes); | |
478 | else | |
479 | m_error = (m_lcount == 0); | |
480 | ||
481 | // Allow read events again | |
482 | m_reading = FALSE; | |
483 | ||
484 | return *this; | |
485 | } | |
486 | ||
487 | wxSocketBase& wxSocketBase::Write(const void *buffer, wxUint32 nbytes) | |
488 | { | |
489 | // Mask write events | |
490 | m_writing = TRUE; | |
491 | ||
492 | m_lcount = _Write(buffer, nbytes); | |
493 | ||
494 | // If in wxSOCKET_WAITALL mode, all bytes should have been written. | |
495 | if (m_flags & wxSOCKET_WAITALL) | |
496 | m_error = (m_lcount != nbytes); | |
497 | else | |
498 | m_error = (m_lcount == 0); | |
499 | ||
500 | // Allow write events again | |
501 | m_writing = FALSE; | |
502 | ||
503 | return *this; | |
504 | } | |
505 | ||
506 | wxUint32 wxSocketBase::_Write(const void *buffer, wxUint32 nbytes) | |
507 | { | |
508 | wxUint32 total = 0; | |
509 | int ret = 1; | |
510 | ||
511 | // If the socket is invalid or parameters are ill, return immediately | |
512 | if (!m_socket || !buffer || !nbytes) | |
513 | return 0; | |
514 | ||
515 | // Possible combinations (they are checked in this order) | |
516 | // wxSOCKET_NOWAIT | |
517 | // wxSOCKET_WAITALL (with or without wxSOCKET_BLOCK) | |
518 | // wxSOCKET_BLOCK | |
519 | // wxSOCKET_NONE | |
520 | // | |
521 | if (m_flags & wxSOCKET_NOWAIT) | |
522 | { | |
523 | GSocket_SetNonBlocking(m_socket, 1); | |
524 | ret = GSocket_Write(m_socket, (const char *)buffer, nbytes); | |
525 | GSocket_SetNonBlocking(m_socket, 0); | |
526 | ||
527 | if (ret > 0) | |
528 | total = ret; | |
529 | } | |
530 | else | |
531 | { | |
532 | bool more = TRUE; | |
533 | ||
534 | while (more) | |
535 | { | |
536 | if ( !(m_flags & wxSOCKET_BLOCK) && !WaitForWrite() ) | |
537 | break; | |
538 | ||
539 | ret = GSocket_Write(m_socket, (const char *)buffer, nbytes); | |
540 | ||
541 | if (ret > 0) | |
542 | { | |
543 | total += ret; | |
544 | nbytes -= ret; | |
545 | buffer = (const char *)buffer + ret; | |
546 | } | |
547 | ||
548 | // If we got here and wxSOCKET_WAITALL is not set, we can leave | |
549 | // now. Otherwise, wait until we send all the data or until there | |
550 | // is an error. | |
551 | // | |
552 | more = (ret > 0 && nbytes > 0 && (m_flags & wxSOCKET_WAITALL)); | |
553 | } | |
554 | } | |
555 | ||
556 | return total; | |
557 | } | |
558 | ||
559 | wxSocketBase& wxSocketBase::WriteMsg(const void *buffer, wxUint32 nbytes) | |
560 | { | |
561 | wxUint32 total; | |
562 | bool error; | |
563 | int old_flags; | |
564 | struct | |
565 | { | |
566 | unsigned char sig[4]; | |
567 | unsigned char len[4]; | |
568 | } msg; | |
569 | ||
570 | // Mask write events | |
571 | m_writing = TRUE; | |
572 | ||
573 | error = TRUE; | |
574 | total = 0; | |
575 | old_flags = m_flags; | |
576 | SetFlags((m_flags & wxSOCKET_BLOCK) | wxSOCKET_WAITALL); | |
577 | ||
578 | msg.sig[0] = (unsigned char) 0xad; | |
579 | msg.sig[1] = (unsigned char) 0xde; | |
580 | msg.sig[2] = (unsigned char) 0xed; | |
581 | msg.sig[3] = (unsigned char) 0xfe; | |
582 | ||
583 | msg.len[0] = (unsigned char) (nbytes & 0xff); | |
584 | msg.len[1] = (unsigned char) ((nbytes >> 8) & 0xff); | |
585 | msg.len[2] = (unsigned char) ((nbytes >> 16) & 0xff); | |
586 | msg.len[3] = (unsigned char) ((nbytes >> 24) & 0xff); | |
587 | ||
588 | if (_Write(&msg, sizeof(msg)) < sizeof(msg)) | |
589 | goto exit; | |
590 | ||
591 | total = _Write(buffer, nbytes); | |
592 | ||
593 | if (total < nbytes) | |
594 | goto exit; | |
595 | ||
596 | msg.sig[0] = (unsigned char) 0xed; | |
597 | msg.sig[1] = (unsigned char) 0xfe; | |
598 | msg.sig[2] = (unsigned char) 0xad; | |
599 | msg.sig[3] = (unsigned char) 0xde; | |
600 | msg.len[0] = msg.len[1] = msg.len[2] = msg.len[3] = (char) 0; | |
601 | ||
602 | if ((_Write(&msg, sizeof(msg))) < sizeof(msg)) | |
603 | goto exit; | |
604 | ||
605 | // everything was OK | |
606 | error = FALSE; | |
607 | ||
608 | exit: | |
609 | m_error = error; | |
610 | m_lcount = total; | |
611 | m_writing = FALSE; | |
612 | ||
613 | return *this; | |
614 | } | |
615 | ||
616 | wxSocketBase& wxSocketBase::Unread(const void *buffer, wxUint32 nbytes) | |
617 | { | |
618 | if (nbytes != 0) | |
619 | Pushback(buffer, nbytes); | |
620 | ||
621 | m_error = FALSE; | |
622 | m_lcount = nbytes; | |
623 | ||
624 | return *this; | |
625 | } | |
626 | ||
627 | wxSocketBase& wxSocketBase::Discard() | |
628 | { | |
629 | int old_flags; | |
630 | char *buffer = new char[MAX_DISCARD_SIZE]; | |
631 | wxUint32 ret; | |
632 | wxUint32 total = 0; | |
633 | ||
634 | // Mask read events | |
635 | m_reading = TRUE; | |
636 | ||
637 | old_flags = m_flags; | |
638 | SetFlags(wxSOCKET_NOWAIT); | |
639 | ||
640 | do | |
641 | { | |
642 | ret = _Read(buffer, MAX_DISCARD_SIZE); | |
643 | total += ret; | |
644 | } | |
645 | while (ret == MAX_DISCARD_SIZE); | |
646 | ||
647 | delete[] buffer; | |
648 | m_lcount = total; | |
649 | m_error = FALSE; | |
650 | ||
651 | // Allow read events again | |
652 | m_reading = FALSE; | |
653 | ||
654 | return *this; | |
655 | } | |
656 | ||
657 | // -------------------------------------------------------------------------- | |
658 | // Wait functions | |
659 | // -------------------------------------------------------------------------- | |
660 | ||
661 | // All Wait functions poll the socket using GSocket_Select() to | |
662 | // check for the specified combination of conditions, until one | |
663 | // of these conditions become true, an error occurs, or the | |
664 | // timeout elapses. The polling loop calls PROCESS_EVENTS(), so | |
665 | // this won't block the GUI. | |
666 | ||
667 | bool wxSocketBase::_Wait(long seconds, | |
668 | long milliseconds, | |
669 | wxSocketEventFlags flags) | |
670 | { | |
671 | GSocketEventFlags result; | |
672 | long timeout; | |
673 | ||
674 | // Set this to TRUE to interrupt ongoing waits | |
675 | m_interrupt = FALSE; | |
676 | ||
677 | // Check for valid socket | |
678 | if (!m_socket) | |
679 | return FALSE; | |
680 | ||
681 | // Check for valid timeout value. | |
682 | if (seconds != -1) | |
683 | timeout = seconds * 1000 + milliseconds; | |
684 | else | |
685 | timeout = m_timeout * 1000; | |
686 | ||
687 | // Wait in an active polling loop. | |
688 | // | |
689 | // NOTE: We duplicate some of the code in OnRequest, but this doesn't | |
690 | // hurt. It has to be here because the (GSocket) event might arrive | |
691 | // a bit delayed, and it has to be in OnRequest as well because we | |
692 | // don't know whether the Wait functions are being used. | |
693 | // | |
694 | // Do this at least once (important if timeout == 0, when | |
695 | // we are just polling). Also, if just polling, do not yield. | |
696 | ||
697 | wxStopWatch chrono; | |
698 | bool done = FALSE; | |
699 | ||
700 | while (!done) | |
701 | { | |
702 | result = GSocket_Select(m_socket, flags | GSOCK_LOST_FLAG); | |
703 | ||
704 | // Incoming connection (server) or connection established (client) | |
705 | if (result & GSOCK_CONNECTION_FLAG) | |
706 | { | |
707 | m_connected = TRUE; | |
708 | m_establishing = FALSE; | |
709 | return TRUE; | |
710 | } | |
711 | ||
712 | // Data available or output buffer ready | |
713 | if ((result & GSOCK_INPUT_FLAG) || (result & GSOCK_OUTPUT_FLAG)) | |
714 | { | |
715 | return TRUE; | |
716 | } | |
717 | ||
718 | // Connection lost | |
719 | if (result & GSOCK_LOST_FLAG) | |
720 | { | |
721 | m_connected = FALSE; | |
722 | m_establishing = FALSE; | |
723 | return (flags & GSOCK_LOST_FLAG) != 0; | |
724 | } | |
725 | ||
726 | // Wait more? | |
727 | if ((!timeout) || (chrono.Time() > timeout) || (m_interrupt)) | |
728 | done = TRUE; | |
729 | else | |
730 | PROCESS_EVENTS(); | |
731 | } | |
732 | ||
733 | return FALSE; | |
734 | } | |
735 | ||
736 | bool wxSocketBase::Wait(long seconds, long milliseconds) | |
737 | { | |
738 | return _Wait(seconds, milliseconds, GSOCK_INPUT_FLAG | | |
739 | GSOCK_OUTPUT_FLAG | | |
740 | GSOCK_CONNECTION_FLAG | | |
741 | GSOCK_LOST_FLAG); | |
742 | } | |
743 | ||
744 | bool wxSocketBase::WaitForRead(long seconds, long milliseconds) | |
745 | { | |
746 | // Check pushback buffer before entering _Wait | |
747 | if (m_unread) | |
748 | return TRUE; | |
749 | ||
750 | // Note that GSOCK_INPUT_LOST has to be explicitly passed to | |
751 | // _Wait becuase of the semantics of WaitForRead: a return | |
752 | // value of TRUE means that a GSocket_Read call will return | |
753 | // immediately, not that there is actually data to read. | |
754 | ||
755 | return _Wait(seconds, milliseconds, GSOCK_INPUT_FLAG | | |
756 | GSOCK_LOST_FLAG); | |
757 | } | |
758 | ||
759 | bool wxSocketBase::WaitForWrite(long seconds, long milliseconds) | |
760 | { | |
761 | return _Wait(seconds, milliseconds, GSOCK_OUTPUT_FLAG); | |
762 | } | |
763 | ||
764 | bool wxSocketBase::WaitForLost(long seconds, long milliseconds) | |
765 | { | |
766 | return _Wait(seconds, milliseconds, GSOCK_LOST_FLAG); | |
767 | } | |
768 | ||
769 | // -------------------------------------------------------------------------- | |
770 | // Miscellaneous | |
771 | // -------------------------------------------------------------------------- | |
772 | ||
773 | // | |
774 | // Get local or peer address | |
775 | // | |
776 | ||
777 | bool wxSocketBase::GetPeer(wxSockAddress& addr_man) const | |
778 | { | |
779 | GAddress *peer; | |
780 | ||
781 | if (!m_socket) | |
782 | return FALSE; | |
783 | ||
784 | peer = GSocket_GetPeer(m_socket); | |
785 | ||
786 | // copying a null address would just trigger an assert anyway | |
787 | ||
788 | if (!peer) | |
789 | return FALSE; | |
790 | ||
791 | addr_man.SetAddress(peer); | |
792 | GAddress_destroy(peer); | |
793 | ||
794 | return TRUE; | |
795 | } | |
796 | ||
797 | bool wxSocketBase::GetLocal(wxSockAddress& addr_man) const | |
798 | { | |
799 | GAddress *local; | |
800 | ||
801 | if (!m_socket) | |
802 | return FALSE; | |
803 | ||
804 | local = GSocket_GetLocal(m_socket); | |
805 | addr_man.SetAddress(local); | |
806 | GAddress_destroy(local); | |
807 | ||
808 | return TRUE; | |
809 | } | |
810 | ||
811 | // | |
812 | // Save and restore socket state | |
813 | // | |
814 | ||
815 | void wxSocketBase::SaveState() | |
816 | { | |
817 | wxSocketState *state; | |
818 | ||
819 | state = new wxSocketState(); | |
820 | ||
821 | state->m_flags = m_flags; | |
822 | state->m_notify = m_notify; | |
823 | state->m_eventmask = m_eventmask; | |
824 | state->m_clientData = m_clientData; | |
825 | #if WXWIN_COMPATIBILITY | |
826 | state->m_cbk = m_cbk; | |
827 | state->m_cdata = m_cdata; | |
828 | #endif // WXWIN_COMPATIBILITY | |
829 | ||
830 | m_states.Append(state); | |
831 | } | |
832 | ||
833 | void wxSocketBase::RestoreState() | |
834 | { | |
835 | wxList::compatibility_iterator node; | |
836 | wxSocketState *state; | |
837 | ||
838 | node = m_states.GetLast(); | |
839 | if (!node) | |
840 | return; | |
841 | ||
842 | state = (wxSocketState *)node->GetData(); | |
843 | ||
844 | m_flags = state->m_flags; | |
845 | m_notify = state->m_notify; | |
846 | m_eventmask = state->m_eventmask; | |
847 | m_clientData = state->m_clientData; | |
848 | #if WXWIN_COMPATIBILITY | |
849 | m_cbk = state->m_cbk; | |
850 | m_cdata = state->m_cdata; | |
851 | #endif // WXWIN_COMPATIBILITY | |
852 | ||
853 | m_states.Erase(node); | |
854 | delete state; | |
855 | } | |
856 | ||
857 | // | |
858 | // Timeout and flags | |
859 | // | |
860 | ||
861 | void wxSocketBase::SetTimeout(long seconds) | |
862 | { | |
863 | m_timeout = seconds; | |
864 | ||
865 | if (m_socket) | |
866 | GSocket_SetTimeout(m_socket, m_timeout * 1000); | |
867 | } | |
868 | ||
869 | void wxSocketBase::SetFlags(wxSocketFlags flags) | |
870 | { | |
871 | m_flags = flags; | |
872 | } | |
873 | ||
874 | ||
875 | // -------------------------------------------------------------------------- | |
876 | // Callbacks (now obsolete - use events instead) | |
877 | // -------------------------------------------------------------------------- | |
878 | ||
879 | #if WXWIN_COMPATIBILITY | |
880 | ||
881 | wxSocketBase::wxSockCbk wxSocketBase::Callback(wxSockCbk cbk_) | |
882 | { | |
883 | wxSockCbk old_cbk = cbk_; | |
884 | ||
885 | m_cbk = cbk_; | |
886 | return old_cbk; | |
887 | } | |
888 | ||
889 | char *wxSocketBase::CallbackData(char *data) | |
890 | { | |
891 | char *old_data = m_cdata; | |
892 | ||
893 | m_cdata = data; | |
894 | return old_data; | |
895 | } | |
896 | ||
897 | #endif // WXWIN_COMPATIBILITY | |
898 | ||
899 | // -------------------------------------------------------------------------- | |
900 | // Event handling | |
901 | // -------------------------------------------------------------------------- | |
902 | ||
903 | // A note on how events are processed, which is probably the most | |
904 | // difficult thing to get working right while keeping the same API | |
905 | // and functionality for all platforms. | |
906 | // | |
907 | // When GSocket detects an event, it calls wx_socket_callback, which in | |
908 | // turn just calls wxSocketBase::OnRequest in the corresponding wxSocket | |
909 | // object. OnRequest does some housekeeping, and if the event is to be | |
910 | // propagated to the user, it creates a new wxSocketEvent object and | |
911 | // posts it. The event is not processed immediately, but delayed with | |
912 | // AddPendingEvent instead. This is necessary in order to decouple the | |
913 | // event processing from wx_socket_callback; otherwise, subsequent IO | |
914 | // calls made from the user event handler would fail, as gtk callbacks | |
915 | // are not reentrant. | |
916 | // | |
917 | // Note that, unlike events, user callbacks (now deprecated) are _not_ | |
918 | // decoupled from wx_socket_callback and thus they suffer from a variety | |
919 | // of problems. Avoid them where possible and use events instead. | |
920 | ||
921 | extern "C" | |
922 | void LINKAGEMODE wx_socket_callback(GSocket * WXUNUSED(socket), | |
923 | GSocketEvent notification, | |
924 | char *cdata) | |
925 | { | |
926 | wxSocketBase *sckobj = (wxSocketBase *)cdata; | |
927 | ||
928 | sckobj->OnRequest((wxSocketNotify) notification); | |
929 | } | |
930 | ||
931 | void wxSocketBase::OnRequest(wxSocketNotify notification) | |
932 | { | |
933 | // NOTE: We duplicate some of the code in _Wait, but this doesn't | |
934 | // hurt. It has to be here because the (GSocket) event might arrive | |
935 | // a bit delayed, and it has to be in _Wait as well because we don't | |
936 | // know whether the Wait functions are being used. | |
937 | ||
938 | switch(notification) | |
939 | { | |
940 | case wxSOCKET_CONNECTION: | |
941 | m_establishing = FALSE; | |
942 | m_connected = TRUE; | |
943 | break; | |
944 | ||
945 | // If we are in the middle of a R/W operation, do not | |
946 | // propagate events to users. Also, filter 'late' events | |
947 | // which are no longer valid. | |
948 | ||
949 | case wxSOCKET_INPUT: | |
950 | if (m_reading || !GSocket_Select(m_socket, GSOCK_INPUT_FLAG)) | |
951 | return; | |
952 | break; | |
953 | ||
954 | case wxSOCKET_OUTPUT: | |
955 | if (m_writing || !GSocket_Select(m_socket, GSOCK_OUTPUT_FLAG)) | |
956 | return; | |
957 | break; | |
958 | ||
959 | case wxSOCKET_LOST: | |
960 | m_connected = FALSE; | |
961 | m_establishing = FALSE; | |
962 | break; | |
963 | ||
964 | default: | |
965 | break; | |
966 | } | |
967 | ||
968 | // Schedule the event | |
969 | ||
970 | wxSocketEventFlags flag = 0; | |
971 | switch (notification) | |
972 | { | |
973 | case GSOCK_INPUT: flag = GSOCK_INPUT_FLAG; break; | |
974 | case GSOCK_OUTPUT: flag = GSOCK_OUTPUT_FLAG; break; | |
975 | case GSOCK_CONNECTION: flag = GSOCK_CONNECTION_FLAG; break; | |
976 | case GSOCK_LOST: flag = GSOCK_LOST_FLAG; break; | |
977 | default: | |
978 | wxLogWarning(_("wxSocket: unknown event!.")); | |
979 | return; | |
980 | } | |
981 | ||
982 | if (((m_eventmask & flag) == flag) && m_notify) | |
983 | { | |
984 | if (m_handler) | |
985 | { | |
986 | wxSocketEvent event(m_id); | |
987 | event.m_event = notification; | |
988 | event.m_clientData = m_clientData; | |
989 | event.SetEventObject(this); | |
990 | ||
991 | m_handler->AddPendingEvent(event); | |
992 | } | |
993 | ||
994 | #if WXWIN_COMPATIBILITY | |
995 | if (m_cbk) | |
996 | m_cbk(*this, notification, m_cdata); | |
997 | #endif // WXWIN_COMPATIBILITY | |
998 | } | |
999 | } | |
1000 | ||
1001 | void wxSocketBase::Notify(bool notify) | |
1002 | { | |
1003 | m_notify = notify; | |
1004 | } | |
1005 | ||
1006 | void wxSocketBase::SetNotify(wxSocketEventFlags flags) | |
1007 | { | |
1008 | m_eventmask = flags; | |
1009 | } | |
1010 | ||
1011 | void wxSocketBase::SetEventHandler(wxEvtHandler& handler, int id) | |
1012 | { | |
1013 | m_handler = &handler; | |
1014 | m_id = id; | |
1015 | } | |
1016 | ||
1017 | // -------------------------------------------------------------------------- | |
1018 | // Pushback buffer | |
1019 | // -------------------------------------------------------------------------- | |
1020 | ||
1021 | void wxSocketBase::Pushback(const void *buffer, wxUint32 size) | |
1022 | { | |
1023 | if (!size) return; | |
1024 | ||
1025 | if (m_unread == NULL) | |
1026 | m_unread = malloc(size); | |
1027 | else | |
1028 | { | |
1029 | void *tmp; | |
1030 | ||
1031 | tmp = malloc(m_unrd_size + size); | |
1032 | memcpy((char *)tmp + size, m_unread, m_unrd_size); | |
1033 | free(m_unread); | |
1034 | ||
1035 | m_unread = tmp; | |
1036 | } | |
1037 | ||
1038 | m_unrd_size += size; | |
1039 | ||
1040 | memcpy(m_unread, buffer, size); | |
1041 | } | |
1042 | ||
1043 | wxUint32 wxSocketBase::GetPushback(void *buffer, wxUint32 size, bool peek) | |
1044 | { | |
1045 | if (!m_unrd_size) | |
1046 | return 0; | |
1047 | ||
1048 | if (size > (m_unrd_size-m_unrd_cur)) | |
1049 | size = m_unrd_size-m_unrd_cur; | |
1050 | ||
1051 | memcpy(buffer, (char *)m_unread + m_unrd_cur, size); | |
1052 | ||
1053 | if (!peek) | |
1054 | { | |
1055 | m_unrd_cur += size; | |
1056 | if (m_unrd_size == m_unrd_cur) | |
1057 | { | |
1058 | free(m_unread); | |
1059 | m_unread = NULL; | |
1060 | m_unrd_size = 0; | |
1061 | m_unrd_cur = 0; | |
1062 | } | |
1063 | } | |
1064 | ||
1065 | return size; | |
1066 | } | |
1067 | ||
1068 | ||
1069 | // ========================================================================== | |
1070 | // wxSocketServer | |
1071 | // ========================================================================== | |
1072 | ||
1073 | // -------------------------------------------------------------------------- | |
1074 | // Ctor | |
1075 | // -------------------------------------------------------------------------- | |
1076 | ||
1077 | wxSocketServer::wxSocketServer(wxSockAddress& addr_man, | |
1078 | wxSocketFlags flags) | |
1079 | : wxSocketBase(flags, wxSOCKET_SERVER) | |
1080 | { | |
1081 | wxLogTrace( wxTRACE_Socket, _T("Opening wxSocketServer") ); | |
1082 | ||
1083 | m_socket = GSocket_new(); | |
1084 | ||
1085 | if (!m_socket) | |
1086 | { | |
1087 | wxLogTrace( wxTRACE_Socket, _T("*** GSocket_new failed") ); | |
1088 | return; | |
1089 | } | |
1090 | ||
1091 | // Setup the socket as server | |
1092 | ||
1093 | GSocket_SetLocal(m_socket, addr_man.GetAddress()); | |
1094 | if (GSocket_SetServer(m_socket) != GSOCK_NOERROR) | |
1095 | { | |
1096 | GSocket_destroy(m_socket); | |
1097 | m_socket = NULL; | |
1098 | ||
1099 | wxLogTrace( wxTRACE_Socket, _T("*** GSocket_SetServer failed") ); | |
1100 | return; | |
1101 | } | |
1102 | ||
1103 | GSocket_SetTimeout(m_socket, m_timeout * 1000); | |
1104 | GSocket_SetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG | | |
1105 | GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG, | |
1106 | wx_socket_callback, (char *)this); | |
1107 | } | |
1108 | ||
1109 | // -------------------------------------------------------------------------- | |
1110 | // Accept | |
1111 | // -------------------------------------------------------------------------- | |
1112 | ||
1113 | bool wxSocketServer::AcceptWith(wxSocketBase& sock, bool wait) | |
1114 | { | |
1115 | GSocket *child_socket; | |
1116 | ||
1117 | if (!m_socket) | |
1118 | return FALSE; | |
1119 | ||
1120 | // If wait == FALSE, then the call should be nonblocking. | |
1121 | // When we are finished, we put the socket to blocking mode | |
1122 | // again. | |
1123 | ||
1124 | if (!wait) | |
1125 | GSocket_SetNonBlocking(m_socket, 1); | |
1126 | ||
1127 | child_socket = GSocket_WaitConnection(m_socket); | |
1128 | ||
1129 | if (!wait) | |
1130 | GSocket_SetNonBlocking(m_socket, 0); | |
1131 | ||
1132 | if (!child_socket) | |
1133 | return FALSE; | |
1134 | ||
1135 | sock.m_type = wxSOCKET_BASE; | |
1136 | sock.m_socket = child_socket; | |
1137 | sock.m_connected = TRUE; | |
1138 | ||
1139 | GSocket_SetTimeout(sock.m_socket, sock.m_timeout * 1000); | |
1140 | GSocket_SetCallback(sock.m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG | | |
1141 | GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG, | |
1142 | wx_socket_callback, (char *)&sock); | |
1143 | ||
1144 | return TRUE; | |
1145 | } | |
1146 | ||
1147 | wxSocketBase *wxSocketServer::Accept(bool wait) | |
1148 | { | |
1149 | wxSocketBase* sock = new wxSocketBase(); | |
1150 | ||
1151 | sock->SetFlags(m_flags); | |
1152 | ||
1153 | if (!AcceptWith(*sock, wait)) | |
1154 | { | |
1155 | sock->Destroy(); | |
1156 | sock = NULL; | |
1157 | } | |
1158 | ||
1159 | return sock; | |
1160 | } | |
1161 | ||
1162 | bool wxSocketServer::WaitForAccept(long seconds, long milliseconds) | |
1163 | { | |
1164 | return _Wait(seconds, milliseconds, GSOCK_CONNECTION_FLAG); | |
1165 | } | |
1166 | ||
1167 | // ========================================================================== | |
1168 | // wxSocketClient | |
1169 | // ========================================================================== | |
1170 | ||
1171 | // -------------------------------------------------------------------------- | |
1172 | // Ctor and dtor | |
1173 | // -------------------------------------------------------------------------- | |
1174 | ||
1175 | wxSocketClient::wxSocketClient(wxSocketFlags flags) | |
1176 | : wxSocketBase(flags, wxSOCKET_CLIENT) | |
1177 | { | |
1178 | } | |
1179 | ||
1180 | wxSocketClient::~wxSocketClient() | |
1181 | { | |
1182 | } | |
1183 | ||
1184 | // -------------------------------------------------------------------------- | |
1185 | // Connect | |
1186 | // -------------------------------------------------------------------------- | |
1187 | ||
1188 | bool wxSocketClient::Connect(wxSockAddress& addr_man, bool wait) | |
1189 | { | |
1190 | GSocketError err; | |
1191 | ||
1192 | if (m_socket) | |
1193 | { | |
1194 | // Shutdown and destroy the socket | |
1195 | Close(); | |
1196 | GSocket_destroy(m_socket); | |
1197 | } | |
1198 | ||
1199 | m_socket = GSocket_new(); | |
1200 | m_connected = FALSE; | |
1201 | m_establishing = FALSE; | |
1202 | ||
1203 | if (!m_socket) | |
1204 | return FALSE; | |
1205 | ||
1206 | GSocket_SetTimeout(m_socket, m_timeout * 1000); | |
1207 | GSocket_SetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG | | |
1208 | GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG, | |
1209 | wx_socket_callback, (char *)this); | |
1210 | ||
1211 | // If wait == FALSE, then the call should be nonblocking. | |
1212 | // When we are finished, we put the socket to blocking mode | |
1213 | // again. | |
1214 | ||
1215 | if (!wait) | |
1216 | GSocket_SetNonBlocking(m_socket, 1); | |
1217 | ||
1218 | GSocket_SetPeer(m_socket, addr_man.GetAddress()); | |
1219 | err = GSocket_Connect(m_socket, GSOCK_STREAMED); | |
1220 | ||
1221 | if (!wait) | |
1222 | GSocket_SetNonBlocking(m_socket, 0); | |
1223 | ||
1224 | if (err != GSOCK_NOERROR) | |
1225 | { | |
1226 | if (err == GSOCK_WOULDBLOCK) | |
1227 | m_establishing = TRUE; | |
1228 | ||
1229 | return FALSE; | |
1230 | } | |
1231 | ||
1232 | m_connected = TRUE; | |
1233 | return TRUE; | |
1234 | } | |
1235 | ||
1236 | bool wxSocketClient::WaitOnConnect(long seconds, long milliseconds) | |
1237 | { | |
1238 | if (m_connected) // Already connected | |
1239 | return TRUE; | |
1240 | ||
1241 | if (!m_establishing || !m_socket) // No connection in progress | |
1242 | return FALSE; | |
1243 | ||
1244 | return _Wait(seconds, milliseconds, GSOCK_CONNECTION_FLAG | | |
1245 | GSOCK_LOST_FLAG); | |
1246 | } | |
1247 | ||
1248 | // ========================================================================== | |
1249 | // wxDatagramSocket | |
1250 | // ========================================================================== | |
1251 | ||
1252 | /* NOTE: experimental stuff - might change */ | |
1253 | ||
1254 | wxDatagramSocket::wxDatagramSocket( wxSockAddress& addr, | |
1255 | wxSocketFlags flags ) | |
1256 | : wxSocketBase( flags, wxSOCKET_DATAGRAM ) | |
1257 | { | |
1258 | // Create the socket | |
1259 | m_socket = GSocket_new(); | |
1260 | ||
1261 | if(!m_socket) | |
1262 | return; | |
1263 | ||
1264 | // Setup the socket as non connection oriented | |
1265 | GSocket_SetLocal(m_socket, addr.GetAddress()); | |
1266 | if( GSocket_SetNonOriented(m_socket) != GSOCK_NOERROR ) | |
1267 | { | |
1268 | GSocket_destroy(m_socket); | |
1269 | m_socket = NULL; | |
1270 | return; | |
1271 | } | |
1272 | ||
1273 | // Initialize all stuff | |
1274 | m_connected = FALSE; | |
1275 | m_establishing = FALSE; | |
1276 | GSocket_SetTimeout( m_socket, m_timeout ); | |
1277 | GSocket_SetCallback( m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG | | |
1278 | GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG, | |
1279 | wx_socket_callback, (char*)this ); | |
1280 | ||
1281 | } | |
1282 | ||
1283 | wxDatagramSocket& wxDatagramSocket::RecvFrom( wxSockAddress& addr, | |
1284 | void* buf, | |
1285 | wxUint32 nBytes ) | |
1286 | { | |
1287 | Read(buf, nBytes); | |
1288 | GetPeer(addr); | |
1289 | return (*this); | |
1290 | } | |
1291 | ||
1292 | wxDatagramSocket& wxDatagramSocket::SendTo( wxSockAddress& addr, | |
1293 | const void* buf, | |
1294 | wxUint32 nBytes ) | |
1295 | { | |
1296 | GSocket_SetPeer(m_socket, addr.GetAddress()); | |
1297 | Write(buf, nBytes); | |
1298 | return (*this); | |
1299 | } | |
1300 | ||
1301 | // ========================================================================== | |
1302 | // wxSocketModule | |
1303 | // ========================================================================== | |
1304 | ||
1305 | class wxSocketModule : public wxModule | |
1306 | { | |
1307 | public: | |
1308 | virtual bool OnInit() | |
1309 | { | |
1310 | // wxSocketBase will call GSocket_Init() itself when/if needed | |
1311 | return TRUE; | |
1312 | } | |
1313 | ||
1314 | virtual void OnExit() | |
1315 | { | |
1316 | if ( wxSocketBase::IsInitialized() ) | |
1317 | wxSocketBase::Shutdown(); | |
1318 | } | |
1319 | ||
1320 | private: | |
1321 | DECLARE_DYNAMIC_CLASS(wxSocketModule) | |
1322 | }; | |
1323 | ||
1324 | IMPLEMENT_DYNAMIC_CLASS(wxSocketModule, wxModule) | |
1325 | ||
1326 | #endif | |
1327 | // wxUSE_SOCKETS | |
1328 | ||
1329 | // vi:sts=4:sw=4:et |