]> git.saurik.com Git - wxWidgets.git/blame - src/common/socket.cpp
* Fixed a bug in notebook.tex
[wxWidgets.git] / src / common / socket.cpp
CommitLineData
f4ada568
GL
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////////////////////////////////////////////////////////////////////////////////
384b4373 12#ifdef __GNUG__
f4ada568 13#pragma implementation "socket.h"
0c32066b
JS
14// #pragma interface
15// #pragma implementation "socket.cpp"
f4ada568
GL
16#endif
17
fcc6dddd
JS
18// For compilers that support precompilation, includes "wx.h".
19#include "wx/wxprec.h"
20
21#ifdef __BORLANDC__
22#pragma hdrstop
23#endif
24
f4ada568
GL
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
7f555861
JS
34// Not enough OS behaviour defined for wxStubs
35#ifndef __WXSTUBS__
36
f4ada568
GL
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
384b4373
RD
60#ifdef sun
61#include <sys/filio.h>
62#endif
63
f4ada568
GL
64#endif // __UNIX__
65
66#include <signal.h>
67#include <errno.h>
68
fcc6dddd
JS
69#ifdef _MSC_VER
70#include <io.h>
71#endif
72
f4ada568
GL
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
f4ada568
GL
96/////////////////////////////////////////////////////////////////////////////
97// Some patch ///// BEGIN
98/////////////////////////////////////////////////////////////////////////////
99#ifdef __WINDOWS__
100#define close closesocket
101#define ioctl ioctlsocket
0c32066b
JS
102#ifdef errno
103#undef errno
104#endif
f4ada568
GL
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__
46ccb510 120#define wxAPP_CONTEXT ((XtAppContext)wxTheApp->GetAppContext())
f4ada568
GL
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 )
129int 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
160IMPLEMENT_CLASS(wxSocketBase, wxObject)
161IMPLEMENT_CLASS(wxSocketServer, wxSocketBase)
162IMPLEMENT_CLASS(wxSocketClient, wxSocketBase)
163IMPLEMENT_CLASS(wxSocketHandler, wxObject)
164IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent, wxEvent)
165#endif
166
167class wxSockWakeUp : public wxTimer {
168public:
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
184class SockRequest : public wxObject {
185public:
186 char *buffer;
187 size_t size, nbytes;
188 bool done;
189 int error;
190 wxSockWakeUp *auto_wakeup;
191 wxSocketBase::wxRequestNotify type;
192};
384b4373 193
f4ada568
GL
194
195/////////////////////////////////////////////////////////////////////////////
196// Some internal define
197/////////////////////////////////////////////////////////////////////////////
198
199// --------------------------------------------------------------
200// --------- wxSocketBase CONSTRUCTOR ---------------------------
201// --------------------------------------------------------------
202wxSocketBase::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
225wxSocketBase::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// --------------------------------------------------------------
251wxSocketBase::~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
270bool 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;
384b4373 279
f4ada568
GL
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// --------------------------------------------------------------
299wxSocketBase& 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
313wxSocketBase& 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
328wxSocketBase& wxSocketBase::Write(const char *buffer, size_t nbytes)
329{
330 WantBuffer((char *)buffer, nbytes, EVT_WRITE);
331 return *this;
332}
333
334wxSocketBase& wxSocketBase::ReadMsg(char* buffer, size_t nbytes)
335{
336 SockMsg msg;
337 size_t len, len2, sig;
384b4373 338
f4ada568
GL
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
378wxSocketBase& wxSocketBase::WriteMsg(const char *buffer, size_t nbytes)
379{
380 SockMsg msg;
384b4373 381
0c32066b
JS
382 msg.sig[0] = (char) 0xad;
383 msg.sig[1] = (char) 0xde;
384 msg.sig[2] = (char) 0xed;
385 msg.sig[3] = (char) 0xfe;
f4ada568 386
0c32066b
JS
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;
f4ada568
GL
391
392 if (Write((char *)&msg, sizeof(msg)).LastCount() < sizeof(msg))
393 return *this;
394 if (Write(buffer, nbytes).LastCount() < nbytes)
384b4373 395 return *this;
f4ada568 396
0c32066b
JS
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;
f4ada568
GL
402 Write((char *)&msg, sizeof(msg));
403
404 return *this;
405}
406
407wxSocketBase& wxSocketBase::Unread(const char *buffer, size_t nbytes)
408{
409 CreatePushbackAfter(buffer, nbytes);
410 return *this;
411}
412
413bool 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);
0c32066b 428 return (FD_ISSET(m_fd, &sock_set) != 0);
f4ada568
GL
429}
430
431// ---------------------------------------------------------------------
432// --------- wxSocketBase Discard(): deletes all byte in the input queue
433// ---------------------------------------------------------------------
434void 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));
384b4373 442
f4ada568
GL
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// --------------------------------------------------------------
456bool 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
f4ada568 464 if (getpeername(m_fd, (struct sockaddr *)&my_addr, (int *)&len_addr) < 0)
384b4373 465 return FALSE;
f4ada568
GL
466
467 addr_man.Disassemble(&my_addr, len_addr);
468 return TRUE;
469}
470
471bool 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
f4ada568 479 if (getsockname(m_fd, (struct sockaddr *)&my_addr, (int *)&len_addr) < 0)
f4ada568
GL
480 return FALSE;
481
482 addr_man.Disassemble(&my_addr, len_addr);
483 return TRUE;
484}
485
486// --------------------------------------------------------------
487// --------- wxSocketBase wait functions ------------------------
488// --------------------------------------------------------------
489void 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
503void wxSocketBase::RestoreState()
504{
505 wxNode *node;
506
507 node = m_states.Last();
508 if (!node)
509 return;
510
511 wxSockState *state = (wxSockState *)node->Data();
384b4373 512
f4ada568
GL
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//
531bool 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);
384b4373 544
f4ada568
GL
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
560bool 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
566bool wxSocketBase::WaitForRead(long seconds, long microseconds)
567{
568 return _Wait(seconds, microseconds, REQ_READ | REQ_LOST);
569}
570
571bool wxSocketBase::WaitForWrite(long seconds, long microseconds)
572{
573 return _Wait(seconds, microseconds, REQ_WRITE);
574}
575
576bool 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__)
587static void wx_socket_read(XtPointer client, int *fid,
588 XtInputId *WXUNUSED(id))
589#define fd *fid
590#else
591static 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__)
624static void wx_socket_write(XtPointer client, int *WXUNUSED(fid),
625 XtInputId *WXUNUSED(id))
626#else
627static 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
641Notify_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
670Notify_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
683wxSocketBase::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
702void 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
719void 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
732void 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
785void 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
812void 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
823void 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
855wxSocketEvent::wxSocketEvent(int id)
856 : wxEvent(id)
857{
858 wxEventType type = (wxEventType)wxEVT_SOCKET;
859
860 SetEventType(type);
861}
862
863void 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// --------------------------------------------------------------
878wxSocketBase::wxSockCbk wxSocketBase::Callback(wxSocketBase::wxSockCbk _cbk)
879{
880 wxSockCbk old_cbk = m_cbk;
881
882 m_cbk = _cbk;
883 return old_cbk;
884}
885
886char *wxSocketBase::CallbackData(char *cdata_)
887{
888 char *old_cdata = m_cdata;
889
890 m_cdata = cdata_;
891 return old_cdata;
892}
893
894void wxSocketBase::SetEventHandler(wxEvtHandler& h_evt, int id)
895{
896 SetNextHandler(&h_evt);
897 m_id = id;
898}
899
900// --------------------------------------------------------------
901// --------- wxSocketBase pushback library ----------------------
902// --------------------------------------------------------------
903void 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
914void 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
929size_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
953bool 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
1019void wxSocketBase::WantSpeedBuffer(char *buffer, size_t nbytes,
1020 wxRequestEvent evt)
1021{
e22036dc 1022 int ret = 0;
f4ada568
GL
1023
1024 switch (evt) {
1025 case EVT_PEEK:
1026 case EVT_READ:
c740f496
GL
1027 ret = recv(m_fd, buffer, nbytes,
1028 (evt == EVT_PEEK) ? MSG_PEEK : 0);
f4ada568
GL
1029 break;
1030 case EVT_WRITE:
c740f496 1031 ret = send(m_fd, buffer, nbytes, 0);
f4ada568
GL
1032 break;
1033 }
1034 if (ret < 0) {
1035 m_lcount = 0;
1036 m_error = errno;
1037 } else
1038 m_lcount = ret;
1039}
1040
1041void wxSocketBase::WantBuffer(char *buffer, size_t nbytes,
1042 wxRequestEvent evt)
1043{
1044 bool buf_timed_out;
1045
1046 if (m_fd == INVALID_SOCKET || !m_handler || !m_connected)
1047 return;
1048
1049 if (m_flags & SPEED) {
1050 WantSpeedBuffer(buffer, nbytes, evt);
1051 return;
1052 }
1053
1054 SockRequest *buf = new SockRequest;
1055 wxSockWakeUp s_wake(NULL, (int *)&buf_timed_out, (int)TRUE);
384b4373 1056
f4ada568
GL
1057 m_wantbuf++;
1058 req_list[evt].Append(buf);
1059
1060 SaveState();
1061 SetNotify(REQ_LOST | EventToNotify(evt));
1062 SetupCallbacks();
1063 buf->buffer = buffer;
1064 buf->size = nbytes;
1065 buf->done = FALSE;
1066 buf->type = evt;
1067 buf->nbytes = 0;
1068 buf->auto_wakeup = &s_wake;
1069 buf->error = 0;
1070 buf_timed_out = FALSE;
1071
1072 s_wake.Start(m_timeout*1000, TRUE);
1073 if (m_flags & NOWAIT) {
1074 DoRequests(evt);
1075 } else {
1076 while (!buf->done && !buf_timed_out)
1077 PROCESS_EVENTS();
1078 }
1079 m_wantbuf--;
1080 m_lcount = buf->nbytes;
1081 if (buf_timed_out)
1082 m_error = ETIMEDOUT;
1083 else
1084 m_error = buf->error;
1085
1086 delete buf;
1087 RestoreState();
1088}
1089
1090// --------------------------------------------------------------
1091// wxSocketServer ///////////////////////////////////////////////
1092// --------------------------------------------------------------
1093
1094// --------------------------------------------------------------
1095// --------- wxSocketServer CONSTRUCTOR -------------------------
1096// --------------------------------------------------------------
1097wxSocketServer::wxSocketServer(wxSockAddress& addr_man,
1098 wxSockFlags flags) :
1099 wxSocketBase(flags, SOCK_SERVER)
1100{
1101 m_fd = socket(addr_man.GetFamily(), SOCK_STREAM, 0);
1102
1103 if (m_fd == INVALID_SOCKET)
1104 return;
384b4373 1105
f4ada568
GL
1106 int flag = 1;
1107 setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(int));
384b4373 1108
f4ada568
GL
1109 struct sockaddr *myaddr;
1110 size_t len;
384b4373 1111
f4ada568
GL
1112 addr_man.Build(myaddr, len);
1113 if (bind(m_fd, myaddr, addr_man.SockAddrLen()) < 0)
1114 return;
384b4373 1115
f4ada568
GL
1116 if (listen(m_fd, 5) < 0) {
1117 m_fd = INVALID_SOCKET;
1118 return;
1119 }
1120}
1121
1122// --------------------------------------------------------------
1123// --------- wxSocketServer Accept ------------------------------
1124// --------------------------------------------------------------
1125bool wxSocketServer::AcceptWith(wxSocketBase& sock)
1126{
1127 int fd2;
384b4373 1128
f4ada568
GL
1129 if ((fd2 = accept(m_fd, 0, 0)) < 0)
1130 return FALSE;
1131
1132 struct linger linger;
1133 linger.l_onoff = 0;
1134 linger.l_linger = 1;
384b4373 1135
f4ada568 1136 setsockopt(fd2, SOL_SOCKET, SO_LINGER, (char*)&linger, sizeof(linger));
384b4373 1137
f4ada568
GL
1138 int flag = 0;
1139 setsockopt(fd2, SOL_SOCKET, SO_KEEPALIVE, (char*)&flag, sizeof(int));
384b4373 1140
f4ada568
GL
1141 if (!(sock.m_flags & SPEED)) {
1142 unsigned long flag2 = 1;
1143 ioctl(fd2, FIONBIO, &flag2);
1144 }
384b4373 1145
f4ada568
GL
1146 sock.m_type = SOCK_INTERNAL;
1147 sock.m_fd = fd2;
1148 sock.m_connected = TRUE;
1149
1150 return TRUE;
1151}
1152
1153wxSocketBase *wxSocketServer::Accept()
1154{
1155 wxSocketBase* sock = new wxSocketBase();
1156
1157 sock->SetFlags((wxSockFlags)m_flags);
1158
1159 if (!AcceptWith(*sock))
1160 return NULL;
1161
1162 if (m_handler)
1163 m_handler->Register(sock);
1164
1165 return sock;
1166}
1167
1168// --------------------------------------------------------------
1169// --------- wxSocketServer callbacks ---------------------------
1170// --------------------------------------------------------------
1171void wxSocketServer::OnRequest(wxRequestEvent evt)
1172{
1173 if (evt == EVT_ACCEPT) {
1174 OldOnNotify(EVT_ACCEPT);
1175 }
1176}
1177
1178// --------------------------------------------------------------
1179// wxSocketClient ///////////////////////////////////////////////
1180// --------------------------------------------------------------
1181
1182// --------- wxSocketClient CONSTRUCTOR -------------------------
1183// --------------------------------------------------------------
1184wxSocketClient::wxSocketClient(wxSockFlags _flags) :
1185 wxSocketBase(_flags, SOCK_CLIENT)
1186{
1187}
1188
1189// --------------------------------------------------------------
1190// --------- wxSocketClient DESTRUCTOR --------------------------
1191// --------------------------------------------------------------
1192wxSocketClient::~wxSocketClient()
1193{
1194}
1195
1196// --------------------------------------------------------------
1197// --------- wxSocketClient Connect functions -------------------
1198// --------------------------------------------------------------
e22036dc 1199bool wxSocketClient::Connect(wxSockAddress& addr_man, bool WXUNUSED(wait) )
f4ada568
GL
1200{
1201 struct linger linger;
1202
1203 if (IsConnected())
1204 Close();
1205
1206 m_fd = socket(addr_man.GetFamily(), SOCK_STREAM, 0);
384b4373 1207
f4ada568
GL
1208 if (m_fd < 0)
1209 return FALSE;
384b4373 1210
f4ada568
GL
1211 m_connected = FALSE;
1212
1213 linger.l_onoff = 1;
384b4373 1214 linger.l_linger = 5;
f4ada568 1215 setsockopt(m_fd, SOL_SOCKET, SO_LINGER, (char*)&linger, sizeof(linger));
384b4373 1216
f4ada568 1217 // Stay in touch with the state of things...
384b4373 1218
f4ada568
GL
1219 unsigned long flag = 1;
1220 setsockopt(m_fd, SOL_SOCKET, SO_KEEPALIVE, (char*)&flag, sizeof(int));
384b4373 1221
f4ada568
GL
1222 // Disable the nagle algorithm, which delays sends till the
1223 // buffer is full (or a certain time period has passed?)...
1224
1225#if defined(__WINDOWS__) || (defined(IPPROTO_TCP) && defined(TCP_NODELAY))
1226 flag = 1;
1227 setsockopt(m_fd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(int));
1228#endif
384b4373 1229
f4ada568
GL
1230 struct sockaddr *remote;
1231 size_t len;
1232
1233 addr_man.Build(remote, len);
1234
1235 if (connect(m_fd, remote, len) != 0)
1236 return FALSE;
1237
1238 if (!(m_flags & SPEED)) {
1239 flag = 1;
1240 ioctl(m_fd, FIONBIO, &flag);
1241 }
384b4373 1242
f4ada568
GL
1243 Notify(TRUE);
1244
1245 m_connected = TRUE;
1246 return TRUE;
1247}
1248
1249bool wxSocketClient::WaitOnConnect(long seconds)
1250{
1251 int ret = _Wait(seconds, 0, REQ_CONNECT | REQ_LOST);
384b4373 1252
f4ada568
GL
1253 if (ret)
1254 m_connected = TRUE;
384b4373
RD
1255
1256 return m_connected;
f4ada568
GL
1257}
1258
1259void wxSocketClient::OnRequest(wxRequestEvent evt)
1260{
1261 if (evt == EVT_CONNECT) {
1262 if (m_connected) {
1263 SetNotify(m_neededreq & ~REQ_CONNECT);
1264 return;
1265 }
1266 m_waitflags = 0x40;
384b4373 1267 m_connected = TRUE;
f4ada568
GL
1268 OldOnNotify(EVT_CONNECT);
1269 DestroyCallbacks();
1270 return;
1271 }
1272 wxSocketBase::OnRequest(evt);
1273}
1274
1275/////////////////////////////////////////////////////////////////
1276// wxSocketHandler ///////////////////////////////////////////////
1277/////////////////////////////////////////////////////////////////
1278
1279wxSocketHandler *wxSocketHandler::master = NULL;
1280#if defined(__WINDOWS__)
1281static int win_initialized = 0;
1282#endif
1283
1284// --------------------------------------------------------------
1285// --------- wxSocketHandler CONSTRUCTOR ------------------------
1286// --------------------------------------------------------------
1287#ifdef __WINDOWS__
1288
1289extern char wxPanelClassName[];
1290
1291LRESULT APIENTRY _EXPORT wxSocketHandlerWndProc(HWND hWnd, UINT message,
1292 WPARAM wParam, LPARAM lParam)
1293{
1294 wxSocketHandler *h_sock = (wxSocketHandler *)GetWindowLong(hWnd, GWL_USERDATA);
1295 wxNode *node = h_sock->smsg_list->Find(message);
1296 wxSocketBase *sock;
1297 wxSocketBase::wxRequestEvent sk_req;
1298 UINT event = WSAGETSELECTEVENT(lParam);
1299
1300 if (!node)
1301 return DefWindowProc(hWnd, message, wParam, lParam);
1302
1303 sock = (wxSocketBase *)node->Data();
1304
1305 switch (event) {
1306 case FD_READ:
1307 sk_req = wxSocketBase::EVT_READ;
1308 break;
1309 case FD_WRITE:
1310 sk_req = wxSocketBase::EVT_WRITE;
1311 break;
1312 case FD_CLOSE:
1313 sk_req = wxSocketBase::EVT_LOST;
1314 break;
1315 case FD_ACCEPT:
1316 sk_req = wxSocketBase::EVT_ACCEPT;
1317 break;
1318 case FD_CONNECT:
1319 sk_req = wxSocketBase::EVT_CONNECT;
1320 break;
1321 }
1322 sock->OnRequest(sk_req);
1323
1324 return (LRESULT)0;
1325}
1326
1327FARPROC wxSocketSubClassProc = NULL;
1328
1329#endif
1330
1331wxSocketHandler::wxSocketHandler()
1332{
1333#if defined(__WINDOWS__)
1334 if (!win_initialized) {
1335 WSADATA wsaData;
1336
1337 WSAStartup((1 << 8) | 1, &wsaData);
1338 win_initialized = 1;
1339 }
1340 internal = new wxSockHandlerInternal;
1341 internal->sockWin = ::CreateWindow(wxPanelClassName, NULL, 0,
1342 0, 0, 0, 0, NULL, (HMENU) NULL,
1343 wxhInstance, 0);
1344
1345 // Subclass the window
1346 if (!wxSocketSubClassProc)
1347 wxSocketSubClassProc = MakeProcInstance((FARPROC) wxSocketHandlerWndProc, wxhInstance);
1348 ::SetWindowLong(internal->sockWin, GWL_WNDPROC, (LONG) wxSocketSubClassProc);
1349 ::SetWindowLong(internal->sockWin, GWL_USERDATA, (LONG) this);
1350
1351 internal->firstAvailableMsg = 5000;
1352 smsg_list = new wxList(wxKEY_INTEGER);
1353#endif
1354
1355 socks = new wxList;
1356
1357#ifndef __WINDOWS__
1358 signal(SIGPIPE, SIG_IGN);
1359#endif
1360}
1361
1362// --------------------------------------------------------------
1363// --------- wxSocketHandler DESTRUCTOR -------------------------
1364// --------------------------------------------------------------
1365wxSocketHandler::~wxSocketHandler()
1366{
1367 wxNode *next_node, *node = socks->First();
1368
1369 while (node) {
1370 wxSocketBase* sock = (wxSocketBase*)node->Data();
1371
1372 delete sock;
1373 next_node = node->Next();
1374 delete node;
1375 node = next_node;
1376 }
1377
1378 delete socks;
1379
1380#ifdef __WINDOWS__
1381 delete smsg_list;
1382
1383 ::DestroyWindow(internal->sockWin);
1384 WSACleanup();
1385 win_initialized = 0;
1386
1387 delete internal;
1388#endif
1389}
1390
1391// --------------------------------------------------------------
1392// --------- wxSocketHandler registering functions --------------
1393// --------------------------------------------------------------
1394void wxSocketHandler::Register(wxSocketBase* sock)
1395{
1396 wxNode *node;
1397
1398 for (node = socks->First(); node != NULL; node = node->Next()) {
1399 wxSocketBase* s = (wxSocketBase*)node->Data();
1400
1401 if (s == sock)
1402 return;
1403 }
1404
1405 if (sock) {
1406 socks->Append(sock);
1407 sock->SetHandler(this);
1408 sock->SetupCallbacks();
1409 }
1410}
1411
1412void wxSocketHandler::UnRegister(wxSocketBase* sock)
1413{
1414 wxNode *node;
1415
1416 for (node = socks->First(); node; node = node->Next()) {
1417 wxSocketBase* s = (wxSocketBase*)node->Data();
384b4373 1418
f4ada568
GL
1419 if (s == sock) {
1420 delete node;
1421 sock->DestroyCallbacks();
1422 sock->SetHandler(NULL);
1423 return;
1424 }
1425 }
1426}
1427
1428unsigned long wxSocketHandler::Count() const
1429{
1430 return socks->Number();
1431}
1432
1433// --------------------------------------------------------------
1434// --------- wxSocketHandler "big" wait functions ---------------
1435// --------------------------------------------------------------
1436void handler_cbk(wxSocketBase& sock,
1437 wxSocketBase::wxRequestEvent WXUNUSED(flags),
1438 char *cdata)
1439{
1440 int *a_wait = (int *)cdata;
1441
1442 (*a_wait)++;
1443 sock.Notify(FALSE);
1444}
1445
1446int wxSocketHandler::Wait(long seconds, long microseconds)
1447{
1448 int i;
1449 int on_wait;
1450 wxSockWakeUp s_wake(NULL, &on_wait, -2);
1451 wxNode *node;
1452
1453 for (node = socks->First(), i=0; node; node = node->Next(), i++) {
1454 wxSocketBase *sock = (wxSocketBase *)node->Data();
1455
1456 sock->SaveState();
1457
1458 sock->SetupCallbacks();
1459
1460 sock->Callback(handler_cbk);
1461 sock->CallbackData((char *)&on_wait);
1462 }
1463 on_wait = 0;
1464 if (seconds != -1)
1465 s_wake.Start((seconds*1000) + (microseconds/1000), TRUE);
1466
1467 while (!on_wait)
1468 PROCESS_EVENTS();
1469
1470 for (node = socks->First(), i=0; node; node = node->Next(), i++) {
1471 wxSocketBase *sock = (wxSocketBase *)node->Data();
1472
1473 sock->RestoreState();
1474 }
1475
1476 if (on_wait == -2)
1477 return 0;
1478
1479 return on_wait;
1480}
1481
1482void wxSocketHandler::YieldSock()
1483{
1484 wxNode *node;
1485
1486 for (node = socks->First(); node; node = node->Next() ) {
1487 wxSocketBase *sock = (wxSocketBase *)node->Data();
1488
1489 sock->SaveState();
1490
1491 sock->SetFlags(wxSocketBase::SPEED);
1492 if (sock->IsData())
1493 sock->DoRequests(wxSocketBase::EVT_READ);
1494 sock->DoRequests(wxSocketBase::EVT_WRITE);
1495
1496 sock->RestoreState();
1497 }
1498}
1499
1500// --------------------------------------------------------------
1501// --------- wxSocketHandler: create and register the socket ----
1502// --------------------------------------------------------------
1503wxSocketServer *wxSocketHandler::CreateServer(wxSockAddress& addr,
1504 wxSocketBase::wxSockFlags flags)
1505{
1506 wxSocketServer *serv = new wxSocketServer(addr, flags);
1507
1508 Register(serv);
1509 return serv;
1510}
1511
1512wxSocketClient *wxSocketHandler::CreateClient(wxSocketBase::wxSockFlags flags)
1513{
1514 wxSocketClient *client = new wxSocketClient(flags);
1515
1516 Register(client);
1517 return client;
1518}
1519
1520#ifdef __WINDOWS__
1521// --------------------------------------------------------------
1522// --------- wxSocketHandler: Windows specific methods ----------
1523// --------------------------------------------------------------
1524UINT wxSocketHandler::NewMessage(wxSocketBase *sock)
1525{
1526 internal->firstAvailableMsg++;
1527 smsg_list->Append(internal->firstAvailableMsg, sock);
1528 return internal->firstAvailableMsg;
1529}
1530
1531void wxSocketHandler::DestroyMessage(UINT msg)
1532{
1533 wxNode *node = smsg_list->Find(msg);
1534 delete node;
1535}
1536
1537HWND wxSocketHandler::GetHWND() const
1538{
1539 return internal->sockWin;
1540}
1541
1542#endif
7f555861
JS
1543
1544#endif
1545 // __WXSTUBS__