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