]>
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 // IRIX requires bstring.h be included to use select()
43 // Not enough OS behaviour defined for wxStubs
50 /////////////////////////////////////////////////////////////////////////////
51 // System specific headers
52 /////////////////////////////////////////////////////////////////////////////
54 // in order to avoid problems with our c library and double definitions
55 #define close closesocket
56 #define ioctl ioctlsocket
58 #include <wx/mac/macsock.h>
62 #if defined(__WINDOWS__)
71 #include <sys/socket.h>
73 #include <sys/ioctl.h>
79 #include <sys/filio.h>
91 /////////////////////////////////////////////////////////////////////////////
93 /////////////////////////////////////////////////////////////////////////////
94 #include <wx/module.h>
95 #define WXSOCK_INTERNAL
96 #include <wx/sckaddr.h>
97 #include <wx/socket.h>
98 #include <wx/sckint.h>
100 // ----------------------
101 // Some patch ----- BEGIN
102 // ----------------------
104 #define close closesocket
105 #define ioctl ioctlsocket
109 #define errno WSAGetLastError()
113 #define EWOULDBLOCK WSAEWOULDBLOCK
114 #define ETIMEDOUT WSAETIMEDOUT
116 #define EINTR WSAEINTR
120 #define INVALID_SOCKET -1
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).
128 #if defined( NEED_WSAFDIsSet ) || defined( __VISUALC__ )
129 int PASCAL FAR
__WSAFDIsSet(SOCKET fd
, fd_set FAR
*set
)
131 int i
= set
->fd_count
;
135 if (set
->fd_array
[i
] == fd
)
144 // -------------------
145 // Some patch ---- END
146 // -------------------
152 // --------------------------------------------------------------
154 // --------------------------------------------------------------
155 class wxSocketModule
: public wxModule
157 DECLARE_DYNAMIC_CLASS(wxSocketModule
)
164 // --------------------------------------------------------------
166 // --------------------------------------------------------------
167 #if !USE_SHARED_LIBRARY
168 IMPLEMENT_CLASS(wxSocketBase
, wxObject
)
169 IMPLEMENT_CLASS(wxSocketServer
, wxSocketBase
)
170 IMPLEMENT_CLASS(wxSocketClient
, wxSocketBase
)
171 IMPLEMENT_CLASS(wxSocketHandler
, wxObject
)
172 IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent
, wxEvent
)
173 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule
, wxModule
)
176 // --------------------------------------------------------------
177 // --------- wxSocketBase CONSTRUCTOR ---------------------------
178 // --------------------------------------------------------------
179 wxSocketBase::wxSocketBase(wxSocketBase::wxSockFlags _flags
,
180 wxSocketBase::wxSockType _type
) :
182 m_flags(_flags
), m_type(_type
), m_connected(FALSE
), m_connecting(FALSE
),
183 m_fd(INVALID_SOCKET
), m_id(-1),
185 m_neededreq((wxRequestNotify
)(REQ_READ
| REQ_LOST
)),
187 m_unread(NULL
), m_unrd_size(0),
188 m_cbk(NULL
), m_cdata(NULL
),
189 m_notify_state(FALSE
)
191 m_internal
= new wxSocketInternal(this);
194 wxSocketBase::wxSocketBase() :
196 m_flags(WAITALL
), m_type(SOCK_UNINIT
), m_connected(FALSE
),
197 m_connecting(FALSE
), m_fd(INVALID_SOCKET
),
198 m_id(-1), m_handler(0),
199 m_neededreq((wxRequestNotify
)(REQ_READ
| REQ_LOST
)),
201 m_unread(NULL
), m_unrd_size(0),
202 m_cbk(NULL
), m_cdata(NULL
),
203 m_notify_state(FALSE
)
205 m_internal
= new wxSocketInternal(this);
208 // --------------------------------------------------------------
210 // --------------------------------------------------------------
212 wxSocketBase::~wxSocketBase()
214 // First, close the file descriptor.
219 // Unregister from the handler database.
221 m_handler
->UnRegister(this);
223 // Destroy all saved states.
224 m_states
.DeleteContents(TRUE
);
226 // Destroy the socket manager.
230 bool wxSocketBase::Close()
232 if (m_fd
!= INVALID_SOCKET
)
234 if (m_notify_state
== TRUE
)
237 // Shutdown the connection.
240 m_fd
= INVALID_SOCKET
;
247 // --------------------------------------------------------------
248 // wxSocketBase base IO function
249 // --------------------------------------------------------------
251 wxSocketBase
& wxSocketBase::Read(char* buffer
, size_t nbytes
)
253 m_lcount
= GetPushback(buffer
, nbytes
, FALSE
);
257 // If we have got the whole needed buffer or if we don't want to
258 // wait then it returns immediately.
259 if (!nbytes
|| (m_lcount
&& !(m_flags
& WAITALL
)) ) {
263 WantBuffer(buffer
, nbytes
, EVT_READ
);
268 wxSocketBase
& wxSocketBase::ReadMsg(char* buffer
, size_t nbytes
)
270 unsigned long len
, len2
, sig
;
276 // sig should be an explicit 32-bit unsigned integer; I've seen
277 // compilers in which size_t was actually a 16-bit unsigned integer
279 Read((char *)&msg
, sizeof(msg
));
280 if (m_lcount
!= sizeof(msg
))
283 sig
= msg
.sig
[0] & 0xff;
284 sig
|= (size_t)(msg
.sig
[1] & 0xff) << 8;
285 sig
|= (size_t)(msg
.sig
[2] & 0xff) << 16;
286 sig
|= (size_t)(msg
.sig
[3] & 0xff) << 24;
288 if (sig
!= 0xfeeddead)
290 len
= msg
.len
[0] & 0xff;
291 len
|= (size_t)(msg
.len
[1] & 0xff) << 8;
292 len
|= (size_t)(msg
.len
[2] & 0xff) << 16;
293 len
|= (size_t)(msg
.len
[3] & 0xff) << 24;
295 // len2 is incorrectly computed in the original; this sequence is
304 // the "len &&" in the following statement is necessary so that
305 // we don't attempt to read (and possibly hang the system)
306 // if the message was zero bytes long
307 if (len
&& Read(buffer
, len
).LastCount() != len
)
309 if (len2
&& (Read(NULL
, len2
).LastCount() != len2
))
311 if (Read((char *)&msg
, sizeof(msg
)).LastCount() != sizeof(msg
))
314 sig
= msg
.sig
[0] & 0xff;
315 sig
|= (size_t)(msg
.sig
[1] & 0xff) << 8;
316 sig
|= (size_t)(msg
.sig
[2] & 0xff) << 16;
317 sig
|= (size_t)(msg
.sig
[3] & 0xff) << 24;
319 // we return *this either way, so a smart optimizer will
320 // optimize the following sequence out; I'm leaving it in anyway
321 if (sig
!= 0xdeadfeed)
327 wxSocketBase
& wxSocketBase::Peek(char* buffer
, size_t nbytes
)
329 m_lcount
= GetPushback(buffer
, nbytes
, TRUE
);
330 if (nbytes
-m_lcount
== 0)
337 WantBuffer(buffer
, nbytes
, EVT_PEEK
);
342 wxSocketBase
& wxSocketBase::Write(const char *buffer
, size_t nbytes
)
345 WantBuffer((char *)buffer
, nbytes
, EVT_WRITE
);
349 wxSocketBase
& wxSocketBase::WriteMsg(const char *buffer
, size_t nbytes
)
356 // warning about 'cast truncates constant value'
358 #pragma warning(disable: 4310)
359 #endif // __VISUALC__
361 msg
.sig
[0] = (char) 0xad;
362 msg
.sig
[1] = (char) 0xde;
363 msg
.sig
[2] = (char) 0xed;
364 msg
.sig
[3] = (char) 0xfe;
366 msg
.len
[0] = (char) nbytes
& 0xff;
367 msg
.len
[1] = (char) (nbytes
>> 8) & 0xff;
368 msg
.len
[2] = (char) (nbytes
>> 16) & 0xff;
369 msg
.len
[3] = (char) (nbytes
>> 24) & 0xff;
371 if (Write((char *)&msg
, sizeof(msg
)).LastCount() < sizeof(msg
))
373 if (Write(buffer
, nbytes
).LastCount() < nbytes
)
376 msg
.sig
[0] = (char) 0xed;
377 msg
.sig
[1] = (char) 0xfe;
378 msg
.sig
[2] = (char) 0xad;
379 msg
.sig
[3] = (char) 0xde;
380 msg
.len
[0] = msg
.len
[1] = msg
.len
[2] = msg
.len
[3] = (char) 0;
381 Write((char *)&msg
, sizeof(msg
));
386 #pragma warning(default: 4310)
387 #endif // __VISUALC__
390 wxSocketBase
& wxSocketBase::Unread(const char *buffer
, size_t nbytes
)
394 CreatePushbackAfter(buffer
, nbytes
);
400 bool wxSocketBase::IsData() const
410 m_internal
->AcquireFD();
415 FD_SET(m_fd
, &sock_set
);
416 select(m_fd
+1, &sock_set
, NULL
, NULL
, &tv
);
418 m_internal
->ReleaseFD();
420 return (FD_ISSET(m_fd
, &sock_set
) != 0);
423 // ---------------------------------------------------------------------
424 // --------- wxSocketBase Discard(): deletes all byte in the input queue
425 // ---------------------------------------------------------------------
426 void wxSocketBase::Discard()
428 #define MAX_BUFSIZE (10*1024)
429 char *my_data
= new char[MAX_BUFSIZE
];
430 size_t recv_size
= MAX_BUFSIZE
;
433 SetFlags((wxSockFlags
)(NOWAIT
| SPEED
));
435 while (recv_size
== MAX_BUFSIZE
)
437 recv_size
= Read(my_data
, MAX_BUFSIZE
).LastCount();
446 // If what? Who seems to need unsigned int?
447 // BTW uint isn't even defined on wxMSW for VC++ for some reason. Even if it
448 // were, getpeername/getsockname don't take unsigned int*, they take int*.
450 // Under glibc 2.0.7, socketbits.h declares socklen_t to be unsigned int
451 // and it uses *socklen_t as the 3rd parameter. Robert.
453 // JACS - How can we detect this?
454 // Meanwhile, if your compiler complains about socklen_t,
455 // switch lines below.
458 # define wxSOCKET_INT socklen_t
460 # define wxSOCKET_INT int
463 // --------------------------------------------------------------
464 // wxSocketBase socket info functions
465 // --------------------------------------------------------------
467 bool wxSocketBase::GetPeer(wxSockAddress
& addr_man
) const
469 struct sockaddr my_addr
;
470 wxSOCKET_INT len_addr
= sizeof(my_addr
);
475 m_internal
->AcquireFD();
477 if (getpeername(m_fd
, (struct sockaddr
*)&my_addr
, &len_addr
) < 0) {
478 m_internal
->ReleaseFD();
482 m_internal
->ReleaseFD();
483 addr_man
.Disassemble(&my_addr
, len_addr
);
487 bool wxSocketBase::GetLocal(wxSockAddress
& addr_man
) const
489 struct sockaddr my_addr
;
490 wxSOCKET_INT len_addr
= sizeof(my_addr
);
495 m_internal
->AcquireFD();
497 if (getsockname(m_fd
, (struct sockaddr
*)&my_addr
, &len_addr
) < 0) {
498 m_internal
->ReleaseFD();
501 m_internal
->ReleaseFD();
503 addr_man
.Disassemble(&my_addr
, len_addr
);
507 // --------------------------------------------------------------
508 // wxSocketBase wait functions
509 // --------------------------------------------------------------
511 void wxSocketBase::SaveState()
513 SocketState
*state
= new SocketState
;
515 state
->notify_state
= m_notify_state
;
516 state
->evt_notify_state
= m_neededreq
;
517 state
->socket_flags
= m_flags
;
518 state
->c_callback
= m_cbk
;
519 state
->c_callback_data
= m_cdata
;
521 m_states
.Append((wxObject
*)state
);
524 void wxSocketBase::RestoreState()
529 node
= m_states
.Last();
533 state
= (SocketState
*)node
->Data();
535 SetFlags(state
->socket_flags
);
536 m_internal
->AcquireData();
537 m_neededreq
= state
->evt_notify_state
;
538 m_internal
->ReleaseData();
539 m_cbk
= state
->c_callback
;
540 m_cdata
= state
->c_callback_data
;
541 Notify(state
->notify_state
);
547 // --------------------------------------------------------------
548 // --------- wxSocketBase callback functions --------------------
549 // --------------------------------------------------------------
551 wxSocketBase::wxSockCbk
wxSocketBase::Callback(wxSockCbk cbk_
)
553 wxSockCbk old_cbk
= cbk_
;
559 char *wxSocketBase::CallbackData(char *data
)
561 char *old_data
= m_cdata
;
567 // --------------------------------------------------------------
568 // --------- wxSocketBase wait functions ------------------------
569 // --------------------------------------------------------------
571 bool wxSocketBase::_Wait(long seconds
, long milliseconds
, int type
)
575 if ((!m_connected
&& !m_connecting
) || m_fd
< 0)
578 req
= new SockRequest
;
580 req
->type
= REQ_WAIT
| type
;
581 req
->timeout
= seconds
* 1000 + milliseconds
;
587 m_internal
->QueueRequest(req
, TRUE
);
589 return (req
->io_nbytes
!= 0);
592 bool wxSocketBase::Wait(long seconds
, long milliseconds
)
594 return _Wait(seconds
, milliseconds
, REQ_ACCEPT
| REQ_CONNECT
|
595 REQ_READ
| REQ_WRITE
| REQ_LOST
);
598 bool wxSocketBase::WaitForRead(long seconds
, long milliseconds
)
600 return _Wait(seconds
, milliseconds
, REQ_READ
| REQ_LOST
);
603 bool wxSocketBase::WaitForWrite(long seconds
, long milliseconds
)
605 return _Wait(seconds
, milliseconds
, REQ_WRITE
);
608 bool wxSocketBase::WaitForLost(long seconds
, long milliseconds
)
610 return _Wait(seconds
, milliseconds
, REQ_LOST
);
613 // --------------------------------------------------------------
614 // --------- wxSocketBase callback management -------------------
615 // --------------------------------------------------------------
617 wxSocketBase::wxRequestNotify
wxSocketBase::EventToNotify(wxRequestEvent evt
)
637 void wxSocketBase::SetFlags(wxSockFlags _flags
)
640 if (_flags
& SPEED
) {
641 // SPEED and WAITALL are antagonists.
642 m_flags
= (wxSockFlags
)(m_flags
& ~WAITALL
);
646 wxSocketBase::wxSockFlags
wxSocketBase::GetFlags() const
651 void wxSocketBase::SetNotify(wxRequestNotify flags
)
653 /* Check if server */
654 if (m_type
!= SOCK_SERVER
)
655 flags
&= ~REQ_ACCEPT
;
657 m_internal
->AcquireData();
659 m_internal
->ReleaseData();
660 if (m_neededreq
== 0)
661 m_internal
->StopWaiter();
663 Notify(m_notify_state
);
666 void wxSocketBase::Notify(bool notify
)
668 m_notify_state
= notify
;
669 if (m_fd
== INVALID_SOCKET
)
673 m_internal
->ResumeWaiter();
675 m_internal
->StopWaiter();
678 void wxSocketBase::OnRequest(wxRequestEvent req_evt
)
680 wxSocketEvent
event(m_id
);
681 wxRequestNotify notify
= EventToNotify(req_evt
);
683 if ((m_neededreq
& notify
) == notify
) {
684 event
.m_socket
= this;
685 event
.m_skevt
= req_evt
;
688 // OldOnNotify(req_evt);
690 // We disable the event reporting.
691 m_neededreq
&= ~notify
;
695 wxSocketEvent::wxSocketEvent(int id
)
698 wxEventType type
= (wxEventType
)wxEVT_SOCKET
;
703 void wxSocketEvent::CopyObject(wxObject
& obj_d
) const
705 wxSocketEvent
*event
= (wxSocketEvent
*)&obj_d
;
707 wxEvent::CopyObject(obj_d
);
709 event
->m_skevt
= m_skevt
;
710 event
->m_socket
= m_socket
;
713 void wxSocketBase::OldOnNotify(wxRequestEvent evt
)
717 // --------------------------------------------------------------
718 // --------- wxSocketBase functions [Callback, CallbackData] ----
719 // --------------------------------------------------------------
721 void wxSocketBase::SetEventHandler(wxEvtHandler
& h_evt
, int id
)
723 SetNextHandler(&h_evt
);
727 // --------------------------------------------------------------
728 // --------- wxSocketBase pushback library ----------------------
729 // --------------------------------------------------------------
731 void wxSocketBase::CreatePushbackAfter(const char *buffer
, size_t size
)
735 if (m_unread
!= NULL
)
736 m_unread
= (char *) realloc(m_unread
, m_unrd_size
+size
);
738 m_unread
= (char *) malloc(size
);
739 curr_pos
= m_unread
+ m_unrd_size
;
741 memcpy(curr_pos
, buffer
, size
);
745 void wxSocketBase::CreatePushbackBefore(const char *buffer
, size_t size
)
747 char *curr_pos
, *new_buf
;
749 new_buf
= (char *) malloc(m_unrd_size
+size
);
750 curr_pos
= new_buf
+ size
;
752 memcpy(new_buf
, buffer
, size
);
753 if (m_unrd_size
!= 0) {
754 memcpy(curr_pos
, m_unread
, m_unrd_size
);
761 size_t wxSocketBase::GetPushback(char *buffer
, size_t size
, bool peek
)
766 if (size
> m_unrd_size
)
768 memcpy(buffer
, m_unread
, size
);
772 if (m_unrd_size
== 0) {
781 // --------------------------------------------------------------
782 // --------- wxSocketBase buffer core requester -----------------
783 // --------------------------------------------------------------
785 void wxSocketBase::WantBuffer(char *buffer
, size_t nbytes
,
790 if (m_fd
== INVALID_SOCKET
|| !m_handler
|| !m_connected
)
793 SockRequest
*buf
= new SockRequest
;
796 buf
->buffer
= buffer
;
799 buf
->type
= EventToNotify(evt
);
804 buf_timed_out
= FALSE
;
806 if ((m_flags
& SPEED
) != 0)
807 m_internal
->QueueRequest(buf
, FALSE
);
809 if ((m_flags
& NOWAIT
) != 0)
810 m_internal
->QueueRequest(buf
, TRUE
);
812 m_internal
->QueueRequest(buf
, TRUE
);
813 m_lcount
+= buf
->io_nbytes
;
817 m_error
= buf
->error
;
823 // --------------------------------------------------------------
825 // --------------------------------------------------------------
827 wxSocketServer::wxSocketServer(wxSockAddress
& addr_man
,
829 wxSocketBase(flags
, SOCK_SERVER
)
831 m_fd
= socket(addr_man
.GetFamily(), SOCK_STREAM
, 0);
833 if (m_fd
== INVALID_SOCKET
)
837 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (char*)&flag
, sizeof(int));
839 struct sockaddr
*myaddr
;
842 addr_man
.Build(myaddr
, len
);
843 if (bind(m_fd
, myaddr
, addr_man
.SockAddrLen()) < 0)
846 if (listen(m_fd
, 5) < 0) {
847 m_fd
= INVALID_SOCKET
;
851 m_internal
->SetFD(m_fd
);
856 // --------------------------------------------------------------
857 // wxSocketServer Accept
858 // --------------------------------------------------------------
860 bool wxSocketServer::AcceptWith(wxSocketBase
& sock
)
864 m_internal
->AcquireFD();
865 if ((fd2
= accept(m_fd
, 0, 0)) < 0) {
866 m_internal
->ReleaseFD();
869 m_internal
->ReleaseFD();
871 struct linger linger
;
875 setsockopt(fd2
, SOL_SOCKET
, SO_LINGER
, (char*)&linger
, sizeof(linger
));
878 setsockopt(fd2
, SOL_SOCKET
, SO_KEEPALIVE
, (char*)&flag
, sizeof(int));
880 sock
.m_type
= SOCK_INTERNAL
;
882 sock
.m_connected
= TRUE
;
884 sock
.m_internal
->SetFD(fd2
);
886 sock
.m_internal
->ResumeWaiter();
891 wxSocketBase
*wxSocketServer::Accept()
893 wxSocketBase
* sock
= new wxSocketBase();
895 sock
->SetFlags((wxSockFlags
)m_flags
);
897 if (!AcceptWith(*sock
))
901 m_handler
->Register(sock
);
906 // --------------------------------------------------------------
908 // --------------------------------------------------------------
910 // --------- wxSocketClient CONSTRUCTOR -------------------------
911 // --------------------------------------------------------------
912 wxSocketClient::wxSocketClient(wxSockFlags _flags
) :
913 wxSocketBase(_flags
, SOCK_CLIENT
)
917 // --------------------------------------------------------------
918 // --------- wxSocketClient DESTRUCTOR --------------------------
919 // --------------------------------------------------------------
920 wxSocketClient::~wxSocketClient()
924 // --------------------------------------------------------------
925 // --------- wxSocketClient Connect functions -------------------
926 // --------------------------------------------------------------
927 bool wxSocketClient::Connect(wxSockAddress
& addr_man
, bool WXUNUSED(wait
) )
929 struct linger linger
;
934 // Initializes all socket stuff ...
935 // --------------------------------
936 m_fd
= socket(addr_man
.GetFamily(), SOCK_STREAM
, 0);
945 setsockopt(m_fd
, SOL_SOCKET
, SO_LINGER
, (char*)&linger
, sizeof(linger
));
947 // Stay in touch with the state of things...
949 unsigned long flag
= 1;
950 setsockopt(m_fd
, SOL_SOCKET
, SO_KEEPALIVE
, (char*)&flag
, sizeof(int));
952 // Disable the nagle algorithm, which delays sends till the
953 // buffer is full (or a certain time period has passed?)...
955 #if defined(__WINDOWS__) || (defined(IPPROTO_TCP) && defined(TCP_NODELAY))
957 setsockopt(m_fd
, IPPROTO_TCP
, TCP_NODELAY
, (char*)&flag
, sizeof(int));
960 struct sockaddr
*remote
;
963 addr_man
.Build(remote
, len
);
965 if (connect(m_fd
, remote
, len
) != 0)
968 m_internal
->SetFD(m_fd
);
970 // Enables bg events.
971 // ------------------
978 bool wxSocketClient::WaitOnConnect(long seconds
, long microseconds
)
980 int ret
= _Wait(seconds
, microseconds
, REQ_CONNECT
| REQ_LOST
);
988 void wxSocketClient::OnRequest(wxRequestEvent evt
)
990 if (evt
== EVT_CONNECT
)
994 m_neededreq
&= ~REQ_CONNECT
;
1000 wxSocketBase::OnRequest(evt
);
1003 /////////////////////////////////////////////////////////////////
1004 // wxSocketHandler ///////////////////////////////////////////////
1005 /////////////////////////////////////////////////////////////////
1007 wxSocketHandler
*wxSocketHandler::master
= NULL
;
1008 #if defined(__WINDOWS__)
1009 static int win_initialized
= 0;
1012 // --------------------------------------------------------------
1013 // --------- wxSocketHandler CONSTRUCTOR ------------------------
1014 // --------------------------------------------------------------
1015 wxSocketHandler::wxSocketHandler()
1017 #if defined(__WINDOWS__)
1018 if (!win_initialized
)
1022 WSAStartup((1 << 8) | 1, &wsaData
);
1023 win_initialized
= 1;
1030 signal(SIGPIPE
, SIG_IGN
);
1034 // --------------------------------------------------------------
1035 // --------- wxSocketHandler DESTRUCTOR -------------------------
1036 // --------------------------------------------------------------
1037 wxSocketHandler::~wxSocketHandler()
1039 wxNode
*next_node
, *node
= socks
->First();
1043 wxSocketBase
* sock
= (wxSocketBase
*)node
->Data();
1046 next_node
= node
->Next();
1055 win_initialized
= 0;
1059 // --------------------------------------------------------------
1060 // --------- wxSocketHandler registering functions --------------
1061 // --------------------------------------------------------------
1063 void wxSocketHandler::Register(wxSocketBase
* sock
)
1067 for (node
= socks
->First(); node
!= NULL
; node
= node
->Next())
1069 wxSocketBase
* s
= (wxSocketBase
*)node
->Data();
1077 socks
->Append(sock
);
1078 sock
->SetHandler(this);
1082 void wxSocketHandler::UnRegister(wxSocketBase
* sock
)
1086 for (node
= socks
->First(); node
; node
= node
->Next())
1088 wxSocketBase
* s
= (wxSocketBase
*)node
->Data();
1093 sock
->SetHandler(NULL
);
1099 unsigned long wxSocketHandler::Count() const
1101 return socks
->Number();
1104 // --------------------------------------------------------------
1105 // --------- wxSocketHandler "big" wait functions ---------------
1106 // --------------------------------------------------------------
1108 int wxSocketHandler::Wait(long seconds
, long microseconds
)
1110 // TODO Needs the completely asynchronous notifier.
1116 for (node = socks->First(), i=0; node; node = node->Next(), i++)
1118 wxSocketBase *sock = (wxSocketBase *)node->Data();
1122 sock->SetupCallbacks();
1124 sock->Callback(handler_cbk);
1125 sock->CallbackData((char *)&on_wait);
1129 s_wake.Start((seconds*1000) + (microseconds/1000), TRUE);
1134 for (node = socks->First(), i=0; node; node = node->Next(), i++)
1136 wxSocketBase *sock = (wxSocketBase *)node->Data();
1138 sock->RestoreState();
1149 void wxSocketHandler::YieldSock()
1153 // Nothing to do anymore here except waiting for the queue emptying.
1154 for (node
= socks
->First(); node
; node
=node
->Next()) {
1155 wxSocketBase
*sock
= (wxSocketBase
*)node
->Data();
1157 sock
->m_internal
->WaitForEnd(NULL
);
1161 // --------------------------------------------------------------
1162 // --------- wxSocketHandler: create and register the socket ----
1163 // --------------------------------------------------------------
1164 wxSocketServer
*wxSocketHandler::CreateServer(wxSockAddress
& addr
,
1165 wxSocketBase::wxSockFlags flags
)
1167 wxSocketServer
*serv
= new wxSocketServer(addr
, flags
);
1173 wxSocketClient
*wxSocketHandler::CreateClient(wxSocketBase::wxSockFlags flags
)
1175 wxSocketClient
*client
= new wxSocketClient(flags
);
1181 bool wxSocketModule::OnInit()
1183 wxSocketHandler::master
= new wxSocketHandler();
1187 void wxSocketModule::OnExit()
1189 delete wxSocketHandler::master
;
1190 wxSocketHandler::master
= NULL
;