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 wxAppTraits
*traits
= wxAppConsole::GetInstance() ?
130 wxAppConsole::GetInstance()->GetTraits() : NULL
;
131 GSocketGUIFunctionsTable
*functions
=
132 traits
? traits
->GetSocketGUIFunctionsTable() : NULL
;
133 GSocket_SetGUIFunctions(functions
);
135 if ( !GSocket_Init() )
146 void wxSocketBase::Shutdown()
148 // we should be initialized
149 wxASSERT_MSG( m_countInit
, _T("extra call to Shutdown()") );
150 if ( !--m_countInit
)
156 // --------------------------------------------------------------------------
158 // --------------------------------------------------------------------------
160 void wxSocketBase::Init()
163 m_type
= wxSOCKET_UNINIT
;
174 m_beingDeleted
= FALSE
;
187 #if WXWIN_COMPATIBILITY
190 #endif // WXWIN_COMPATIBILITY
192 if ( !IsInitialized() )
194 // this Initialize() will be undone by wxSocketModule::OnExit(), all the
195 // other calls to it should be matched by a call to Shutdown()
200 wxSocketBase::wxSocketBase()
205 wxSocketBase::wxSocketBase(wxSocketFlags flags
, wxSocketType type
)
213 wxSocketBase::~wxSocketBase()
215 // Just in case the app called Destroy() *and* then deleted
216 // the socket immediately: don't leave dangling pointers.
217 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
219 traits
->RemoveFromPendingDelete(this);
221 // Shutdown and close the socket
225 // Destroy the GSocket object
227 GSocket_destroy(m_socket
);
229 // Free the pushback buffer
234 bool wxSocketBase::Destroy()
236 // Delayed destruction: the socket will be deleted during the next
237 // idle loop iteration. This ensures that all pending events have
239 m_beingDeleted
= TRUE
;
241 // Shutdown and close the socket
244 // Supress events from now on
247 // schedule this object for deletion
248 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
251 // let the traits object decide what to do with us
252 traits
->ScheduleForDestroy(this);
254 else // no app or no traits
256 // in wxBase we might have no app object at all, don't leak memory
263 // --------------------------------------------------------------------------
265 // --------------------------------------------------------------------------
267 // The following IO operations update m_error and m_lcount:
268 // {Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard}
270 // TODO: Should Connect, Accept and AcceptWith update m_error?
272 bool wxSocketBase::Close()
274 // Interrupt pending waits
280 GSocket_UnsetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
281 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
);
283 // Shutdown the connection
284 GSocket_Shutdown(m_socket
);
288 m_establishing
= FALSE
;
292 wxSocketBase
& wxSocketBase::Read(void* buffer
, wxUint32 nbytes
)
297 m_lcount
= _Read(buffer
, nbytes
);
299 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
300 if (m_flags
& wxSOCKET_WAITALL
)
301 m_error
= (m_lcount
!= nbytes
);
303 m_error
= (m_lcount
== 0);
305 // Allow read events from now on
311 wxUint32
wxSocketBase::_Read(void* buffer
, wxUint32 nbytes
)
316 // Try the pushback buffer first
317 total
= GetPushback(buffer
, nbytes
, FALSE
);
319 buffer
= (char *)buffer
+ total
;
321 // Return now in one of the following cases:
322 // - the socket is invalid,
323 // - we got all the data,
324 // - we got *some* data and we are not using wxSOCKET_WAITALL.
327 ((total
!= 0) && !(m_flags
& wxSOCKET_WAITALL
)) )
330 // Possible combinations (they are checked in this order)
332 // wxSOCKET_WAITALL (with or without wxSOCKET_BLOCK)
336 if (m_flags
& wxSOCKET_NOWAIT
)
338 GSocket_SetNonBlocking(m_socket
, 1);
339 ret
= GSocket_Read(m_socket
, (char *)buffer
, nbytes
);
340 GSocket_SetNonBlocking(m_socket
, 0);
351 if ( !(m_flags
& wxSOCKET_BLOCK
) && !WaitForRead() )
354 ret
= GSocket_Read(m_socket
, (char *)buffer
, nbytes
);
360 buffer
= (char *)buffer
+ ret
;
363 // If we got here and wxSOCKET_WAITALL is not set, we can leave
364 // now. Otherwise, wait until we recv all the data or until there
367 more
= (ret
> 0 && nbytes
> 0 && (m_flags
& wxSOCKET_WAITALL
));
374 wxSocketBase
& wxSocketBase::ReadMsg(void* buffer
, wxUint32 nbytes
)
376 wxUint32 len
, len2
, sig
, total
;
381 unsigned char sig
[4];
382 unsigned char len
[4];
391 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
393 if (_Read(&msg
, sizeof(msg
)) != sizeof(msg
))
396 sig
= (wxUint32
)msg
.sig
[0];
397 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
398 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
399 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
401 if (sig
!= 0xfeeddead)
403 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
407 len
= (wxUint32
)msg
.len
[0];
408 len
|= (wxUint32
)(msg
.len
[1] << 8);
409 len
|= (wxUint32
)(msg
.len
[2] << 16);
410 len
|= (wxUint32
)(msg
.len
[3] << 24);
420 // Don't attemp to read if the msg was zero bytes long.
423 total
= _Read(buffer
, len
);
430 char *discard_buffer
= new char[MAX_DISCARD_SIZE
];
433 // NOTE: discarded bytes don't add to m_lcount.
436 discard_len
= ((len2
> MAX_DISCARD_SIZE
)? MAX_DISCARD_SIZE
: len2
);
437 discard_len
= _Read(discard_buffer
, (wxUint32
)discard_len
);
438 len2
-= (wxUint32
)discard_len
;
440 while ((discard_len
> 0) && len2
);
442 delete [] discard_buffer
;
447 if (_Read(&msg
, sizeof(msg
)) != sizeof(msg
))
450 sig
= (wxUint32
)msg
.sig
[0];
451 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
452 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
453 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
455 if (sig
!= 0xdeadfeed)
457 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
473 wxSocketBase
& wxSocketBase::Peek(void* buffer
, wxUint32 nbytes
)
478 m_lcount
= _Read(buffer
, nbytes
);
479 Pushback(buffer
, m_lcount
);
481 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
482 if (m_flags
& wxSOCKET_WAITALL
)
483 m_error
= (m_lcount
!= nbytes
);
485 m_error
= (m_lcount
== 0);
487 // Allow read events again
493 wxSocketBase
& wxSocketBase::Write(const void *buffer
, wxUint32 nbytes
)
498 m_lcount
= _Write(buffer
, nbytes
);
500 // If in wxSOCKET_WAITALL mode, all bytes should have been written.
501 if (m_flags
& wxSOCKET_WAITALL
)
502 m_error
= (m_lcount
!= nbytes
);
504 m_error
= (m_lcount
== 0);
506 // Allow write events again
512 wxUint32
wxSocketBase::_Write(const void *buffer
, wxUint32 nbytes
)
517 // If the socket is invalid or parameters are ill, return immediately
518 if (!m_socket
|| !buffer
|| !nbytes
)
521 // Possible combinations (they are checked in this order)
523 // wxSOCKET_WAITALL (with or without wxSOCKET_BLOCK)
527 if (m_flags
& wxSOCKET_NOWAIT
)
529 GSocket_SetNonBlocking(m_socket
, 1);
530 ret
= GSocket_Write(m_socket
, (const char *)buffer
, nbytes
);
531 GSocket_SetNonBlocking(m_socket
, 0);
542 if ( !(m_flags
& wxSOCKET_BLOCK
) && !WaitForWrite() )
545 ret
= GSocket_Write(m_socket
, (const char *)buffer
, nbytes
);
551 buffer
= (const char *)buffer
+ ret
;
554 // If we got here and wxSOCKET_WAITALL is not set, we can leave
555 // now. Otherwise, wait until we send all the data or until there
558 more
= (ret
> 0 && nbytes
> 0 && (m_flags
& wxSOCKET_WAITALL
));
565 wxSocketBase
& wxSocketBase::WriteMsg(const void *buffer
, wxUint32 nbytes
)
572 unsigned char sig
[4];
573 unsigned char len
[4];
582 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
584 msg
.sig
[0] = (unsigned char) 0xad;
585 msg
.sig
[1] = (unsigned char) 0xde;
586 msg
.sig
[2] = (unsigned char) 0xed;
587 msg
.sig
[3] = (unsigned char) 0xfe;
589 msg
.len
[0] = (unsigned char) (nbytes
& 0xff);
590 msg
.len
[1] = (unsigned char) ((nbytes
>> 8) & 0xff);
591 msg
.len
[2] = (unsigned char) ((nbytes
>> 16) & 0xff);
592 msg
.len
[3] = (unsigned char) ((nbytes
>> 24) & 0xff);
594 if (_Write(&msg
, sizeof(msg
)) < sizeof(msg
))
597 total
= _Write(buffer
, nbytes
);
602 msg
.sig
[0] = (unsigned char) 0xed;
603 msg
.sig
[1] = (unsigned char) 0xfe;
604 msg
.sig
[2] = (unsigned char) 0xad;
605 msg
.sig
[3] = (unsigned char) 0xde;
606 msg
.len
[0] = msg
.len
[1] = msg
.len
[2] = msg
.len
[3] = (char) 0;
608 if ((_Write(&msg
, sizeof(msg
))) < sizeof(msg
))
622 wxSocketBase
& wxSocketBase::Unread(const void *buffer
, wxUint32 nbytes
)
625 Pushback(buffer
, nbytes
);
633 wxSocketBase
& wxSocketBase::Discard()
636 char *buffer
= new char[MAX_DISCARD_SIZE
];
644 SetFlags(wxSOCKET_NOWAIT
);
648 ret
= _Read(buffer
, MAX_DISCARD_SIZE
);
651 while (ret
== MAX_DISCARD_SIZE
);
657 // Allow read events again
663 // --------------------------------------------------------------------------
665 // --------------------------------------------------------------------------
667 // All Wait functions poll the socket using GSocket_Select() to
668 // check for the specified combination of conditions, until one
669 // of these conditions become true, an error occurs, or the
670 // timeout elapses. The polling loop calls PROCESS_EVENTS(), so
671 // this won't block the GUI.
673 bool wxSocketBase::_Wait(long seconds
,
675 wxSocketEventFlags flags
)
677 GSocketEventFlags result
;
680 // Set this to TRUE to interrupt ongoing waits
683 // Check for valid socket
687 // Check for valid timeout value.
689 timeout
= seconds
* 1000 + milliseconds
;
691 timeout
= m_timeout
* 1000;
693 // Wait in an active polling loop.
695 // NOTE: We duplicate some of the code in OnRequest, but this doesn't
696 // hurt. It has to be here because the (GSocket) event might arrive
697 // a bit delayed, and it has to be in OnRequest as well because we
698 // don't know whether the Wait functions are being used.
700 // Do this at least once (important if timeout == 0, when
701 // we are just polling). Also, if just polling, do not yield.
708 result
= GSocket_Select(m_socket
, flags
| GSOCK_LOST_FLAG
);
710 // Incoming connection (server) or connection established (client)
711 if (result
& GSOCK_CONNECTION_FLAG
)
714 m_establishing
= FALSE
;
718 // Data available or output buffer ready
719 if ((result
& GSOCK_INPUT_FLAG
) || (result
& GSOCK_OUTPUT_FLAG
))
725 if (result
& GSOCK_LOST_FLAG
)
728 m_establishing
= FALSE
;
729 return (flags
& GSOCK_LOST_FLAG
) != 0;
733 if ((!timeout
) || (chrono
.Time() > timeout
) || (m_interrupt
))
742 bool wxSocketBase::Wait(long seconds
, long milliseconds
)
744 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
746 GSOCK_CONNECTION_FLAG
|
750 bool wxSocketBase::WaitForRead(long seconds
, long milliseconds
)
752 // Check pushback buffer before entering _Wait
756 // Note that GSOCK_INPUT_LOST has to be explicitly passed to
757 // _Wait becuase of the semantics of WaitForRead: a return
758 // value of TRUE means that a GSocket_Read call will return
759 // immediately, not that there is actually data to read.
761 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
765 bool wxSocketBase::WaitForWrite(long seconds
, long milliseconds
)
767 return _Wait(seconds
, milliseconds
, GSOCK_OUTPUT_FLAG
);
770 bool wxSocketBase::WaitForLost(long seconds
, long milliseconds
)
772 return _Wait(seconds
, milliseconds
, GSOCK_LOST_FLAG
);
775 // --------------------------------------------------------------------------
777 // --------------------------------------------------------------------------
780 // Get local or peer address
783 bool wxSocketBase::GetPeer(wxSockAddress
& addr_man
) const
790 peer
= GSocket_GetPeer(m_socket
);
792 // copying a null address would just trigger an assert anyway
797 addr_man
.SetAddress(peer
);
798 GAddress_destroy(peer
);
803 bool wxSocketBase::GetLocal(wxSockAddress
& addr_man
) const
810 local
= GSocket_GetLocal(m_socket
);
811 addr_man
.SetAddress(local
);
812 GAddress_destroy(local
);
818 // Save and restore socket state
821 void wxSocketBase::SaveState()
823 wxSocketState
*state
;
825 state
= new wxSocketState();
827 state
->m_flags
= m_flags
;
828 state
->m_notify
= m_notify
;
829 state
->m_eventmask
= m_eventmask
;
830 state
->m_clientData
= m_clientData
;
831 #if WXWIN_COMPATIBILITY
832 state
->m_cbk
= m_cbk
;
833 state
->m_cdata
= m_cdata
;
834 #endif // WXWIN_COMPATIBILITY
836 m_states
.Append(state
);
839 void wxSocketBase::RestoreState()
841 wxList::compatibility_iterator node
;
842 wxSocketState
*state
;
844 node
= m_states
.GetLast();
848 state
= (wxSocketState
*)node
->GetData();
850 m_flags
= state
->m_flags
;
851 m_notify
= state
->m_notify
;
852 m_eventmask
= state
->m_eventmask
;
853 m_clientData
= state
->m_clientData
;
854 #if WXWIN_COMPATIBILITY
855 m_cbk
= state
->m_cbk
;
856 m_cdata
= state
->m_cdata
;
857 #endif // WXWIN_COMPATIBILITY
859 m_states
.Erase(node
);
867 void wxSocketBase::SetTimeout(long seconds
)
872 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
875 void wxSocketBase::SetFlags(wxSocketFlags flags
)
881 // --------------------------------------------------------------------------
882 // Callbacks (now obsolete - use events instead)
883 // --------------------------------------------------------------------------
885 #if WXWIN_COMPATIBILITY
887 wxSocketBase::wxSockCbk
wxSocketBase::Callback(wxSockCbk cbk_
)
889 wxSockCbk old_cbk
= cbk_
;
895 char *wxSocketBase::CallbackData(char *data
)
897 char *old_data
= m_cdata
;
903 #endif // WXWIN_COMPATIBILITY
905 // --------------------------------------------------------------------------
907 // --------------------------------------------------------------------------
909 // A note on how events are processed, which is probably the most
910 // difficult thing to get working right while keeping the same API
911 // and functionality for all platforms.
913 // When GSocket detects an event, it calls wx_socket_callback, which in
914 // turn just calls wxSocketBase::OnRequest in the corresponding wxSocket
915 // object. OnRequest does some housekeeping, and if the event is to be
916 // propagated to the user, it creates a new wxSocketEvent object and
917 // posts it. The event is not processed immediately, but delayed with
918 // AddPendingEvent instead. This is necessary in order to decouple the
919 // event processing from wx_socket_callback; otherwise, subsequent IO
920 // calls made from the user event handler would fail, as gtk callbacks
921 // are not reentrant.
923 // Note that, unlike events, user callbacks (now deprecated) are _not_
924 // decoupled from wx_socket_callback and thus they suffer from a variety
925 // of problems. Avoid them where possible and use events instead.
928 void LINKAGEMODE
wx_socket_callback(GSocket
* WXUNUSED(socket
),
929 GSocketEvent notification
,
932 wxSocketBase
*sckobj
= (wxSocketBase
*)cdata
;
934 sckobj
->OnRequest((wxSocketNotify
) notification
);
937 void wxSocketBase::OnRequest(wxSocketNotify notification
)
939 // NOTE: We duplicate some of the code in _Wait, but this doesn't
940 // hurt. It has to be here because the (GSocket) event might arrive
941 // a bit delayed, and it has to be in _Wait as well because we don't
942 // know whether the Wait functions are being used.
946 case wxSOCKET_CONNECTION
:
947 m_establishing
= FALSE
;
951 // If we are in the middle of a R/W operation, do not
952 // propagate events to users. Also, filter 'late' events
953 // which are no longer valid.
956 if (m_reading
|| !GSocket_Select(m_socket
, GSOCK_INPUT_FLAG
))
960 case wxSOCKET_OUTPUT
:
961 if (m_writing
|| !GSocket_Select(m_socket
, GSOCK_OUTPUT_FLAG
))
967 m_establishing
= FALSE
;
974 // Schedule the event
976 wxSocketEventFlags flag
= 0;
977 switch (notification
)
979 case GSOCK_INPUT
: flag
= GSOCK_INPUT_FLAG
; break;
980 case GSOCK_OUTPUT
: flag
= GSOCK_OUTPUT_FLAG
; break;
981 case GSOCK_CONNECTION
: flag
= GSOCK_CONNECTION_FLAG
; break;
982 case GSOCK_LOST
: flag
= GSOCK_LOST_FLAG
; break;
984 wxLogWarning(_("wxSocket: unknown event!."));
988 if (((m_eventmask
& flag
) == flag
) && m_notify
)
992 wxSocketEvent
event(m_id
);
993 event
.m_event
= notification
;
994 event
.m_clientData
= m_clientData
;
995 event
.SetEventObject(this);
997 m_handler
->AddPendingEvent(event
);
1000 #if WXWIN_COMPATIBILITY
1002 m_cbk(*this, notification
, m_cdata
);
1003 #endif // WXWIN_COMPATIBILITY
1007 void wxSocketBase::Notify(bool notify
)
1012 void wxSocketBase::SetNotify(wxSocketEventFlags flags
)
1014 m_eventmask
= flags
;
1017 void wxSocketBase::SetEventHandler(wxEvtHandler
& handler
, int id
)
1019 m_handler
= &handler
;
1023 // --------------------------------------------------------------------------
1025 // --------------------------------------------------------------------------
1027 void wxSocketBase::Pushback(const void *buffer
, wxUint32 size
)
1031 if (m_unread
== NULL
)
1032 m_unread
= malloc(size
);
1037 tmp
= malloc(m_unrd_size
+ size
);
1038 memcpy((char *)tmp
+ size
, m_unread
, m_unrd_size
);
1044 m_unrd_size
+= size
;
1046 memcpy(m_unread
, buffer
, size
);
1049 wxUint32
wxSocketBase::GetPushback(void *buffer
, wxUint32 size
, bool peek
)
1054 if (size
> (m_unrd_size
-m_unrd_cur
))
1055 size
= m_unrd_size
-m_unrd_cur
;
1057 memcpy(buffer
, (char *)m_unread
+ m_unrd_cur
, size
);
1062 if (m_unrd_size
== m_unrd_cur
)
1075 // ==========================================================================
1077 // ==========================================================================
1079 // --------------------------------------------------------------------------
1081 // --------------------------------------------------------------------------
1083 wxSocketServer::wxSocketServer(wxSockAddress
& addr_man
,
1084 wxSocketFlags flags
)
1085 : wxSocketBase(flags
, wxSOCKET_SERVER
)
1087 wxLogTrace( wxTRACE_Socket
, _T("Opening wxSocketServer") );
1089 m_socket
= GSocket_new();
1093 wxLogTrace( wxTRACE_Socket
, _T("*** GSocket_new failed") );
1097 // Setup the socket as server
1099 GSocket_SetLocal(m_socket
, addr_man
.GetAddress());
1100 if (GSocket_SetServer(m_socket
) != GSOCK_NOERROR
)
1102 GSocket_destroy(m_socket
);
1105 wxLogTrace( wxTRACE_Socket
, _T("*** GSocket_SetServer failed") );
1109 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
1110 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1111 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1112 wx_socket_callback
, (char *)this);
1115 // --------------------------------------------------------------------------
1117 // --------------------------------------------------------------------------
1119 bool wxSocketServer::AcceptWith(wxSocketBase
& sock
, bool wait
)
1121 GSocket
*child_socket
;
1126 // If wait == FALSE, then the call should be nonblocking.
1127 // When we are finished, we put the socket to blocking mode
1131 GSocket_SetNonBlocking(m_socket
, 1);
1133 child_socket
= GSocket_WaitConnection(m_socket
);
1136 GSocket_SetNonBlocking(m_socket
, 0);
1141 sock
.m_type
= wxSOCKET_BASE
;
1142 sock
.m_socket
= child_socket
;
1143 sock
.m_connected
= TRUE
;
1145 GSocket_SetTimeout(sock
.m_socket
, sock
.m_timeout
* 1000);
1146 GSocket_SetCallback(sock
.m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1147 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1148 wx_socket_callback
, (char *)&sock
);
1153 wxSocketBase
*wxSocketServer::Accept(bool wait
)
1155 wxSocketBase
* sock
= new wxSocketBase();
1157 sock
->SetFlags(m_flags
);
1159 if (!AcceptWith(*sock
, wait
))
1168 bool wxSocketServer::WaitForAccept(long seconds
, long milliseconds
)
1170 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
);
1173 // ==========================================================================
1175 // ==========================================================================
1177 // --------------------------------------------------------------------------
1179 // --------------------------------------------------------------------------
1181 wxSocketClient::wxSocketClient(wxSocketFlags flags
)
1182 : wxSocketBase(flags
, wxSOCKET_CLIENT
)
1186 wxSocketClient::~wxSocketClient()
1190 // --------------------------------------------------------------------------
1192 // --------------------------------------------------------------------------
1194 bool wxSocketClient::Connect(wxSockAddress
& addr_man
, bool wait
)
1200 // Shutdown and destroy the socket
1202 GSocket_destroy(m_socket
);
1205 m_socket
= GSocket_new();
1206 m_connected
= FALSE
;
1207 m_establishing
= FALSE
;
1212 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
1213 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1214 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1215 wx_socket_callback
, (char *)this);
1217 // If wait == FALSE, then the call should be nonblocking.
1218 // When we are finished, we put the socket to blocking mode
1222 GSocket_SetNonBlocking(m_socket
, 1);
1224 GSocket_SetPeer(m_socket
, addr_man
.GetAddress());
1225 err
= GSocket_Connect(m_socket
, GSOCK_STREAMED
);
1228 GSocket_SetNonBlocking(m_socket
, 0);
1230 if (err
!= GSOCK_NOERROR
)
1232 if (err
== GSOCK_WOULDBLOCK
)
1233 m_establishing
= TRUE
;
1242 bool wxSocketClient::WaitOnConnect(long seconds
, long milliseconds
)
1244 if (m_connected
) // Already connected
1247 if (!m_establishing
|| !m_socket
) // No connection in progress
1250 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
|
1254 // ==========================================================================
1256 // ==========================================================================
1258 /* NOTE: experimental stuff - might change */
1260 wxDatagramSocket::wxDatagramSocket( wxSockAddress
& addr
,
1261 wxSocketFlags flags
)
1262 : wxSocketBase( flags
, wxSOCKET_DATAGRAM
)
1264 // Create the socket
1265 m_socket
= GSocket_new();
1270 // Setup the socket as non connection oriented
1271 GSocket_SetLocal(m_socket
, addr
.GetAddress());
1272 if( GSocket_SetNonOriented(m_socket
) != GSOCK_NOERROR
)
1274 GSocket_destroy(m_socket
);
1279 // Initialize all stuff
1280 m_connected
= FALSE
;
1281 m_establishing
= FALSE
;
1282 GSocket_SetTimeout( m_socket
, m_timeout
);
1283 GSocket_SetCallback( m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1284 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1285 wx_socket_callback
, (char*)this );
1289 wxDatagramSocket
& wxDatagramSocket::RecvFrom( wxSockAddress
& addr
,
1298 wxDatagramSocket
& wxDatagramSocket::SendTo( wxSockAddress
& addr
,
1302 GSocket_SetPeer(m_socket
, addr
.GetAddress());
1307 // ==========================================================================
1309 // ==========================================================================
1311 class wxSocketModule
: public wxModule
1314 virtual bool OnInit()
1316 // wxSocketBase will call GSocket_Init() itself when/if needed
1320 virtual void OnExit()
1322 if ( wxSocketBase::IsInitialized() )
1323 wxSocketBase::Shutdown();
1327 DECLARE_DYNAMIC_CLASS(wxSocketModule
)
1330 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule
, wxModule
)