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