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) 1998, 1997, Guilhem Lavaux
10 // License: see wxWindows license
11 ////////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "socket.h"
15 // #pragma implementation "socket.cpp"
19 typedef int socklen_t
;
22 // For compilers that support precompilation, includes "wx.h".
23 #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>
54 extern GUSISpinFn GUSISpin
;
55 #define PROCESS_EVENTS() wxMacProcessEvents()
56 const short kwxMacNetEventsMax
= 1000 ;
57 short wxMacNetEventsTop
= 0 ;
58 short wxMacNetEventsBottom
= 0 ;
59 short wxMacNetEventsEvents
[kwxMacNetEventsMax
] ;
60 void *wxMacNetEventsReferences
[kwxMacNetEventsMax
] ;
69 extern "C" void wxMacSocketHandlerProc( void *refcon
, short event
) ; // adds events
70 extern "C" void wxMacSocketOnRequestProc( void *refcon
, short event
) ; // consumes them
71 extern "C" void GUSISetReference( short sock
, short eventmask
, void * data
) ;
72 void wxMacProcessEvents() ;
75 #if defined(__WINDOWS__)
84 #include <sys/socket.h>
86 #include <sys/ioctl.h>
92 #include <sys/filio.h>
104 #if defined(__WXMOTIF__) || defined(__WXXT__)
105 #include <X11/Intrinsic.h>
107 /////////////////////////////
108 // Needs internal variables
109 /////////////////////////////
111 #define Uses_XtIntrinsic
116 #if defined(__WXGTK__)
120 /////////////////////////////////////////////////////////////////////////////
122 /////////////////////////////////////////////////////////////////////////////
123 #include "wx/module.h"
124 #define WXSOCK_INTERNAL
125 #include "wx/sckaddr.h"
126 #include "wx/socket.h"
128 /////////////////////////////////////////////////////////////////////////////
129 // Some patch ///// BEGIN
130 /////////////////////////////////////////////////////////////////////////////
132 #define close closesocket
133 #define ioctl ioctlsocket
137 #define errno WSAGetLastError()
141 #define EWOULDBLOCK WSAEWOULDBLOCK
142 #define ETIMEDOUT WSAETIMEDOUT
144 #define EINTR WSAEINTR
148 #define INVALID_SOCKET -1
152 #define wxAPP_CONTEXT ((XtAppContext)wxTheApp->GetAppContext())
156 // This is an MS TCP/IP routine and is not needed here. Some WinSock
157 // implementations (such as PC-NFS) will require you to include this
158 // or a similar routine (see appendix in WinSock doc or help file).
160 #if defined( NEED_WSAFDIsSet ) || defined( _MSC_VER )
161 int PASCAL FAR
__WSAFDIsSet(SOCKET fd
, fd_set FAR
*set
)
163 int i
= set
->fd_count
;
167 if (set
->fd_array
[i
] == fd
)
176 #if defined(__WINDOWS__)
177 #define PROCESS_EVENTS() wxYield()
178 #elif defined(__WXXT__) || defined(__WXMOTIF__)
179 #define PROCESS_EVENTS() XtAppProcessEvent(wxAPP_CONTEXT, XtIMAll)
180 #elif defined(__WXGTK__)
181 #define PROCESS_EVENTS() gtk_main_iteration()
184 /////////////////////////////////////////////////////////////////////////////
185 // Some patch ///// END
186 /////////////////////////////////////////////////////////////////////////////
188 // --------------------------------------------------------------
190 // --------------------------------------------------------------
191 class wxSocketModule
: public wxModule
193 DECLARE_DYNAMIC_CLASS(wxSocketModule
)
200 // --------------------------------------------------------------
202 // --------------------------------------------------------------
203 #if !USE_SHARED_LIBRARY
204 IMPLEMENT_CLASS(wxSocketBase
, wxObject
)
205 IMPLEMENT_CLASS(wxSocketServer
, wxSocketBase
)
206 IMPLEMENT_CLASS(wxSocketClient
, wxSocketBase
)
207 IMPLEMENT_CLASS(wxSocketHandler
, wxObject
)
208 IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent
, wxEvent
)
209 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule
, wxModule
)
212 class wxSockWakeUp
: public wxTimer
219 wxSockWakeUp(wxSocketBase
*_sock
, int *id
, int new_val
)
221 my_id
= id
; n_val
= new_val
;
224 virtual void Notify()
227 if (sock
) sock
->Notify(FALSE
);
232 class SockRequest
: public wxObject
239 wxSockWakeUp
*auto_wakeup
;
240 wxSocketBase::wxRequestNotify type
;
244 /////////////////////////////////////////////////////////////////////////////
245 // Some internal define
246 /////////////////////////////////////////////////////////////////////////////
248 // --------------------------------------------------------------
249 // --------- wxSocketBase CONSTRUCTOR ---------------------------
250 // --------------------------------------------------------------
251 wxSocketBase::wxSocketBase(wxSocketBase::wxSockFlags _flags
,
252 wxSocketBase::wxSockType _type
) :
254 m_flags(_flags
), m_type(_type
), m_connected(FALSE
), m_connecting(FALSE
),
255 m_fd(INVALID_SOCKET
), m_waitflags(0), m_cbk(0), m_cdata(0), m_id(-1),
257 m_neededreq((wxRequestNotify
)(REQ_READ
| REQ_LOST
)),
259 m_unread(NULL
), m_unrd_size(0),
261 m_timeout(3600), m_wantbuf(0)
263 m_internal
= new wxSockInternal
;
264 #if defined(__WXXT__) || defined(__WXMOTIF__) || defined(__WXGTK__)
265 m_internal
->sock_inputid
= 0;
266 m_internal
->sock_outputid
= 0;
267 m_internal
->sock_exceptid
= 0;
270 m_internal
->my_msg
= 0;
274 wxSocketBase::wxSocketBase() :
276 m_flags(WAITALL
), m_type(SOCK_UNINIT
), m_connected(FALSE
),
277 m_connecting(FALSE
), m_fd(INVALID_SOCKET
), m_waitflags(0),
278 m_cbk(0), m_cdata(0),
279 m_id(-1), m_handler(0),
280 m_neededreq((wxRequestNotify
)(REQ_READ
| REQ_LOST
)),
282 m_unread(NULL
), m_unrd_size(0),
284 m_timeout(3600), m_wantbuf(0)
286 m_internal
= new wxSockInternal
;
287 #if defined(__WXXT__) || defined(__WXMOTIF__) || defined(__WXGTK__)
288 m_internal
->sock_inputid
= 0;
289 m_internal
->sock_outputid
= 0;
290 m_internal
->sock_exceptid
= 0;
293 m_internal
->my_msg
= 0;
297 // --------------------------------------------------------------
299 // --------------------------------------------------------------
301 wxSocketBase::~wxSocketBase()
311 if (m_internal
->my_msg
)
312 m_handler
->DestroyMessage(m_internal
->my_msg
);
314 m_handler
->UnRegister(this);
316 m_states
.DeleteContents(TRUE
);
321 bool wxSocketBase::Close()
323 if (m_fd
!= INVALID_SOCKET
)
325 for (int i
=0;i
<3;i
++)
327 wxNode
*n
, *node
= req_list
[i
].First();
331 SockRequest
*req
= (SockRequest
*)node
->Data();
343 m_fd
= INVALID_SOCKET
;
350 // --------------------------------------------------------------
351 // wxSocketBase base IO function
352 // --------------------------------------------------------------
354 wxSocketBase
& wxSocketBase::Read(char* buffer
, size_t nbytes
)
356 m_lcount
= GetPushback(buffer
, nbytes
, FALSE
);
358 // If we have got the whole needed buffer or if we don't want to
359 // wait then it returns immediately.
360 if (!nbytes
|| (m_lcount
&& !(m_flags
& WAITALL
)) )
363 WantBuffer(buffer
, nbytes
, EVT_READ
);
368 wxSocketBase
& wxSocketBase::Peek(char* buffer
, size_t nbytes
)
370 size_t nbytes_old
= nbytes
;
372 nbytes
-= GetPushback(buffer
, nbytes
, TRUE
);
375 m_lcount
= nbytes_old
;
379 WantBuffer(buffer
, nbytes
, EVT_PEEK
);
384 wxSocketBase
& wxSocketBase::Write(const char *buffer
, size_t nbytes
)
386 WantBuffer((char *)buffer
, nbytes
, EVT_WRITE
);
390 wxSocketBase
& wxSocketBase::ReadMsg(char* buffer
, size_t nbytes
)
393 size_t len
, len2
, sig
;
395 Read((char *)&msg
, sizeof(msg
));
396 if (m_lcount
!= sizeof(msg
))
399 sig
= msg
.sig
[0] & 0xff;
400 sig
|= (size_t)(msg
.sig
[1] & 0xff) << 8;
401 sig
|= (size_t)(msg
.sig
[2] & 0xff) << 16;
402 sig
|= (size_t)(msg
.sig
[3] & 0xff) << 24;
404 if (sig
!= 0xfeeddead)
406 len
= msg
.len
[0] & 0xff;
407 len
|= (size_t)(msg
.len
[1] & 0xff) << 8;
408 len
|= (size_t)(msg
.len
[2] & 0xff) << 16;
409 len
|= (size_t)(msg
.len
[3] & 0xff) << 24;
416 if (Read(buffer
, len
).LastCount() != len
)
418 if (len2
&& (Read(NULL
, len2
).LastCount() != len2
))
420 if (Read((char *)&msg
, sizeof(msg
)).LastCount() != sizeof(msg
))
423 sig
= msg
.sig
[0] & 0xff;
424 sig
|= (size_t)(msg
.sig
[1] & 0xff) << 8;
425 sig
|= (size_t)(msg
.sig
[2] & 0xff) << 16;
426 sig
|= (size_t)(msg
.sig
[3] & 0xff) << 24;
428 if (sig
!= 0xdeadfeed)
434 wxSocketBase
& wxSocketBase::WriteMsg(const char *buffer
, size_t nbytes
)
438 msg
.sig
[0] = (char) 0xad;
439 msg
.sig
[1] = (char) 0xde;
440 msg
.sig
[2] = (char) 0xed;
441 msg
.sig
[3] = (char) 0xfe;
443 msg
.len
[0] = (char) nbytes
& 0xff;
444 msg
.len
[1] = (char) (nbytes
>> 8) & 0xff;
445 msg
.len
[2] = (char) (nbytes
>> 16) & 0xff;
446 msg
.len
[3] = (char) (nbytes
>> 24) & 0xff;
448 if (Write((char *)&msg
, sizeof(msg
)).LastCount() < sizeof(msg
))
450 if (Write(buffer
, nbytes
).LastCount() < nbytes
)
453 msg
.sig
[0] = (char) 0xed;
454 msg
.sig
[1] = (char) 0xfe;
455 msg
.sig
[2] = (char) 0xad;
456 msg
.sig
[3] = (char) 0xde;
457 msg
.len
[0] = msg
.len
[1] = msg
.len
[2] = msg
.len
[3] = (char) 0;
458 Write((char *)&msg
, sizeof(msg
));
463 wxSocketBase
& wxSocketBase::Unread(const char *buffer
, size_t nbytes
)
465 CreatePushbackAfter(buffer
, nbytes
);
469 bool wxSocketBase::IsData() const
482 FD_SET(m_fd
, &sock_set
);
483 select(FD_SETSIZE
, &sock_set
, NULL
, NULL
, &tv
);
484 return (FD_ISSET(m_fd
, &sock_set
) != 0);
487 // ---------------------------------------------------------------------
488 // --------- wxSocketBase Discard(): deletes all byte in the input queue
489 // ---------------------------------------------------------------------
490 void wxSocketBase::Discard()
492 #define MAX_BUFSIZE (10*1024)
493 char *my_data
= new char[MAX_BUFSIZE
];
494 size_t recv_size
= MAX_BUFSIZE
;
497 SetFlags((wxSockFlags
)(NOWAIT
| SPEED
));
499 while (recv_size
== MAX_BUFSIZE
)
501 recv_size
= Read(my_data
, MAX_BUFSIZE
).LastCount();
510 // If what? Who seems to need unsigned int?
511 // BTW uint isn't even defined on wxMSW for VC++ for some reason. Even if it
512 // were, getpeername/getsockname don't take unsigned int*, they take int*.
514 // Under glibc 2.0.7, socketbits.h declares socklen_t to be unsigned int
515 // and it uses *socklen_t as the 3rd parameter. Robert.
518 #define wxSOCKET_INT socklen_t
520 #define wxSOCKET_INT int
523 // --------------------------------------------------------------
524 // wxSocketBase socket info functions
525 // --------------------------------------------------------------
527 bool wxSocketBase::GetPeer(wxSockAddress
& addr_man
) const
529 struct sockaddr my_addr
;
530 wxSOCKET_INT len_addr
= sizeof(my_addr
);
535 if (getpeername(m_fd
, (struct sockaddr
*)&my_addr
, &len_addr
) < 0)
538 addr_man
.Disassemble(&my_addr
, len_addr
);
542 bool wxSocketBase::GetLocal(wxSockAddress
& addr_man
) const
544 struct sockaddr my_addr
;
545 wxSOCKET_INT len_addr
= sizeof(my_addr
);
550 if (getsockname(m_fd
, (struct sockaddr
*)&my_addr
, &len_addr
) < 0)
554 addr_man
.Disassemble(&my_addr
, len_addr
);
558 // --------------------------------------------------------------
559 // wxSocketBase wait functions
560 // --------------------------------------------------------------
562 void wxSocketBase::SaveState()
564 wxSockState
*state
= new wxSockState
;
566 state
->cbk_on
= m_cbkon
;
567 state
->cbk_set
= m_neededreq
;
569 state
->cdata
= m_cdata
;
570 state
->flags
= m_flags
;
571 state
->notif
= m_notifyme
;
573 m_states
.Append(state
);
576 void wxSocketBase::RestoreState()
580 node
= m_states
.Last();
584 wxSockState
*state
= (wxSockState
*)node
->Data();
586 SetFlags(state
->flags
);
587 m_neededreq
= state
->cbk_set
;
589 m_cdata
= state
->cdata
;
590 m_notifyme
= state
->notif
;
600 // --------------------------------------------------------------
601 // --------- wxSocketBase wait functions ------------------------
602 // --------------------------------------------------------------
604 bool wxSocketBase::_Wait(long seconds
, long microseconds
, int type
)
606 if ((!m_connected
&& !m_connecting
) || m_fd
< 0)
609 wxSockWakeUp
wakeup(this, &m_waitflags
, 0);
612 SetNotify((wxRequestNotify
)type
);
616 wakeup
.Start((int)(seconds
*1000 + (microseconds
/ 1000)), TRUE
);
618 m_waitflags
= 0x80 | type
;
619 while (m_waitflags
& 0x80)
624 if (m_waitflags
& 0x40)
634 bool wxSocketBase::Wait(long seconds
, long microseconds
)
636 return _Wait(seconds
, microseconds
, REQ_ACCEPT
| REQ_CONNECT
|
637 REQ_READ
| REQ_WRITE
| REQ_LOST
);
640 bool wxSocketBase::WaitForRead(long seconds
, long microseconds
)
642 return _Wait(seconds
, microseconds
, REQ_READ
| REQ_LOST
);
645 bool wxSocketBase::WaitForWrite(long seconds
, long microseconds
)
647 return _Wait(seconds
, microseconds
, REQ_WRITE
);
650 bool wxSocketBase::WaitForLost(long seconds
, long microseconds
)
652 return _Wait(seconds
, microseconds
, REQ_LOST
);
655 // --------------------------------------------------------------
656 // --------- wxSocketBase callback management -------------------
657 // --------------------------------------------------------------
659 #if defined(__WXMOTIF__) || defined(__WXXT__) || defined(__WXGTK__)
660 #if defined(__WXMOTIF__) || defined(__WXXT__)
661 static void wx_socket_read(XtPointer client
, int *fid
,
662 XtInputId
*WXUNUSED(id
))
665 static void wx_socket_read(gpointer client
, gint fd
,
666 GdkInputCondition
WXUNUSED(cond
))
670 wxSocketBase
*sock
= (wxSocketBase
*)client
;
674 i
= recv(fd
, &c
, 1, MSG_PEEK
);
676 if (i
== -1 && (sock
->NeededReq() & wxSocketBase::REQ_ACCEPT
))
678 sock
->OnRequest(wxSocketBase::EVT_ACCEPT
);
684 if (!(sock
->NeededReq() & wxSocketBase::REQ_READ
))
689 sock
->OnRequest(wxSocketBase::EVT_READ
);
693 if (!(sock
->NeededReq() & wxSocketBase::REQ_LOST
))
699 sock
->OnRequest(wxSocketBase::EVT_LOST
);
704 #if defined(__WXMOTIF__) || defined(__WXXT__)
705 static void wx_socket_write(XtPointer client
, int *WXUNUSED(fid
),
706 XtInputId
*WXUNUSED(id
))
708 static void wx_socket_write(gpointer client
, gint
WXUNUSED(fd
),
709 GdkInputCondition
WXUNUSED(cond
))
712 wxSocketBase
*sock
= (wxSocketBase
*)client
;
714 if (!sock
->IsConnected())
715 sock
->OnRequest(wxSocketBase::EVT_CONNECT
);
717 sock
->OnRequest(wxSocketBase::EVT_WRITE
);
722 Notify_value
wx_sock_read_xview (Notify_client client
, int fd
)
724 wxSocketBase
*sock
= (wxSocketBase
*)client
;
728 i
= recv(fd
, &c
, 1, MSG_PEEK
);
730 if (i
== -1 && (sock
->NeededReq() & wxSocketBase::REQ_ACCEPT
))
732 sock
->OnRequest(wxSocketBase::EVT_ACCEPT
);
739 if (!(sock
->NeededReq() & wxSocketBase::REQ_READ
))
740 return (Notify_value
) FALSE
;
742 sock
->OnRequest(wxSocketBase::EVT_READ
);
746 if (!(sock
->NeededReq() & wxSocketBase::REQ_LOST
))
749 sock
->OnRequest(wxSocketBase::EVT_LOST
);
752 return (Notify_value
) FALSE
;
755 Notify_value
wx_sock_write_xview (Notify_client client
, int fd
)
757 wxSocketBase
*sock
= (wxSocketBase
*)client
;
759 if (!sock
->IsConnected())
760 sock
->OnRequest(wxSocketBase::EVT_CONNECT
);
762 sock
->OnRequest(wxSocketBase::EVT_WRITE
);
764 return (Notify_value
) TRUE
;
768 wxSocketBase::wxRequestNotify
wxSocketBase::EventToNotify(wxRequestEvent evt
)
788 void wxSocketBase::SetFlags(wxSockFlags _flags
)
793 unsigned long flag
= 0;
794 ioctl(m_fd
, FIONBIO
, &flag
);
796 // SPEED and WAITALL are antagonists.
797 m_flags
= (wxSockFlags
)(m_flags
& ~WAITALL
);
803 unsigned long flag
= 1;
804 ioctl(m_fd
, FIONBIO
, &flag
);
808 void wxSocketBase::SetNotify(wxRequestNotify flags
)
810 wxRequestNotify old_needed_req
= m_neededreq
;
811 if (flags
& REQ_ACCEPT
)
813 /* Check if server */
814 if (!(GetClassInfo()->IsKindOf(CLASSINFO(wxSocketServer
))))
815 flags
&= ~REQ_ACCEPT
;
820 if (m_cbkon && old_needed_req != flags) seems to be wrong, Robert Roebling
824 if (old_needed_req
!= flags
)
828 void wxSocketBase::SetupCallbacks()
830 if (m_fd
== INVALID_SOCKET
|| !m_handler
|| (m_flags
& SPEED
))
833 #if defined(__WXMOTIF__) || defined(__WXXT__)
834 if (m_neededreq
& (REQ_ACCEPT
| REQ_READ
| REQ_LOST
))
836 if (m_internal
->sock_inputid
<= 0)
838 m_internal
->sock_inputid
= XtAppAddInput (wxAPP_CONTEXT
, m_fd
,
839 (XtPointer
*) XtInputReadMask
,
840 (XtInputCallbackProc
) wx_socket_read
,
846 if (m_internal
->sock_inputid
> 0)
848 XtRemoveInput(m_internal
->sock_inputid
);
849 m_internal
->sock_inputid
= 0;
853 if (m_neededreq
& (REQ_CONNECT
| REQ_WRITE
))
855 if (m_internal
->sock_outputid
<= 0)
857 m_internal
->sock_outputid
= XtAppAddInput (wxAPP_CONTEXT
, m_fd
,
858 (XtPointer
*) XtInputWriteMask
,
859 (XtInputCallbackProc
) wx_socket_write
,
865 if (m_internal
->sock_outputid
> 0)
867 XtRemoveInput(m_internal
->sock_outputid
);
868 m_internal
->sock_outputid
= 0;
875 if (m_neededreq
& (REQ_ACCEPT
| REQ_READ
| REQ_LOST
))
877 if (m_internal
->sock_inputid
<= 0)
879 m_internal
->sock_inputid
= gdk_input_add(m_fd
, GDK_INPUT_READ
,
880 wx_socket_read
, (gpointer
)this);
885 if (m_internal
->sock_inputid
> 0)
888 gdk_input_remove(m_internal->sock_inputid);
889 m_internal->sock_inputid = 0;
894 if (m_neededreq
& (REQ_CONNECT
| REQ_WRITE
))
896 if (m_internal
->sock_outputid
<= 0)
898 m_internal
->sock_outputid
= gdk_input_add(m_fd
, GDK_INPUT_WRITE
,
899 wx_socket_write
, (gpointer
)this);
904 if (m_internal
->sock_outputid
> 0)
907 gdk_input_remove(m_internal->sock_outputid);
908 m_internal->sock_outputid = 0;
918 if (m_neededreq
& REQ_READ
)
920 if (m_neededreq
& REQ_WRITE
)
922 if (m_neededreq
& REQ_LOST
)
924 if (m_neededreq
& REQ_ACCEPT
)
926 if (m_neededreq
& REQ_CONNECT
)
929 if (!m_internal
->my_msg
)
930 m_internal
->my_msg
= m_handler
->NewMessage(this);
931 WSAAsyncSelect(m_fd
, m_handler
->GetHWND(), m_internal
->my_msg
, mask
);
936 if (m_neededreq
& REQ_READ
)
938 if (m_neededreq
& REQ_WRITE
)
940 if (m_neededreq
& REQ_LOST
)
942 if (m_neededreq
& REQ_ACCEPT
)
944 if (m_neededreq
& REQ_CONNECT
)
947 GUSISetReference( m_fd
,mask
, this ) ;
948 unsigned long flag
= 1;
949 ioctl(m_fd
, FIONBIO
, &flag
);
952 m_processing
= FALSE
;
955 void wxSocketBase::DestroyCallbacks()
957 if (!m_cbkon
|| !m_handler
)
961 m_processing
= FALSE
;
962 #if defined(__WXMOTIF__) || defined(__WXXT__)
963 if (m_internal
->sock_inputid
> 0)
964 XtRemoveInput(m_internal
->sock_inputid
);
965 m_internal
->sock_inputid
= 0;
966 if (m_internal
->sock_outputid
> 0)
967 XtRemoveInput(m_internal
->sock_outputid
);
968 m_internal
->sock_outputid
= 0;
971 if (m_internal
->sock_inputid
> 0)
972 gdk_input_remove(m_internal
->sock_inputid
);
973 m_internal
->sock_inputid
= 0;
974 if (m_internal
->sock_outputid
> 0)
975 gdk_input_remove(m_internal
->sock_outputid
);
976 m_internal
->sock_outputid
= 0;
979 WSAAsyncSelect(m_fd
, m_handler
->GetHWND(), 0, 0);
982 GUSISetReference( m_fd
, 0 , 0 ) ;
983 int bottom
= wxMacNetEventsBottom
;
984 while ( wxMacNetEventsTop
!= bottom
)
986 // set all events that reference this socket to nil
987 if ( wxMacNetEventsReferences
[bottom
] == (void*) this )
988 wxMacNetEventsReferences
[bottom
] = NULL
;
990 if ( bottom
== kwxMacNetEventsMax
)
993 SetFlags( m_flags
) ;
997 void wxSocketBase::Notify(bool notify
)
999 if (m_notifyme
== notify
)
1005 m_notifyme
= notify
;
1008 void wxSocketBase::OnRequest(wxRequestEvent req_evt
)
1010 wxRequestNotify req_notif
= EventToNotify(req_evt
);
1012 // Mask the current event
1013 SetNotify(m_neededreq
& ~req_notif
);
1015 if (req_evt
<= EVT_WRITE
&& DoRequests(req_evt
))
1018 if (m_waitflags
& 0xF0)
1021 if ((m_waitflags
& 0x0F) == req_evt
)
1031 if (req_evt
== EVT_LOST
)
1033 m_connected
= FALSE
;
1037 OldOnNotify(req_evt
);
1040 SetNotify(m_neededreq
| req_notif
);
1043 wxSocketEvent::wxSocketEvent(int id
)
1046 wxEventType type
= (wxEventType
)wxEVT_SOCKET
;
1051 void wxSocketBase::OldOnNotify(wxRequestEvent evt
)
1053 wxSocketEvent
event(m_id
);
1055 event
.SetEventObject(this);
1056 event
.m_skevt
= evt
;
1057 ProcessEvent(event
);
1060 m_cbk(*this, evt
, m_cdata
);
1063 // --------------------------------------------------------------
1064 // --------- wxSocketBase functions [Callback, CallbackData] ----
1065 // --------------------------------------------------------------
1067 wxSocketBase::wxSockCbk
wxSocketBase::Callback(wxSocketBase::wxSockCbk _cbk
)
1069 wxSockCbk old_cbk
= m_cbk
;
1075 char *wxSocketBase::CallbackData(char *cdata_
)
1077 char *old_cdata
= m_cdata
;
1083 void wxSocketBase::SetEventHandler(wxEvtHandler
& h_evt
, int id
)
1085 SetNextHandler(&h_evt
);
1089 // --------------------------------------------------------------
1090 // --------- wxSocketBase pushback library ----------------------
1091 // --------------------------------------------------------------
1093 void wxSocketBase::CreatePushbackAfter(const char *buffer
, size_t size
)
1097 m_unread
= (char *) realloc(m_unread
, m_unrd_size
+size
);
1098 curr_pos
= m_unread
+ m_unrd_size
;
1100 memcpy(curr_pos
, buffer
, size
);
1101 m_unrd_size
+= size
;
1104 void wxSocketBase::CreatePushbackBefore(const char *buffer
, size_t size
)
1106 char *curr_pos
, *new_buf
;
1108 new_buf
= (char *) malloc(m_unrd_size
+size
);
1109 curr_pos
= new_buf
+ size
;
1111 memcpy(new_buf
, buffer
, size
);
1112 memcpy(curr_pos
, m_unread
, m_unrd_size
);
1116 m_unrd_size
+= size
;
1119 size_t wxSocketBase::GetPushback(char *buffer
, size_t size
, bool peek
)
1124 if (size
> m_unrd_size
)
1126 memcpy(buffer
, m_unread
, size
);
1129 m_unrd_size
-= size
;
1139 // --------------------------------------------------------------
1140 // --------- wxSocketBase "multi-thread" core -------------------
1141 // --------------------------------------------------------------
1143 bool wxSocketBase::DoRequests(wxRequestEvent req_flag
)
1145 wxNode
*node
= req_list
[req_flag
].First();
1152 SockRequest
*req
= (SockRequest
*)node
->Data();
1160 ret
= recv(m_fd
, req
->buffer
, req
->size
,
1161 (req
->type
== EVT_PEEK
) ? MSG_PEEK
: 0);
1169 if ((len
< req
->size
) && (m_flags
& WAITALL
))
1174 req
->auto_wakeup
->Start(m_timeout
*1000, TRUE
);
1175 req_list
[req_flag
].Insert(req
);
1185 ret
= send(m_fd
, req
->buffer
, req
->size
, 0);
1193 if ((len
< req
->size
) && (m_flags
& WAITALL
))
1198 req
->auto_wakeup
->Start(m_timeout
*1000, TRUE
);
1199 req_list
[req_flag
].Insert(req
);
1214 void wxSocketBase::WantSpeedBuffer(char *buffer
, size_t nbytes
,
1223 ret
= recv(m_fd
, buffer
, nbytes
,
1224 (evt
== EVT_PEEK
) ? MSG_PEEK
: 0);
1227 ret
= send(m_fd
, buffer
, nbytes
, 0);
1242 void wxSocketBase::WantBuffer(char *buffer
, size_t nbytes
,
1247 if (m_fd
== INVALID_SOCKET
|| !m_handler
|| !m_connected
)
1250 if (m_flags
& SPEED
)
1252 WantSpeedBuffer(buffer
, nbytes
, evt
);
1256 SockRequest
*buf
= new SockRequest
;
1257 wxSockWakeUp
s_wake(NULL
, (int *)&buf_timed_out
, (int)TRUE
);
1260 req_list
[evt
].Append(buf
);
1263 SetNotify(REQ_LOST
| EventToNotify(evt
));
1265 buf
->buffer
= buffer
;
1270 buf
->auto_wakeup
= &s_wake
;
1272 buf_timed_out
= FALSE
;
1274 s_wake
.Start(m_timeout
*1000, TRUE
);
1275 if (m_flags
& NOWAIT
)
1281 while (!buf
->done
&& !buf_timed_out
)
1285 m_lcount
= buf
->nbytes
;
1287 m_error
= ETIMEDOUT
;
1289 m_error
= buf
->error
;
1295 // --------------------------------------------------------------
1297 // --------------------------------------------------------------
1299 wxSocketServer::wxSocketServer(wxSockAddress
& addr_man
,
1300 wxSockFlags flags
) :
1301 wxSocketBase(flags
, SOCK_SERVER
)
1303 m_fd
= socket(addr_man
.GetFamily(), SOCK_STREAM
, 0);
1305 if (m_fd
== INVALID_SOCKET
)
1309 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (char*)&flag
, sizeof(int));
1311 struct sockaddr
*myaddr
;
1314 addr_man
.Build(myaddr
, len
);
1315 if (bind(m_fd
, myaddr
, addr_man
.SockAddrLen()) < 0)
1318 if (listen(m_fd
, 5) < 0) {
1319 m_fd
= INVALID_SOCKET
;
1324 // --------------------------------------------------------------
1325 // wxSocketServer Accept
1326 // --------------------------------------------------------------
1328 bool wxSocketServer::AcceptWith(wxSocketBase
& sock
)
1332 if ((fd2
= accept(m_fd
, 0, 0)) < 0)
1335 struct linger linger
;
1337 linger
.l_linger
= 1;
1339 setsockopt(fd2
, SOL_SOCKET
, SO_LINGER
, (char*)&linger
, sizeof(linger
));
1342 setsockopt(fd2
, SOL_SOCKET
, SO_KEEPALIVE
, (char*)&flag
, sizeof(int));
1344 if (!(sock
.m_flags
& SPEED
))
1346 unsigned long flag2
= 1;
1347 ioctl(fd2
, FIONBIO
, &flag2
);
1350 sock
.m_type
= SOCK_INTERNAL
;
1352 sock
.m_connected
= TRUE
;
1357 wxSocketBase
*wxSocketServer::Accept()
1359 wxSocketBase
* sock
= new wxSocketBase();
1361 sock
->SetFlags((wxSockFlags
)m_flags
);
1363 if (!AcceptWith(*sock
))
1367 m_handler
->Register(sock
);
1372 // --------------------------------------------------------------
1373 // wxSocketServer callbacks
1374 // --------------------------------------------------------------
1376 void wxSocketServer::OnRequest(wxRequestEvent evt
)
1378 if (evt
== EVT_ACCEPT
)
1380 OldOnNotify(EVT_ACCEPT
);
1384 // --------------------------------------------------------------
1386 // --------------------------------------------------------------
1388 // --------- wxSocketClient CONSTRUCTOR -------------------------
1389 // --------------------------------------------------------------
1390 wxSocketClient::wxSocketClient(wxSockFlags _flags
) :
1391 wxSocketBase(_flags
, SOCK_CLIENT
)
1395 // --------------------------------------------------------------
1396 // --------- wxSocketClient DESTRUCTOR --------------------------
1397 // --------------------------------------------------------------
1398 wxSocketClient::~wxSocketClient()
1402 // --------------------------------------------------------------
1403 // --------- wxSocketClient Connect functions -------------------
1404 // --------------------------------------------------------------
1405 bool wxSocketClient::Connect(wxSockAddress
& addr_man
, bool WXUNUSED(wait
) )
1407 struct linger linger
;
1412 m_fd
= socket(addr_man
.GetFamily(), SOCK_STREAM
, 0);
1417 m_connected
= FALSE
;
1420 linger
.l_linger
= 5;
1421 setsockopt(m_fd
, SOL_SOCKET
, SO_LINGER
, (char*)&linger
, sizeof(linger
));
1423 // Stay in touch with the state of things...
1425 unsigned long flag
= 1;
1426 setsockopt(m_fd
, SOL_SOCKET
, SO_KEEPALIVE
, (char*)&flag
, sizeof(int));
1428 // Disable the nagle algorithm, which delays sends till the
1429 // buffer is full (or a certain time period has passed?)...
1431 #if defined(__WINDOWS__) || (defined(IPPROTO_TCP) && defined(TCP_NODELAY))
1433 setsockopt(m_fd
, IPPROTO_TCP
, TCP_NODELAY
, (char*)&flag
, sizeof(int));
1436 struct sockaddr
*remote
;
1439 addr_man
.Build(remote
, len
);
1441 if (connect(m_fd
, remote
, len
) != 0)
1444 if (!(m_flags
& SPEED
))
1447 ioctl(m_fd
, FIONBIO
, &flag
);
1456 bool wxSocketClient::WaitOnConnect(long seconds
, long microseconds
)
1458 int ret
= _Wait(seconds
, microseconds
, REQ_CONNECT
| REQ_LOST
);
1466 void wxSocketClient::OnRequest(wxRequestEvent evt
)
1468 if (evt
== EVT_CONNECT
)
1473 SetNotify(m_neededreq
& ~REQ_CONNECT
);
1479 OldOnNotify(EVT_CONNECT
);
1485 wxSocketBase::OnRequest(evt
);
1488 /////////////////////////////////////////////////////////////////
1489 // wxSocketHandler ///////////////////////////////////////////////
1490 /////////////////////////////////////////////////////////////////
1492 wxSocketHandler
*wxSocketHandler::master
= NULL
;
1493 #if defined(__WINDOWS__)
1494 static int win_initialized
= 0;
1497 // --------------------------------------------------------------
1498 // --------- wxSocketHandler CONSTRUCTOR ------------------------
1499 // --------------------------------------------------------------
1502 extern "C" int updatestatus(int s
) ;
1504 void wxMacSocketOnRequestProc( void *refcon
, short event
)
1508 wxSocketBase
*sock
= (wxSocketBase
*) refcon
;
1510 wxSocketBase::wxRequestEvent sk_req
;
1518 sk_req
= wxSocketBase::EVT_READ
;
1519 sock
->OnRequest(sk_req
);
1522 sk_req
= wxSocketBase::EVT_WRITE
;
1523 sock
->OnRequest(sk_req
);
1526 sk_req
= wxSocketBase::EVT_LOST
;
1527 sock
->OnRequest(sk_req
);
1530 sk_req
= wxSocketBase::EVT_ACCEPT
;
1531 sock
->OnRequest(sk_req
);
1534 sk_req
= wxSocketBase::EVT_CONNECT
;
1535 sock
->OnRequest(sk_req
);
1540 updatestatus ( sock
->m_fd
) ;
1544 void wxMacSocketHandlerProc( void *refcon
, short event
)
1546 wxMacNetEventsReferences
[wxMacNetEventsTop
] = refcon
;
1547 wxMacNetEventsEvents
[wxMacNetEventsTop
] = event
;
1549 // clumsy construct in order to never have a incorrect wxMacNetEventsTop (above limits)
1551 if ( wxMacNetEventsTop
+ 1 == kwxMacNetEventsMax
)
1552 wxMacNetEventsTop
= 0 ;
1554 wxMacNetEventsTop
++ ;
1560 extern char wxPanelClassName
[];
1562 LRESULT APIENTRY _EXPORT
wxSocketHandlerWndProc(HWND hWnd
, UINT message
,
1563 WPARAM wParam
, LPARAM lParam
)
1565 if(message
==WM_DESTROY
)
1567 ::SetWindowLong(hWnd
, GWL_WNDPROC
, (LONG
) DefWindowProc
);
1568 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
1570 wxSocketHandler
*h_sock
= (wxSocketHandler
*)GetWindowLong(hWnd
, GWL_USERDATA
);
1571 wxNode
*node
= h_sock
->smsg_list
->Find(message
);
1573 wxSocketBase::wxRequestEvent sk_req
;
1574 UINT event
= WSAGETSELECTEVENT(lParam
);
1577 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
1579 sock
= (wxSocketBase
*)node
->Data();
1583 sk_req
= wxSocketBase::EVT_READ
;
1586 sk_req
= wxSocketBase::EVT_WRITE
;
1589 sk_req
= wxSocketBase::EVT_LOST
;
1592 sk_req
= wxSocketBase::EVT_ACCEPT
;
1595 sk_req
= wxSocketBase::EVT_CONNECT
;
1598 sock
->OnRequest(sk_req
);
1603 FARPROC wxSocketSubClassProc
= NULL
;
1607 wxSocketHandler::wxSocketHandler()
1609 #if defined(__WINDOWS__)
1610 if (!win_initialized
)
1614 WSAStartup((1 << 8) | 1, &wsaData
);
1615 win_initialized
= 1;
1617 internal
= new wxSockHandlerInternal
;
1618 internal
->sockWin
= ::CreateWindow(wxPanelClassName
, NULL
, 0,
1619 0, 0, 0, 0, NULL
, (HMENU
) NULL
,
1622 // Subclass the window
1623 if (!wxSocketSubClassProc
)
1624 wxSocketSubClassProc
= MakeProcInstance((FARPROC
) wxSocketHandlerWndProc
, wxhInstance
);
1625 ::SetWindowLong(internal
->sockWin
, GWL_WNDPROC
, (LONG
) wxSocketSubClassProc
);
1626 ::SetWindowLong(internal
->sockWin
, GWL_USERDATA
, (LONG
) this);
1628 internal
->firstAvailableMsg
= 5000;
1629 smsg_list
= new wxList(wxKEY_INTEGER
);
1635 signal(SIGPIPE
, SIG_IGN
);
1639 // --------------------------------------------------------------
1640 // --------- wxSocketHandler DESTRUCTOR -------------------------
1641 // --------------------------------------------------------------
1642 wxSocketHandler::~wxSocketHandler()
1644 wxNode
*next_node
, *node
= socks
->First();
1648 wxSocketBase
* sock
= (wxSocketBase
*)node
->Data();
1651 next_node
= node
->Next();
1661 ::DestroyWindow(internal
->sockWin
);
1663 win_initialized
= 0;
1669 // --------------------------------------------------------------
1670 // --------- wxSocketHandler registering functions --------------
1671 // --------------------------------------------------------------
1673 void wxSocketHandler::Register(wxSocketBase
* sock
)
1677 for (node
= socks
->First(); node
!= NULL
; node
= node
->Next())
1679 wxSocketBase
* s
= (wxSocketBase
*)node
->Data();
1687 socks
->Append(sock
);
1688 sock
->SetHandler(this);
1689 sock
->SetupCallbacks();
1693 void wxSocketHandler::UnRegister(wxSocketBase
* sock
)
1697 for (node
= socks
->First(); node
; node
= node
->Next())
1699 wxSocketBase
* s
= (wxSocketBase
*)node
->Data();
1704 sock
->DestroyCallbacks();
1705 sock
->SetHandler(NULL
);
1711 unsigned long wxSocketHandler::Count() const
1713 return socks
->Number();
1716 // --------------------------------------------------------------
1717 // --------- wxSocketHandler "big" wait functions ---------------
1718 // --------------------------------------------------------------
1719 void handler_cbk(wxSocketBase
& sock
,
1720 wxSocketBase::wxRequestEvent
WXUNUSED(flags
),
1723 int *a_wait
= (int *)cdata
;
1729 int wxSocketHandler::Wait(long seconds
, long microseconds
)
1733 wxSockWakeUp
s_wake(NULL
, &on_wait
, -2);
1736 for (node
= socks
->First(), i
=0; node
; node
= node
->Next(), i
++)
1738 wxSocketBase
*sock
= (wxSocketBase
*)node
->Data();
1742 sock
->SetupCallbacks();
1744 sock
->Callback(handler_cbk
);
1745 sock
->CallbackData((char *)&on_wait
);
1749 s_wake
.Start((seconds
*1000) + (microseconds
/1000), TRUE
);
1754 for (node
= socks
->First(), i
=0; node
; node
= node
->Next(), i
++)
1756 wxSocketBase
*sock
= (wxSocketBase
*)node
->Data();
1758 sock
->RestoreState();
1767 void wxSocketHandler::YieldSock()
1771 for (node
= socks
->First(); node
; node
= node
->Next() )
1773 wxSocketBase
*sock
= (wxSocketBase
*)node
->Data();
1777 sock
->SetFlags(wxSocketBase::SPEED
);
1779 sock
->DoRequests(wxSocketBase::EVT_READ
);
1780 sock
->DoRequests(wxSocketBase::EVT_WRITE
);
1782 sock
->RestoreState();
1786 // --------------------------------------------------------------
1787 // --------- wxSocketHandler: create and register the socket ----
1788 // --------------------------------------------------------------
1789 wxSocketServer
*wxSocketHandler::CreateServer(wxSockAddress
& addr
,
1790 wxSocketBase::wxSockFlags flags
)
1792 wxSocketServer
*serv
= new wxSocketServer(addr
, flags
);
1798 wxSocketClient
*wxSocketHandler::CreateClient(wxSocketBase::wxSockFlags flags
)
1800 wxSocketClient
*client
= new wxSocketClient(flags
);
1807 // --------------------------------------------------------------
1808 // --------- wxSocketHandler: Windows specific methods ----------
1809 // --------------------------------------------------------------
1811 UINT
wxSocketHandler::NewMessage(wxSocketBase
*sock
)
1813 internal
->firstAvailableMsg
++;
1814 smsg_list
->Append(internal
->firstAvailableMsg
, sock
);
1815 return internal
->firstAvailableMsg
;
1818 void wxSocketHandler::DestroyMessage(UINT msg
)
1820 wxNode
*node
= smsg_list
->Find(msg
);
1824 HWND
wxSocketHandler::GetHWND() const
1826 return internal
->sockWin
;
1831 bool wxSocketModule::OnInit()
1833 wxSocketHandler::master
= new wxSocketHandler();
1837 void wxSocketModule::OnExit()
1839 delete wxSocketHandler::master
;
1840 wxSocketHandler::master
= NULL
;
1844 void wxMacProcessSocketEvents() ;
1845 void wxMacProcessEvents()
1847 wxMacProcessSocketEvents() ;
1848 (*GUSISpin
)(SP_MISC
, 0) ;
1851 void wxMacProcessSocketEvents()
1853 while ( wxMacNetEventsTop
!= wxMacNetEventsBottom
)
1855 // consume event at wxMacNetEventsBottom
1856 wxMacSocketOnRequestProc(wxMacNetEventsReferences
[wxMacNetEventsBottom
] , wxMacNetEventsEvents
[wxMacNetEventsBottom
] ) ;
1857 wxMacNetEventsBottom
++ ;
1858 if ( wxMacNetEventsBottom
== kwxMacNetEventsMax
)
1859 wxMacNetEventsBottom
= 0 ;