]>
git.saurik.com Git - wxWidgets.git/blob - src/common/socket.cpp
1 ////////////////////////////////////////////////////////////////////////////////
3 // Purpose: Socket handler classes
4 // Authors: Guilhem Lavaux (completely rewritten from a basic API of Andrew
5 // Davidson(1995) in wxWeb)
8 // Copyright: (C) 1999 1998, 1997, Guilhem Lavaux
10 // License: see wxWindows license
11 ////////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "socket.h"
17 typedef int socklen_t
;
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 /////////////////////////////////////////////////////////////////////////////
31 /////////////////////////////////////////////////////////////////////////////
33 #include <wx/object.h>
34 #include <wx/string.h>
38 // Not enough OS behaviour defined for wxStubs
45 /////////////////////////////////////////////////////////////////////////////
46 // System specific headers
47 /////////////////////////////////////////////////////////////////////////////
49 // in order to avoid problems with our c library and double definitions
50 #define close closesocket
51 #define ioctl ioctlsocket
53 #include <wx/mac/macsock.h>
57 #if defined(__WINDOWS__)
66 #include <sys/socket.h>
68 #include <sys/ioctl.h>
74 #include <sys/filio.h>
86 /////////////////////////////////////////////////////////////////////////////
88 /////////////////////////////////////////////////////////////////////////////
89 #include <wx/module.h>
90 #define WXSOCK_INTERNAL
91 #include <wx/sckaddr.h>
92 #include <wx/socket.h>
93 #include <wx/sckint.h>
95 // ----------------------
96 // Some patch ----- BEGIN
97 // ----------------------
99 #define close closesocket
100 #define ioctl ioctlsocket
104 #define errno WSAGetLastError()
108 #define EWOULDBLOCK WSAEWOULDBLOCK
109 #define ETIMEDOUT WSAETIMEDOUT
111 #define EINTR WSAEINTR
115 #define INVALID_SOCKET -1
119 // This is an MS TCP/IP routine and is not needed here. Some WinSock
120 // implementations (such as PC-NFS) will require you to include this
121 // or a similar routine (see appendix in WinSock doc or help file).
123 #if defined( NEED_WSAFDIsSet ) || defined( __VISUALC__ )
124 int PASCAL FAR
__WSAFDIsSet(SOCKET fd
, fd_set FAR
*set
)
126 int i
= set
->fd_count
;
130 if (set
->fd_array
[i
] == fd
)
139 // -------------------
140 // Some patch ---- END
141 // -------------------
147 // --------------------------------------------------------------
149 // --------------------------------------------------------------
150 class wxSocketModule
: public wxModule
152 DECLARE_DYNAMIC_CLASS(wxSocketModule
)
159 // --------------------------------------------------------------
161 // --------------------------------------------------------------
162 #if !USE_SHARED_LIBRARY
163 IMPLEMENT_CLASS(wxSocketBase
, wxObject
)
164 IMPLEMENT_CLASS(wxSocketServer
, wxSocketBase
)
165 IMPLEMENT_CLASS(wxSocketClient
, wxSocketBase
)
166 IMPLEMENT_CLASS(wxSocketHandler
, wxObject
)
167 IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent
, wxEvent
)
168 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule
, wxModule
)
171 // --------------------------------------------------------------
172 // --------- wxSocketBase CONSTRUCTOR ---------------------------
173 // --------------------------------------------------------------
174 wxSocketBase::wxSocketBase(wxSocketBase::wxSockFlags _flags
,
175 wxSocketBase::wxSockType _type
) :
177 m_flags(_flags
), m_type(_type
), m_connected(FALSE
), m_connecting(FALSE
),
178 m_fd(INVALID_SOCKET
), m_id(-1),
180 m_neededreq((wxRequestNotify
)(REQ_READ
| REQ_LOST
)),
182 m_unread(NULL
), m_unrd_size(0),
183 m_cbk(NULL
), m_cdata(NULL
),
184 m_notify_state(FALSE
)
186 m_internal
= new wxSocketInternal(this);
189 wxSocketBase::wxSocketBase() :
191 m_flags(WAITALL
), m_type(SOCK_UNINIT
), m_connected(FALSE
),
192 m_connecting(FALSE
), m_fd(INVALID_SOCKET
),
193 m_id(-1), m_handler(0),
194 m_neededreq((wxRequestNotify
)(REQ_READ
| REQ_LOST
)),
196 m_unread(NULL
), m_unrd_size(0),
197 m_cbk(NULL
), m_cdata(NULL
),
198 m_notify_state(FALSE
)
200 m_internal
= new wxSocketInternal(this);
203 // --------------------------------------------------------------
205 // --------------------------------------------------------------
207 wxSocketBase::~wxSocketBase()
209 // First, close the file descriptor.
214 // Unregister from the handler database.
216 m_handler
->UnRegister(this);
218 // Destroy all saved states.
219 m_states
.DeleteContents(TRUE
);
221 // Destroy the socket manager.
225 bool wxSocketBase::Close()
227 if (m_fd
!= INVALID_SOCKET
)
229 if (m_notify_state
== TRUE
)
232 // Shutdown the connection.
235 m_fd
= INVALID_SOCKET
;
242 // --------------------------------------------------------------
243 // wxSocketBase base IO function
244 // --------------------------------------------------------------
246 wxSocketBase
& wxSocketBase::Read(char* buffer
, size_t nbytes
)
248 m_lcount
= GetPushback(buffer
, nbytes
, FALSE
);
252 // If we have got the whole needed buffer or if we don't want to
253 // wait then it returns immediately.
254 if (!nbytes
|| (m_lcount
&& !(m_flags
& WAITALL
)) ) {
258 WantBuffer(buffer
, nbytes
, EVT_READ
);
263 wxSocketBase
& wxSocketBase::ReadMsg(char* buffer
, size_t nbytes
)
265 unsigned long len
, len2
, sig
;
271 // sig should be an explicit 32-bit unsigned integer; I've seen
272 // compilers in which size_t was actually a 16-bit unsigned integer
274 Read((char *)&msg
, sizeof(msg
));
275 if (m_lcount
!= sizeof(msg
))
278 sig
= msg
.sig
[0] & 0xff;
279 sig
|= (size_t)(msg
.sig
[1] & 0xff) << 8;
280 sig
|= (size_t)(msg
.sig
[2] & 0xff) << 16;
281 sig
|= (size_t)(msg
.sig
[3] & 0xff) << 24;
283 if (sig
!= 0xfeeddead)
285 len
= msg
.len
[0] & 0xff;
286 len
|= (size_t)(msg
.len
[1] & 0xff) << 8;
287 len
|= (size_t)(msg
.len
[2] & 0xff) << 16;
288 len
|= (size_t)(msg
.len
[3] & 0xff) << 24;
290 // len2 is incorrectly computed in the original; this sequence is
299 // the "len &&" in the following statement is necessary so that
300 // we don't attempt to read (and possibly hang the system)
301 // if the message was zero bytes long
302 if (len
&& Read(buffer
, len
).LastCount() != len
)
304 if (len2
&& (Read(NULL
, len2
).LastCount() != len2
))
306 if (Read((char *)&msg
, sizeof(msg
)).LastCount() != sizeof(msg
))
309 sig
= msg
.sig
[0] & 0xff;
310 sig
|= (size_t)(msg
.sig
[1] & 0xff) << 8;
311 sig
|= (size_t)(msg
.sig
[2] & 0xff) << 16;
312 sig
|= (size_t)(msg
.sig
[3] & 0xff) << 24;
314 // we return *this either way, so a smart optimizer will
315 // optimize the following sequence out; I'm leaving it in anyway
316 if (sig
!= 0xdeadfeed)
322 wxSocketBase
& wxSocketBase::Peek(char* buffer
, size_t nbytes
)
324 m_lcount
= GetPushback(buffer
, nbytes
, TRUE
);
325 if (nbytes
-m_lcount
== 0)
332 WantBuffer(buffer
, nbytes
, EVT_PEEK
);
337 wxSocketBase
& wxSocketBase::Write(const char *buffer
, size_t nbytes
)
340 WantBuffer((char *)buffer
, nbytes
, EVT_WRITE
);
344 wxSocketBase
& wxSocketBase::WriteMsg(const char *buffer
, size_t nbytes
)
351 // warning about 'cast truncates constant value'
353 #pragma warning(disable: 4310)
354 #endif // __VISUALC__
356 msg
.sig
[0] = (char) 0xad;
357 msg
.sig
[1] = (char) 0xde;
358 msg
.sig
[2] = (char) 0xed;
359 msg
.sig
[3] = (char) 0xfe;
361 msg
.len
[0] = (char) nbytes
& 0xff;
362 msg
.len
[1] = (char) (nbytes
>> 8) & 0xff;
363 msg
.len
[2] = (char) (nbytes
>> 16) & 0xff;
364 msg
.len
[3] = (char) (nbytes
>> 24) & 0xff;
366 if (Write((char *)&msg
, sizeof(msg
)).LastCount() < sizeof(msg
))
368 if (Write(buffer
, nbytes
).LastCount() < nbytes
)
371 msg
.sig
[0] = (char) 0xed;
372 msg
.sig
[1] = (char) 0xfe;
373 msg
.sig
[2] = (char) 0xad;
374 msg
.sig
[3] = (char) 0xde;
375 msg
.len
[0] = msg
.len
[1] = msg
.len
[2] = msg
.len
[3] = (char) 0;
376 Write((char *)&msg
, sizeof(msg
));
381 #pragma warning(default: 4310)
382 #endif // __VISUALC__
385 wxSocketBase
& wxSocketBase::Unread(const char *buffer
, size_t nbytes
)
389 CreatePushbackAfter(buffer
, nbytes
);
395 bool wxSocketBase::IsData() const
405 m_internal
->AcquireFD();
410 FD_SET(m_fd
, &sock_set
);
411 select(m_fd
+1, &sock_set
, NULL
, NULL
, &tv
);
413 m_internal
->ReleaseFD();
415 return (FD_ISSET(m_fd
, &sock_set
) != 0);
418 // ---------------------------------------------------------------------
419 // --------- wxSocketBase Discard(): deletes all byte in the input queue
420 // ---------------------------------------------------------------------
421 void wxSocketBase::Discard()
423 #define MAX_BUFSIZE (10*1024)
424 char *my_data
= new char[MAX_BUFSIZE
];
425 size_t recv_size
= MAX_BUFSIZE
;
428 SetFlags((wxSockFlags
)(NOWAIT
| SPEED
));
430 while (recv_size
== MAX_BUFSIZE
)
432 recv_size
= Read(my_data
, MAX_BUFSIZE
).LastCount();
441 // If what? Who seems to need unsigned int?
442 // BTW uint isn't even defined on wxMSW for VC++ for some reason. Even if it
443 // were, getpeername/getsockname don't take unsigned int*, they take int*.
445 // Under glibc 2.0.7, socketbits.h declares socklen_t to be unsigned int
446 // and it uses *socklen_t as the 3rd parameter. Robert.
448 // JACS - How can we detect this?
449 // Meanwhile, if your compiler complains about socklen_t,
450 // switch lines below.
453 # define wxSOCKET_INT socklen_t
455 # define wxSOCKET_INT int
458 // --------------------------------------------------------------
459 // wxSocketBase socket info functions
460 // --------------------------------------------------------------
462 bool wxSocketBase::GetPeer(wxSockAddress
& addr_man
) const
464 struct sockaddr my_addr
;
465 wxSOCKET_INT len_addr
= sizeof(my_addr
);
470 m_internal
->AcquireFD();
472 if (getpeername(m_fd
, (struct sockaddr
*)&my_addr
, &len_addr
) < 0) {
473 m_internal
->ReleaseFD();
477 m_internal
->ReleaseFD();
478 addr_man
.Disassemble(&my_addr
, len_addr
);
482 bool wxSocketBase::GetLocal(wxSockAddress
& addr_man
) const
484 struct sockaddr my_addr
;
485 wxSOCKET_INT len_addr
= sizeof(my_addr
);
490 m_internal
->AcquireFD();
492 if (getsockname(m_fd
, (struct sockaddr
*)&my_addr
, &len_addr
) < 0) {
493 m_internal
->ReleaseFD();
496 m_internal
->ReleaseFD();
498 addr_man
.Disassemble(&my_addr
, len_addr
);
502 // --------------------------------------------------------------
503 // wxSocketBase wait functions
504 // --------------------------------------------------------------
506 void wxSocketBase::SaveState()
508 SocketState
*state
= new SocketState
;
510 state
->notify_state
= m_notify_state
;
511 state
->evt_notify_state
= m_neededreq
;
512 state
->socket_flags
= m_flags
;
513 state
->c_callback
= m_cbk
;
514 state
->c_callback_data
= m_cdata
;
516 m_states
.Append((wxObject
*)state
);
519 void wxSocketBase::RestoreState()
524 node
= m_states
.Last();
528 state
= (SocketState
*)node
->Data();
530 SetFlags(state
->socket_flags
);
531 m_internal
->AcquireData();
532 m_neededreq
= state
->evt_notify_state
;
533 m_internal
->ReleaseData();
534 m_cbk
= state
->c_callback
;
535 m_cdata
= state
->c_callback_data
;
536 Notify(state
->notify_state
);
542 // --------------------------------------------------------------
543 // --------- wxSocketBase callback functions --------------------
544 // --------------------------------------------------------------
546 wxSocketBase::wxSockCbk
wxSocketBase::Callback(wxSockCbk cbk_
)
548 wxSockCbk old_cbk
= cbk_
;
554 char *wxSocketBase::CallbackData(char *data
)
556 char *old_data
= m_cdata
;
562 // --------------------------------------------------------------
563 // --------- wxSocketBase wait functions ------------------------
564 // --------------------------------------------------------------
566 bool wxSocketBase::_Wait(long seconds
, long milliseconds
, int type
)
570 if ((!m_connected
&& !m_connecting
) || m_fd
< 0)
573 req
= new SockRequest
;
575 req
->type
= REQ_WAIT
| type
;
576 req
->timeout
= seconds
* 1000 + milliseconds
;
582 m_internal
->QueueRequest(req
, TRUE
);
584 return (req
->io_nbytes
!= 0);
587 bool wxSocketBase::Wait(long seconds
, long milliseconds
)
589 return _Wait(seconds
, milliseconds
, REQ_ACCEPT
| REQ_CONNECT
|
590 REQ_READ
| REQ_WRITE
| REQ_LOST
);
593 bool wxSocketBase::WaitForRead(long seconds
, long milliseconds
)
595 return _Wait(seconds
, milliseconds
, REQ_READ
| REQ_LOST
);
598 bool wxSocketBase::WaitForWrite(long seconds
, long milliseconds
)
600 return _Wait(seconds
, milliseconds
, REQ_WRITE
);
603 bool wxSocketBase::WaitForLost(long seconds
, long milliseconds
)
605 return _Wait(seconds
, milliseconds
, REQ_LOST
);
608 // --------------------------------------------------------------
609 // --------- wxSocketBase callback management -------------------
610 // --------------------------------------------------------------
612 wxSocketBase::wxRequestNotify
wxSocketBase::EventToNotify(wxRequestEvent evt
)
632 void wxSocketBase::SetFlags(wxSockFlags _flags
)
635 if (_flags
& SPEED
) {
636 // SPEED and WAITALL are antagonists.
637 m_flags
= (wxSockFlags
)(m_flags
& ~WAITALL
);
641 wxSocketBase::wxSockFlags
wxSocketBase::GetFlags() const
646 void wxSocketBase::SetNotify(wxRequestNotify flags
)
648 /* Check if server */
649 if (m_type
!= SOCK_SERVER
)
650 flags
&= ~REQ_ACCEPT
;
652 m_internal
->AcquireData();
654 m_internal
->ReleaseData();
655 if (m_neededreq
== 0)
656 m_internal
->StopWaiter();
658 Notify(m_notify_state
);
661 void wxSocketBase::Notify(bool notify
)
663 m_notify_state
= notify
;
664 if (m_fd
== INVALID_SOCKET
)
668 m_internal
->ResumeWaiter();
670 m_internal
->StopWaiter();
673 void wxSocketBase::OnRequest(wxRequestEvent req_evt
)
675 wxSocketEvent
event(m_id
);
676 wxRequestNotify notify
= EventToNotify(req_evt
);
678 if ((m_neededreq
& notify
) == notify
) {
679 event
.m_socket
= this;
680 event
.m_skevt
= req_evt
;
683 // OldOnNotify(req_evt);
685 // We disable the event reporting.
686 m_neededreq
&= ~notify
;
690 wxSocketEvent::wxSocketEvent(int id
)
693 wxEventType type
= (wxEventType
)wxEVT_SOCKET
;
698 void wxSocketEvent::CopyObject(wxObject
& obj_d
) const
700 wxSocketEvent
*event
= (wxSocketEvent
*)&obj_d
;
702 wxEvent::CopyObject(obj_d
);
704 event
->m_skevt
= m_skevt
;
705 event
->m_socket
= m_socket
;
708 void wxSocketBase::OldOnNotify(wxRequestEvent evt
)
712 // --------------------------------------------------------------
713 // --------- wxSocketBase functions [Callback, CallbackData] ----
714 // --------------------------------------------------------------
716 void wxSocketBase::SetEventHandler(wxEvtHandler
& h_evt
, int id
)
718 SetNextHandler(&h_evt
);
722 // --------------------------------------------------------------
723 // --------- wxSocketBase pushback library ----------------------
724 // --------------------------------------------------------------
726 void wxSocketBase::CreatePushbackAfter(const char *buffer
, size_t size
)
730 if (m_unread
!= NULL
)
731 m_unread
= (char *) realloc(m_unread
, m_unrd_size
+size
);
733 m_unread
= (char *) malloc(size
);
734 curr_pos
= m_unread
+ m_unrd_size
;
736 memcpy(curr_pos
, buffer
, size
);
740 void wxSocketBase::CreatePushbackBefore(const char *buffer
, size_t size
)
742 char *curr_pos
, *new_buf
;
744 new_buf
= (char *) malloc(m_unrd_size
+size
);
745 curr_pos
= new_buf
+ size
;
747 memcpy(new_buf
, buffer
, size
);
748 if (m_unrd_size
!= 0) {
749 memcpy(curr_pos
, m_unread
, m_unrd_size
);
756 size_t wxSocketBase::GetPushback(char *buffer
, size_t size
, bool peek
)
761 if (size
> m_unrd_size
)
763 memcpy(buffer
, m_unread
, size
);
767 if (m_unrd_size
== 0) {
776 // --------------------------------------------------------------
777 // --------- wxSocketBase buffer core requester -----------------
778 // --------------------------------------------------------------
780 void wxSocketBase::WantBuffer(char *buffer
, size_t nbytes
,
785 if (m_fd
== INVALID_SOCKET
|| !m_handler
|| !m_connected
)
788 SockRequest
*buf
= new SockRequest
;
791 buf
->buffer
= buffer
;
794 buf
->type
= EventToNotify(evt
);
799 buf_timed_out
= FALSE
;
801 if ((m_flags
& SPEED
) != 0)
802 m_internal
->QueueRequest(buf
, FALSE
);
804 if ((m_flags
& NOWAIT
) != 0)
805 m_internal
->QueueRequest(buf
, TRUE
);
807 m_internal
->QueueRequest(buf
, TRUE
);
808 m_lcount
+= buf
->io_nbytes
;
812 m_error
= buf
->error
;
818 // --------------------------------------------------------------
820 // --------------------------------------------------------------
822 wxSocketServer::wxSocketServer(wxSockAddress
& addr_man
,
824 wxSocketBase(flags
, SOCK_SERVER
)
826 m_fd
= socket(addr_man
.GetFamily(), SOCK_STREAM
, 0);
828 if (m_fd
== INVALID_SOCKET
)
832 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (char*)&flag
, sizeof(int));
834 struct sockaddr
*myaddr
;
837 addr_man
.Build(myaddr
, len
);
838 if (bind(m_fd
, myaddr
, addr_man
.SockAddrLen()) < 0)
841 if (listen(m_fd
, 5) < 0) {
842 m_fd
= INVALID_SOCKET
;
846 m_internal
->SetFD(m_fd
);
851 // --------------------------------------------------------------
852 // wxSocketServer Accept
853 // --------------------------------------------------------------
855 bool wxSocketServer::AcceptWith(wxSocketBase
& sock
)
859 m_internal
->AcquireFD();
860 if ((fd2
= accept(m_fd
, 0, 0)) < 0) {
861 m_internal
->ReleaseFD();
864 m_internal
->ReleaseFD();
866 struct linger linger
;
870 setsockopt(fd2
, SOL_SOCKET
, SO_LINGER
, (char*)&linger
, sizeof(linger
));
873 setsockopt(fd2
, SOL_SOCKET
, SO_KEEPALIVE
, (char*)&flag
, sizeof(int));
875 sock
.m_type
= SOCK_INTERNAL
;
877 sock
.m_connected
= TRUE
;
879 sock
.m_internal
->SetFD(fd2
);
881 sock
.m_internal
->ResumeWaiter();
886 wxSocketBase
*wxSocketServer::Accept()
888 wxSocketBase
* sock
= new wxSocketBase();
890 sock
->SetFlags((wxSockFlags
)m_flags
);
892 if (!AcceptWith(*sock
))
896 m_handler
->Register(sock
);
901 // --------------------------------------------------------------
903 // --------------------------------------------------------------
905 // --------- wxSocketClient CONSTRUCTOR -------------------------
906 // --------------------------------------------------------------
907 wxSocketClient::wxSocketClient(wxSockFlags _flags
) :
908 wxSocketBase(_flags
, SOCK_CLIENT
)
912 // --------------------------------------------------------------
913 // --------- wxSocketClient DESTRUCTOR --------------------------
914 // --------------------------------------------------------------
915 wxSocketClient::~wxSocketClient()
919 // --------------------------------------------------------------
920 // --------- wxSocketClient Connect functions -------------------
921 // --------------------------------------------------------------
922 bool wxSocketClient::Connect(wxSockAddress
& addr_man
, bool WXUNUSED(wait
) )
924 struct linger linger
;
929 // Initializes all socket stuff ...
930 // --------------------------------
931 m_fd
= socket(addr_man
.GetFamily(), SOCK_STREAM
, 0);
940 setsockopt(m_fd
, SOL_SOCKET
, SO_LINGER
, (char*)&linger
, sizeof(linger
));
942 // Stay in touch with the state of things...
944 unsigned long flag
= 1;
945 setsockopt(m_fd
, SOL_SOCKET
, SO_KEEPALIVE
, (char*)&flag
, sizeof(int));
947 // Disable the nagle algorithm, which delays sends till the
948 // buffer is full (or a certain time period has passed?)...
950 #if defined(__WINDOWS__) || (defined(IPPROTO_TCP) && defined(TCP_NODELAY))
952 setsockopt(m_fd
, IPPROTO_TCP
, TCP_NODELAY
, (char*)&flag
, sizeof(int));
955 struct sockaddr
*remote
;
958 addr_man
.Build(remote
, len
);
960 if (connect(m_fd
, remote
, len
) != 0)
963 m_internal
->SetFD(m_fd
);
965 // Enables bg events.
966 // ------------------
973 bool wxSocketClient::WaitOnConnect(long seconds
, long microseconds
)
975 int ret
= _Wait(seconds
, microseconds
, REQ_CONNECT
| REQ_LOST
);
983 void wxSocketClient::OnRequest(wxRequestEvent evt
)
985 if (evt
== EVT_CONNECT
)
989 m_neededreq
&= ~REQ_CONNECT
;
995 wxSocketBase::OnRequest(evt
);
998 /////////////////////////////////////////////////////////////////
999 // wxSocketHandler ///////////////////////////////////////////////
1000 /////////////////////////////////////////////////////////////////
1002 wxSocketHandler
*wxSocketHandler::master
= NULL
;
1003 #if defined(__WINDOWS__)
1004 static int win_initialized
= 0;
1007 // --------------------------------------------------------------
1008 // --------- wxSocketHandler CONSTRUCTOR ------------------------
1009 // --------------------------------------------------------------
1010 wxSocketHandler::wxSocketHandler()
1012 #if defined(__WINDOWS__)
1013 if (!win_initialized
)
1017 WSAStartup((1 << 8) | 1, &wsaData
);
1018 win_initialized
= 1;
1025 signal(SIGPIPE
, SIG_IGN
);
1029 // --------------------------------------------------------------
1030 // --------- wxSocketHandler DESTRUCTOR -------------------------
1031 // --------------------------------------------------------------
1032 wxSocketHandler::~wxSocketHandler()
1034 wxNode
*next_node
, *node
= socks
->First();
1038 wxSocketBase
* sock
= (wxSocketBase
*)node
->Data();
1041 next_node
= node
->Next();
1050 win_initialized
= 0;
1054 // --------------------------------------------------------------
1055 // --------- wxSocketHandler registering functions --------------
1056 // --------------------------------------------------------------
1058 void wxSocketHandler::Register(wxSocketBase
* sock
)
1062 for (node
= socks
->First(); node
!= NULL
; node
= node
->Next())
1064 wxSocketBase
* s
= (wxSocketBase
*)node
->Data();
1072 socks
->Append(sock
);
1073 sock
->SetHandler(this);
1077 void wxSocketHandler::UnRegister(wxSocketBase
* sock
)
1081 for (node
= socks
->First(); node
; node
= node
->Next())
1083 wxSocketBase
* s
= (wxSocketBase
*)node
->Data();
1088 sock
->SetHandler(NULL
);
1094 unsigned long wxSocketHandler::Count() const
1096 return socks
->Number();
1099 // --------------------------------------------------------------
1100 // --------- wxSocketHandler "big" wait functions ---------------
1101 // --------------------------------------------------------------
1103 int wxSocketHandler::Wait(long seconds
, long microseconds
)
1105 // TODO Needs the completely asynchronous notifier.
1111 for (node = socks->First(), i=0; node; node = node->Next(), i++)
1113 wxSocketBase *sock = (wxSocketBase *)node->Data();
1117 sock->SetupCallbacks();
1119 sock->Callback(handler_cbk);
1120 sock->CallbackData((char *)&on_wait);
1124 s_wake.Start((seconds*1000) + (microseconds/1000), TRUE);
1129 for (node = socks->First(), i=0; node; node = node->Next(), i++)
1131 wxSocketBase *sock = (wxSocketBase *)node->Data();
1133 sock->RestoreState();
1144 void wxSocketHandler::YieldSock()
1148 // Nothing to do anymore here except waiting for the queue emptying.
1149 for (node
= socks
->First(); node
; node
=node
->Next()) {
1150 wxSocketBase
*sock
= (wxSocketBase
*)node
->Data();
1152 sock
->m_internal
->WaitForEnd(NULL
);
1156 // --------------------------------------------------------------
1157 // --------- wxSocketHandler: create and register the socket ----
1158 // --------------------------------------------------------------
1159 wxSocketServer
*wxSocketHandler::CreateServer(wxSockAddress
& addr
,
1160 wxSocketBase::wxSockFlags flags
)
1162 wxSocketServer
*serv
= new wxSocketServer(addr
, flags
);
1168 wxSocketClient
*wxSocketHandler::CreateClient(wxSocketBase::wxSockFlags flags
)
1170 wxSocketClient
*client
= new wxSocketClient(flags
);
1176 bool wxSocketModule::OnInit()
1178 wxSocketHandler::master
= new wxSocketHandler();
1182 void wxSocketModule::OnExit()
1184 delete wxSocketHandler::master
;
1185 wxSocketHandler::master
= NULL
;