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