]> git.saurik.com Git - wxWidgets.git/blob - src/common/socket.cpp
Updated the Remstar ODBC files, got the db sample compiling; added Freq and SubString
[wxWidgets.git] / src / common / socket.cpp
1 ////////////////////////////////////////////////////////////////////////////////
2 // Name: socket.cpp
3 // Purpose: Socket handler classes
4 // Authors: Guilhem Lavaux (completely rewritten from a basic API of Andrew
5 // Davidson(1995) in wxWeb)
6 // Created: April 1997
7 // Updated: March 1998
8 // Copyright: (C) 1998, 1997, Guilhem Lavaux
9 // RCS_ID: $Id$
10 // License: see wxWindows license
11 ////////////////////////////////////////////////////////////////////////////////
12 #ifdef __GNUG__
13 #pragma implementation "socket.h"
14 // #pragma interface
15 // #pragma implementation "socket.cpp"
16 #endif
17
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 /////////////////////////////////////////////////////////////////////////////
26 // wxWindows headers
27 /////////////////////////////////////////////////////////////////////////////
28 #include <wx/defs.h>
29 #include <wx/object.h>
30 #include <wx/string.h>
31 #include <wx/timer.h>
32 #include <wx/utils.h>
33
34 // Not enough OS behaviour defined for wxStubs
35 #ifndef __WXSTUBS__
36
37 #include <stdlib.h>
38 #include <string.h>
39 #include <ctype.h>
40
41 /////////////////////////////////////////////////////////////////////////////
42 // System specific headers
43 /////////////////////////////////////////////////////////////////////////////
44 #if defined(__WINDOWS__)
45 #include <winsock.h>
46 #endif // __WINDOWS__
47
48 #if defined(__UNIX__)
49
50 #ifdef VMS
51 #include <socket.h>
52 #else
53 #include <sys/socket.h>
54 #endif
55 #include <sys/ioctl.h>
56
57 #include <sys/time.h>
58 #include <unistd.h>
59
60 #ifdef sun
61 #include <sys/filio.h>
62 #endif
63
64 #endif // __UNIX__
65
66 #include <signal.h>
67 #include <errno.h>
68
69 #ifdef _MSC_VER
70 #include <io.h>
71 #endif
72
73 #if defined(__WXMOTIF__) || defined(__WXXT__)
74 #include <X11/Intrinsic.h>
75
76 /////////////////////////////
77 // Needs internal variables
78 /////////////////////////////
79 #ifdef __WXXT__
80 #define Uses_XtIntrinsic
81 #endif
82
83 #endif
84
85 #if defined(__WXGTK__)
86 #include <gtk/gtk.h>
87 #endif
88
89 /////////////////////////////////////////////////////////////////////////////
90 // wxSocket headers
91 /////////////////////////////////////////////////////////////////////////////
92 #include "wx/module.h"
93 #define WXSOCK_INTERNAL
94 #include "wx/sckaddr.h"
95 #include "wx/socket.h"
96
97 /////////////////////////////////////////////////////////////////////////////
98 // Some patch ///// BEGIN
99 /////////////////////////////////////////////////////////////////////////////
100 #ifdef __WINDOWS__
101 #define close closesocket
102 #define ioctl ioctlsocket
103 #ifdef errno
104 #undef errno
105 #endif
106 #define errno WSAGetLastError()
107 #ifdef EWOULDBLOCK
108 #undef EWOULDBLOCK
109 #endif
110 #define EWOULDBLOCK WSAEWOULDBLOCK
111 #define ETIMEDOUT WSAETIMEDOUT
112 #undef EINTR
113 #define EINTR WSAEINTR
114 #endif
115
116 #ifndef __WINDOWS__
117 #define INVALID_SOCKET -1
118 #endif
119
120 #ifdef __WXMOTIF__
121 #define wxAPP_CONTEXT ((XtAppContext)wxTheApp->GetAppContext())
122 #endif
123
124 #ifdef __WINDOWS__
125 // This is an MS TCP/IP routine and is not needed here. Some WinSock
126 // implementations (such as PC-NFS) will require you to include this
127 // or a similar routine (see appendix in WinSock doc or help file).
128
129 #if defined( NEED_WSAFDIsSet ) || defined( _MSC_VER )
130 int PASCAL FAR __WSAFDIsSet(SOCKET fd, fd_set FAR *set)
131 {
132 int i = set->fd_count;
133
134 while (i--)
135 {
136 if (set->fd_array[i] == fd)
137 return 1;
138 }
139
140 return 0;
141 }
142 #endif
143 #endif
144
145 #if defined(__WINDOWS__)
146 #define PROCESS_EVENTS() wxYield()
147 #elif defined(__WXXT__) || defined(__WXMOTIF__)
148 #define PROCESS_EVENTS() XtAppProcessEvent(wxAPP_CONTEXT, XtIMAll)
149 #elif defined(__WXGTK__)
150 #define PROCESS_EVENTS() wxYield()
151 #endif
152
153 /////////////////////////////////////////////////////////////////////////////
154 // Some patch ///// END
155 /////////////////////////////////////////////////////////////////////////////
156
157 // --------------------------------------------------------------
158 // Module
159 // --------------------------------------------------------------
160 class wxSocketModule: public wxModule
161 {
162 DECLARE_DYNAMIC_CLASS(wxSocketModule)
163 public:
164 wxSocketModule() {}
165 bool OnInit();
166 void OnExit();
167 };
168
169 // --------------------------------------------------------------
170 // ClassInfos
171 // --------------------------------------------------------------
172 #if !USE_SHARED_LIBRARY
173 IMPLEMENT_CLASS(wxSocketBase, wxObject)
174 IMPLEMENT_CLASS(wxSocketServer, wxSocketBase)
175 IMPLEMENT_CLASS(wxSocketClient, wxSocketBase)
176 IMPLEMENT_CLASS(wxSocketHandler, wxObject)
177 IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent, wxEvent)
178 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule, wxModule)
179 #endif
180
181 class wxSockWakeUp : public wxTimer
182 {
183 public:
184 int *my_id;
185 int n_val;
186 wxSocketBase *sock;
187
188 wxSockWakeUp(wxSocketBase *_sock, int *id, int new_val)
189 {
190 my_id = id; n_val = new_val;
191 sock = _sock;
192 }
193 virtual void Notify()
194 {
195 *my_id = n_val;
196 if (sock) sock->Notify(FALSE);
197 }
198 };
199
200 /// Socket request
201 class SockRequest : public wxObject
202 {
203 public:
204 char *buffer;
205 size_t size, nbytes;
206 bool done;
207 int error;
208 wxSockWakeUp *auto_wakeup;
209 wxSocketBase::wxRequestNotify type;
210 };
211
212
213 /////////////////////////////////////////////////////////////////////////////
214 // Some internal define
215 /////////////////////////////////////////////////////////////////////////////
216
217 // --------------------------------------------------------------
218 // --------- wxSocketBase CONSTRUCTOR ---------------------------
219 // --------------------------------------------------------------
220 wxSocketBase::wxSocketBase(wxSocketBase::wxSockFlags _flags,
221 wxSocketBase::wxSockType _type) :
222 wxEvtHandler(),
223 m_flags(_flags), m_type(_type), m_connected(FALSE), m_connecting(FALSE),
224 m_fd(INVALID_SOCKET), m_waitflags(0), m_cbk(0), m_cdata(0), m_id(-1),
225 m_handler(0),
226 m_neededreq((wxRequestNotify)(REQ_READ | REQ_LOST)),
227 m_cbkon(FALSE),
228 m_unread(NULL), m_unrd_size(0),
229 m_processing(FALSE),
230 m_timeout(1), m_wantbuf(0)
231 {
232 m_internal = new wxSockInternal;
233 #if defined(__WXXT__) || defined(__WXMOTIF__) || defined(__WXGTK__)
234 m_internal->sock_inputid = 0;
235 m_internal->sock_outputid = 0;
236 m_internal->sock_exceptid = 0;
237 #endif
238 #ifdef __WINDOWS__
239 m_internal->my_msg = 0;
240 #endif
241 }
242
243 wxSocketBase::wxSocketBase() :
244 wxEvtHandler(),
245 m_flags(WAITALL), m_type(SOCK_UNINIT), m_connected(FALSE),
246 m_connecting(FALSE), m_fd(INVALID_SOCKET), m_waitflags(0),
247 m_cbk(0), m_cdata(0),
248 m_id(-1), m_handler(0),
249 m_neededreq((wxRequestNotify)(REQ_READ | REQ_LOST)),
250 m_cbkon(FALSE),
251 m_unread(NULL), m_unrd_size(0),
252 m_processing(FALSE),
253 m_timeout(1), m_wantbuf(0)
254 {
255 m_internal = new wxSockInternal;
256 #if defined(__WXXT__) || defined(__WXMOTIF__) || defined(__WXGTK__)
257 m_internal->sock_inputid = 0;
258 m_internal->sock_outputid = 0;
259 m_internal->sock_exceptid = 0;
260 #endif
261 #ifdef __WINDOWS__
262 m_internal->my_msg = 0;
263 #endif
264 }
265
266 // --------------------------------------------------------------
267 // --------- wxSocketBase CONSTRUCTOR ---------------------------
268 // --------------------------------------------------------------
269 wxSocketBase::~wxSocketBase()
270 {
271 DestroyCallbacks();
272 Close();
273
274 if (m_unread)
275 free(m_unread);
276 if (m_handler) {
277 #ifdef __WINDOWS__
278 if (m_internal->my_msg)
279 m_handler->DestroyMessage(m_internal->my_msg);
280 #endif
281 m_handler->UnRegister(this);
282 }
283 m_states.DeleteContents(TRUE);
284
285 delete m_internal;
286 }
287
288 bool wxSocketBase::Close()
289 {
290 if (m_fd != INVALID_SOCKET) {
291 for (int i=0;i<3;i++) {
292 wxNode *n, *node = req_list[i].First();
293
294 while (node) {
295 SockRequest *req = (SockRequest *)node->Data();
296 req->done = TRUE;
297
298 n = node->Next();
299 delete node;
300 node = n;
301 }
302 }
303
304 DestroyCallbacks();
305 shutdown(m_fd, 2);
306 close(m_fd);
307 m_fd = INVALID_SOCKET;
308 m_connected = FALSE;
309 }
310
311 return TRUE;
312 }
313
314 // --------------------------------------------------------------
315 // --------- wxSocketBase base IO functions ---------------------
316 // --------------------------------------------------------------
317 wxSocketBase& wxSocketBase::Read(char* buffer, size_t nbytes)
318 {
319 m_lcount = GetPushback(buffer, nbytes, FALSE);
320
321 // If we have got the whole needed buffer or if we don't want to
322 // wait then it returns immediately.
323 if (!nbytes || (m_lcount && !(m_flags & WAITALL)) )
324 return *this;
325
326 WantBuffer(buffer, nbytes, EVT_READ);
327
328 return *this;
329 }
330
331 wxSocketBase& wxSocketBase::Peek(char* buffer, size_t nbytes)
332 {
333 size_t nbytes_old = nbytes;
334
335 nbytes -= GetPushback(buffer, nbytes, TRUE);
336 if (!nbytes) {
337 m_lcount = nbytes_old;
338 return *this;
339 }
340
341 WantBuffer(buffer, nbytes, EVT_PEEK);
342
343 return *this;
344 }
345
346 wxSocketBase& wxSocketBase::Write(const char *buffer, size_t nbytes)
347 {
348 WantBuffer((char *)buffer, nbytes, EVT_WRITE);
349 return *this;
350 }
351
352 wxSocketBase& wxSocketBase::ReadMsg(char* buffer, size_t nbytes)
353 {
354 SockMsg msg;
355 size_t len, len2, sig;
356
357 Read((char *)&msg, sizeof(msg));
358 if (m_lcount != sizeof(msg))
359 return *this;
360
361 sig = msg.sig[0] & 0xff;
362 sig |= (size_t)(msg.sig[1] & 0xff) << 8;
363 sig |= (size_t)(msg.sig[2] & 0xff) << 16;
364 sig |= (size_t)(msg.sig[3] & 0xff) << 24;
365
366 if (sig != 0xfeeddead)
367 return *this;
368 len = msg.len[0] & 0xff;
369 len |= (size_t)(msg.len[1] & 0xff) << 8;
370 len |= (size_t)(msg.len[2] & 0xff) << 16;
371 len |= (size_t)(msg.len[3] & 0xff) << 24;
372 len2 = len;
373 if (len > nbytes)
374 len = nbytes;
375 else
376 len2 = 0;
377
378 if (Read(buffer, len).LastCount() != len)
379 return *this;
380 if (len2 && (Read(NULL, len2).LastCount() != len2))
381 return *this;
382 if (Read((char *)&msg, sizeof(msg)).LastCount() != sizeof(msg))
383 return *this;
384
385 sig = msg.sig[0] & 0xff;
386 sig |= (size_t)(msg.sig[1] & 0xff) << 8;
387 sig |= (size_t)(msg.sig[2] & 0xff) << 16;
388 sig |= (size_t)(msg.sig[3] & 0xff) << 24;
389 // ERROR
390 if (sig != 0xdeadfeed)
391 return *this;
392
393 return *this;
394 }
395
396 wxSocketBase& wxSocketBase::WriteMsg(const char *buffer, size_t nbytes)
397 {
398 SockMsg msg;
399
400 msg.sig[0] = (char) 0xad;
401 msg.sig[1] = (char) 0xde;
402 msg.sig[2] = (char) 0xed;
403 msg.sig[3] = (char) 0xfe;
404
405 msg.len[0] = (char) nbytes & 0xff;
406 msg.len[1] = (char) (nbytes >> 8) & 0xff;
407 msg.len[2] = (char) (nbytes >> 16) & 0xff;
408 msg.len[3] = (char) (nbytes >> 24) & 0xff;
409
410 if (Write((char *)&msg, sizeof(msg)).LastCount() < sizeof(msg))
411 return *this;
412 if (Write(buffer, nbytes).LastCount() < nbytes)
413 return *this;
414
415 msg.sig[0] = (char) 0xed;
416 msg.sig[1] = (char) 0xfe;
417 msg.sig[2] = (char) 0xad;
418 msg.sig[3] = (char) 0xde;
419 msg.len[0] = msg.len[1] = msg.len[2] = msg.len[3] = (char) 0;
420 Write((char *)&msg, sizeof(msg));
421
422 return *this;
423 }
424
425 wxSocketBase& wxSocketBase::Unread(const char *buffer, size_t nbytes)
426 {
427 CreatePushbackAfter(buffer, nbytes);
428 return *this;
429 }
430
431 bool wxSocketBase::IsData() const
432 {
433 struct timeval tv;
434 fd_set sock_set;
435
436 if (m_fd < 0)
437 return FALSE;
438 if (m_unrd_size > 0)
439 return TRUE;
440
441 tv.tv_sec = 0;
442 tv.tv_usec = 0;
443 FD_ZERO(&sock_set);
444 FD_SET(m_fd, &sock_set);
445 select(FD_SETSIZE, &sock_set, NULL, NULL, &tv);
446 return (FD_ISSET(m_fd, &sock_set) != 0);
447 }
448
449 // ---------------------------------------------------------------------
450 // --------- wxSocketBase Discard(): deletes all byte in the input queue
451 // ---------------------------------------------------------------------
452 void wxSocketBase::Discard()
453 {
454 #define MAX_BUFSIZE (10*1024)
455 char *my_data = new char[MAX_BUFSIZE];
456 size_t recv_size = MAX_BUFSIZE;
457
458 SaveState();
459 SetFlags((wxSockFlags)(NOWAIT | SPEED));
460
461 while (recv_size == MAX_BUFSIZE) {
462 recv_size = Read(my_data, MAX_BUFSIZE).LastCount();
463 }
464
465 RestoreState();
466 delete [] my_data;
467
468 #undef MAX_BUFSIZE
469 }
470
471 // --------------------------------------------------------------
472 // --------- wxSocketBase socket info functions -----------------
473 // --------------------------------------------------------------
474 bool wxSocketBase::GetPeer(wxSockAddress& addr_man) const
475 {
476 struct sockaddr my_addr;
477 uint len_addr = sizeof(my_addr);
478
479 if (m_fd < 0)
480 return FALSE;
481
482 if (getpeername(m_fd, (struct sockaddr *)&my_addr, &len_addr) < 0)
483 return FALSE;
484
485 addr_man.Disassemble(&my_addr, len_addr);
486 return TRUE;
487 }
488
489 bool wxSocketBase::GetLocal(wxSockAddress& addr_man) const
490 {
491 struct sockaddr my_addr;
492 uint len_addr = sizeof(my_addr);
493
494 if (m_fd < 0)
495 return FALSE;
496
497 if (getsockname(m_fd, (struct sockaddr *)&my_addr, &len_addr) < 0)
498
499 return FALSE;
500
501 addr_man.Disassemble(&my_addr, len_addr);
502 return TRUE;
503 }
504
505 // --------------------------------------------------------------
506 // --------- wxSocketBase wait functions ------------------------
507 // --------------------------------------------------------------
508
509 void wxSocketBase::SaveState()
510 {
511 wxSockState *state = new wxSockState;
512
513 state->cbk_on = m_cbkon;
514 state->cbk_set= m_neededreq;
515 state->cbk = m_cbk;
516 state->cdata = m_cdata;
517 state->flags = m_flags;
518 state->notif = m_notifyme;
519
520 m_states.Append(state);
521 }
522
523 void wxSocketBase::RestoreState()
524 {
525 wxNode *node;
526
527 node = m_states.Last();
528 if (!node)
529 return;
530
531 wxSockState *state = (wxSockState *)node->Data();
532
533 SetFlags(state->flags);
534 m_neededreq = state->cbk_set;
535 m_cbk = state->cbk;
536 m_cdata = state->cdata;
537 m_notifyme = state->notif;
538 if (state->cbk_on)
539 SetupCallbacks();
540 else
541 DestroyCallbacks();
542
543 delete node;
544 delete state;
545 }
546
547 // --------------------------------------------------------------
548 // --------- wxSocketBase wait functions ------------------------
549 // --------------------------------------------------------------
550
551 bool wxSocketBase::_Wait(long seconds, long microseconds, int type)
552 {
553 if ((!m_connected && !m_connecting) || m_fd < 0)
554 return FALSE;
555
556 wxSockWakeUp wakeup(this, &m_waitflags, 0);
557
558 SaveState();
559 SetNotify((wxRequestNotify)type);
560 SetupCallbacks();
561
562 if (seconds != -1)
563 wakeup.Start((int)(seconds*1000 + (microseconds / 1000)), TRUE);
564
565 m_waitflags = 0x80 | type;
566 while (m_waitflags & 0x80)
567 PROCESS_EVENTS();
568
569 RestoreState();
570
571 if (m_waitflags & 0x40) {
572 m_waitflags = 0;
573 return TRUE;
574 }
575 m_waitflags = 0;
576
577 return FALSE;
578 }
579
580 bool wxSocketBase::Wait(long seconds, long microseconds)
581 {
582 return _Wait(seconds, microseconds, REQ_ACCEPT | REQ_CONNECT |
583 REQ_READ | REQ_WRITE | REQ_LOST);
584 }
585
586 bool wxSocketBase::WaitForRead(long seconds, long microseconds)
587 {
588 return _Wait(seconds, microseconds, REQ_READ | REQ_LOST);
589 }
590
591 bool wxSocketBase::WaitForWrite(long seconds, long microseconds)
592 {
593 return _Wait(seconds, microseconds, REQ_WRITE);
594 }
595
596 bool wxSocketBase::WaitForLost(long seconds, long microseconds)
597 {
598 return _Wait(seconds, microseconds, REQ_LOST);
599 }
600
601 // --------------------------------------------------------------
602 // --------- wxSocketBase callback management -------------------
603 // --------------------------------------------------------------
604
605 #if defined(__WXMOTIF__) || defined(__WXXT__) || defined(__WXGTK__)
606 #if defined(__WXMOTIF__) || defined(__WXXT__)
607 static void wx_socket_read(XtPointer client, int *fid,
608 XtInputId *WXUNUSED(id))
609 #define fd *fid
610 #else
611 static void wx_socket_read(gpointer client, gint fd,
612 GdkInputCondition WXUNUSED(cond))
613 #define fd fd
614 #endif
615 {
616 wxSocketBase *sock = (wxSocketBase *)client;
617 char c;
618 int i;
619
620 i = recv(fd, &c, 1, MSG_PEEK);
621
622 if (i == -1 && (sock->NeededReq() & wxSocketBase::REQ_ACCEPT)) {
623 sock->OnRequest(wxSocketBase::EVT_ACCEPT);
624 return;
625 }
626
627 if (i != 0) {
628 if (!(sock->NeededReq() & wxSocketBase::REQ_READ))
629 return;
630
631 sock->OnRequest(wxSocketBase::EVT_READ);
632 } else {
633 if (!(sock->NeededReq() & wxSocketBase::REQ_LOST)) {
634 sock->Close();
635 return;
636 }
637
638 sock->OnRequest(wxSocketBase::EVT_LOST);
639 }
640 }
641 #undef fd
642
643 #if defined(__WXMOTIF__) || defined(__WXXT__)
644 static void wx_socket_write(XtPointer client, int *WXUNUSED(fid),
645 XtInputId *WXUNUSED(id))
646 #else
647 static void wx_socket_write(gpointer client, gint WXUNUSED(fd),
648 GdkInputCondition WXUNUSED(cond))
649 #endif
650 {
651 wxSocketBase *sock = (wxSocketBase *)client;
652
653 if (!sock->IsConnected())
654 sock->OnRequest(wxSocketBase::EVT_CONNECT);
655 else
656 sock->OnRequest(wxSocketBase::EVT_WRITE);
657 }
658 #endif
659
660 #ifdef wx_xview
661 Notify_value wx_sock_read_xview (Notify_client client, int fd)
662 {
663 wxSocketBase *sock = (wxSocketBase *)client;
664 char c;
665 int i;
666
667 i = recv(fd, &c, 1, MSG_PEEK);
668
669 if (i == -1 && (sock->NeededReq() & wxSocketBase::REQ_ACCEPT)) {
670 sock->OnRequest(wxSocketBase::EVT_ACCEPT);
671 return;
672 }
673
674 /* Bytes arrived */
675 if (i != 0) {
676 if (!(sock->NeededReq() & wxSocketBase::REQ_READ))
677 return (Notify_value) FALSE;
678
679 sock->OnRequest(wxSocketBase::EVT_READ);
680 } else {
681 if (!(sock->NeededReq() & wxSocketBase::REQ_LOST))
682 return;
683
684 sock->OnRequest(wxSocketBase::EVT_LOST);
685 }
686
687 return (Notify_value) FALSE;
688 }
689
690 Notify_value wx_sock_write_xview (Notify_client client, int fd)
691 {
692 wxSocketBase *sock = (wxSocketBase *)client;
693
694 if (!sock->IsConnected())
695 sock->OnRequest(wxSocketBase::EVT_CONNECT);
696 else
697 sock->OnRequest(wxSocketBase::EVT_WRITE);
698
699 return (Notify_value) TRUE;
700 }
701 #endif
702
703 wxSocketBase::wxRequestNotify wxSocketBase::EventToNotify(wxRequestEvent evt)
704 {
705 switch (evt)
706 {
707 case EVT_READ:
708 return REQ_READ;
709 case EVT_PEEK:
710 return REQ_PEEK;
711 case EVT_WRITE:
712 return REQ_WRITE;
713 case EVT_LOST:
714 return REQ_LOST;
715 case EVT_ACCEPT:
716 return REQ_ACCEPT;
717 case EVT_CONNECT:
718 return REQ_CONNECT;
719 }
720 return 0;
721 }
722
723 void wxSocketBase::SetFlags(wxSockFlags _flags)
724 {
725 m_flags = _flags;
726 if (_flags & SPEED)
727 {
728 unsigned long flag = 0;
729 ioctl(m_fd, FIONBIO, &flag);
730
731 // SPEED and WAITALL are antagonists.
732 m_flags = (wxSockFlags)(m_flags & ~WAITALL);
733
734 Notify(FALSE);
735 }
736 else
737 {
738 unsigned long flag = 1;
739 ioctl(m_fd, FIONBIO, &flag);
740 }
741 }
742
743 void wxSocketBase::SetNotify(wxRequestNotify flags)
744 {
745 wxRequestNotify old_needed_req = m_neededreq;
746 if (flags & REQ_ACCEPT)
747 {
748 /* Check if server */
749 if (!(GetClassInfo()->IsKindOf(CLASSINFO(wxSocketServer))))
750 flags &= ~REQ_ACCEPT;
751 }
752 m_neededreq = flags;
753
754 /*
755 if (m_cbkon && old_needed_req != flags) seems to be wrong, Robert Roebling
756 SetupCallbacks();
757 */
758
759 if ((!m_cbkon) || (old_needed_req != flags))
760 SetupCallbacks();
761 }
762
763 void wxSocketBase::SetupCallbacks()
764 {
765 if (m_fd == INVALID_SOCKET || !m_handler || (m_flags & SPEED))
766 return;
767
768 #if defined(__WXMOTIF__) || defined(__WXXT__) || defined(__WXGTK__)
769 if (m_cbkon)
770 DestroyCallbacks();
771 if (m_neededreq & (REQ_ACCEPT | REQ_READ | REQ_LOST)) {
772 #ifdef __WXGTK__
773 m_internal->sock_inputid = gdk_input_add(m_fd, GDK_INPUT_READ,
774 wx_socket_read, (gpointer)this);
775 #else
776 m_internal->sock_inputid = XtAppAddInput (wxAPP_CONTEXT, m_fd,
777 (XtPointer *) XtInputReadMask,
778 (XtInputCallbackProc) wx_socket_read,
779 (XtPointer) this);
780 #endif
781 }
782 if (m_neededreq & (REQ_CONNECT | REQ_WRITE)) {
783 #ifdef __WXGTK__
784 m_internal->sock_outputid = gdk_input_add(m_fd, GDK_INPUT_WRITE,
785 wx_socket_write, (gpointer)this);
786 #else
787 m_internal->sock_outputid = XtAppAddInput (wxAPP_CONTEXT, m_fd,
788 (XtPointer *) XtInputWriteMask,
789 (XtInputCallbackProc) wx_socket_write,
790 (XtPointer) this);
791 #endif
792 }
793 #endif
794 #ifdef __WINDOWS__
795 WORD mask = 0;
796
797 if (m_neededreq & REQ_READ)
798 mask |= FD_READ;
799 if (m_neededreq & REQ_WRITE)
800 mask |= FD_WRITE;
801 if (m_neededreq & REQ_LOST)
802 mask |= FD_CLOSE;
803 if (m_neededreq & REQ_ACCEPT)
804 mask |= FD_ACCEPT;
805 if (m_neededreq & REQ_CONNECT)
806 mask |= FD_CONNECT;
807
808 if (!m_internal->my_msg)
809 m_internal->my_msg = m_handler->NewMessage(this);
810 WSAAsyncSelect(m_fd, m_handler->GetHWND(), m_internal->my_msg, mask);
811 #endif
812 m_cbkon = TRUE;
813 m_processing = FALSE;
814 }
815
816 void wxSocketBase::DestroyCallbacks()
817 {
818 if (!m_cbkon || !m_handler)
819 return;
820
821 m_cbkon = FALSE;
822 m_processing = FALSE;
823 #if defined(__WXMOTIF__) || defined(__WXXT__)
824 if (m_internal->sock_inputid > 0)
825 XtRemoveInput(m_internal->sock_inputid);
826 m_internal->sock_inputid = 0;
827 if (m_internal->sock_outputid > 0)
828 XtRemoveInput(m_internal->sock_outputid);
829 m_internal->sock_outputid = 0;
830 #endif
831 #ifdef __WXGTK__
832 if (m_internal->sock_inputid > 0)
833 gdk_input_remove(m_internal->sock_inputid);
834 m_internal->sock_inputid = 0;
835 if (m_internal->sock_outputid > 0)
836 gdk_input_remove(m_internal->sock_outputid);
837 m_internal->sock_outputid = 0;
838 #endif
839 #ifdef __WINDOWS__
840 WSAAsyncSelect(m_fd, m_handler->GetHWND(), 0, 0);
841 #endif
842 }
843
844 void wxSocketBase::Notify(bool notify)
845 {
846 if (m_notifyme == notify)
847 return;
848 if (notify)
849 SetupCallbacks();
850 else
851 DestroyCallbacks();
852 m_notifyme = notify;
853 }
854
855 void wxSocketBase::OnRequest(wxRequestEvent req_evt)
856 {
857 wxRequestNotify req_notif = EventToNotify(req_evt);
858
859 // Mask the current event
860 SetNotify(m_neededreq & ~req_notif);
861
862 if (req_evt <= EVT_WRITE && DoRequests(req_evt))
863 return;
864
865 if (m_waitflags & 0xF0)
866 {
867 // Wake up
868 if ((m_waitflags & 0x0F) == req_evt)
869 {
870 m_waitflags = 0x80;
871 #ifndef __WXGTK__
872 DestroyCallbacks(); // I disable it to prevent infinite loop on X11.
873 #endif
874 }
875 return;
876 }
877
878 if (req_evt == EVT_LOST)
879 {
880 m_connected = FALSE;
881 Close();
882 }
883 if (m_notifyme)
884 OldOnNotify(req_evt);
885
886 // Unmask
887 SetNotify(m_neededreq | req_notif);
888 }
889
890 wxSocketEvent::wxSocketEvent(int id)
891 : wxEvent(id)
892 {
893 wxEventType type = (wxEventType)wxEVT_SOCKET;
894
895 SetEventType(type);
896 }
897
898 void wxSocketBase::OldOnNotify(wxRequestEvent evt)
899 {
900 wxSocketEvent event(m_id);
901
902 event.SetEventObject(this);
903 event.m_skevt = evt;
904 ProcessEvent(event);
905
906 if (m_cbk)
907 m_cbk(*this, evt, m_cdata);
908 }
909
910 // --------------------------------------------------------------
911 // --------- wxSocketBase functions [Callback, CallbackData] ----
912 // --------------------------------------------------------------
913
914 wxSocketBase::wxSockCbk wxSocketBase::Callback(wxSocketBase::wxSockCbk _cbk)
915 {
916 wxSockCbk old_cbk = m_cbk;
917
918 m_cbk = _cbk;
919 return old_cbk;
920 }
921
922 char *wxSocketBase::CallbackData(char *cdata_)
923 {
924 char *old_cdata = m_cdata;
925
926 m_cdata = cdata_;
927 return old_cdata;
928 }
929
930 void wxSocketBase::SetEventHandler(wxEvtHandler& h_evt, int id)
931 {
932 SetNextHandler(&h_evt);
933 m_id = id;
934 }
935
936 // --------------------------------------------------------------
937 // --------- wxSocketBase pushback library ----------------------
938 // --------------------------------------------------------------
939
940 void wxSocketBase::CreatePushbackAfter(const char *buffer, size_t size)
941 {
942 char *curr_pos;
943
944 m_unread = (char *) realloc(m_unread, m_unrd_size+size);
945 curr_pos = m_unread + m_unrd_size;
946
947 memcpy(curr_pos, buffer, size);
948 m_unrd_size += size;
949 }
950
951 void wxSocketBase::CreatePushbackBefore(const char *buffer, size_t size)
952 {
953 char *curr_pos, *new_buf;
954
955 new_buf = (char *) malloc(m_unrd_size+size);
956 curr_pos = new_buf + size;
957
958 memcpy(new_buf, buffer, size);
959 memcpy(curr_pos, m_unread, m_unrd_size);
960
961 free(m_unread);
962 m_unread = new_buf;
963 m_unrd_size += size;
964 }
965
966 size_t wxSocketBase::GetPushback(char *buffer, size_t size, bool peek)
967 {
968 if (!m_unrd_size)
969 return 0;
970
971 if (size > m_unrd_size)
972 size = m_unrd_size;
973 memcpy(buffer, m_unread, size);
974
975 if (!peek) {
976 m_unrd_size -= size;
977 if (!m_unrd_size) {
978 free(m_unread);
979 m_unread = NULL;
980 }
981 }
982
983 return size;
984 }
985
986 // --------------------------------------------------------------
987 // --------- wxSocketBase "multi-thread" core -------------------
988 // --------------------------------------------------------------
989
990 bool wxSocketBase::DoRequests(wxRequestEvent req_flag)
991 {
992 wxNode *node = req_list[req_flag].First();
993 size_t len;
994 int ret;
995
996 if (!node)
997 return FALSE;
998
999 SockRequest *req = (SockRequest *)node->Data();
1000
1001 delete node;
1002
1003 switch (req->type)
1004 {
1005 case EVT_READ:
1006 case EVT_PEEK:
1007 ret = recv(m_fd, req->buffer, req->size,
1008 (req->type == EVT_PEEK) ? MSG_PEEK : 0);
1009 if (ret < 0) {
1010 req->error = errno;
1011 req->done = TRUE;
1012 break;
1013 }
1014 len = ret;
1015 if ((len < req->size) && (m_flags & WAITALL)) {
1016 req->size -= len;
1017 req->nbytes += len;
1018 req->buffer += len;
1019 req->auto_wakeup->Start(m_timeout*1000, TRUE);
1020 req_list[req_flag].Insert(req);
1021 break;
1022 }
1023 req->done = TRUE;
1024 req->nbytes += len;
1025 #ifndef __WXGTK__
1026 DestroyCallbacks();
1027 #endif
1028 break;
1029 case EVT_WRITE:
1030 ret = send(m_fd, req->buffer, req->size, 0);
1031 if (ret < 0) {
1032 req->error = errno;
1033 req->done = TRUE;
1034 break;
1035 }
1036 len = ret;
1037 if ((len < req->size) && (m_flags & WAITALL)) {
1038 req->size -= len;
1039 req->nbytes += len;
1040 req->buffer += len;
1041 req->auto_wakeup->Start(m_timeout*1000, TRUE);
1042 req_list[req_flag].Insert(req);
1043 break;
1044 }
1045 req->done = TRUE;
1046 req->nbytes += len;
1047 #ifndef __WXGTK__
1048 DestroyCallbacks();
1049 #endif
1050 break;
1051 default:
1052 return FALSE;
1053 }
1054 return TRUE;
1055 }
1056
1057 void wxSocketBase::WantSpeedBuffer(char *buffer, size_t nbytes,
1058 wxRequestEvent evt)
1059 {
1060 int ret = 0;
1061
1062 switch (evt) {
1063 case EVT_PEEK:
1064 case EVT_READ:
1065 ret = recv(m_fd, buffer, nbytes,
1066 (evt == EVT_PEEK) ? MSG_PEEK : 0);
1067 break;
1068 case EVT_WRITE:
1069 ret = send(m_fd, buffer, nbytes, 0);
1070 break;
1071 }
1072 if (ret < 0) {
1073 m_lcount = 0;
1074 m_error = errno;
1075 } else {
1076 m_lcount = ret;
1077 m_error = 0;
1078 }
1079 }
1080
1081 void wxSocketBase::WantBuffer(char *buffer, size_t nbytes,
1082 wxRequestEvent evt)
1083 {
1084 bool buf_timed_out;
1085
1086 if (m_fd == INVALID_SOCKET || !m_handler || !m_connected)
1087 return;
1088
1089 if (m_flags & SPEED) {
1090 WantSpeedBuffer(buffer, nbytes, evt);
1091 return;
1092 }
1093
1094 SockRequest *buf = new SockRequest;
1095 wxSockWakeUp s_wake(NULL, (int *)&buf_timed_out, (int)TRUE);
1096
1097 m_wantbuf++;
1098 req_list[evt].Append(buf);
1099
1100 SaveState();
1101 SetNotify(REQ_LOST | EventToNotify(evt));
1102 SetupCallbacks();
1103 buf->buffer = buffer;
1104 buf->size = nbytes;
1105 buf->done = FALSE;
1106 buf->type = evt;
1107 buf->nbytes = 0;
1108 buf->auto_wakeup = &s_wake;
1109 buf->error = 0;
1110 buf_timed_out = FALSE;
1111
1112 s_wake.Start(m_timeout*1000, TRUE);
1113 if (m_flags & NOWAIT)
1114 {
1115 DoRequests(evt);
1116 }
1117 else
1118 {
1119 while (!buf->done && !buf_timed_out)
1120 PROCESS_EVENTS();
1121 }
1122 m_wantbuf--;
1123 m_lcount = buf->nbytes;
1124 if (buf_timed_out)
1125 m_error = ETIMEDOUT;
1126 else
1127 m_error = buf->error;
1128
1129 delete buf;
1130 RestoreState();
1131 }
1132
1133 // --------------------------------------------------------------
1134 // wxSocketServer ///////////////////////////////////////////////
1135 // --------------------------------------------------------------
1136
1137 // --------------------------------------------------------------
1138 // --------- wxSocketServer CONSTRUCTOR -------------------------
1139 // --------------------------------------------------------------
1140 wxSocketServer::wxSocketServer(wxSockAddress& addr_man,
1141 wxSockFlags flags) :
1142 wxSocketBase(flags, SOCK_SERVER)
1143 {
1144 m_fd = socket(addr_man.GetFamily(), SOCK_STREAM, 0);
1145
1146 if (m_fd == INVALID_SOCKET)
1147 return;
1148
1149 int flag = 1;
1150 setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(int));
1151
1152 struct sockaddr *myaddr;
1153 size_t len;
1154
1155 addr_man.Build(myaddr, len);
1156 if (bind(m_fd, myaddr, addr_man.SockAddrLen()) < 0)
1157 return;
1158
1159 if (listen(m_fd, 5) < 0) {
1160 m_fd = INVALID_SOCKET;
1161 return;
1162 }
1163 }
1164
1165 // --------------------------------------------------------------
1166 // --------- wxSocketServer Accept ------------------------------
1167 // --------------------------------------------------------------
1168 bool wxSocketServer::AcceptWith(wxSocketBase& sock)
1169 {
1170 int fd2;
1171
1172 if ((fd2 = accept(m_fd, 0, 0)) < 0)
1173 return FALSE;
1174
1175 struct linger linger;
1176 linger.l_onoff = 0;
1177 linger.l_linger = 1;
1178
1179 setsockopt(fd2, SOL_SOCKET, SO_LINGER, (char*)&linger, sizeof(linger));
1180
1181 int flag = 0;
1182 setsockopt(fd2, SOL_SOCKET, SO_KEEPALIVE, (char*)&flag, sizeof(int));
1183
1184 if (!(sock.m_flags & SPEED)) {
1185 unsigned long flag2 = 1;
1186 ioctl(fd2, FIONBIO, &flag2);
1187 }
1188
1189 sock.m_type = SOCK_INTERNAL;
1190 sock.m_fd = fd2;
1191 sock.m_connected = TRUE;
1192
1193 return TRUE;
1194 }
1195
1196 wxSocketBase *wxSocketServer::Accept()
1197 {
1198 wxSocketBase* sock = new wxSocketBase();
1199
1200 sock->SetFlags((wxSockFlags)m_flags);
1201
1202 if (!AcceptWith(*sock))
1203 return NULL;
1204
1205 if (m_handler)
1206 m_handler->Register(sock);
1207
1208 return sock;
1209 }
1210
1211 // --------------------------------------------------------------
1212 // --------- wxSocketServer callbacks ---------------------------
1213 // --------------------------------------------------------------
1214 void wxSocketServer::OnRequest(wxRequestEvent evt)
1215 {
1216 if (evt == EVT_ACCEPT) {
1217 OldOnNotify(EVT_ACCEPT);
1218 }
1219 }
1220
1221 // --------------------------------------------------------------
1222 // wxSocketClient ///////////////////////////////////////////////
1223 // --------------------------------------------------------------
1224
1225 // --------- wxSocketClient CONSTRUCTOR -------------------------
1226 // --------------------------------------------------------------
1227 wxSocketClient::wxSocketClient(wxSockFlags _flags) :
1228 wxSocketBase(_flags, SOCK_CLIENT)
1229 {
1230 }
1231
1232 // --------------------------------------------------------------
1233 // --------- wxSocketClient DESTRUCTOR --------------------------
1234 // --------------------------------------------------------------
1235 wxSocketClient::~wxSocketClient()
1236 {
1237 }
1238
1239 // --------------------------------------------------------------
1240 // --------- wxSocketClient Connect functions -------------------
1241 // --------------------------------------------------------------
1242 bool wxSocketClient::Connect(wxSockAddress& addr_man, bool WXUNUSED(wait) )
1243 {
1244 struct linger linger;
1245
1246 if (IsConnected())
1247 Close();
1248
1249 m_fd = socket(addr_man.GetFamily(), SOCK_STREAM, 0);
1250
1251 if (m_fd < 0)
1252 return FALSE;
1253
1254 m_connected = FALSE;
1255
1256 linger.l_onoff = 1;
1257 linger.l_linger = 5;
1258 setsockopt(m_fd, SOL_SOCKET, SO_LINGER, (char*)&linger, sizeof(linger));
1259
1260 // Stay in touch with the state of things...
1261
1262 unsigned long flag = 1;
1263 setsockopt(m_fd, SOL_SOCKET, SO_KEEPALIVE, (char*)&flag, sizeof(int));
1264
1265 // Disable the nagle algorithm, which delays sends till the
1266 // buffer is full (or a certain time period has passed?)...
1267
1268 #if defined(__WINDOWS__) || (defined(IPPROTO_TCP) && defined(TCP_NODELAY))
1269 flag = 1;
1270 setsockopt(m_fd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(int));
1271 #endif
1272
1273 struct sockaddr *remote;
1274 size_t len;
1275
1276 addr_man.Build(remote, len);
1277
1278 if (connect(m_fd, remote, len) != 0)
1279 return FALSE;
1280
1281 if (!(m_flags & SPEED)) {
1282 flag = 1;
1283 ioctl(m_fd, FIONBIO, &flag);
1284 }
1285
1286 Notify(TRUE);
1287
1288 m_connected = TRUE;
1289 return TRUE;
1290 }
1291
1292 bool wxSocketClient::WaitOnConnect(long seconds, long microseconds)
1293 {
1294 int ret = _Wait(seconds, microseconds, REQ_CONNECT | REQ_LOST);
1295
1296 if (ret)
1297 m_connected = TRUE;
1298
1299 return m_connected;
1300 }
1301
1302 void wxSocketClient::OnRequest(wxRequestEvent evt)
1303 {
1304 if (evt == EVT_CONNECT) {
1305 if (m_connected) {
1306 SetNotify(m_neededreq & ~REQ_CONNECT);
1307 return;
1308 }
1309 m_waitflags = 0x40;
1310 m_connected = TRUE;
1311 OldOnNotify(EVT_CONNECT);
1312 DestroyCallbacks();
1313 return;
1314 }
1315 wxSocketBase::OnRequest(evt);
1316 }
1317
1318 /////////////////////////////////////////////////////////////////
1319 // wxSocketHandler ///////////////////////////////////////////////
1320 /////////////////////////////////////////////////////////////////
1321
1322 wxSocketHandler *wxSocketHandler::master = NULL;
1323 #if defined(__WINDOWS__)
1324 static int win_initialized = 0;
1325 #endif
1326
1327 // --------------------------------------------------------------
1328 // --------- wxSocketHandler CONSTRUCTOR ------------------------
1329 // --------------------------------------------------------------
1330 #ifdef __WINDOWS__
1331
1332 extern char wxPanelClassName[];
1333
1334 LRESULT APIENTRY _EXPORT wxSocketHandlerWndProc(HWND hWnd, UINT message,
1335 WPARAM wParam, LPARAM lParam)
1336 {
1337 if(message==WM_DESTROY)
1338 {
1339 ::SetWindowLong(hWnd, GWL_WNDPROC, (LONG) DefWindowProc);
1340 return DefWindowProc(hWnd, message, wParam, lParam);
1341 }
1342 wxSocketHandler *h_sock = (wxSocketHandler *)GetWindowLong(hWnd, GWL_USERDATA);
1343 wxNode *node = h_sock->smsg_list->Find(message);
1344 wxSocketBase *sock;
1345 wxSocketBase::wxRequestEvent sk_req;
1346 UINT event = WSAGETSELECTEVENT(lParam);
1347
1348 if (!node)
1349 return DefWindowProc(hWnd, message, wParam, lParam);
1350
1351 sock = (wxSocketBase *)node->Data();
1352
1353 switch (event) {
1354 case FD_READ:
1355 sk_req = wxSocketBase::EVT_READ;
1356 break;
1357 case FD_WRITE:
1358 sk_req = wxSocketBase::EVT_WRITE;
1359 break;
1360 case FD_CLOSE:
1361 sk_req = wxSocketBase::EVT_LOST;
1362 break;
1363 case FD_ACCEPT:
1364 sk_req = wxSocketBase::EVT_ACCEPT;
1365 break;
1366 case FD_CONNECT:
1367 sk_req = wxSocketBase::EVT_CONNECT;
1368 break;
1369 }
1370 sock->OnRequest(sk_req);
1371
1372 return (LRESULT)0;
1373 }
1374
1375 FARPROC wxSocketSubClassProc = NULL;
1376
1377 #endif
1378
1379 wxSocketHandler::wxSocketHandler()
1380 {
1381 #if defined(__WINDOWS__)
1382 if (!win_initialized) {
1383 WSADATA wsaData;
1384
1385 WSAStartup((1 << 8) | 1, &wsaData);
1386 win_initialized = 1;
1387 }
1388 internal = new wxSockHandlerInternal;
1389 internal->sockWin = ::CreateWindow(wxPanelClassName, NULL, 0,
1390 0, 0, 0, 0, NULL, (HMENU) NULL,
1391 wxhInstance, 0);
1392
1393 // Subclass the window
1394 if (!wxSocketSubClassProc)
1395 wxSocketSubClassProc = MakeProcInstance((FARPROC) wxSocketHandlerWndProc, wxhInstance);
1396 ::SetWindowLong(internal->sockWin, GWL_WNDPROC, (LONG) wxSocketSubClassProc);
1397 ::SetWindowLong(internal->sockWin, GWL_USERDATA, (LONG) this);
1398
1399 internal->firstAvailableMsg = 5000;
1400 smsg_list = new wxList(wxKEY_INTEGER);
1401 #endif
1402
1403 socks = new wxList;
1404
1405 #ifndef __WINDOWS__
1406 signal(SIGPIPE, SIG_IGN);
1407 #endif
1408 }
1409
1410 // --------------------------------------------------------------
1411 // --------- wxSocketHandler DESTRUCTOR -------------------------
1412 // --------------------------------------------------------------
1413 wxSocketHandler::~wxSocketHandler()
1414 {
1415 wxNode *next_node, *node = socks->First();
1416
1417 while (node) {
1418 wxSocketBase* sock = (wxSocketBase*)node->Data();
1419
1420 delete sock;
1421 next_node = node->Next();
1422 delete node;
1423 node = next_node;
1424 }
1425
1426 delete socks;
1427
1428 #ifdef __WINDOWS__
1429 delete smsg_list;
1430
1431 ::DestroyWindow(internal->sockWin);
1432 WSACleanup();
1433 win_initialized = 0;
1434
1435 delete internal;
1436 #endif
1437 }
1438
1439 // --------------------------------------------------------------
1440 // --------- wxSocketHandler registering functions --------------
1441 // --------------------------------------------------------------
1442 void wxSocketHandler::Register(wxSocketBase* sock)
1443 {
1444 wxNode *node;
1445
1446 for (node = socks->First(); node != NULL; node = node->Next()) {
1447 wxSocketBase* s = (wxSocketBase*)node->Data();
1448
1449 if (s == sock)
1450 return;
1451 }
1452
1453 if (sock) {
1454 socks->Append(sock);
1455 sock->SetHandler(this);
1456 sock->SetupCallbacks();
1457 }
1458 }
1459
1460 void wxSocketHandler::UnRegister(wxSocketBase* sock)
1461 {
1462 wxNode *node;
1463
1464 for (node = socks->First(); node; node = node->Next()) {
1465 wxSocketBase* s = (wxSocketBase*)node->Data();
1466
1467 if (s == sock) {
1468 delete node;
1469 sock->DestroyCallbacks();
1470 sock->SetHandler(NULL);
1471 return;
1472 }
1473 }
1474 }
1475
1476 unsigned long wxSocketHandler::Count() const
1477 {
1478 return socks->Number();
1479 }
1480
1481 // --------------------------------------------------------------
1482 // --------- wxSocketHandler "big" wait functions ---------------
1483 // --------------------------------------------------------------
1484 void handler_cbk(wxSocketBase& sock,
1485 wxSocketBase::wxRequestEvent WXUNUSED(flags),
1486 char *cdata)
1487 {
1488 int *a_wait = (int *)cdata;
1489
1490 (*a_wait)++;
1491 sock.Notify(FALSE);
1492 }
1493
1494 int wxSocketHandler::Wait(long seconds, long microseconds)
1495 {
1496 int i;
1497 int on_wait;
1498 wxSockWakeUp s_wake(NULL, &on_wait, -2);
1499 wxNode *node;
1500
1501 for (node = socks->First(), i=0; node; node = node->Next(), i++) {
1502 wxSocketBase *sock = (wxSocketBase *)node->Data();
1503
1504 sock->SaveState();
1505
1506 sock->SetupCallbacks();
1507
1508 sock->Callback(handler_cbk);
1509 sock->CallbackData((char *)&on_wait);
1510 }
1511 on_wait = 0;
1512 if (seconds != -1)
1513 s_wake.Start((seconds*1000) + (microseconds/1000), TRUE);
1514
1515 while (!on_wait)
1516 PROCESS_EVENTS();
1517
1518 for (node = socks->First(), i=0; node; node = node->Next(), i++) {
1519 wxSocketBase *sock = (wxSocketBase *)node->Data();
1520
1521 sock->RestoreState();
1522 }
1523
1524 if (on_wait == -2)
1525 return 0;
1526
1527 return on_wait;
1528 }
1529
1530 void wxSocketHandler::YieldSock()
1531 {
1532 wxNode *node;
1533
1534 for (node = socks->First(); node; node = node->Next() ) {
1535 wxSocketBase *sock = (wxSocketBase *)node->Data();
1536
1537 sock->SaveState();
1538
1539 sock->SetFlags(wxSocketBase::SPEED);
1540 if (sock->IsData())
1541 sock->DoRequests(wxSocketBase::EVT_READ);
1542 sock->DoRequests(wxSocketBase::EVT_WRITE);
1543
1544 sock->RestoreState();
1545 }
1546 }
1547
1548 // --------------------------------------------------------------
1549 // --------- wxSocketHandler: create and register the socket ----
1550 // --------------------------------------------------------------
1551 wxSocketServer *wxSocketHandler::CreateServer(wxSockAddress& addr,
1552 wxSocketBase::wxSockFlags flags)
1553 {
1554 wxSocketServer *serv = new wxSocketServer(addr, flags);
1555
1556 Register(serv);
1557 return serv;
1558 }
1559
1560 wxSocketClient *wxSocketHandler::CreateClient(wxSocketBase::wxSockFlags flags)
1561 {
1562 wxSocketClient *client = new wxSocketClient(flags);
1563
1564 Register(client);
1565 return client;
1566 }
1567
1568 #ifdef __WINDOWS__
1569 // --------------------------------------------------------------
1570 // --------- wxSocketHandler: Windows specific methods ----------
1571 // --------------------------------------------------------------
1572 UINT wxSocketHandler::NewMessage(wxSocketBase *sock)
1573 {
1574 internal->firstAvailableMsg++;
1575 smsg_list->Append(internal->firstAvailableMsg, sock);
1576 return internal->firstAvailableMsg;
1577 }
1578
1579 void wxSocketHandler::DestroyMessage(UINT msg)
1580 {
1581 wxNode *node = smsg_list->Find(msg);
1582 delete node;
1583 }
1584
1585 HWND wxSocketHandler::GetHWND() const
1586 {
1587 return internal->sockWin;
1588 }
1589
1590 #endif
1591
1592 bool wxSocketModule::OnInit() {
1593 wxSocketHandler::master = new wxSocketHandler();
1594 return TRUE;
1595 }
1596
1597 void wxSocketModule::OnExit() {
1598 delete wxSocketHandler::master;
1599 wxSocketHandler::master = NULL;
1600 }
1601
1602 #endif
1603 // __WXSTUBS__