1 /////////////////////////////////////////////////////////////////////////////
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: see wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ==========================================================================
14 // ==========================================================================
17 #pragma implementation "socket.h"
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
30 #include "wx/apptrait.h"
32 #include "wx/object.h"
33 #include "wx/string.h"
36 #include "wx/module.h"
41 #include "wx/sckaddr.h"
42 #include "wx/socket.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 wxWindows) 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
;
97 #if WXWIN_COMPATIBILITY
98 wxSocketBase::wxSockCbk m_cbk
;
100 #endif // WXWIN_COMPATIBILITY
103 wxSocketState() : wxObject() {}
105 DECLARE_NO_COPY_CLASS(wxSocketState
)
108 // ==========================================================================
110 // ==========================================================================
112 // --------------------------------------------------------------------------
113 // Initialization and shutdown
114 // --------------------------------------------------------------------------
116 // FIXME-MT: all this is MT-unsafe, of course, we should protect all accesses
117 // to m_countInit with a crit section
118 size_t wxSocketBase::m_countInit
= 0;
120 bool wxSocketBase::IsInitialized()
122 return m_countInit
> 0;
125 bool wxSocketBase::Initialize()
127 if ( !m_countInit
++ )
129 if ( !GSocket_Init() )
140 void wxSocketBase::Shutdown()
142 // we should be initialized
143 wxASSERT_MSG( m_countInit
, _T("extra call to Shutdown()") );
144 if ( !--m_countInit
)
150 // --------------------------------------------------------------------------
152 // --------------------------------------------------------------------------
154 void wxSocketBase::Init()
157 m_type
= wxSOCKET_UNINIT
;
168 m_beingDeleted
= FALSE
;
181 #if WXWIN_COMPATIBILITY
184 #endif // WXWIN_COMPATIBILITY
186 if ( !IsInitialized() )
188 // this Initialize() will be undone by wxSocketModule::OnExit(), all the
189 // other calls to it should be matched by a call to Shutdown()
194 wxSocketBase::wxSocketBase()
199 wxSocketBase::wxSocketBase(wxSocketFlags flags
, wxSocketType type
)
207 wxSocketBase::~wxSocketBase()
209 // Just in case the app called Destroy() *and* then deleted
210 // the socket immediately: don't leave dangling pointers.
211 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
213 traits
->RemoveFromPendingDelete(this);
215 // Shutdown and close the socket
219 // Destroy the GSocket object
221 GSocket_destroy(m_socket
);
223 // Free the pushback buffer
228 bool wxSocketBase::Destroy()
230 // Delayed destruction: the socket will be deleted during the next
231 // idle loop iteration. This ensures that all pending events have
233 m_beingDeleted
= TRUE
;
235 // Shutdown and close the socket
238 // Supress events from now on
241 // schedule this object for deletion
242 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
245 // let the traits object decide what to do with us
246 traits
->ScheduleForDestroy(this);
248 else // no app or no traits
250 // in wxBase we might have no app object at all, don't leak memory
257 // --------------------------------------------------------------------------
259 // --------------------------------------------------------------------------
261 // The following IO operations update m_error and m_lcount:
262 // {Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard}
264 // TODO: Should Connect, Accept and AcceptWith update m_error?
266 bool wxSocketBase::Close()
268 // Interrupt pending waits
274 GSocket_UnsetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
275 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
);
277 // Shutdown the connection
278 GSocket_Shutdown(m_socket
);
282 m_establishing
= FALSE
;
286 wxSocketBase
& wxSocketBase::Read(void* buffer
, wxUint32 nbytes
)
291 m_lcount
= _Read(buffer
, nbytes
);
293 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
294 if (m_flags
& wxSOCKET_WAITALL
)
295 m_error
= (m_lcount
!= nbytes
);
297 m_error
= (m_lcount
== 0);
299 // Allow read events from now on
305 wxUint32
wxSocketBase::_Read(void* buffer
, wxUint32 nbytes
)
310 // Try the pushback buffer first
311 total
= GetPushback(buffer
, nbytes
, FALSE
);
313 buffer
= (char *)buffer
+ total
;
315 // Return now in one of the following cases:
316 // - the socket is invalid,
317 // - we got all the data,
318 // - we got *some* data and we are not using wxSOCKET_WAITALL.
321 ((total
!= 0) && !(m_flags
& wxSOCKET_WAITALL
)) )
324 // Possible combinations (they are checked in this order)
326 // wxSOCKET_WAITALL (with or without wxSOCKET_BLOCK)
330 if (m_flags
& wxSOCKET_NOWAIT
)
332 GSocket_SetNonBlocking(m_socket
, 1);
333 ret
= GSocket_Read(m_socket
, (char *)buffer
, nbytes
);
334 GSocket_SetNonBlocking(m_socket
, 0);
345 if ( !(m_flags
& wxSOCKET_BLOCK
) && !WaitForRead() )
348 ret
= GSocket_Read(m_socket
, (char *)buffer
, nbytes
);
354 buffer
= (char *)buffer
+ ret
;
357 // If we got here and wxSOCKET_WAITALL is not set, we can leave
358 // now. Otherwise, wait until we recv all the data or until there
361 more
= (ret
> 0 && nbytes
> 0 && (m_flags
& wxSOCKET_WAITALL
));
368 wxSocketBase
& wxSocketBase::ReadMsg(void* buffer
, wxUint32 nbytes
)
370 wxUint32 len
, len2
, sig
, total
;
375 unsigned char sig
[4];
376 unsigned char len
[4];
385 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
387 if (_Read(&msg
, sizeof(msg
)) != sizeof(msg
))
390 sig
= (wxUint32
)msg
.sig
[0];
391 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
392 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
393 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
395 if (sig
!= 0xfeeddead)
397 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
401 len
= (wxUint32
)msg
.len
[0];
402 len
|= (wxUint32
)(msg
.len
[1] << 8);
403 len
|= (wxUint32
)(msg
.len
[2] << 16);
404 len
|= (wxUint32
)(msg
.len
[3] << 24);
414 // Don't attemp to read if the msg was zero bytes long.
417 total
= _Read(buffer
, len
);
424 char *discard_buffer
= new char[MAX_DISCARD_SIZE
];
427 // NOTE: discarded bytes don't add to m_lcount.
430 discard_len
= ((len2
> MAX_DISCARD_SIZE
)? MAX_DISCARD_SIZE
: len2
);
431 discard_len
= _Read(discard_buffer
, (wxUint32
)discard_len
);
432 len2
-= (wxUint32
)discard_len
;
434 while ((discard_len
> 0) && len2
);
436 delete [] discard_buffer
;
441 if (_Read(&msg
, sizeof(msg
)) != sizeof(msg
))
444 sig
= (wxUint32
)msg
.sig
[0];
445 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
446 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
447 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
449 if (sig
!= 0xdeadfeed)
451 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
467 wxSocketBase
& wxSocketBase::Peek(void* buffer
, wxUint32 nbytes
)
472 m_lcount
= _Read(buffer
, nbytes
);
473 Pushback(buffer
, m_lcount
);
475 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
476 if (m_flags
& wxSOCKET_WAITALL
)
477 m_error
= (m_lcount
!= nbytes
);
479 m_error
= (m_lcount
== 0);
481 // Allow read events again
487 wxSocketBase
& wxSocketBase::Write(const void *buffer
, wxUint32 nbytes
)
492 m_lcount
= _Write(buffer
, nbytes
);
494 // If in wxSOCKET_WAITALL mode, all bytes should have been written.
495 if (m_flags
& wxSOCKET_WAITALL
)
496 m_error
= (m_lcount
!= nbytes
);
498 m_error
= (m_lcount
== 0);
500 // Allow write events again
506 wxUint32
wxSocketBase::_Write(const void *buffer
, wxUint32 nbytes
)
511 // If the socket is invalid or parameters are ill, return immediately
512 if (!m_socket
|| !buffer
|| !nbytes
)
515 // Possible combinations (they are checked in this order)
517 // wxSOCKET_WAITALL (with or without wxSOCKET_BLOCK)
521 if (m_flags
& wxSOCKET_NOWAIT
)
523 GSocket_SetNonBlocking(m_socket
, 1);
524 ret
= GSocket_Write(m_socket
, (const char *)buffer
, nbytes
);
525 GSocket_SetNonBlocking(m_socket
, 0);
536 if ( !(m_flags
& wxSOCKET_BLOCK
) && !WaitForWrite() )
539 ret
= GSocket_Write(m_socket
, (const char *)buffer
, nbytes
);
545 buffer
= (const char *)buffer
+ ret
;
548 // If we got here and wxSOCKET_WAITALL is not set, we can leave
549 // now. Otherwise, wait until we send all the data or until there
552 more
= (ret
> 0 && nbytes
> 0 && (m_flags
& wxSOCKET_WAITALL
));
559 wxSocketBase
& wxSocketBase::WriteMsg(const void *buffer
, wxUint32 nbytes
)
566 unsigned char sig
[4];
567 unsigned char len
[4];
576 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
578 msg
.sig
[0] = (unsigned char) 0xad;
579 msg
.sig
[1] = (unsigned char) 0xde;
580 msg
.sig
[2] = (unsigned char) 0xed;
581 msg
.sig
[3] = (unsigned char) 0xfe;
583 msg
.len
[0] = (unsigned char) (nbytes
& 0xff);
584 msg
.len
[1] = (unsigned char) ((nbytes
>> 8) & 0xff);
585 msg
.len
[2] = (unsigned char) ((nbytes
>> 16) & 0xff);
586 msg
.len
[3] = (unsigned char) ((nbytes
>> 24) & 0xff);
588 if (_Write(&msg
, sizeof(msg
)) < sizeof(msg
))
591 total
= _Write(buffer
, nbytes
);
596 msg
.sig
[0] = (unsigned char) 0xed;
597 msg
.sig
[1] = (unsigned char) 0xfe;
598 msg
.sig
[2] = (unsigned char) 0xad;
599 msg
.sig
[3] = (unsigned char) 0xde;
600 msg
.len
[0] = msg
.len
[1] = msg
.len
[2] = msg
.len
[3] = (char) 0;
602 if ((_Write(&msg
, sizeof(msg
))) < sizeof(msg
))
616 wxSocketBase
& wxSocketBase::Unread(const void *buffer
, wxUint32 nbytes
)
619 Pushback(buffer
, nbytes
);
627 wxSocketBase
& wxSocketBase::Discard()
630 char *buffer
= new char[MAX_DISCARD_SIZE
];
638 SetFlags(wxSOCKET_NOWAIT
);
642 ret
= _Read(buffer
, MAX_DISCARD_SIZE
);
645 while (ret
== MAX_DISCARD_SIZE
);
651 // Allow read events again
657 // --------------------------------------------------------------------------
659 // --------------------------------------------------------------------------
661 // All Wait functions poll the socket using GSocket_Select() to
662 // check for the specified combination of conditions, until one
663 // of these conditions become true, an error occurs, or the
664 // timeout elapses. The polling loop calls PROCESS_EVENTS(), so
665 // this won't block the GUI.
667 bool wxSocketBase::_Wait(long seconds
,
669 wxSocketEventFlags flags
)
671 GSocketEventFlags result
;
674 // Set this to TRUE to interrupt ongoing waits
677 // Check for valid socket
681 // Check for valid timeout value.
683 timeout
= seconds
* 1000 + milliseconds
;
685 timeout
= m_timeout
* 1000;
687 // Wait in an active polling loop.
689 // NOTE: We duplicate some of the code in OnRequest, but this doesn't
690 // hurt. It has to be here because the (GSocket) event might arrive
691 // a bit delayed, and it has to be in OnRequest as well because we
692 // don't know whether the Wait functions are being used.
694 // Do this at least once (important if timeout == 0, when
695 // we are just polling). Also, if just polling, do not yield.
702 result
= GSocket_Select(m_socket
, flags
| GSOCK_LOST_FLAG
);
704 // Incoming connection (server) or connection established (client)
705 if (result
& GSOCK_CONNECTION_FLAG
)
708 m_establishing
= FALSE
;
712 // Data available or output buffer ready
713 if ((result
& GSOCK_INPUT_FLAG
) || (result
& GSOCK_OUTPUT_FLAG
))
719 if (result
& GSOCK_LOST_FLAG
)
722 m_establishing
= FALSE
;
723 return (flags
& GSOCK_LOST_FLAG
) != 0;
727 if ((!timeout
) || (chrono
.Time() > timeout
) || (m_interrupt
))
736 bool wxSocketBase::Wait(long seconds
, long milliseconds
)
738 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
740 GSOCK_CONNECTION_FLAG
|
744 bool wxSocketBase::WaitForRead(long seconds
, long milliseconds
)
746 // Check pushback buffer before entering _Wait
750 // Note that GSOCK_INPUT_LOST has to be explicitly passed to
751 // _Wait becuase of the semantics of WaitForRead: a return
752 // value of TRUE means that a GSocket_Read call will return
753 // immediately, not that there is actually data to read.
755 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
759 bool wxSocketBase::WaitForWrite(long seconds
, long milliseconds
)
761 return _Wait(seconds
, milliseconds
, GSOCK_OUTPUT_FLAG
);
764 bool wxSocketBase::WaitForLost(long seconds
, long milliseconds
)
766 return _Wait(seconds
, milliseconds
, GSOCK_LOST_FLAG
);
769 // --------------------------------------------------------------------------
771 // --------------------------------------------------------------------------
774 // Get local or peer address
777 bool wxSocketBase::GetPeer(wxSockAddress
& addr_man
) const
784 peer
= GSocket_GetPeer(m_socket
);
786 // copying a null address would just trigger an assert anyway
791 addr_man
.SetAddress(peer
);
792 GAddress_destroy(peer
);
797 bool wxSocketBase::GetLocal(wxSockAddress
& addr_man
) const
804 local
= GSocket_GetLocal(m_socket
);
805 addr_man
.SetAddress(local
);
806 GAddress_destroy(local
);
812 // Save and restore socket state
815 void wxSocketBase::SaveState()
817 wxSocketState
*state
;
819 state
= new wxSocketState();
821 state
->m_flags
= m_flags
;
822 state
->m_notify
= m_notify
;
823 state
->m_eventmask
= m_eventmask
;
824 state
->m_clientData
= m_clientData
;
825 #if WXWIN_COMPATIBILITY
826 state
->m_cbk
= m_cbk
;
827 state
->m_cdata
= m_cdata
;
828 #endif // WXWIN_COMPATIBILITY
830 m_states
.Append(state
);
833 void wxSocketBase::RestoreState()
835 wxList::compatibility_iterator node
;
836 wxSocketState
*state
;
838 node
= m_states
.GetLast();
842 state
= (wxSocketState
*)node
->GetData();
844 m_flags
= state
->m_flags
;
845 m_notify
= state
->m_notify
;
846 m_eventmask
= state
->m_eventmask
;
847 m_clientData
= state
->m_clientData
;
848 #if WXWIN_COMPATIBILITY
849 m_cbk
= state
->m_cbk
;
850 m_cdata
= state
->m_cdata
;
851 #endif // WXWIN_COMPATIBILITY
853 m_states
.Erase(node
);
861 void wxSocketBase::SetTimeout(long seconds
)
866 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
869 void wxSocketBase::SetFlags(wxSocketFlags flags
)
875 // --------------------------------------------------------------------------
876 // Callbacks (now obsolete - use events instead)
877 // --------------------------------------------------------------------------
879 #if WXWIN_COMPATIBILITY
881 wxSocketBase::wxSockCbk
wxSocketBase::Callback(wxSockCbk cbk_
)
883 wxSockCbk old_cbk
= cbk_
;
889 char *wxSocketBase::CallbackData(char *data
)
891 char *old_data
= m_cdata
;
897 #endif // WXWIN_COMPATIBILITY
899 // --------------------------------------------------------------------------
901 // --------------------------------------------------------------------------
903 // A note on how events are processed, which is probably the most
904 // difficult thing to get working right while keeping the same API
905 // and functionality for all platforms.
907 // When GSocket detects an event, it calls wx_socket_callback, which in
908 // turn just calls wxSocketBase::OnRequest in the corresponding wxSocket
909 // object. OnRequest does some housekeeping, and if the event is to be
910 // propagated to the user, it creates a new wxSocketEvent object and
911 // posts it. The event is not processed immediately, but delayed with
912 // AddPendingEvent instead. This is necessary in order to decouple the
913 // event processing from wx_socket_callback; otherwise, subsequent IO
914 // calls made from the user event handler would fail, as gtk callbacks
915 // are not reentrant.
917 // Note that, unlike events, user callbacks (now deprecated) are _not_
918 // decoupled from wx_socket_callback and thus they suffer from a variety
919 // of problems. Avoid them where possible and use events instead.
922 void LINKAGEMODE
wx_socket_callback(GSocket
* WXUNUSED(socket
),
923 GSocketEvent notification
,
926 wxSocketBase
*sckobj
= (wxSocketBase
*)cdata
;
928 sckobj
->OnRequest((wxSocketNotify
) notification
);
931 void wxSocketBase::OnRequest(wxSocketNotify notification
)
933 // NOTE: We duplicate some of the code in _Wait, but this doesn't
934 // hurt. It has to be here because the (GSocket) event might arrive
935 // a bit delayed, and it has to be in _Wait as well because we don't
936 // know whether the Wait functions are being used.
940 case wxSOCKET_CONNECTION
:
941 m_establishing
= FALSE
;
945 // If we are in the middle of a R/W operation, do not
946 // propagate events to users. Also, filter 'late' events
947 // which are no longer valid.
950 if (m_reading
|| !GSocket_Select(m_socket
, GSOCK_INPUT_FLAG
))
954 case wxSOCKET_OUTPUT
:
955 if (m_writing
|| !GSocket_Select(m_socket
, GSOCK_OUTPUT_FLAG
))
961 m_establishing
= FALSE
;
968 // Schedule the event
970 wxSocketEventFlags flag
= 0;
971 switch (notification
)
973 case GSOCK_INPUT
: flag
= GSOCK_INPUT_FLAG
; break;
974 case GSOCK_OUTPUT
: flag
= GSOCK_OUTPUT_FLAG
; break;
975 case GSOCK_CONNECTION
: flag
= GSOCK_CONNECTION_FLAG
; break;
976 case GSOCK_LOST
: flag
= GSOCK_LOST_FLAG
; break;
978 wxLogWarning(_("wxSocket: unknown event!."));
982 if (((m_eventmask
& flag
) == flag
) && m_notify
)
986 wxSocketEvent
event(m_id
);
987 event
.m_event
= notification
;
988 event
.m_clientData
= m_clientData
;
989 event
.SetEventObject(this);
991 m_handler
->AddPendingEvent(event
);
994 #if WXWIN_COMPATIBILITY
996 m_cbk(*this, notification
, m_cdata
);
997 #endif // WXWIN_COMPATIBILITY
1001 void wxSocketBase::Notify(bool notify
)
1006 void wxSocketBase::SetNotify(wxSocketEventFlags flags
)
1008 m_eventmask
= flags
;
1011 void wxSocketBase::SetEventHandler(wxEvtHandler
& handler
, int id
)
1013 m_handler
= &handler
;
1017 // --------------------------------------------------------------------------
1019 // --------------------------------------------------------------------------
1021 void wxSocketBase::Pushback(const void *buffer
, wxUint32 size
)
1025 if (m_unread
== NULL
)
1026 m_unread
= malloc(size
);
1031 tmp
= malloc(m_unrd_size
+ size
);
1032 memcpy((char *)tmp
+ size
, m_unread
, m_unrd_size
);
1038 m_unrd_size
+= size
;
1040 memcpy(m_unread
, buffer
, size
);
1043 wxUint32
wxSocketBase::GetPushback(void *buffer
, wxUint32 size
, bool peek
)
1048 if (size
> (m_unrd_size
-m_unrd_cur
))
1049 size
= m_unrd_size
-m_unrd_cur
;
1051 memcpy(buffer
, (char *)m_unread
+ m_unrd_cur
, size
);
1056 if (m_unrd_size
== m_unrd_cur
)
1069 // ==========================================================================
1071 // ==========================================================================
1073 // --------------------------------------------------------------------------
1075 // --------------------------------------------------------------------------
1077 wxSocketServer::wxSocketServer(wxSockAddress
& addr_man
,
1078 wxSocketFlags flags
)
1079 : wxSocketBase(flags
, wxSOCKET_SERVER
)
1081 wxLogTrace( wxTRACE_Socket
, _T("Opening wxSocketServer") );
1083 m_socket
= GSocket_new();
1087 wxLogTrace( wxTRACE_Socket
, _T("*** GSocket_new failed") );
1091 // Setup the socket as server
1093 GSocket_SetLocal(m_socket
, addr_man
.GetAddress());
1094 if (GSocket_SetServer(m_socket
) != GSOCK_NOERROR
)
1096 GSocket_destroy(m_socket
);
1099 wxLogTrace( wxTRACE_Socket
, _T("*** GSocket_SetServer failed") );
1103 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
1104 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1105 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1106 wx_socket_callback
, (char *)this);
1109 // --------------------------------------------------------------------------
1111 // --------------------------------------------------------------------------
1113 bool wxSocketServer::AcceptWith(wxSocketBase
& sock
, bool wait
)
1115 GSocket
*child_socket
;
1120 // If wait == FALSE, then the call should be nonblocking.
1121 // When we are finished, we put the socket to blocking mode
1125 GSocket_SetNonBlocking(m_socket
, 1);
1127 child_socket
= GSocket_WaitConnection(m_socket
);
1130 GSocket_SetNonBlocking(m_socket
, 0);
1135 sock
.m_type
= wxSOCKET_BASE
;
1136 sock
.m_socket
= child_socket
;
1137 sock
.m_connected
= TRUE
;
1139 GSocket_SetTimeout(sock
.m_socket
, sock
.m_timeout
* 1000);
1140 GSocket_SetCallback(sock
.m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1141 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1142 wx_socket_callback
, (char *)&sock
);
1147 wxSocketBase
*wxSocketServer::Accept(bool wait
)
1149 wxSocketBase
* sock
= new wxSocketBase();
1151 sock
->SetFlags(m_flags
);
1153 if (!AcceptWith(*sock
, wait
))
1162 bool wxSocketServer::WaitForAccept(long seconds
, long milliseconds
)
1164 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
);
1167 // ==========================================================================
1169 // ==========================================================================
1171 // --------------------------------------------------------------------------
1173 // --------------------------------------------------------------------------
1175 wxSocketClient::wxSocketClient(wxSocketFlags flags
)
1176 : wxSocketBase(flags
, wxSOCKET_CLIENT
)
1180 wxSocketClient::~wxSocketClient()
1184 // --------------------------------------------------------------------------
1186 // --------------------------------------------------------------------------
1188 bool wxSocketClient::Connect(wxSockAddress
& addr_man
, bool wait
)
1194 // Shutdown and destroy the socket
1196 GSocket_destroy(m_socket
);
1199 m_socket
= GSocket_new();
1200 m_connected
= FALSE
;
1201 m_establishing
= FALSE
;
1206 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
1207 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1208 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1209 wx_socket_callback
, (char *)this);
1211 // If wait == FALSE, then the call should be nonblocking.
1212 // When we are finished, we put the socket to blocking mode
1216 GSocket_SetNonBlocking(m_socket
, 1);
1218 GSocket_SetPeer(m_socket
, addr_man
.GetAddress());
1219 err
= GSocket_Connect(m_socket
, GSOCK_STREAMED
);
1222 GSocket_SetNonBlocking(m_socket
, 0);
1224 if (err
!= GSOCK_NOERROR
)
1226 if (err
== GSOCK_WOULDBLOCK
)
1227 m_establishing
= TRUE
;
1236 bool wxSocketClient::WaitOnConnect(long seconds
, long milliseconds
)
1238 if (m_connected
) // Already connected
1241 if (!m_establishing
|| !m_socket
) // No connection in progress
1244 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
|
1248 // ==========================================================================
1250 // ==========================================================================
1252 /* NOTE: experimental stuff - might change */
1254 wxDatagramSocket::wxDatagramSocket( wxSockAddress
& addr
,
1255 wxSocketFlags flags
)
1256 : wxSocketBase( flags
, wxSOCKET_DATAGRAM
)
1258 // Create the socket
1259 m_socket
= GSocket_new();
1264 // Setup the socket as non connection oriented
1265 GSocket_SetLocal(m_socket
, addr
.GetAddress());
1266 if( GSocket_SetNonOriented(m_socket
) != GSOCK_NOERROR
)
1268 GSocket_destroy(m_socket
);
1273 // Initialize all stuff
1274 m_connected
= FALSE
;
1275 m_establishing
= FALSE
;
1276 GSocket_SetTimeout( m_socket
, m_timeout
);
1277 GSocket_SetCallback( m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1278 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1279 wx_socket_callback
, (char*)this );
1283 wxDatagramSocket
& wxDatagramSocket::RecvFrom( wxSockAddress
& addr
,
1292 wxDatagramSocket
& wxDatagramSocket::SendTo( wxSockAddress
& addr
,
1296 GSocket_SetPeer(m_socket
, addr
.GetAddress());
1301 // ==========================================================================
1303 // ==========================================================================
1305 class wxSocketModule
: public wxModule
1308 virtual bool OnInit()
1310 // wxSocketBase will call GSocket_Init() itself when/if needed
1314 virtual void OnExit()
1316 if ( wxSocketBase::IsInitialized() )
1317 wxSocketBase::Shutdown();
1321 DECLARE_DYNAMIC_CLASS(wxSocketModule
)
1324 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule
, wxModule
)