1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/socket.cpp
3 // Purpose: Socket handler classes
4 // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia
6 // Copyright: (C) 1999-1997, Guilhem Lavaux
7 // (C) 2000-1999, Guillermo Rodriguez Garcia
9 // License: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ==========================================================================
14 // ==========================================================================
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/socket.h"
28 #include "wx/object.h"
29 #include "wx/string.h"
36 #include "wx/module.h"
39 #include "wx/apptrait.h"
40 #include "wx/sckaddr.h"
41 #include "wx/stopwatch.h"
42 #include "wx/thread.h"
43 #include "wx/evtloop.h"
44 #include "wx/private/fd.h"
46 // DLL options compatibility check:
48 WX_CHECK_BUILD_OPTIONS("wxNet")
50 // --------------------------------------------------------------------------
51 // macros and constants
52 // --------------------------------------------------------------------------
55 #define MAX_DISCARD_SIZE (10 * 1024)
57 #define wxTRACE_Socket _T("wxSocket")
59 // --------------------------------------------------------------------------
61 // --------------------------------------------------------------------------
63 IMPLEMENT_CLASS(wxSocketBase
, wxObject
)
64 IMPLEMENT_CLASS(wxSocketServer
, wxSocketBase
)
65 IMPLEMENT_CLASS(wxSocketClient
, wxSocketBase
)
66 IMPLEMENT_CLASS(wxDatagramSocket
, wxSocketBase
)
67 IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent
, wxEvent
)
69 // --------------------------------------------------------------------------
71 // --------------------------------------------------------------------------
73 class wxSocketState
: public wxObject
76 wxSocketFlags m_flags
;
77 wxSocketEventFlags m_eventmask
;
82 wxSocketState() : wxObject() {}
84 DECLARE_NO_COPY_CLASS(wxSocketState
)
87 // Conditionally make the socket non-blocking for the lifetime of this object.
88 class wxSocketUnblocker
91 wxSocketUnblocker(GSocket
*socket
, bool unblock
= true)
96 m_socket
->SetNonBlocking(true);
102 m_socket
->SetNonBlocking(false);
106 GSocket
* const m_socket
;
109 DECLARE_NO_COPY_CLASS(wxSocketUnblocker
)
112 // ============================================================================
114 // ============================================================================
116 GSocketManager
*GSocketManager::ms_manager
= NULL
;
119 void GSocketManager::Set(GSocketManager
*manager
)
121 wxASSERT_MSG( !ms_manager
, "too late to set manager now" );
123 ms_manager
= manager
;
127 void GSocketManager::Init()
129 wxASSERT_MSG( !ms_manager
, "shouldn't be initialized twice" );
132 Details: Initialize() creates a hidden window as a sink for socket
133 events, such as 'read completed'. wxMSW has only one message loop
134 for the main thread. If Initialize is called in a secondary thread,
135 the socket window will be created for the secondary thread, but
136 since there is no message loop on this thread, it will never
137 receive events and all socket operations will time out.
138 BTW, the main thread must not be stopped using sleep or block
139 on a semaphore (a bad idea in any case) or socket operations
142 On the Mac side, Initialize() stores a pointer to the CFRunLoop for
143 the main thread. Because secondary threads do not have run loops,
144 adding event notifications to the "Current" loop would have no
145 effect at all, events would never fire.
147 wxASSERT_MSG( wxIsMainThread(),
148 "sockets must be initialized from the main thread" );
150 wxAppConsole
* const app
= wxAppConsole::GetInstance();
151 wxCHECK_RET( app
, "sockets can't be initialized without wxApp" );
153 ms_manager
= app
->GetTraits()->GetSocketManager();
156 // ==========================================================================
158 // ==========================================================================
161 GSocket
*GSocketBase::Create(wxSocketBase
& wxsocket
)
163 GSocket
* const newsocket
= new GSocket(wxsocket
);
164 if ( !GSocketManager::Get()->Init_Socket(newsocket
) )
173 GSocketBase::GSocketBase(wxSocketBase
& wxsocket
)
174 : m_wxsocket(&wxsocket
)
176 m_fd
= INVALID_SOCKET
;
180 m_error
= GSOCK_NOERROR
;
183 m_non_blocking
= false;
185 SetTimeout(wxsocket
.GetTimeout() * 1000);
187 m_establishing
= false;
191 m_initialRecvBufferSize
= -1;
192 m_initialSendBufferSize
= -1;
195 GSocketBase::~GSocketBase()
197 if (m_fd
!= INVALID_SOCKET
)
201 GAddress_destroy(m_local
);
204 GAddress_destroy(m_peer
);
206 // cast is ok as all GSocketBase objects we have in our code are really
208 GSocketManager::Get()->Destroy_Socket(static_cast<GSocket
*>(this));
212 * Disallow further read/write operations on this socket, close
213 * the fd and disable all callbacks.
215 void GSocketBase::Shutdown()
217 if ( m_fd
!= INVALID_SOCKET
)
219 shutdown(m_fd
, 1 /* SD_SEND */);
223 m_detected
= GSOCK_LOST_FLAG
;
226 /* GSocket_SetTimeout:
227 * Sets the timeout for blocking calls. Time is expressed in
230 void GSocketBase::SetTimeout(unsigned long millis
)
233 m_timeout
.tv_sec
= (millis
/ 1000);
234 m_timeout
.tv_usec
= (millis
% 1000) * 1000;
240 void GSocketBase::NotifyOnStateChange(GSocketEvent event
)
242 // GSocketEvent and wxSocketNotify enums have the same elements with the
244 m_wxsocket
->OnRequest(static_cast<wxSocketNotify
>(event
));
247 // ==========================================================================
249 // ==========================================================================
251 // --------------------------------------------------------------------------
252 // Initialization and shutdown
253 // --------------------------------------------------------------------------
255 // FIXME-MT: all this is MT-unsafe, of course, we should protect all accesses
256 // to m_countInit with a crit section
257 size_t wxSocketBase::m_countInit
= 0;
259 bool wxSocketBase::IsInitialized()
261 return m_countInit
> 0;
264 bool wxSocketBase::Initialize()
266 if ( !m_countInit
++ )
268 if ( !GSocket_Init() )
279 void wxSocketBase::Shutdown()
281 // we should be initialized
282 wxASSERT_MSG( m_countInit
> 0, _T("extra call to Shutdown()") );
283 if ( --m_countInit
== 0 )
289 // --------------------------------------------------------------------------
291 // --------------------------------------------------------------------------
293 void wxSocketBase::Init()
296 m_type
= wxSOCKET_UNINIT
;
308 m_beingDeleted
= false;
322 if ( !IsInitialized() )
324 // this Initialize() will be undone by wxSocketModule::OnExit(), all
325 // the other calls to it should be matched by a call to Shutdown()
330 wxSocketBase::wxSocketBase()
335 wxSocketBase::wxSocketBase(wxSocketFlags flags
, wxSocketType type
)
344 wxSocketBase::~wxSocketBase()
346 // Just in case the app called Destroy() *and* then deleted the socket
347 // immediately: don't leave dangling pointers.
348 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
350 traits
->RemoveFromPendingDelete(this);
352 // Shutdown and close the socket
356 // Destroy the GSocket object
360 // Free the pushback buffer
365 bool wxSocketBase::Destroy()
367 // Delayed destruction: the socket will be deleted during the next idle
368 // loop iteration. This ensures that all pending events have been
370 m_beingDeleted
= true;
372 // Shutdown and close the socket
375 // Supress events from now on
378 // schedule this object for deletion
379 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
382 // let the traits object decide what to do with us
383 traits
->ScheduleForDestroy(this);
385 else // no app or no traits
387 // in wxBase we might have no app object at all, don't leak memory
394 // --------------------------------------------------------------------------
396 // --------------------------------------------------------------------------
398 // The following IO operations update m_error and m_lcount:
399 // {Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard}
401 // TODO: Should Connect, Accept and AcceptWith update m_error?
403 bool wxSocketBase::Close()
405 // Interrupt pending waits
409 m_socket
->Shutdown();
412 m_establishing
= false;
416 wxSocketBase
& wxSocketBase::Read(void* buffer
, wxUint32 nbytes
)
421 m_lcount
= DoRead(buffer
, nbytes
);
423 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
424 if (m_flags
& wxSOCKET_WAITALL
)
425 m_error
= (m_lcount
!= nbytes
);
427 m_error
= (m_lcount
== 0);
429 // Allow read events from now on
435 wxUint32
wxSocketBase::DoRead(void* buffer_
, wxUint32 nbytes
)
437 // We use pointer arithmetic here which doesn't work with void pointers.
438 char *buffer
= static_cast<char *>(buffer_
);
440 // Try the push back buffer first, even before checking whether the socket
441 // is valid to allow reading previously pushed back data from an already
443 wxUint32 total
= GetPushback(buffer
, nbytes
, false);
447 // If it's indeed closed or if read everything, there is nothing more to do.
448 if ( !m_socket
|| !nbytes
)
451 wxCHECK_MSG( buffer
, 0, "NULL buffer" );
454 // wxSOCKET_NOWAIT overrides all the other flags and means that we are
455 // polling the socket and don't block at all.
456 if ( m_flags
& wxSOCKET_NOWAIT
)
458 wxSocketUnblocker
unblock(m_socket
);
459 int ret
= m_socket
->Read(buffer
, nbytes
);
465 else // blocking socket
469 // Wait until socket becomes ready for reading dispatching the GUI
470 // events in the meanwhile unless wxSOCKET_BLOCK was explicitly
471 // specified to disable this.
472 if ( !(m_flags
& wxSOCKET_BLOCK
) && !WaitForRead() )
475 const int ret
= m_socket
->Read(buffer
, nbytes
);
478 // for connection-oriented (e.g. TCP) sockets we can only read
479 // 0 bytes if the other end has been closed, and for
480 // connectionless ones (UDP) this flag doesn't make sense
481 // anyhow so we can set it to true too without doing any harm
488 // this will be always interpreted as error by Read()
494 // If wxSOCKET_WAITALL is not set, we can leave now as we did read
495 // something and we don't need to wait for all nbytes bytes to be
497 if ( !(m_flags
& wxSOCKET_WAITALL
) )
500 // Otherwise continue reading until we do read everything.
512 wxSocketBase
& wxSocketBase::ReadMsg(void* buffer
, wxUint32 nbytes
)
514 wxUint32 len
, len2
, sig
, total
;
519 unsigned char sig
[4];
520 unsigned char len
[4];
529 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
531 if (DoRead(&msg
, sizeof(msg
)) != sizeof(msg
))
534 sig
= (wxUint32
)msg
.sig
[0];
535 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
536 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
537 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
539 if (sig
!= 0xfeeddead)
541 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
545 len
= (wxUint32
)msg
.len
[0];
546 len
|= (wxUint32
)(msg
.len
[1] << 8);
547 len
|= (wxUint32
)(msg
.len
[2] << 16);
548 len
|= (wxUint32
)(msg
.len
[3] << 24);
558 // Don't attempt to read if the msg was zero bytes long.
561 total
= DoRead(buffer
, len
);
569 char *discard_buffer
= new char[MAX_DISCARD_SIZE
];
572 // NOTE: discarded bytes don't add to m_lcount.
575 discard_len
= ((len2
> MAX_DISCARD_SIZE
)? MAX_DISCARD_SIZE
: len2
);
576 discard_len
= DoRead(discard_buffer
, (wxUint32
)discard_len
);
577 len2
-= (wxUint32
)discard_len
;
579 while ((discard_len
> 0) && len2
);
581 delete [] discard_buffer
;
586 if (DoRead(&msg
, sizeof(msg
)) != sizeof(msg
))
589 sig
= (wxUint32
)msg
.sig
[0];
590 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
591 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
592 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
594 if (sig
!= 0xdeadfeed)
596 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
612 wxSocketBase
& wxSocketBase::Peek(void* buffer
, wxUint32 nbytes
)
617 m_lcount
= DoRead(buffer
, nbytes
);
618 Pushback(buffer
, m_lcount
);
620 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
621 if (m_flags
& wxSOCKET_WAITALL
)
622 m_error
= (m_lcount
!= nbytes
);
624 m_error
= (m_lcount
== 0);
626 // Allow read events again
632 wxSocketBase
& wxSocketBase::Write(const void *buffer
, wxUint32 nbytes
)
637 m_lcount
= DoWrite(buffer
, nbytes
);
639 // If in wxSOCKET_WAITALL mode, all bytes should have been written.
640 if (m_flags
& wxSOCKET_WAITALL
)
641 m_error
= (m_lcount
!= nbytes
);
643 m_error
= (m_lcount
== 0);
645 // Allow write events again
651 // This function is a mirror image of DoRead() except that it doesn't use the
652 // push back buffer, please see comments there
653 wxUint32
wxSocketBase::DoWrite(const void *buffer_
, wxUint32 nbytes
)
655 const char *buffer
= static_cast<const char *>(buffer_
);
657 // Return if there is nothing to read or the socket is (already?) closed.
658 if ( !m_socket
|| !nbytes
)
661 wxCHECK_MSG( buffer
, 0, "NULL buffer" );
664 if ( m_flags
& wxSOCKET_NOWAIT
)
666 wxSocketUnblocker
unblock(m_socket
);
667 const int ret
= m_socket
->Write(buffer
, nbytes
);
671 else // blocking socket
675 if ( !(m_flags
& wxSOCKET_BLOCK
) && !WaitForWrite() )
678 const int ret
= m_socket
->Write(buffer
, nbytes
);
689 if ( !(m_flags
& wxSOCKET_WAITALL
) )
703 wxSocketBase
& wxSocketBase::WriteMsg(const void *buffer
, wxUint32 nbytes
)
709 unsigned char sig
[4];
710 unsigned char len
[4];
718 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
720 msg
.sig
[0] = (unsigned char) 0xad;
721 msg
.sig
[1] = (unsigned char) 0xde;
722 msg
.sig
[2] = (unsigned char) 0xed;
723 msg
.sig
[3] = (unsigned char) 0xfe;
725 msg
.len
[0] = (unsigned char) (nbytes
& 0xff);
726 msg
.len
[1] = (unsigned char) ((nbytes
>> 8) & 0xff);
727 msg
.len
[2] = (unsigned char) ((nbytes
>> 16) & 0xff);
728 msg
.len
[3] = (unsigned char) ((nbytes
>> 24) & 0xff);
730 if (DoWrite(&msg
, sizeof(msg
)) < sizeof(msg
))
733 total
= DoWrite(buffer
, nbytes
);
738 msg
.sig
[0] = (unsigned char) 0xed;
739 msg
.sig
[1] = (unsigned char) 0xfe;
740 msg
.sig
[2] = (unsigned char) 0xad;
741 msg
.sig
[3] = (unsigned char) 0xde;
745 msg
.len
[3] = (char) 0;
747 if ((DoWrite(&msg
, sizeof(msg
))) < sizeof(msg
))
761 wxSocketBase
& wxSocketBase::Unread(const void *buffer
, wxUint32 nbytes
)
764 Pushback(buffer
, nbytes
);
772 wxSocketBase
& wxSocketBase::Discard()
774 char *buffer
= new char[MAX_DISCARD_SIZE
];
781 SetFlags(wxSOCKET_NOWAIT
);
785 ret
= DoRead(buffer
, MAX_DISCARD_SIZE
);
788 while (ret
== MAX_DISCARD_SIZE
);
794 // Allow read events again
800 // --------------------------------------------------------------------------
802 // --------------------------------------------------------------------------
805 * Polls the socket to determine its status. This function will
806 * check for the events specified in the 'flags' parameter, and
807 * it will return a mask indicating which operations can be
808 * performed. This function won't block, regardless of the
809 * mode (blocking | nonblocking) of the socket.
811 GSocketEventFlags
GSocketBase::Select(GSocketEventFlags flags
)
815 GSocketEventFlags result
= 0;
822 return (GSOCK_LOST_FLAG
& flags
);
824 /* Do not use a static struct, Linux can garble it */
829 wxFD_ZERO(&writefds
);
830 wxFD_ZERO(&exceptfds
);
831 wxFD_SET(m_fd
, &readfds
);
832 if (flags
& GSOCK_OUTPUT_FLAG
|| flags
& GSOCK_CONNECTION_FLAG
)
833 wxFD_SET(m_fd
, &writefds
);
834 wxFD_SET(m_fd
, &exceptfds
);
836 /* Check 'sticky' CONNECTION flag first */
837 result
|= GSOCK_CONNECTION_FLAG
& m_detected
;
839 /* If we have already detected a LOST event, then don't try
840 * to do any further processing.
842 if ((m_detected
& GSOCK_LOST_FLAG
) != 0)
844 m_establishing
= false;
845 return (GSOCK_LOST_FLAG
& flags
);
849 if (select(m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) < 0)
851 /* What to do here? */
852 return (result
& flags
);
855 /* Check for exceptions and errors */
856 if (wxFD_ISSET(m_fd
, &exceptfds
))
858 m_establishing
= false;
859 m_detected
= GSOCK_LOST_FLAG
;
861 /* LOST event: Abort any further processing */
862 return (GSOCK_LOST_FLAG
& flags
);
865 /* Check for readability */
866 if (wxFD_ISSET(m_fd
, &readfds
))
868 result
|= GSOCK_INPUT_FLAG
;
870 if (m_server
&& m_stream
)
872 /* This is a TCP server socket that detected a connection.
873 While the INPUT_FLAG is also set, it doesn't matter on
874 this kind of sockets, as we can only Accept() from them. */
875 m_detected
|= GSOCK_CONNECTION_FLAG
;
879 /* Check for writability */
880 if (wxFD_ISSET(m_fd
, &writefds
))
882 if (m_establishing
&& !m_server
)
885 SOCKOPTLEN_T len
= sizeof(error
);
886 m_establishing
= false;
887 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
891 m_detected
= GSOCK_LOST_FLAG
;
893 /* LOST event: Abort any further processing */
894 return (GSOCK_LOST_FLAG
& flags
);
898 m_detected
|= GSOCK_CONNECTION_FLAG
;
903 result
|= GSOCK_OUTPUT_FLAG
;
907 return (result
| m_detected
) & flags
;
910 // All Wait functions poll the socket using GSocket_Select() to
911 // check for the specified combination of conditions, until one
912 // of these conditions become true, an error occurs, or the
913 // timeout elapses. The polling loop runs the event loop so that
914 // this won't block the GUI.
917 wxSocketBase::DoWait(long seconds
, long milliseconds
, wxSocketEventFlags flags
)
919 wxCHECK_MSG( m_socket
, false, "can't wait on invalid socket" );
921 // This can be set to true from Interrupt() to exit this function a.s.a.p.
925 // Use either the provided timeout or the default timeout value associated
928 // TODO: allow waiting forever, see #9443
929 const long timeout
= seconds
== -1 ? m_timeout
* 1000
930 : seconds
* 1000 + milliseconds
;
931 const wxMilliClock_t timeEnd
= wxGetLocalTimeMillis() + timeout
;
933 // Get the active event loop which we'll use for the message dispatching
934 // when running in the main thread
935 wxEventLoopBase
*eventLoop
;
936 if ( wxIsMainThread() )
938 eventLoop
= wxEventLoop::GetActive();
940 else // in worker thread
942 // We never dispatch messages from threads other than the main one.
946 // Wait in an active polling loop: notice that the loop is executed at
947 // least once, even if timeout is 0 (i.e. polling).
948 bool gotEvent
= false;
951 // We always stop waiting when the connection is lost as it doesn't
952 // make sense to continue further, even if GSOCK_LOST_FLAG is not
953 // specified in flags to wait for.
954 const GSocketEventFlags
955 result
= m_socket
->Select(flags
| GSOCK_LOST_FLAG
);
957 // Incoming connection (server) or connection established (client)?
958 if ( result
& GSOCK_CONNECTION_FLAG
)
961 m_establishing
= false;
966 // Data available or output buffer ready?
967 if ( (result
& GSOCK_INPUT_FLAG
) || (result
& GSOCK_OUTPUT_FLAG
) )
974 if ( result
& GSOCK_LOST_FLAG
)
977 m_establishing
= false;
978 if ( flags
& GSOCK_LOST_FLAG
)
987 const wxMilliClock_t timeNow
= wxGetLocalTimeMillis();
988 if ( timeNow
>= timeEnd
)
993 // This function is only called if wxSOCKET_BLOCK flag was not used
994 // and so we should dispatch the events if there is an event loop
995 // capable of doing it.
996 if ( eventLoop
->Pending() )
997 eventLoop
->Dispatch();
1000 else // no event loop or waiting in another thread
1002 // We're busy waiting but at least give up the rest of our current
1006 #endif // wxUSE_THREADS
1012 bool wxSocketBase::Wait(long seconds
, long milliseconds
)
1014 return DoWait(seconds
, milliseconds
,
1017 GSOCK_CONNECTION_FLAG
|
1022 bool wxSocketBase::WaitForRead(long seconds
, long milliseconds
)
1024 // Check pushback buffer before entering DoWait
1028 // Note that GSOCK_INPUT_LOST has to be explicitly passed to DoWait
1029 // because of the semantics of WaitForRead: a return value of true means
1030 // that a GSocket_Read call will return immediately, not that there is
1031 // actually data to read.
1032 return DoWait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
| GSOCK_LOST_FLAG
);
1036 bool wxSocketBase::WaitForWrite(long seconds
, long milliseconds
)
1038 return DoWait(seconds
, milliseconds
, GSOCK_OUTPUT_FLAG
| GSOCK_LOST_FLAG
);
1041 bool wxSocketBase::WaitForLost(long seconds
, long milliseconds
)
1043 return DoWait(seconds
, milliseconds
, GSOCK_LOST_FLAG
);
1046 // --------------------------------------------------------------------------
1048 // --------------------------------------------------------------------------
1051 // Get local or peer address
1054 bool wxSocketBase::GetPeer(wxSockAddress
& addr_man
) const
1061 peer
= m_socket
->GetPeer();
1063 // copying a null address would just trigger an assert anyway
1068 addr_man
.SetAddress(peer
);
1069 GAddress_destroy(peer
);
1074 bool wxSocketBase::GetLocal(wxSockAddress
& addr_man
) const
1081 local
= m_socket
->GetLocal();
1082 addr_man
.SetAddress(local
);
1083 GAddress_destroy(local
);
1089 // Save and restore socket state
1092 void wxSocketBase::SaveState()
1094 wxSocketState
*state
;
1096 state
= new wxSocketState();
1098 state
->m_flags
= m_flags
;
1099 state
->m_notify
= m_notify
;
1100 state
->m_eventmask
= m_eventmask
;
1101 state
->m_clientData
= m_clientData
;
1103 m_states
.Append(state
);
1106 void wxSocketBase::RestoreState()
1108 wxList::compatibility_iterator node
;
1109 wxSocketState
*state
;
1111 node
= m_states
.GetLast();
1115 state
= (wxSocketState
*)node
->GetData();
1117 m_flags
= state
->m_flags
;
1118 m_notify
= state
->m_notify
;
1119 m_eventmask
= state
->m_eventmask
;
1120 m_clientData
= state
->m_clientData
;
1122 m_states
.Erase(node
);
1127 // Timeout and flags
1130 void wxSocketBase::SetTimeout(long seconds
)
1132 m_timeout
= seconds
;
1135 m_socket
->SetTimeout(m_timeout
* 1000);
1138 void wxSocketBase::SetFlags(wxSocketFlags flags
)
1140 // Do some sanity checking on the flags used: not all values can be used
1142 wxASSERT_MSG( !(flags
& wxSOCKET_NOWAIT
) ||
1143 !(flags
& (wxSOCKET_WAITALL
| wxSOCKET_BLOCK
)),
1144 "Using wxSOCKET_WAITALL or wxSOCKET_BLOCK with "
1145 "wxSOCKET_NOWAIT doesn't make sense" );
1151 // --------------------------------------------------------------------------
1153 // --------------------------------------------------------------------------
1155 void wxSocketBase::OnRequest(wxSocketNotify notification
)
1157 switch(notification
)
1159 case wxSOCKET_CONNECTION
:
1160 m_establishing
= false;
1164 // If we are in the middle of a R/W operation, do not
1165 // propagate events to users. Also, filter 'late' events
1166 // which are no longer valid.
1168 case wxSOCKET_INPUT
:
1169 if (m_reading
|| !m_socket
->Select(GSOCK_INPUT_FLAG
))
1173 case wxSOCKET_OUTPUT
:
1174 if (m_writing
|| !m_socket
->Select(GSOCK_OUTPUT_FLAG
))
1179 m_connected
= false;
1180 m_establishing
= false;
1184 // Schedule the event
1186 wxSocketEventFlags flag
= 0;
1188 switch (notification
)
1190 case GSOCK_INPUT
: flag
= GSOCK_INPUT_FLAG
; break;
1191 case GSOCK_OUTPUT
: flag
= GSOCK_OUTPUT_FLAG
; break;
1192 case GSOCK_CONNECTION
: flag
= GSOCK_CONNECTION_FLAG
; break;
1193 case GSOCK_LOST
: flag
= GSOCK_LOST_FLAG
; break;
1195 wxLogWarning(_("wxSocket: unknown event!."));
1199 if (((m_eventmask
& flag
) == flag
) && m_notify
)
1203 wxSocketEvent
event(m_id
);
1204 event
.m_event
= notification
;
1205 event
.m_clientData
= m_clientData
;
1206 event
.SetEventObject(this);
1208 m_handler
->AddPendingEvent(event
);
1213 void wxSocketBase::Notify(bool notify
)
1217 m_socket
->Notify(notify
);
1220 void wxSocketBase::SetNotify(wxSocketEventFlags flags
)
1222 m_eventmask
= flags
;
1225 void wxSocketBase::SetEventHandler(wxEvtHandler
& handler
, int id
)
1227 m_handler
= &handler
;
1231 // --------------------------------------------------------------------------
1233 // --------------------------------------------------------------------------
1235 void wxSocketBase::Pushback(const void *buffer
, wxUint32 size
)
1239 if (m_unread
== NULL
)
1240 m_unread
= malloc(size
);
1245 tmp
= malloc(m_unrd_size
+ size
);
1246 memcpy((char *)tmp
+ size
, m_unread
, m_unrd_size
);
1252 m_unrd_size
+= size
;
1254 memcpy(m_unread
, buffer
, size
);
1257 wxUint32
wxSocketBase::GetPushback(void *buffer
, wxUint32 size
, bool peek
)
1259 wxCHECK_MSG( buffer
, 0, "NULL buffer" );
1264 if (size
> (m_unrd_size
-m_unrd_cur
))
1265 size
= m_unrd_size
-m_unrd_cur
;
1267 memcpy(buffer
, (char *)m_unread
+ m_unrd_cur
, size
);
1272 if (m_unrd_size
== m_unrd_cur
)
1285 // ==========================================================================
1287 // ==========================================================================
1289 // --------------------------------------------------------------------------
1291 // --------------------------------------------------------------------------
1293 wxSocketServer::wxSocketServer(const wxSockAddress
& addr_man
,
1294 wxSocketFlags flags
)
1295 : wxSocketBase(flags
, wxSOCKET_SERVER
)
1297 wxLogTrace( wxTRACE_Socket
, _T("Opening wxSocketServer") );
1299 m_socket
= GSocket::Create(*this);
1303 wxLogTrace( wxTRACE_Socket
, _T("*** GSocket_new failed") );
1307 // Setup the socket as server
1308 m_socket
->Notify(m_notify
);
1309 m_socket
->SetLocal(addr_man
.GetAddress());
1311 if (GetFlags() & wxSOCKET_REUSEADDR
) {
1312 m_socket
->SetReusable();
1314 if (GetFlags() & wxSOCKET_BROADCAST
) {
1315 m_socket
->SetBroadcast();
1317 if (GetFlags() & wxSOCKET_NOBIND
) {
1318 m_socket
->DontDoBind();
1321 if (m_socket
->SetServer() != GSOCK_NOERROR
)
1326 wxLogTrace( wxTRACE_Socket
, _T("*** GSocket_SetServer failed") );
1330 wxLogTrace( wxTRACE_Socket
, _T("wxSocketServer on fd %d"), m_socket
->m_fd
);
1333 // --------------------------------------------------------------------------
1335 // --------------------------------------------------------------------------
1337 bool wxSocketServer::AcceptWith(wxSocketBase
& sock
, bool wait
)
1342 // If wait == false, then the call should be nonblocking.
1343 // When we are finished, we put the socket to blocking mode
1345 wxSocketUnblocker
unblock(m_socket
, !wait
);
1346 sock
.m_socket
= m_socket
->WaitConnection(sock
);
1348 if ( !sock
.m_socket
)
1351 sock
.m_type
= wxSOCKET_BASE
;
1352 sock
.m_connected
= true;
1357 wxSocketBase
*wxSocketServer::Accept(bool wait
)
1359 wxSocketBase
* sock
= new wxSocketBase();
1361 sock
->SetFlags(m_flags
);
1363 if (!AcceptWith(*sock
, wait
))
1372 bool wxSocketServer::WaitForAccept(long seconds
, long milliseconds
)
1374 return DoWait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
);
1377 bool wxSocketBase::GetOption(int level
, int optname
, void *optval
, int *optlen
)
1379 wxASSERT_MSG( m_socket
, _T("Socket not initialised") );
1381 if (m_socket
->GetSockOpt(level
, optname
, optval
, optlen
)
1389 bool wxSocketBase::SetOption(int level
, int optname
, const void *optval
,
1392 wxASSERT_MSG( m_socket
, _T("Socket not initialised") );
1394 if (m_socket
->SetSockOpt(level
, optname
, optval
, optlen
)
1402 bool wxSocketBase::SetLocal(const wxIPV4address
& local
)
1404 GAddress
* la
= local
.GetAddress();
1406 // If the address is valid, save it for use when we call Connect
1407 if (la
&& la
->m_addr
)
1409 m_localAddress
= local
;
1417 // ==========================================================================
1419 // ==========================================================================
1421 // --------------------------------------------------------------------------
1423 // --------------------------------------------------------------------------
1425 wxSocketClient::wxSocketClient(wxSocketFlags flags
)
1426 : wxSocketBase(flags
, wxSOCKET_CLIENT
)
1428 m_initialRecvBufferSize
=
1429 m_initialSendBufferSize
= -1;
1432 wxSocketClient::~wxSocketClient()
1436 // --------------------------------------------------------------------------
1438 // --------------------------------------------------------------------------
1440 bool wxSocketClient::DoConnect(const wxSockAddress
& addr_man
,
1441 const wxSockAddress
* local
,
1446 // Shutdown and destroy the socket
1451 m_socket
= GSocket::Create(*this);
1452 m_connected
= false;
1453 m_establishing
= false;
1458 // If wait == false, then the call should be nonblocking. When we are
1459 // finished, we put the socket to blocking mode again.
1460 wxSocketUnblocker
unblock(m_socket
, !wait
);
1462 // Reuse makes sense for clients too, if we are trying to rebind to the same port
1463 if (GetFlags() & wxSOCKET_REUSEADDR
)
1465 m_socket
->SetReusable();
1467 if (GetFlags() & wxSOCKET_BROADCAST
)
1469 m_socket
->SetBroadcast();
1471 if (GetFlags() & wxSOCKET_NOBIND
)
1473 m_socket
->DontDoBind();
1476 // If no local address was passed and one has been set, use the one that was Set
1477 if (!local
&& m_localAddress
.GetAddress())
1479 local
= &m_localAddress
;
1482 // Bind to the local IP address and port, when provided
1485 GAddress
* la
= local
->GetAddress();
1487 if (la
&& la
->m_addr
)
1488 m_socket
->SetLocal(la
);
1491 #if defined(__WXMSW__) || defined(__WXGTK__)
1492 m_socket
->SetInitialSocketBuffers(m_initialRecvBufferSize
, m_initialSendBufferSize
);
1495 m_socket
->SetPeer(addr_man
.GetAddress());
1496 const GSocketError err
= m_socket
->Connect(GSOCK_STREAMED
);
1498 //this will register for callbacks - must be called after m_socket->m_fd was initialized
1499 m_socket
->Notify(m_notify
);
1501 if (err
!= GSOCK_NOERROR
)
1503 if (err
== GSOCK_WOULDBLOCK
)
1504 m_establishing
= true;
1513 bool wxSocketClient::Connect(const wxSockAddress
& addr_man
, bool wait
)
1515 return DoConnect(addr_man
, NULL
, wait
);
1518 bool wxSocketClient::Connect(const wxSockAddress
& addr_man
,
1519 const wxSockAddress
& local
,
1522 return DoConnect(addr_man
, &local
, wait
);
1525 bool wxSocketClient::WaitOnConnect(long seconds
, long milliseconds
)
1529 // this happens if the initial attempt to connect succeeded without
1534 wxCHECK_MSG( m_establishing
&& m_socket
, false,
1535 "No connection establishment attempt in progress" );
1537 // we must specify GSOCK_LOST_FLAG here explicitly because we must return
1538 // true if the connection establishment process is finished, whether it is
1539 // over because we successfully connected or because we were not able to
1541 return DoWait(seconds
, milliseconds
,
1542 GSOCK_CONNECTION_FLAG
| GSOCK_LOST_FLAG
);
1545 // ==========================================================================
1547 // ==========================================================================
1549 /* NOTE: experimental stuff - might change */
1551 wxDatagramSocket::wxDatagramSocket( const wxSockAddress
& addr
,
1552 wxSocketFlags flags
)
1553 : wxSocketBase( flags
, wxSOCKET_DATAGRAM
)
1555 // Create the socket
1556 m_socket
= GSocket::Create(*this);
1561 m_socket
->Notify(m_notify
);
1562 // Setup the socket as non connection oriented
1563 m_socket
->SetLocal(addr
.GetAddress());
1564 if (flags
& wxSOCKET_REUSEADDR
)
1566 m_socket
->SetReusable();
1568 if (GetFlags() & wxSOCKET_BROADCAST
)
1570 m_socket
->SetBroadcast();
1572 if (GetFlags() & wxSOCKET_NOBIND
)
1574 m_socket
->DontDoBind();
1576 if ( m_socket
->SetNonOriented() != GSOCK_NOERROR
)
1583 // Initialize all stuff
1584 m_connected
= false;
1585 m_establishing
= false;
1588 wxDatagramSocket
& wxDatagramSocket::RecvFrom( wxSockAddress
& addr
,
1597 wxDatagramSocket
& wxDatagramSocket::SendTo( const wxSockAddress
& addr
,
1601 wxASSERT_MSG( m_socket
, _T("Socket not initialised") );
1603 m_socket
->SetPeer(addr
.GetAddress());
1608 // ==========================================================================
1610 // ==========================================================================
1612 class wxSocketModule
: public wxModule
1615 virtual bool OnInit()
1617 // wxSocketBase will call GSocket_Init() itself when/if needed
1621 virtual void OnExit()
1623 if ( wxSocketBase::IsInitialized() )
1624 wxSocketBase::Shutdown();
1628 DECLARE_DYNAMIC_CLASS(wxSocketModule
)
1631 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule
, wxModule
)
1633 #endif // wxUSE_SOCKETS