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"
45 // DLL options compatibility check:
47 WX_CHECK_BUILD_OPTIONS("wxNet")
49 // --------------------------------------------------------------------------
50 // macros and constants
51 // --------------------------------------------------------------------------
54 #define MAX_DISCARD_SIZE (10 * 1024)
56 #define wxTRACE_Socket _T("wxSocket")
58 // --------------------------------------------------------------------------
60 // --------------------------------------------------------------------------
62 IMPLEMENT_CLASS(wxSocketBase
, wxObject
)
63 IMPLEMENT_CLASS(wxSocketServer
, wxSocketBase
)
64 IMPLEMENT_CLASS(wxSocketClient
, wxSocketBase
)
65 IMPLEMENT_CLASS(wxDatagramSocket
, wxSocketBase
)
66 IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent
, wxEvent
)
68 // --------------------------------------------------------------------------
70 // --------------------------------------------------------------------------
72 class wxSocketState
: public wxObject
75 wxSocketFlags m_flags
;
76 wxSocketEventFlags m_eventmask
;
81 wxSocketState() : wxObject() {}
83 DECLARE_NO_COPY_CLASS(wxSocketState
)
86 // Conditionally make the socket non-blocking for the lifetime of this object.
87 class wxSocketUnblocker
90 wxSocketUnblocker(GSocket
*socket
, bool unblock
= true)
95 m_socket
->SetNonBlocking(true);
101 m_socket
->SetNonBlocking(false);
105 GSocket
* const m_socket
;
108 DECLARE_NO_COPY_CLASS(wxSocketUnblocker
)
111 // ============================================================================
113 // ============================================================================
115 GSocketManager
*GSocketManager::ms_manager
= NULL
;
118 void GSocketManager::Set(GSocketManager
*manager
)
120 wxASSERT_MSG( !ms_manager
, "too late to set manager now" );
122 ms_manager
= manager
;
126 void GSocketManager::Init()
128 wxASSERT_MSG( !ms_manager
, "shouldn't be initialized twice" );
131 Details: Initialize() creates a hidden window as a sink for socket
132 events, such as 'read completed'. wxMSW has only one message loop
133 for the main thread. If Initialize is called in a secondary thread,
134 the socket window will be created for the secondary thread, but
135 since there is no message loop on this thread, it will never
136 receive events and all socket operations will time out.
137 BTW, the main thread must not be stopped using sleep or block
138 on a semaphore (a bad idea in any case) or socket operations
141 On the Mac side, Initialize() stores a pointer to the CFRunLoop for
142 the main thread. Because secondary threads do not have run loops,
143 adding event notifications to the "Current" loop would have no
144 effect at all, events would never fire.
146 wxASSERT_MSG( wxIsMainThread(),
147 "sockets must be initialized from the main thread" );
149 wxAppConsole
* const app
= wxAppConsole::GetInstance();
150 wxCHECK_RET( app
, "sockets can't be initialized without wxApp" );
152 ms_manager
= app
->GetTraits()->GetSocketManager();
155 // ==========================================================================
157 // ==========================================================================
159 // --------------------------------------------------------------------------
160 // Initialization and shutdown
161 // --------------------------------------------------------------------------
163 // FIXME-MT: all this is MT-unsafe, of course, we should protect all accesses
164 // to m_countInit with a crit section
165 size_t wxSocketBase::m_countInit
= 0;
167 bool wxSocketBase::IsInitialized()
169 return m_countInit
> 0;
172 bool wxSocketBase::Initialize()
174 if ( !m_countInit
++ )
176 if ( !GSocket_Init() )
187 void wxSocketBase::Shutdown()
189 // we should be initialized
190 wxASSERT_MSG( m_countInit
> 0, _T("extra call to Shutdown()") );
191 if ( --m_countInit
== 0 )
197 // --------------------------------------------------------------------------
199 // --------------------------------------------------------------------------
201 void wxSocketBase::Init()
204 m_type
= wxSOCKET_UNINIT
;
216 m_beingDeleted
= false;
230 if ( !IsInitialized() )
232 // this Initialize() will be undone by wxSocketModule::OnExit(), all
233 // the other calls to it should be matched by a call to Shutdown()
238 wxSocketBase::wxSocketBase()
243 wxSocketBase::wxSocketBase(wxSocketFlags flags
, wxSocketType type
)
252 wxSocketBase::~wxSocketBase()
254 // Just in case the app called Destroy() *and* then deleted the socket
255 // immediately: don't leave dangling pointers.
256 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
258 traits
->RemoveFromPendingDelete(this);
260 // Shutdown and close the socket
264 // Destroy the GSocket object
268 // Free the pushback buffer
273 bool wxSocketBase::Destroy()
275 // Delayed destruction: the socket will be deleted during the next idle
276 // loop iteration. This ensures that all pending events have been
278 m_beingDeleted
= true;
280 // Shutdown and close the socket
283 // Supress events from now on
286 // schedule this object for deletion
287 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
290 // let the traits object decide what to do with us
291 traits
->ScheduleForDestroy(this);
293 else // no app or no traits
295 // in wxBase we might have no app object at all, don't leak memory
302 // --------------------------------------------------------------------------
304 // --------------------------------------------------------------------------
306 // The following IO operations update m_error and m_lcount:
307 // {Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard}
309 // TODO: Should Connect, Accept and AcceptWith update m_error?
311 bool wxSocketBase::Close()
313 // Interrupt pending waits
319 m_socket
->UnsetCallback(
323 GSOCK_CONNECTION_FLAG
326 // Shutdown the connection
327 m_socket
->Shutdown();
331 m_establishing
= false;
335 wxSocketBase
& wxSocketBase::Read(void* buffer
, wxUint32 nbytes
)
340 m_lcount
= DoRead(buffer
, nbytes
);
342 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
343 if (m_flags
& wxSOCKET_WAITALL
)
344 m_error
= (m_lcount
!= nbytes
);
346 m_error
= (m_lcount
== 0);
348 // Allow read events from now on
354 wxUint32
wxSocketBase::DoRead(void* buffer_
, wxUint32 nbytes
)
356 // We use pointer arithmetic here which doesn't work with void pointers.
357 char *buffer
= static_cast<char *>(buffer_
);
359 // Try the push back buffer first, even before checking whether the socket
360 // is valid to allow reading previously pushed back data from an already
362 wxUint32 total
= GetPushback(buffer
, nbytes
, false);
366 // If it's indeed closed or if read everything, there is nothing more to do.
367 if ( !m_socket
|| !nbytes
)
370 wxCHECK_MSG( buffer
, 0, "NULL buffer" );
373 // wxSOCKET_NOWAIT overrides all the other flags and means that we are
374 // polling the socket and don't block at all.
375 if ( m_flags
& wxSOCKET_NOWAIT
)
377 wxSocketUnblocker
unblock(m_socket
);
378 int ret
= m_socket
->Read(buffer
, nbytes
);
384 else // blocking socket
388 // Wait until socket becomes ready for reading dispatching the GUI
389 // events in the meanwhile unless wxSOCKET_BLOCK was explicitly
390 // specified to disable this.
391 if ( !(m_flags
& wxSOCKET_BLOCK
) && !WaitForRead() )
394 const int ret
= m_socket
->Read(buffer
, nbytes
);
397 // for connection-oriented (e.g. TCP) sockets we can only read
398 // 0 bytes if the other end has been closed, and for
399 // connectionless ones (UDP) this flag doesn't make sense
400 // anyhow so we can set it to true too without doing any harm
407 // this will be always interpreted as error by Read()
413 // If wxSOCKET_WAITALL is not set, we can leave now as we did read
414 // something and we don't need to wait for all nbytes bytes to be
416 if ( !(m_flags
& wxSOCKET_WAITALL
) )
419 // Otherwise continue reading until we do read everything.
431 wxSocketBase
& wxSocketBase::ReadMsg(void* buffer
, wxUint32 nbytes
)
433 wxUint32 len
, len2
, sig
, total
;
438 unsigned char sig
[4];
439 unsigned char len
[4];
448 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
450 if (DoRead(&msg
, sizeof(msg
)) != sizeof(msg
))
453 sig
= (wxUint32
)msg
.sig
[0];
454 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
455 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
456 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
458 if (sig
!= 0xfeeddead)
460 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
464 len
= (wxUint32
)msg
.len
[0];
465 len
|= (wxUint32
)(msg
.len
[1] << 8);
466 len
|= (wxUint32
)(msg
.len
[2] << 16);
467 len
|= (wxUint32
)(msg
.len
[3] << 24);
477 // Don't attempt to read if the msg was zero bytes long.
480 total
= DoRead(buffer
, len
);
488 char *discard_buffer
= new char[MAX_DISCARD_SIZE
];
491 // NOTE: discarded bytes don't add to m_lcount.
494 discard_len
= ((len2
> MAX_DISCARD_SIZE
)? MAX_DISCARD_SIZE
: len2
);
495 discard_len
= DoRead(discard_buffer
, (wxUint32
)discard_len
);
496 len2
-= (wxUint32
)discard_len
;
498 while ((discard_len
> 0) && len2
);
500 delete [] discard_buffer
;
505 if (DoRead(&msg
, sizeof(msg
)) != sizeof(msg
))
508 sig
= (wxUint32
)msg
.sig
[0];
509 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
510 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
511 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
513 if (sig
!= 0xdeadfeed)
515 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
531 wxSocketBase
& wxSocketBase::Peek(void* buffer
, wxUint32 nbytes
)
536 m_lcount
= DoRead(buffer
, nbytes
);
537 Pushback(buffer
, m_lcount
);
539 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
540 if (m_flags
& wxSOCKET_WAITALL
)
541 m_error
= (m_lcount
!= nbytes
);
543 m_error
= (m_lcount
== 0);
545 // Allow read events again
551 wxSocketBase
& wxSocketBase::Write(const void *buffer
, wxUint32 nbytes
)
556 m_lcount
= DoWrite(buffer
, nbytes
);
558 // If in wxSOCKET_WAITALL mode, all bytes should have been written.
559 if (m_flags
& wxSOCKET_WAITALL
)
560 m_error
= (m_lcount
!= nbytes
);
562 m_error
= (m_lcount
== 0);
564 // Allow write events again
570 // This function is a mirror image of DoRead() except that it doesn't use the
571 // push back buffer, please see comments there
572 wxUint32
wxSocketBase::DoWrite(const void *buffer_
, wxUint32 nbytes
)
574 const char *buffer
= static_cast<const char *>(buffer_
);
576 // Return if there is nothing to read or the socket is (already?) closed.
577 if ( !m_socket
|| !nbytes
)
580 wxCHECK_MSG( buffer
, 0, "NULL buffer" );
583 if ( m_flags
& wxSOCKET_NOWAIT
)
585 wxSocketUnblocker
unblock(m_socket
);
586 const int ret
= m_socket
->Write(buffer
, nbytes
);
590 else // blocking socket
594 if ( !(m_flags
& wxSOCKET_BLOCK
) && !WaitForWrite() )
597 const int ret
= m_socket
->Write(buffer
, nbytes
);
608 if ( !(m_flags
& wxSOCKET_WAITALL
) )
622 wxSocketBase
& wxSocketBase::WriteMsg(const void *buffer
, wxUint32 nbytes
)
628 unsigned char sig
[4];
629 unsigned char len
[4];
637 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
639 msg
.sig
[0] = (unsigned char) 0xad;
640 msg
.sig
[1] = (unsigned char) 0xde;
641 msg
.sig
[2] = (unsigned char) 0xed;
642 msg
.sig
[3] = (unsigned char) 0xfe;
644 msg
.len
[0] = (unsigned char) (nbytes
& 0xff);
645 msg
.len
[1] = (unsigned char) ((nbytes
>> 8) & 0xff);
646 msg
.len
[2] = (unsigned char) ((nbytes
>> 16) & 0xff);
647 msg
.len
[3] = (unsigned char) ((nbytes
>> 24) & 0xff);
649 if (DoWrite(&msg
, sizeof(msg
)) < sizeof(msg
))
652 total
= DoWrite(buffer
, nbytes
);
657 msg
.sig
[0] = (unsigned char) 0xed;
658 msg
.sig
[1] = (unsigned char) 0xfe;
659 msg
.sig
[2] = (unsigned char) 0xad;
660 msg
.sig
[3] = (unsigned char) 0xde;
664 msg
.len
[3] = (char) 0;
666 if ((DoWrite(&msg
, sizeof(msg
))) < sizeof(msg
))
680 wxSocketBase
& wxSocketBase::Unread(const void *buffer
, wxUint32 nbytes
)
683 Pushback(buffer
, nbytes
);
691 wxSocketBase
& wxSocketBase::Discard()
693 char *buffer
= new char[MAX_DISCARD_SIZE
];
700 SetFlags(wxSOCKET_NOWAIT
);
704 ret
= DoRead(buffer
, MAX_DISCARD_SIZE
);
707 while (ret
== MAX_DISCARD_SIZE
);
713 // Allow read events again
719 // --------------------------------------------------------------------------
721 // --------------------------------------------------------------------------
723 // All Wait functions poll the socket using GSocket_Select() to
724 // check for the specified combination of conditions, until one
725 // of these conditions become true, an error occurs, or the
726 // timeout elapses. The polling loop runs the event loop so that
727 // this won't block the GUI.
730 wxSocketBase::DoWait(long seconds
, long milliseconds
, wxSocketEventFlags flags
)
732 wxCHECK_MSG( m_socket
, false, "can't wait on invalid socket" );
734 // This can be set to true from Interrupt() to exit this function a.s.a.p.
738 // Use either the provided timeout or the default timeout value associated
741 // TODO: allow waiting forever, see #9443
742 const long timeout
= seconds
== -1 ? m_timeout
* 1000
743 : seconds
* 1000 + milliseconds
;
744 const wxMilliClock_t timeEnd
= wxGetLocalTimeMillis() + timeout
;
746 // Get the active event loop which we'll use for the message dispatching
747 // when running in the main thread
748 wxEventLoopBase
*eventLoop
;
749 if ( wxIsMainThread() )
751 eventLoop
= wxEventLoop::GetActive();
754 wxASSERT_MSG( eventLoop
,
755 "Sockets won't work without a running event loop" );
758 else // in worker thread
760 // We never dispatch messages from threads other than the main one.
764 // Wait in an active polling loop: notice that the loop is executed at
765 // least once, even if timeout is 0 (i.e. polling).
766 bool gotEvent
= false;
769 // We always stop waiting when the connection is lost as it doesn't
770 // make sense to continue further, even if GSOCK_LOST_FLAG is not
771 // specified in flags to wait for.
772 const GSocketEventFlags
773 result
= m_socket
->Select(flags
| GSOCK_LOST_FLAG
);
775 // Incoming connection (server) or connection established (client)?
776 if ( result
& GSOCK_CONNECTION_FLAG
)
779 m_establishing
= false;
784 // Data available or output buffer ready?
785 if ( (result
& GSOCK_INPUT_FLAG
) || (result
& GSOCK_OUTPUT_FLAG
) )
792 if ( result
& GSOCK_LOST_FLAG
)
795 m_establishing
= false;
796 if ( flags
& GSOCK_LOST_FLAG
)
805 const wxMilliClock_t timeNow
= wxGetLocalTimeMillis();
806 if ( timeNow
>= timeEnd
)
811 // Dispatch the events when we run in the main thread and have an
812 // active event loop: without this sockets don't work at all under
813 // MSW as socket flags are only updated when socket messages are
815 if ( eventLoop
->Pending() )
816 eventLoop
->Dispatch();
819 else // no event loop or waiting in another thread
821 // We're busy waiting but at least give up the rest of our current
825 #endif // wxUSE_THREADS
831 bool wxSocketBase::Wait(long seconds
, long milliseconds
)
833 return DoWait(seconds
, milliseconds
,
836 GSOCK_CONNECTION_FLAG
|
841 bool wxSocketBase::WaitForRead(long seconds
, long milliseconds
)
843 // Check pushback buffer before entering DoWait
847 // Note that GSOCK_INPUT_LOST has to be explicitly passed to DoWait
848 // because of the semantics of WaitForRead: a return value of true means
849 // that a GSocket_Read call will return immediately, not that there is
850 // actually data to read.
851 return DoWait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
| GSOCK_LOST_FLAG
);
855 bool wxSocketBase::WaitForWrite(long seconds
, long milliseconds
)
857 return DoWait(seconds
, milliseconds
, GSOCK_OUTPUT_FLAG
| GSOCK_LOST_FLAG
);
860 bool wxSocketBase::WaitForLost(long seconds
, long milliseconds
)
862 return DoWait(seconds
, milliseconds
, GSOCK_LOST_FLAG
);
865 // --------------------------------------------------------------------------
867 // --------------------------------------------------------------------------
870 // Get local or peer address
873 bool wxSocketBase::GetPeer(wxSockAddress
& addr_man
) const
880 peer
= m_socket
->GetPeer();
882 // copying a null address would just trigger an assert anyway
887 addr_man
.SetAddress(peer
);
888 GAddress_destroy(peer
);
893 bool wxSocketBase::GetLocal(wxSockAddress
& addr_man
) const
900 local
= m_socket
->GetLocal();
901 addr_man
.SetAddress(local
);
902 GAddress_destroy(local
);
908 // Save and restore socket state
911 void wxSocketBase::SaveState()
913 wxSocketState
*state
;
915 state
= new wxSocketState();
917 state
->m_flags
= m_flags
;
918 state
->m_notify
= m_notify
;
919 state
->m_eventmask
= m_eventmask
;
920 state
->m_clientData
= m_clientData
;
922 m_states
.Append(state
);
925 void wxSocketBase::RestoreState()
927 wxList::compatibility_iterator node
;
928 wxSocketState
*state
;
930 node
= m_states
.GetLast();
934 state
= (wxSocketState
*)node
->GetData();
936 m_flags
= state
->m_flags
;
937 m_notify
= state
->m_notify
;
938 m_eventmask
= state
->m_eventmask
;
939 m_clientData
= state
->m_clientData
;
941 m_states
.Erase(node
);
949 void wxSocketBase::SetTimeout(long seconds
)
954 m_socket
->SetTimeout(m_timeout
* 1000);
957 void wxSocketBase::SetFlags(wxSocketFlags flags
)
959 // Do some sanity checking on the flags used: not all values can be used
961 wxASSERT_MSG( !(flags
& wxSOCKET_NOWAIT
) ||
962 !(flags
& (wxSOCKET_WAITALL
| wxSOCKET_BLOCK
)),
963 "Using wxSOCKET_WAITALL or wxSOCKET_BLOCK with "
964 "wxSOCKET_NOWAIT doesn't make sense" );
970 // --------------------------------------------------------------------------
972 // --------------------------------------------------------------------------
974 // A note on how events are processed, which is probably the most
975 // difficult thing to get working right while keeping the same API
976 // and functionality for all platforms.
978 // When GSocket detects an event, it calls wx_socket_callback, which in
979 // turn just calls wxSocketBase::OnRequest in the corresponding wxSocket
980 // object. OnRequest does some housekeeping, and if the event is to be
981 // propagated to the user, it creates a new wxSocketEvent object and
982 // posts it. The event is not processed immediately, but delayed with
983 // AddPendingEvent instead. This is necessary in order to decouple the
984 // event processing from wx_socket_callback; otherwise, subsequent IO
985 // calls made from the user event handler would fail, as gtk callbacks
986 // are not reentrant.
988 // Note that, unlike events, user callbacks (now deprecated) are _not_
989 // decoupled from wx_socket_callback and thus they suffer from a variety
990 // of problems. Avoid them where possible and use events instead.
993 void LINKAGEMODE
wx_socket_callback(GSocket
* WXUNUSED(socket
),
994 GSocketEvent notification
,
997 wxSocketBase
*sckobj
= (wxSocketBase
*)cdata
;
999 sckobj
->OnRequest((wxSocketNotify
) notification
);
1002 void wxSocketBase::OnRequest(wxSocketNotify notification
)
1004 switch(notification
)
1006 case wxSOCKET_CONNECTION
:
1007 m_establishing
= false;
1011 // If we are in the middle of a R/W operation, do not
1012 // propagate events to users. Also, filter 'late' events
1013 // which are no longer valid.
1015 case wxSOCKET_INPUT
:
1016 if (m_reading
|| !m_socket
->Select(GSOCK_INPUT_FLAG
))
1020 case wxSOCKET_OUTPUT
:
1021 if (m_writing
|| !m_socket
->Select(GSOCK_OUTPUT_FLAG
))
1026 m_connected
= false;
1027 m_establishing
= false;
1031 // Schedule the event
1033 wxSocketEventFlags flag
= 0;
1035 switch (notification
)
1037 case GSOCK_INPUT
: flag
= GSOCK_INPUT_FLAG
; break;
1038 case GSOCK_OUTPUT
: flag
= GSOCK_OUTPUT_FLAG
; break;
1039 case GSOCK_CONNECTION
: flag
= GSOCK_CONNECTION_FLAG
; break;
1040 case GSOCK_LOST
: flag
= GSOCK_LOST_FLAG
; break;
1042 wxLogWarning(_("wxSocket: unknown event!."));
1046 if (((m_eventmask
& flag
) == flag
) && m_notify
)
1050 wxSocketEvent
event(m_id
);
1051 event
.m_event
= notification
;
1052 event
.m_clientData
= m_clientData
;
1053 event
.SetEventObject(this);
1055 m_handler
->AddPendingEvent(event
);
1060 void wxSocketBase::Notify(bool notify
)
1064 m_socket
->Notify(notify
);
1067 void wxSocketBase::SetNotify(wxSocketEventFlags flags
)
1069 m_eventmask
= flags
;
1072 void wxSocketBase::SetEventHandler(wxEvtHandler
& handler
, int id
)
1074 m_handler
= &handler
;
1078 // --------------------------------------------------------------------------
1080 // --------------------------------------------------------------------------
1082 void wxSocketBase::Pushback(const void *buffer
, wxUint32 size
)
1086 if (m_unread
== NULL
)
1087 m_unread
= malloc(size
);
1092 tmp
= malloc(m_unrd_size
+ size
);
1093 memcpy((char *)tmp
+ size
, m_unread
, m_unrd_size
);
1099 m_unrd_size
+= size
;
1101 memcpy(m_unread
, buffer
, size
);
1104 wxUint32
wxSocketBase::GetPushback(void *buffer
, wxUint32 size
, bool peek
)
1106 wxCHECK_MSG( buffer
, 0, "NULL buffer" );
1111 if (size
> (m_unrd_size
-m_unrd_cur
))
1112 size
= m_unrd_size
-m_unrd_cur
;
1114 memcpy(buffer
, (char *)m_unread
+ m_unrd_cur
, size
);
1119 if (m_unrd_size
== m_unrd_cur
)
1132 // ==========================================================================
1134 // ==========================================================================
1136 // --------------------------------------------------------------------------
1138 // --------------------------------------------------------------------------
1140 wxSocketServer::wxSocketServer(const wxSockAddress
& addr_man
,
1141 wxSocketFlags flags
)
1142 : wxSocketBase(flags
, wxSOCKET_SERVER
)
1144 wxLogTrace( wxTRACE_Socket
, _T("Opening wxSocketServer") );
1146 m_socket
= GSocket_new();
1150 wxLogTrace( wxTRACE_Socket
, _T("*** GSocket_new failed") );
1154 // Setup the socket as server
1155 m_socket
->Notify(m_notify
);
1156 m_socket
->SetLocal(addr_man
.GetAddress());
1158 if (GetFlags() & wxSOCKET_REUSEADDR
) {
1159 m_socket
->SetReusable();
1161 if (GetFlags() & wxSOCKET_BROADCAST
) {
1162 m_socket
->SetBroadcast();
1164 if (GetFlags() & wxSOCKET_NOBIND
) {
1165 m_socket
->DontDoBind();
1168 if (m_socket
->SetServer() != GSOCK_NOERROR
)
1173 wxLogTrace( wxTRACE_Socket
, _T("*** GSocket_SetServer failed") );
1177 m_socket
->SetTimeout(m_timeout
* 1000);
1178 m_socket
->SetCallback(GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1179 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1180 wx_socket_callback
, (char *)this);
1182 wxLogTrace( wxTRACE_Socket
, _T("wxSocketServer on fd %d"), m_socket
->m_fd
);
1185 // --------------------------------------------------------------------------
1187 // --------------------------------------------------------------------------
1189 bool wxSocketServer::AcceptWith(wxSocketBase
& sock
, bool wait
)
1194 // If wait == false, then the call should be nonblocking.
1195 // When we are finished, we put the socket to blocking mode
1197 wxSocketUnblocker
unblock(m_socket
, !wait
);
1198 GSocket
* const child_socket
= m_socket
->WaitConnection();
1203 sock
.m_type
= wxSOCKET_BASE
;
1204 sock
.m_socket
= child_socket
;
1205 sock
.m_connected
= true;
1207 sock
.m_socket
->SetTimeout(sock
.m_timeout
* 1000);
1208 sock
.m_socket
->SetCallback(GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1209 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1210 wx_socket_callback
, (char *)&sock
);
1215 wxSocketBase
*wxSocketServer::Accept(bool wait
)
1217 wxSocketBase
* sock
= new wxSocketBase();
1219 sock
->SetFlags(m_flags
);
1221 if (!AcceptWith(*sock
, wait
))
1230 bool wxSocketServer::WaitForAccept(long seconds
, long milliseconds
)
1232 return DoWait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
);
1235 bool wxSocketBase::GetOption(int level
, int optname
, void *optval
, int *optlen
)
1237 wxASSERT_MSG( m_socket
, _T("Socket not initialised") );
1239 if (m_socket
->GetSockOpt(level
, optname
, optval
, optlen
)
1247 bool wxSocketBase::SetOption(int level
, int optname
, const void *optval
,
1250 wxASSERT_MSG( m_socket
, _T("Socket not initialised") );
1252 if (m_socket
->SetSockOpt(level
, optname
, optval
, optlen
)
1260 bool wxSocketBase::SetLocal(const wxIPV4address
& local
)
1262 GAddress
* la
= local
.GetAddress();
1264 // If the address is valid, save it for use when we call Connect
1265 if (la
&& la
->m_addr
)
1267 m_localAddress
= local
;
1275 // ==========================================================================
1277 // ==========================================================================
1279 // --------------------------------------------------------------------------
1281 // --------------------------------------------------------------------------
1283 wxSocketClient::wxSocketClient(wxSocketFlags flags
)
1284 : wxSocketBase(flags
, wxSOCKET_CLIENT
)
1286 m_initialRecvBufferSize
=
1287 m_initialSendBufferSize
= -1;
1290 wxSocketClient::~wxSocketClient()
1294 // --------------------------------------------------------------------------
1296 // --------------------------------------------------------------------------
1298 bool wxSocketClient::DoConnect(const wxSockAddress
& addr_man
,
1299 const wxSockAddress
* local
,
1304 // Shutdown and destroy the socket
1309 m_socket
= GSocket_new();
1310 m_connected
= false;
1311 m_establishing
= false;
1316 m_socket
->SetTimeout(m_timeout
* 1000);
1317 m_socket
->SetCallback(
1321 GSOCK_CONNECTION_FLAG
,
1326 // If wait == false, then the call should be nonblocking. When we are
1327 // finished, we put the socket to blocking mode again.
1328 wxSocketUnblocker
unblock(m_socket
, !wait
);
1330 // Reuse makes sense for clients too, if we are trying to rebind to the same port
1331 if (GetFlags() & wxSOCKET_REUSEADDR
)
1333 m_socket
->SetReusable();
1335 if (GetFlags() & wxSOCKET_BROADCAST
)
1337 m_socket
->SetBroadcast();
1339 if (GetFlags() & wxSOCKET_NOBIND
)
1341 m_socket
->DontDoBind();
1344 // If no local address was passed and one has been set, use the one that was Set
1345 if (!local
&& m_localAddress
.GetAddress())
1347 local
= &m_localAddress
;
1350 // Bind to the local IP address and port, when provided
1353 GAddress
* la
= local
->GetAddress();
1355 if (la
&& la
->m_addr
)
1356 m_socket
->SetLocal(la
);
1359 #if defined(__WXMSW__) || defined(__WXGTK__)
1360 m_socket
->SetInitialSocketBuffers(m_initialRecvBufferSize
, m_initialSendBufferSize
);
1363 m_socket
->SetPeer(addr_man
.GetAddress());
1364 const GSocketError err
= m_socket
->Connect(GSOCK_STREAMED
);
1366 //this will register for callbacks - must be called after m_socket->m_fd was initialized
1367 m_socket
->Notify(m_notify
);
1369 if (err
!= GSOCK_NOERROR
)
1371 if (err
== GSOCK_WOULDBLOCK
)
1372 m_establishing
= true;
1381 bool wxSocketClient::Connect(const wxSockAddress
& addr_man
, bool wait
)
1383 return DoConnect(addr_man
, NULL
, wait
);
1386 bool wxSocketClient::Connect(const wxSockAddress
& addr_man
,
1387 const wxSockAddress
& local
,
1390 return DoConnect(addr_man
, &local
, wait
);
1393 bool wxSocketClient::WaitOnConnect(long seconds
, long milliseconds
)
1397 // this happens if the initial attempt to connect succeeded without
1402 wxCHECK_MSG( m_establishing
&& m_socket
, false,
1403 "No connection establishment attempt in progress" );
1405 // we must specify GSOCK_LOST_FLAG here explicitly because we must return
1406 // true if the connection establishment process is finished, whether it is
1407 // over because we successfully connected or because we were not able to
1409 return DoWait(seconds
, milliseconds
,
1410 GSOCK_CONNECTION_FLAG
| GSOCK_LOST_FLAG
);
1413 // ==========================================================================
1415 // ==========================================================================
1417 /* NOTE: experimental stuff - might change */
1419 wxDatagramSocket::wxDatagramSocket( const wxSockAddress
& addr
,
1420 wxSocketFlags flags
)
1421 : wxSocketBase( flags
, wxSOCKET_DATAGRAM
)
1423 // Create the socket
1424 m_socket
= GSocket_new();
1428 wxFAIL_MSG( _T("datagram socket not new'd") );
1431 m_socket
->Notify(m_notify
);
1432 // Setup the socket as non connection oriented
1433 m_socket
->SetLocal(addr
.GetAddress());
1434 if (flags
& wxSOCKET_REUSEADDR
)
1436 m_socket
->SetReusable();
1438 if (GetFlags() & wxSOCKET_BROADCAST
)
1440 m_socket
->SetBroadcast();
1442 if (GetFlags() & wxSOCKET_NOBIND
)
1444 m_socket
->DontDoBind();
1446 if ( m_socket
->SetNonOriented() != GSOCK_NOERROR
)
1453 // Initialize all stuff
1454 m_connected
= false;
1455 m_establishing
= false;
1456 m_socket
->SetTimeout( m_timeout
);
1457 m_socket
->SetCallback( GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1458 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1459 wx_socket_callback
, (char*)this );
1462 wxDatagramSocket
& wxDatagramSocket::RecvFrom( wxSockAddress
& addr
,
1471 wxDatagramSocket
& wxDatagramSocket::SendTo( const wxSockAddress
& addr
,
1475 wxASSERT_MSG( m_socket
, _T("Socket not initialised") );
1477 m_socket
->SetPeer(addr
.GetAddress());
1482 // ==========================================================================
1484 // ==========================================================================
1486 class wxSocketModule
: public wxModule
1489 virtual bool OnInit()
1491 // wxSocketBase will call GSocket_Init() itself when/if needed
1495 virtual void OnExit()
1497 if ( wxSocketBase::IsInitialized() )
1498 wxSocketBase::Shutdown();
1502 DECLARE_DYNAMIC_CLASS(wxSocketModule
)
1505 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule
, wxModule
)
1507 #endif // wxUSE_SOCKETS