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"
41 #include "wx/sckaddr.h"
42 #include "wx/stopwatch.h"
44 // DLL options compatibility check:
46 WX_CHECK_BUILD_OPTIONS("wxNet")
48 // --------------------------------------------------------------------------
49 // macros and constants
50 // --------------------------------------------------------------------------
53 #define MAX_DISCARD_SIZE (10 * 1024)
55 // what to do within waits: we have 2 cases: from the main thread itself we
56 // have to call wxYield() to let the events (including the GUI events and the
57 // low-level (not wxWidgets) events from GSocket) be processed. From another
58 // thread it is enough to just call wxThread::Yield() which will give away the
59 // rest of our time slice: the explanation is that the events will be processed
60 // by the main thread anyhow, without calling wxYield(), but we don't want to
61 // eat the CPU time uselessly while sitting in the loop waiting for the data
63 #define PROCESS_EVENTS() \
65 if ( wxThread::IsMain() ) \
70 #else // !wxUSE_THREADS
71 #define PROCESS_EVENTS() wxYield()
72 #endif // wxUSE_THREADS/!wxUSE_THREADS
74 #define wxTRACE_Socket _T("wxSocket")
76 // --------------------------------------------------------------------------
78 // --------------------------------------------------------------------------
80 IMPLEMENT_CLASS(wxSocketBase
, wxObject
)
81 IMPLEMENT_CLASS(wxSocketServer
, wxSocketBase
)
82 IMPLEMENT_CLASS(wxSocketClient
, wxSocketBase
)
83 IMPLEMENT_CLASS(wxDatagramSocket
, wxSocketBase
)
84 IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent
, wxEvent
)
86 // --------------------------------------------------------------------------
88 // --------------------------------------------------------------------------
90 class wxSocketState
: public wxObject
93 wxSocketFlags m_flags
;
94 wxSocketEventFlags m_eventmask
;
99 wxSocketState() : wxObject() {}
101 DECLARE_NO_COPY_CLASS(wxSocketState
)
104 // ==========================================================================
106 // ==========================================================================
108 // --------------------------------------------------------------------------
109 // Initialization and shutdown
110 // --------------------------------------------------------------------------
112 // FIXME-MT: all this is MT-unsafe, of course, we should protect all accesses
113 // to m_countInit with a crit section
114 size_t wxSocketBase::m_countInit
= 0;
116 bool wxSocketBase::IsInitialized()
118 return m_countInit
> 0;
121 bool wxSocketBase::Initialize()
123 if ( !m_countInit
++ )
126 Details: Initialize() creates a hidden window as a sink for socket
127 events, such as 'read completed'. wxMSW has only one message loop
128 for the main thread. If Initialize is called in a secondary thread,
129 the socket window will be created for the secondary thread, but
130 since there is no message loop on this thread, it will never
131 receive events and all socket operations will time out.
132 BTW, the main thread must not be stopped using sleep or block
133 on a semaphore (a bad idea in any case) or socket operations
136 On the Mac side, Initialize() stores a pointer to the CFRunLoop for
137 the main thread. Because secondary threads do not have run loops,
138 adding event notifications to the "Current" loop would have no
139 effect at all, events would never fire.
141 wxASSERT_MSG( wxIsMainThread(),
142 wxT("Call wxSocketBase::Initialize() from the main thread first!"));
144 wxAppTraits
*traits
= wxAppConsole::GetInstance() ?
145 wxAppConsole::GetInstance()->GetTraits() : NULL
;
146 GSocketGUIFunctionsTable
*functions
=
147 traits
? traits
->GetSocketGUIFunctionsTable() : NULL
;
148 GSocket_SetGUIFunctions(functions
);
150 if ( !GSocket_Init() )
161 void wxSocketBase::Shutdown()
163 // we should be initialized
164 wxASSERT_MSG( m_countInit
, _T("extra call to Shutdown()") );
165 if ( --m_countInit
== 0 )
171 // --------------------------------------------------------------------------
173 // --------------------------------------------------------------------------
175 void wxSocketBase::Init()
178 m_type
= wxSOCKET_UNINIT
;
189 m_beingDeleted
= false;
203 if ( !IsInitialized() )
205 // this Initialize() will be undone by wxSocketModule::OnExit(), all the
206 // other calls to it should be matched by a call to Shutdown()
211 wxSocketBase::wxSocketBase()
216 wxSocketBase::wxSocketBase(wxSocketFlags flags
, wxSocketType type
)
224 wxSocketBase::~wxSocketBase()
226 // Just in case the app called Destroy() *and* then deleted
227 // the socket immediately: don't leave dangling pointers.
228 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
230 traits
->RemoveFromPendingDelete(this);
232 // Shutdown and close the socket
236 // Destroy the GSocket object
240 // Free the pushback buffer
245 bool wxSocketBase::Destroy()
247 // Delayed destruction: the socket will be deleted during the next
248 // idle loop iteration. This ensures that all pending events have
250 m_beingDeleted
= true;
252 // Shutdown and close the socket
255 // Supress events from now on
258 // schedule this object for deletion
259 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
262 // let the traits object decide what to do with us
263 traits
->ScheduleForDestroy(this);
265 else // no app or no traits
267 // in wxBase we might have no app object at all, don't leak memory
274 // --------------------------------------------------------------------------
276 // --------------------------------------------------------------------------
278 // The following IO operations update m_error and m_lcount:
279 // {Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard}
281 // TODO: Should Connect, Accept and AcceptWith update m_error?
283 bool wxSocketBase::Close()
285 // Interrupt pending waits
291 m_socket
->UnsetCallback(GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
292 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
);
294 // Shutdown the connection
295 m_socket
->Shutdown();
299 m_establishing
= false;
303 wxSocketBase
& wxSocketBase::Read(void* buffer
, wxUint32 nbytes
)
308 m_lcount
= _Read(buffer
, nbytes
);
310 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
311 if (m_flags
& wxSOCKET_WAITALL
)
312 m_error
= (m_lcount
!= nbytes
);
314 m_error
= (m_lcount
== 0);
316 // Allow read events from now on
322 wxUint32
wxSocketBase::_Read(void* buffer
, wxUint32 nbytes
)
326 // Try the pushback buffer first
327 total
= GetPushback(buffer
, nbytes
, false);
329 buffer
= (char *)buffer
+ total
;
331 // Return now in one of the following cases:
332 // - the socket is invalid,
333 // - we got all the data
338 // Possible combinations (they are checked in this order)
340 // wxSOCKET_WAITALL (with or without wxSOCKET_BLOCK)
345 if (m_flags
& wxSOCKET_NOWAIT
)
347 m_socket
->SetNonBlocking(1);
348 ret
= m_socket
->Read((char *)buffer
, nbytes
);
349 m_socket
->SetNonBlocking(0);
360 if ( !(m_flags
& wxSOCKET_BLOCK
) && !WaitForRead() )
363 ret
= m_socket
->Read((char *)buffer
, nbytes
);
369 buffer
= (char *)buffer
+ ret
;
372 // If we got here and wxSOCKET_WAITALL is not set, we can leave
373 // now. Otherwise, wait until we recv all the data or until there
376 more
= (ret
> 0 && nbytes
> 0 && (m_flags
& wxSOCKET_WAITALL
));
383 wxSocketBase
& wxSocketBase::ReadMsg(void* buffer
, wxUint32 nbytes
)
385 wxUint32 len
, len2
, sig
, total
;
390 unsigned char sig
[4];
391 unsigned char len
[4];
400 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
402 if (_Read(&msg
, sizeof(msg
)) != sizeof(msg
))
405 sig
= (wxUint32
)msg
.sig
[0];
406 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
407 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
408 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
410 if (sig
!= 0xfeeddead)
412 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
416 len
= (wxUint32
)msg
.len
[0];
417 len
|= (wxUint32
)(msg
.len
[1] << 8);
418 len
|= (wxUint32
)(msg
.len
[2] << 16);
419 len
|= (wxUint32
)(msg
.len
[3] << 24);
429 // Don't attemp to read if the msg was zero bytes long.
432 total
= _Read(buffer
, len
);
439 char *discard_buffer
= new char[MAX_DISCARD_SIZE
];
442 // NOTE: discarded bytes don't add to m_lcount.
445 discard_len
= ((len2
> MAX_DISCARD_SIZE
)? MAX_DISCARD_SIZE
: len2
);
446 discard_len
= _Read(discard_buffer
, (wxUint32
)discard_len
);
447 len2
-= (wxUint32
)discard_len
;
449 while ((discard_len
> 0) && len2
);
451 delete [] discard_buffer
;
456 if (_Read(&msg
, sizeof(msg
)) != sizeof(msg
))
459 sig
= (wxUint32
)msg
.sig
[0];
460 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
461 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
462 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
464 if (sig
!= 0xdeadfeed)
466 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
482 wxSocketBase
& wxSocketBase::Peek(void* buffer
, wxUint32 nbytes
)
487 m_lcount
= _Read(buffer
, nbytes
);
488 Pushback(buffer
, m_lcount
);
490 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
491 if (m_flags
& wxSOCKET_WAITALL
)
492 m_error
= (m_lcount
!= nbytes
);
494 m_error
= (m_lcount
== 0);
496 // Allow read events again
502 wxSocketBase
& wxSocketBase::Write(const void *buffer
, wxUint32 nbytes
)
507 m_lcount
= _Write(buffer
, nbytes
);
509 // If in wxSOCKET_WAITALL mode, all bytes should have been written.
510 if (m_flags
& wxSOCKET_WAITALL
)
511 m_error
= (m_lcount
!= nbytes
);
513 m_error
= (m_lcount
== 0);
515 // Allow write events again
521 wxUint32
wxSocketBase::_Write(const void *buffer
, wxUint32 nbytes
)
525 // If the socket is invalid or parameters are ill, return immediately
526 if (!m_socket
|| !buffer
|| !nbytes
)
529 // Possible combinations (they are checked in this order)
531 // wxSOCKET_WAITALL (with or without wxSOCKET_BLOCK)
536 if (m_flags
& wxSOCKET_NOWAIT
)
538 m_socket
->SetNonBlocking(1);
539 ret
= m_socket
->Write((const char *)buffer
, nbytes
);
540 m_socket
->SetNonBlocking(0);
551 if ( !(m_flags
& wxSOCKET_BLOCK
) && !WaitForWrite() )
554 ret
= m_socket
->Write((const char *)buffer
, nbytes
);
560 buffer
= (const char *)buffer
+ ret
;
563 // If we got here and wxSOCKET_WAITALL is not set, we can leave
564 // now. Otherwise, wait until we send all the data or until there
567 more
= (ret
> 0 && nbytes
> 0 && (m_flags
& wxSOCKET_WAITALL
));
574 wxSocketBase
& wxSocketBase::WriteMsg(const void *buffer
, wxUint32 nbytes
)
580 unsigned char sig
[4];
581 unsigned char len
[4];
589 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
591 msg
.sig
[0] = (unsigned char) 0xad;
592 msg
.sig
[1] = (unsigned char) 0xde;
593 msg
.sig
[2] = (unsigned char) 0xed;
594 msg
.sig
[3] = (unsigned char) 0xfe;
596 msg
.len
[0] = (unsigned char) (nbytes
& 0xff);
597 msg
.len
[1] = (unsigned char) ((nbytes
>> 8) & 0xff);
598 msg
.len
[2] = (unsigned char) ((nbytes
>> 16) & 0xff);
599 msg
.len
[3] = (unsigned char) ((nbytes
>> 24) & 0xff);
601 if (_Write(&msg
, sizeof(msg
)) < sizeof(msg
))
604 total
= _Write(buffer
, nbytes
);
609 msg
.sig
[0] = (unsigned char) 0xed;
610 msg
.sig
[1] = (unsigned char) 0xfe;
611 msg
.sig
[2] = (unsigned char) 0xad;
612 msg
.sig
[3] = (unsigned char) 0xde;
613 msg
.len
[0] = msg
.len
[1] = msg
.len
[2] = msg
.len
[3] = (char) 0;
615 if ((_Write(&msg
, sizeof(msg
))) < sizeof(msg
))
629 wxSocketBase
& wxSocketBase::Unread(const void *buffer
, wxUint32 nbytes
)
632 Pushback(buffer
, nbytes
);
640 wxSocketBase
& wxSocketBase::Discard()
642 char *buffer
= new char[MAX_DISCARD_SIZE
];
649 SetFlags(wxSOCKET_NOWAIT
);
653 ret
= _Read(buffer
, MAX_DISCARD_SIZE
);
656 while (ret
== MAX_DISCARD_SIZE
);
662 // Allow read events again
668 // --------------------------------------------------------------------------
670 // --------------------------------------------------------------------------
672 // All Wait functions poll the socket using GSocket_Select() to
673 // check for the specified combination of conditions, until one
674 // of these conditions become true, an error occurs, or the
675 // timeout elapses. The polling loop calls PROCESS_EVENTS(), so
676 // this won't block the GUI.
678 bool wxSocketBase::_Wait(long seconds
,
680 wxSocketEventFlags flags
)
682 GSocketEventFlags result
;
683 long timeout
; // in ms
685 // Set this to true to interrupt ongoing waits
688 // Check for valid socket
692 // Check for valid timeout value.
694 timeout
= seconds
* 1000 + milliseconds
;
696 timeout
= m_timeout
* 1000;
698 bool has_event_loop
= wxTheApp
->GetTraits() ? (wxTheApp
->GetTraits()->GetSocketGUIFunctionsTable() ? true : false) : false;
700 // Wait in an active polling loop.
702 // NOTE: We duplicate some of the code in OnRequest, but this doesn't
703 // hurt. It has to be here because the (GSocket) event might arrive
704 // a bit delayed, and it has to be in OnRequest as well because we
705 // don't know whether the Wait functions are being used.
707 // Do this at least once (important if timeout == 0, when
708 // we are just polling). Also, if just polling, do not yield.
710 const wxMilliClock_t time_limit
= wxGetLocalTimeMillis() + timeout
;
712 bool valid_result
= false;
716 // This is used to avoid a busy loop on wxBase - having a select
717 // timeout of 50 ms per iteration should be enough.
719 m_socket
->SetTimeout(50);
721 m_socket
->SetTimeout(timeout
);
726 result
= m_socket
->Select(flags
| GSOCK_LOST_FLAG
);
728 // Incoming connection (server) or connection established (client)
729 if (result
& GSOCK_CONNECTION_FLAG
)
732 m_establishing
= false;
737 // Data available or output buffer ready
738 if ((result
& GSOCK_INPUT_FLAG
) || (result
& GSOCK_OUTPUT_FLAG
))
745 if (result
& GSOCK_LOST_FLAG
)
748 m_establishing
= false;
749 valid_result
= ((flags
& GSOCK_LOST_FLAG
) != 0);
754 long time_left
= wxMilliClockToLong(time_limit
- wxGetLocalTimeMillis());
755 if ((!timeout
) || (time_left
<= 0) || (m_interrupt
))
765 // If there's less than 50 ms left, just call select with that timeout.
767 m_socket
->SetTimeout(time_left
);
772 // Set timeout back to original value (we overwrote it for polling)
774 m_socket
->SetTimeout(m_timeout
*1000);
779 bool wxSocketBase::Wait(long seconds
, long milliseconds
)
781 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
783 GSOCK_CONNECTION_FLAG
|
787 bool wxSocketBase::WaitForRead(long seconds
, long milliseconds
)
789 // Check pushback buffer before entering _Wait
793 // Note that GSOCK_INPUT_LOST has to be explicitly passed to
794 // _Wait because of the semantics of WaitForRead: a return
795 // value of true means that a GSocket_Read call will return
796 // immediately, not that there is actually data to read.
798 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
803 bool wxSocketBase::WaitForWrite(long seconds
, long milliseconds
)
805 return _Wait(seconds
, milliseconds
, GSOCK_OUTPUT_FLAG
);
808 bool wxSocketBase::WaitForLost(long seconds
, long milliseconds
)
810 return _Wait(seconds
, milliseconds
, GSOCK_LOST_FLAG
);
813 // --------------------------------------------------------------------------
815 // --------------------------------------------------------------------------
818 // Get local or peer address
821 bool wxSocketBase::GetPeer(wxSockAddress
& addr_man
) const
828 peer
= m_socket
->GetPeer();
830 // copying a null address would just trigger an assert anyway
835 addr_man
.SetAddress(peer
);
836 GAddress_destroy(peer
);
841 bool wxSocketBase::GetLocal(wxSockAddress
& addr_man
) const
848 local
= m_socket
->GetLocal();
849 addr_man
.SetAddress(local
);
850 GAddress_destroy(local
);
856 // Save and restore socket state
859 void wxSocketBase::SaveState()
861 wxSocketState
*state
;
863 state
= new wxSocketState();
865 state
->m_flags
= m_flags
;
866 state
->m_notify
= m_notify
;
867 state
->m_eventmask
= m_eventmask
;
868 state
->m_clientData
= m_clientData
;
870 m_states
.Append(state
);
873 void wxSocketBase::RestoreState()
875 wxList::compatibility_iterator node
;
876 wxSocketState
*state
;
878 node
= m_states
.GetLast();
882 state
= (wxSocketState
*)node
->GetData();
884 m_flags
= state
->m_flags
;
885 m_notify
= state
->m_notify
;
886 m_eventmask
= state
->m_eventmask
;
887 m_clientData
= state
->m_clientData
;
889 m_states
.Erase(node
);
897 void wxSocketBase::SetTimeout(long seconds
)
902 m_socket
->SetTimeout(m_timeout
* 1000);
905 void wxSocketBase::SetFlags(wxSocketFlags flags
)
911 // --------------------------------------------------------------------------
913 // --------------------------------------------------------------------------
915 // A note on how events are processed, which is probably the most
916 // difficult thing to get working right while keeping the same API
917 // and functionality for all platforms.
919 // When GSocket detects an event, it calls wx_socket_callback, which in
920 // turn just calls wxSocketBase::OnRequest in the corresponding wxSocket
921 // object. OnRequest does some housekeeping, and if the event is to be
922 // propagated to the user, it creates a new wxSocketEvent object and
923 // posts it. The event is not processed immediately, but delayed with
924 // AddPendingEvent instead. This is necessary in order to decouple the
925 // event processing from wx_socket_callback; otherwise, subsequent IO
926 // calls made from the user event handler would fail, as gtk callbacks
927 // are not reentrant.
929 // Note that, unlike events, user callbacks (now deprecated) are _not_
930 // decoupled from wx_socket_callback and thus they suffer from a variety
931 // of problems. Avoid them where possible and use events instead.
934 void LINKAGEMODE
wx_socket_callback(GSocket
* WXUNUSED(socket
),
935 GSocketEvent notification
,
938 wxSocketBase
*sckobj
= (wxSocketBase
*)cdata
;
940 sckobj
->OnRequest((wxSocketNotify
) notification
);
943 void wxSocketBase::OnRequest(wxSocketNotify notification
)
945 // NOTE: We duplicate some of the code in _Wait, but this doesn't
946 // hurt. It has to be here because the (GSocket) event might arrive
947 // a bit delayed, and it has to be in _Wait as well because we don't
948 // know whether the Wait functions are being used.
952 case wxSOCKET_CONNECTION
:
953 m_establishing
= false;
957 // If we are in the middle of a R/W operation, do not
958 // propagate events to users. Also, filter 'late' events
959 // which are no longer valid.
962 if (m_reading
|| !m_socket
->Select(GSOCK_INPUT_FLAG
))
966 case wxSOCKET_OUTPUT
:
967 if (m_writing
|| !m_socket
->Select(GSOCK_OUTPUT_FLAG
))
973 m_establishing
= false;
980 // Schedule the event
982 wxSocketEventFlags flag
= 0;
984 switch (notification
)
986 case GSOCK_INPUT
: flag
= GSOCK_INPUT_FLAG
; break;
987 case GSOCK_OUTPUT
: flag
= GSOCK_OUTPUT_FLAG
; break;
988 case GSOCK_CONNECTION
: flag
= GSOCK_CONNECTION_FLAG
; break;
989 case GSOCK_LOST
: flag
= GSOCK_LOST_FLAG
; break;
991 wxLogWarning(_("wxSocket: unknown event!."));
995 if (((m_eventmask
& flag
) == flag
) && m_notify
)
999 wxSocketEvent
event(m_id
);
1000 event
.m_event
= notification
;
1001 event
.m_clientData
= m_clientData
;
1002 event
.SetEventObject(this);
1004 m_handler
->AddPendingEvent(event
);
1009 void wxSocketBase::Notify(bool notify
)
1014 void wxSocketBase::SetNotify(wxSocketEventFlags flags
)
1016 m_eventmask
= flags
;
1019 void wxSocketBase::SetEventHandler(wxEvtHandler
& handler
, int id
)
1021 m_handler
= &handler
;
1025 // --------------------------------------------------------------------------
1027 // --------------------------------------------------------------------------
1029 void wxSocketBase::Pushback(const void *buffer
, wxUint32 size
)
1033 if (m_unread
== NULL
)
1034 m_unread
= malloc(size
);
1039 tmp
= malloc(m_unrd_size
+ size
);
1040 memcpy((char *)tmp
+ size
, m_unread
, m_unrd_size
);
1046 m_unrd_size
+= size
;
1048 memcpy(m_unread
, buffer
, size
);
1051 wxUint32
wxSocketBase::GetPushback(void *buffer
, wxUint32 size
, bool peek
)
1056 if (size
> (m_unrd_size
-m_unrd_cur
))
1057 size
= m_unrd_size
-m_unrd_cur
;
1059 memcpy(buffer
, (char *)m_unread
+ m_unrd_cur
, size
);
1064 if (m_unrd_size
== m_unrd_cur
)
1077 // ==========================================================================
1079 // ==========================================================================
1081 // --------------------------------------------------------------------------
1083 // --------------------------------------------------------------------------
1085 wxSocketServer::wxSocketServer(const wxSockAddress
& addr_man
,
1086 wxSocketFlags flags
)
1087 : wxSocketBase(flags
, wxSOCKET_SERVER
)
1089 wxLogTrace( wxTRACE_Socket
, _T("Opening wxSocketServer") );
1091 m_socket
= GSocket_new();
1095 wxLogTrace( wxTRACE_Socket
, _T("*** GSocket_new failed") );
1099 // Setup the socket as server
1101 m_socket
->SetLocal(addr_man
.GetAddress());
1103 if (GetFlags() & wxSOCKET_REUSEADDR
) {
1104 m_socket
->SetReusable();
1106 if (GetFlags() & wxSOCKET_BROADCAST
) {
1107 m_socket
->SetBroadcast();
1109 if (GetFlags() & wxSOCKET_NOBIND
) {
1110 m_socket
->DontDoBind();
1113 if (m_socket
->SetServer() != GSOCK_NOERROR
)
1118 wxLogTrace( wxTRACE_Socket
, _T("*** GSocket_SetServer failed") );
1122 m_socket
->SetTimeout(m_timeout
* 1000);
1123 m_socket
->SetCallback(GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1124 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1125 wx_socket_callback
, (char *)this);
1128 // --------------------------------------------------------------------------
1130 // --------------------------------------------------------------------------
1132 bool wxSocketServer::AcceptWith(wxSocketBase
& sock
, bool wait
)
1134 GSocket
*child_socket
;
1139 // If wait == false, then the call should be nonblocking.
1140 // When we are finished, we put the socket to blocking mode
1144 m_socket
->SetNonBlocking(1);
1146 child_socket
= m_socket
->WaitConnection();
1149 m_socket
->SetNonBlocking(0);
1154 sock
.m_type
= wxSOCKET_BASE
;
1155 sock
.m_socket
= child_socket
;
1156 sock
.m_connected
= true;
1158 sock
.m_socket
->SetTimeout(sock
.m_timeout
* 1000);
1159 sock
.m_socket
->SetCallback(GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1160 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1161 wx_socket_callback
, (char *)&sock
);
1166 wxSocketBase
*wxSocketServer::Accept(bool wait
)
1168 wxSocketBase
* sock
= new wxSocketBase();
1170 sock
->SetFlags(m_flags
);
1172 if (!AcceptWith(*sock
, wait
))
1181 bool wxSocketServer::WaitForAccept(long seconds
, long milliseconds
)
1183 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
);
1186 bool wxSocketBase::GetOption(int level
, int optname
, void *optval
, int *optlen
)
1188 wxASSERT_MSG( m_socket
, _T("Socket not initialised") );
1190 if (m_socket
->GetSockOpt(level
, optname
, optval
, optlen
)
1198 bool wxSocketBase::SetOption(int level
, int optname
, const void *optval
,
1201 wxASSERT_MSG( m_socket
, _T("Socket not initialised") );
1203 if (m_socket
->SetSockOpt(level
, optname
, optval
, optlen
)
1211 bool wxSocketBase::SetLocal(wxIPV4address
& local
)
1213 GAddress
* la
= local
.GetAddress();
1215 // If the address is valid, save it for use when we call Connect
1216 if (la
&& la
->m_addr
)
1218 m_localAddress
= local
;
1226 // ==========================================================================
1228 // ==========================================================================
1230 // --------------------------------------------------------------------------
1232 // --------------------------------------------------------------------------
1234 wxSocketClient::wxSocketClient(wxSocketFlags flags
)
1235 : wxSocketBase(flags
, wxSOCKET_CLIENT
)
1239 wxSocketClient::~wxSocketClient()
1243 // --------------------------------------------------------------------------
1245 // --------------------------------------------------------------------------
1247 bool wxSocketClient::DoConnect(wxSockAddress
& addr_man
, wxSockAddress
* local
, bool wait
)
1253 // Shutdown and destroy the socket
1258 m_socket
= GSocket_new();
1259 m_connected
= false;
1260 m_establishing
= false;
1265 m_socket
->SetTimeout(m_timeout
* 1000);
1266 m_socket
->SetCallback(GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1267 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1268 wx_socket_callback
, (char *)this);
1270 // If wait == false, then the call should be nonblocking.
1271 // When we are finished, we put the socket to blocking mode
1275 m_socket
->SetNonBlocking(1);
1277 // Reuse makes sense for clients too, if we are trying to rebind to the same port
1278 if (GetFlags() & wxSOCKET_REUSEADDR
)
1280 m_socket
->SetReusable();
1282 if (GetFlags() & wxSOCKET_BROADCAST
)
1284 m_socket
->SetBroadcast();
1286 if (GetFlags() & wxSOCKET_NOBIND
)
1288 m_socket
->DontDoBind();
1291 // If no local address was passed and one has been set, use the one that was Set
1292 if (!local
&& m_localAddress
.GetAddress())
1294 local
= &m_localAddress
;
1297 // Bind to the local IP address and port, when provided
1300 GAddress
* la
= local
->GetAddress();
1302 if (la
&& la
->m_addr
)
1303 m_socket
->SetLocal(la
);
1306 m_socket
->SetPeer(addr_man
.GetAddress());
1307 err
= m_socket
->Connect(GSOCK_STREAMED
);
1310 m_socket
->SetNonBlocking(0);
1312 if (err
!= GSOCK_NOERROR
)
1314 if (err
== GSOCK_WOULDBLOCK
)
1315 m_establishing
= true;
1324 bool wxSocketClient::Connect(wxSockAddress
& addr_man
, bool wait
)
1326 return (DoConnect(addr_man
, NULL
, wait
));
1329 bool wxSocketClient::Connect(wxSockAddress
& addr_man
, wxSockAddress
& local
, bool wait
)
1331 return (DoConnect(addr_man
, &local
, wait
));
1334 bool wxSocketClient::WaitOnConnect(long seconds
, long milliseconds
)
1336 if (m_connected
) // Already connected
1339 if (!m_establishing
|| !m_socket
) // No connection in progress
1342 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
|
1346 // ==========================================================================
1348 // ==========================================================================
1350 /* NOTE: experimental stuff - might change */
1352 wxDatagramSocket::wxDatagramSocket( const wxSockAddress
& addr
,
1353 wxSocketFlags flags
)
1354 : wxSocketBase( flags
, wxSOCKET_DATAGRAM
)
1356 // Create the socket
1357 m_socket
= GSocket_new();
1361 wxFAIL_MSG( _T("datagram socket not new'd") );
1364 // Setup the socket as non connection oriented
1365 m_socket
->SetLocal(addr
.GetAddress());
1366 if (flags
& wxSOCKET_REUSEADDR
)
1368 m_socket
->SetReusable();
1370 if (GetFlags() & wxSOCKET_BROADCAST
)
1372 m_socket
->SetBroadcast();
1374 if (GetFlags() & wxSOCKET_NOBIND
)
1376 m_socket
->DontDoBind();
1378 if ( m_socket
->SetNonOriented() != GSOCK_NOERROR
)
1385 // Initialize all stuff
1386 m_connected
= false;
1387 m_establishing
= false;
1388 m_socket
->SetTimeout( m_timeout
);
1389 m_socket
->SetCallback( GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1390 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1391 wx_socket_callback
, (char*)this );
1394 wxDatagramSocket
& wxDatagramSocket::RecvFrom( wxSockAddress
& addr
,
1403 wxDatagramSocket
& wxDatagramSocket::SendTo( const wxSockAddress
& addr
,
1407 wxASSERT_MSG( m_socket
, _T("Socket not initialised") );
1409 m_socket
->SetPeer(addr
.GetAddress());
1414 // ==========================================================================
1416 // ==========================================================================
1418 class wxSocketModule
: public wxModule
1421 virtual bool OnInit()
1423 // wxSocketBase will call GSocket_Init() itself when/if needed
1427 virtual void OnExit()
1429 if ( wxSocketBase::IsInitialized() )
1430 wxSocketBase::Shutdown();
1434 DECLARE_DYNAMIC_CLASS(wxSocketModule
)
1437 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule
, wxModule
)