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 license
10 /////////////////////////////////////////////////////////////////////////////
12 // ==========================================================================
14 // ==========================================================================
17 #pragma implementation "socket.h"
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
31 #include "wx/object.h"
32 #include "wx/string.h"
35 #include "wx/module.h"
41 #include "wx/gdicmn.h" // for wxPendingDelete
44 #include "wx/sckaddr.h"
45 #include "wx/socket.h"
47 // --------------------------------------------------------------------------
48 // macros and constants
49 // --------------------------------------------------------------------------
52 #define MAX_DISCARD_SIZE (10 * 1024)
54 // what to do within waits: in wxBase we don't do anything as we don't have
55 // the event loop anyhow (for now). In GUI apps we have 2 cases: from the main
56 // thread itself we have to call wxYield() to let the events (including the
57 // GUI events and the low-level (not wxWindows) events from GSocket) be
58 // processed. From another thread it is enough to just call wxThread::Yield()
59 // which will give away the rest of our time slice: the explanation is that
60 // the events will be processed by the main thread anyhow, without calling
61 // wxYield(), but we don't want to eat the CPU time uselessly while sitting
62 // in the loop waiting for the data
65 #define PROCESS_EVENTS() \
67 if ( wxThread::IsMain() ) \
72 #else // !wxUSE_THREADS
73 #define PROCESS_EVENTS() wxYield()
74 #endif // wxUSE_THREADS/!wxUSE_THREADS
76 #define PROCESS_EVENTS()
77 #endif // wxUSE_GUI/!wxUSE_GUI
79 #define wxTRACE_Socket _T("wxSocket")
81 // --------------------------------------------------------------------------
83 // --------------------------------------------------------------------------
85 IMPLEMENT_CLASS(wxSocketBase
, wxObject
)
86 IMPLEMENT_CLASS(wxSocketServer
, wxSocketBase
)
87 IMPLEMENT_CLASS(wxSocketClient
, wxSocketBase
)
88 IMPLEMENT_CLASS(wxDatagramSocket
, wxSocketBase
)
89 IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent
, wxEvent
)
91 // --------------------------------------------------------------------------
93 // --------------------------------------------------------------------------
95 class wxSocketState
: public wxObject
98 wxSocketFlags m_flags
;
99 wxSocketEventFlags m_eventmask
;
102 #if WXWIN_COMPATIBILITY
103 wxSocketBase::wxSockCbk m_cbk
;
105 #endif // WXWIN_COMPATIBILITY
108 wxSocketState() : wxObject() {}
111 // ==========================================================================
113 // ==========================================================================
115 // --------------------------------------------------------------------------
116 // Initialization and shutdown
117 // --------------------------------------------------------------------------
119 // FIXME-MT: all this is MT-unsafe, of course, we should protect all accesses
120 // to m_countInit with a crit section
121 size_t wxSocketBase::m_countInit
= 0;
123 bool wxSocketBase::IsInitialized()
125 return m_countInit
> 0;
128 bool wxSocketBase::Initialize()
130 if ( !m_countInit
++ )
132 if ( !GSocket_Init() )
143 void wxSocketBase::Shutdown()
145 // we should be initialized
146 wxASSERT_MSG( m_countInit
, _T("extra call to Shutdown()") );
147 if ( !--m_countInit
)
153 // --------------------------------------------------------------------------
155 // --------------------------------------------------------------------------
157 void wxSocketBase::Init()
160 m_type
= wxSOCKET_UNINIT
;
171 m_beingDeleted
= FALSE
;
184 #if WXWIN_COMPATIBILITY
187 #endif // WXWIN_COMPATIBILITY
189 if ( !IsInitialized() )
191 // this Initialize() will be undone by wxSocketModule::OnExit(), all the
192 // other calls to it should be matched by a call to Shutdown()
197 wxSocketBase::wxSocketBase()
202 wxSocketBase::wxSocketBase(wxSocketFlags flags
, wxSocketType type
)
210 wxSocketBase::~wxSocketBase()
212 // Just in case the app called Destroy() *and* then deleted
213 // the socket immediately: don't leave dangling pointers.
215 wxPendingDelete
.DeleteObject(this);
218 // Shutdown and close the socket
222 // Destroy the GSocket object
224 GSocket_destroy(m_socket
);
226 // Free the pushback buffer
231 bool wxSocketBase::Destroy()
233 // Delayed destruction: the socket will be deleted during the next
234 // idle loop iteration. This ensures that all pending events have
236 m_beingDeleted
= TRUE
;
238 // Shutdown and close the socket
241 // Supress events from now on
245 if ( !wxPendingDelete
.Member(this) )
246 wxPendingDelete
.Append(this);
254 // --------------------------------------------------------------------------
256 // --------------------------------------------------------------------------
258 // The following IO operations update m_error and m_lcount:
259 // {Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard}
261 // TODO: Should Connect, Accept and AcceptWith update m_error?
263 bool wxSocketBase::Close()
265 // Interrupt pending waits
271 GSocket_UnsetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
272 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
);
274 // Shutdown the connection
275 GSocket_Shutdown(m_socket
);
279 m_establishing
= FALSE
;
283 wxSocketBase
& wxSocketBase::Read(void* buffer
, wxUint32 nbytes
)
288 m_lcount
= _Read(buffer
, nbytes
);
290 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
291 if (m_flags
& wxSOCKET_WAITALL
)
292 m_error
= (m_lcount
!= nbytes
);
294 m_error
= (m_lcount
== 0);
296 // Allow read events from now on
302 wxUint32
wxSocketBase::_Read(void* buffer
, wxUint32 nbytes
)
307 // Try the pushback buffer first
308 total
= GetPushback(buffer
, nbytes
, FALSE
);
310 buffer
= (char *)buffer
+ total
;
312 // Return now in one of the following cases:
313 // - the socket is invalid,
314 // - we got all the data,
315 // - we got *some* data and we are not using wxSOCKET_WAITALL.
318 ((total
!= 0) && !(m_flags
& wxSOCKET_WAITALL
)) )
321 // Possible combinations (they are checked in this order)
323 // wxSOCKET_WAITALL (with or without wxSOCKET_BLOCK)
327 if (m_flags
& wxSOCKET_NOWAIT
)
329 GSocket_SetNonBlocking(m_socket
, 1);
330 ret
= GSocket_Read(m_socket
, (char *)buffer
, nbytes
);
331 GSocket_SetNonBlocking(m_socket
, 0);
342 if ( !(m_flags
& wxSOCKET_BLOCK
) && !WaitForRead() )
345 ret
= GSocket_Read(m_socket
, (char *)buffer
, nbytes
);
351 buffer
= (char *)buffer
+ ret
;
354 // If we got here and wxSOCKET_WAITALL is not set, we can leave
355 // now. Otherwise, wait until we recv all the data or until there
358 more
= (ret
> 0 && nbytes
> 0 && (m_flags
& wxSOCKET_WAITALL
));
365 wxSocketBase
& wxSocketBase::ReadMsg(void* buffer
, wxUint32 nbytes
)
367 wxUint32 len
, len2
, sig
, total
;
372 unsigned char sig
[4];
373 unsigned char len
[4];
382 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
384 if (_Read(&msg
, sizeof(msg
)) != sizeof(msg
))
387 sig
= (wxUint32
)msg
.sig
[0];
388 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
389 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
390 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
392 if (sig
!= 0xfeeddead)
394 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
398 len
= (wxUint32
)msg
.len
[0];
399 len
|= (wxUint32
)(msg
.len
[1] << 8);
400 len
|= (wxUint32
)(msg
.len
[2] << 16);
401 len
|= (wxUint32
)(msg
.len
[3] << 24);
411 // Don't attemp to read if the msg was zero bytes long.
414 total
= _Read(buffer
, len
);
421 char *discard_buffer
= new char[MAX_DISCARD_SIZE
];
424 // NOTE: discarded bytes don't add to m_lcount.
427 discard_len
= ((len2
> MAX_DISCARD_SIZE
)? MAX_DISCARD_SIZE
: len2
);
428 discard_len
= _Read(discard_buffer
, (wxUint32
)discard_len
);
429 len2
-= (wxUint32
)discard_len
;
431 while ((discard_len
> 0) && len2
);
433 delete [] discard_buffer
;
438 if (_Read(&msg
, sizeof(msg
)) != sizeof(msg
))
441 sig
= (wxUint32
)msg
.sig
[0];
442 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
443 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
444 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
446 if (sig
!= 0xdeadfeed)
448 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
464 wxSocketBase
& wxSocketBase::Peek(void* buffer
, wxUint32 nbytes
)
469 m_lcount
= _Read(buffer
, nbytes
);
470 Pushback(buffer
, m_lcount
);
472 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
473 if (m_flags
& wxSOCKET_WAITALL
)
474 m_error
= (m_lcount
!= nbytes
);
476 m_error
= (m_lcount
== 0);
478 // Allow read events again
484 wxSocketBase
& wxSocketBase::Write(const void *buffer
, wxUint32 nbytes
)
489 m_lcount
= _Write(buffer
, nbytes
);
491 // If in wxSOCKET_WAITALL mode, all bytes should have been written.
492 if (m_flags
& wxSOCKET_WAITALL
)
493 m_error
= (m_lcount
!= nbytes
);
495 m_error
= (m_lcount
== 0);
497 // Allow write events again
503 wxUint32
wxSocketBase::_Write(const void *buffer
, wxUint32 nbytes
)
508 // If the socket is invalid or parameters are ill, return immediately
509 if (!m_socket
|| !buffer
|| !nbytes
)
512 // Possible combinations (they are checked in this order)
514 // wxSOCKET_WAITALL (with or without wxSOCKET_BLOCK)
518 if (m_flags
& wxSOCKET_NOWAIT
)
520 GSocket_SetNonBlocking(m_socket
, 1);
521 ret
= GSocket_Write(m_socket
, (const char *)buffer
, nbytes
);
522 GSocket_SetNonBlocking(m_socket
, 0);
533 if ( !(m_flags
& wxSOCKET_BLOCK
) && !WaitForWrite() )
536 ret
= GSocket_Write(m_socket
, (const char *)buffer
, nbytes
);
542 buffer
= (const char *)buffer
+ ret
;
545 // If we got here and wxSOCKET_WAITALL is not set, we can leave
546 // now. Otherwise, wait until we send all the data or until there
549 more
= (ret
> 0 && nbytes
> 0 && (m_flags
& wxSOCKET_WAITALL
));
556 wxSocketBase
& wxSocketBase::WriteMsg(const void *buffer
, wxUint32 nbytes
)
563 unsigned char sig
[4];
564 unsigned char len
[4];
573 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
575 msg
.sig
[0] = (unsigned char) 0xad;
576 msg
.sig
[1] = (unsigned char) 0xde;
577 msg
.sig
[2] = (unsigned char) 0xed;
578 msg
.sig
[3] = (unsigned char) 0xfe;
580 msg
.len
[0] = (unsigned char) (nbytes
& 0xff);
581 msg
.len
[1] = (unsigned char) ((nbytes
>> 8) & 0xff);
582 msg
.len
[2] = (unsigned char) ((nbytes
>> 16) & 0xff);
583 msg
.len
[3] = (unsigned char) ((nbytes
>> 24) & 0xff);
585 if (_Write(&msg
, sizeof(msg
)) < sizeof(msg
))
588 total
= _Write(buffer
, nbytes
);
593 msg
.sig
[0] = (unsigned char) 0xed;
594 msg
.sig
[1] = (unsigned char) 0xfe;
595 msg
.sig
[2] = (unsigned char) 0xad;
596 msg
.sig
[3] = (unsigned char) 0xde;
597 msg
.len
[0] = msg
.len
[1] = msg
.len
[2] = msg
.len
[3] = (char) 0;
599 if ((_Write(&msg
, sizeof(msg
))) < sizeof(msg
))
613 wxSocketBase
& wxSocketBase::Unread(const void *buffer
, wxUint32 nbytes
)
616 Pushback(buffer
, nbytes
);
624 wxSocketBase
& wxSocketBase::Discard()
627 char *buffer
= new char[MAX_DISCARD_SIZE
];
635 SetFlags(wxSOCKET_NOWAIT
);
639 ret
= _Read(buffer
, MAX_DISCARD_SIZE
);
642 while (ret
== MAX_DISCARD_SIZE
);
648 // Allow read events again
654 // --------------------------------------------------------------------------
656 // --------------------------------------------------------------------------
658 // All Wait functions poll the socket using GSocket_Select() to
659 // check for the specified combination of conditions, until one
660 // of these conditions become true, an error occurs, or the
661 // timeout elapses. The polling loop calls PROCESS_EVENTS(), so
662 // this won't block the GUI.
664 bool wxSocketBase::_Wait(long seconds
,
666 wxSocketEventFlags flags
)
668 GSocketEventFlags result
;
671 // Set this to TRUE to interrupt ongoing waits
674 // Check for valid socket
678 // Check for valid timeout value.
680 timeout
= seconds
* 1000 + milliseconds
;
682 timeout
= m_timeout
* 1000;
684 // Wait in an active polling loop.
686 // NOTE: We duplicate some of the code in OnRequest, but this doesn't
687 // hurt. It has to be here because the (GSocket) event might arrive
688 // a bit delayed, and it has to be in OnRequest as well because we
689 // don't know whether the Wait functions are being used.
691 // Do this at least once (important if timeout == 0, when
692 // we are just polling). Also, if just polling, do not yield.
699 result
= GSocket_Select(m_socket
, flags
| GSOCK_LOST_FLAG
);
701 // Incoming connection (server) or connection established (client)
702 if (result
& GSOCK_CONNECTION_FLAG
)
705 m_establishing
= FALSE
;
709 // Data available or output buffer ready
710 if ((result
& GSOCK_INPUT_FLAG
) || (result
& GSOCK_OUTPUT_FLAG
))
716 if (result
& GSOCK_LOST_FLAG
)
719 m_establishing
= FALSE
;
720 return (flags
& GSOCK_LOST_FLAG
) != 0;
724 if ((!timeout
) || (chrono
.Time() > timeout
) || (m_interrupt
))
733 bool wxSocketBase::Wait(long seconds
, long milliseconds
)
735 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
737 GSOCK_CONNECTION_FLAG
|
741 bool wxSocketBase::WaitForRead(long seconds
, long milliseconds
)
743 // Check pushback buffer before entering _Wait
747 // Note that GSOCK_INPUT_LOST has to be explicitly passed to
748 // _Wait becuase of the semantics of WaitForRead: a return
749 // value of TRUE means that a GSocket_Read call will return
750 // immediately, not that there is actually data to read.
752 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
756 bool wxSocketBase::WaitForWrite(long seconds
, long milliseconds
)
758 return _Wait(seconds
, milliseconds
, GSOCK_OUTPUT_FLAG
);
761 bool wxSocketBase::WaitForLost(long seconds
, long milliseconds
)
763 return _Wait(seconds
, milliseconds
, GSOCK_LOST_FLAG
);
766 // --------------------------------------------------------------------------
768 // --------------------------------------------------------------------------
771 // Get local or peer address
774 bool wxSocketBase::GetPeer(wxSockAddress
& addr_man
) const
781 peer
= GSocket_GetPeer(m_socket
);
783 // copying a null address would just trigger an assert anyway
788 addr_man
.SetAddress(peer
);
789 GAddress_destroy(peer
);
794 bool wxSocketBase::GetLocal(wxSockAddress
& addr_man
) const
801 local
= GSocket_GetLocal(m_socket
);
802 addr_man
.SetAddress(local
);
803 GAddress_destroy(local
);
809 // Save and restore socket state
812 void wxSocketBase::SaveState()
814 wxSocketState
*state
;
816 state
= new wxSocketState();
818 state
->m_flags
= m_flags
;
819 state
->m_notify
= m_notify
;
820 state
->m_eventmask
= m_eventmask
;
821 state
->m_clientData
= m_clientData
;
822 #if WXWIN_COMPATIBILITY
823 state
->m_cbk
= m_cbk
;
824 state
->m_cdata
= m_cdata
;
825 #endif // WXWIN_COMPATIBILITY
827 m_states
.Append(state
);
830 void wxSocketBase::RestoreState()
833 wxSocketState
*state
;
835 node
= m_states
.Last();
839 state
= (wxSocketState
*)node
->Data();
841 m_flags
= state
->m_flags
;
842 m_notify
= state
->m_notify
;
843 m_eventmask
= state
->m_eventmask
;
844 m_clientData
= state
->m_clientData
;
845 #if WXWIN_COMPATIBILITY
846 m_cbk
= state
->m_cbk
;
847 m_cdata
= state
->m_cdata
;
848 #endif // WXWIN_COMPATIBILITY
858 void wxSocketBase::SetTimeout(long seconds
)
863 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
866 void wxSocketBase::SetFlags(wxSocketFlags flags
)
872 // --------------------------------------------------------------------------
873 // Callbacks (now obsolete - use events instead)
874 // --------------------------------------------------------------------------
876 #if WXWIN_COMPATIBILITY
878 wxSocketBase::wxSockCbk
wxSocketBase::Callback(wxSockCbk cbk_
)
880 wxSockCbk old_cbk
= cbk_
;
886 char *wxSocketBase::CallbackData(char *data
)
888 char *old_data
= m_cdata
;
894 #endif // WXWIN_COMPATIBILITY
896 // --------------------------------------------------------------------------
898 // --------------------------------------------------------------------------
900 // A note on how events are processed, which is probably the most
901 // difficult thing to get working right while keeping the same API
902 // and functionality for all platforms.
904 // When GSocket detects an event, it calls wx_socket_callback, which in
905 // turn just calls wxSocketBase::OnRequest in the corresponding wxSocket
906 // object. OnRequest does some housekeeping, and if the event is to be
907 // propagated to the user, it creates a new wxSocketEvent object and
908 // posts it. The event is not processed immediately, but delayed with
909 // AddPendingEvent instead. This is necessary in order to decouple the
910 // event processing from wx_socket_callback; otherwise, subsequent IO
911 // calls made from the user event handler would fail, as gtk callbacks
912 // are not reentrant.
914 // Note that, unlike events, user callbacks (now deprecated) are _not_
915 // decoupled from wx_socket_callback and thus they suffer from a variety
916 // of problems. Avoid them where possible and use events instead.
919 void LINKAGEMODE
wx_socket_callback(GSocket
* WXUNUSED(socket
),
920 GSocketEvent notification
,
923 wxSocketBase
*sckobj
= (wxSocketBase
*)cdata
;
925 sckobj
->OnRequest((wxSocketNotify
) notification
);
928 void wxSocketBase::OnRequest(wxSocketNotify notification
)
930 // NOTE: We duplicate some of the code in _Wait, but this doesn't
931 // hurt. It has to be here because the (GSocket) event might arrive
932 // a bit delayed, and it has to be in _Wait as well because we don't
933 // know whether the Wait functions are being used.
937 case wxSOCKET_CONNECTION
:
938 m_establishing
= FALSE
;
942 // If we are in the middle of a R/W operation, do not
943 // propagate events to users. Also, filter 'late' events
944 // which are no longer valid.
947 if (m_reading
|| !GSocket_Select(m_socket
, GSOCK_INPUT_FLAG
))
951 case wxSOCKET_OUTPUT
:
952 if (m_writing
|| !GSocket_Select(m_socket
, GSOCK_OUTPUT_FLAG
))
958 m_establishing
= FALSE
;
965 // Schedule the event
967 wxSocketEventFlags flag
= 0;
968 switch (notification
)
970 case GSOCK_INPUT
: flag
= GSOCK_INPUT_FLAG
; break;
971 case GSOCK_OUTPUT
: flag
= GSOCK_OUTPUT_FLAG
; break;
972 case GSOCK_CONNECTION
: flag
= GSOCK_CONNECTION_FLAG
; break;
973 case GSOCK_LOST
: flag
= GSOCK_LOST_FLAG
; break;
975 wxLogWarning(_("wxSocket: unknown event!."));
979 if (((m_eventmask
& flag
) == flag
) && m_notify
)
983 wxSocketEvent
event(m_id
);
984 event
.m_event
= notification
;
985 event
.m_clientData
= m_clientData
;
986 event
.SetEventObject(this);
988 m_handler
->AddPendingEvent(event
);
991 #if WXWIN_COMPATIBILITY
993 m_cbk(*this, notification
, m_cdata
);
994 #endif // WXWIN_COMPATIBILITY
998 void wxSocketBase::Notify(bool notify
)
1003 void wxSocketBase::SetNotify(wxSocketEventFlags flags
)
1005 m_eventmask
= flags
;
1008 void wxSocketBase::SetEventHandler(wxEvtHandler
& handler
, int id
)
1010 m_handler
= &handler
;
1014 // --------------------------------------------------------------------------
1016 // --------------------------------------------------------------------------
1018 void wxSocketBase::Pushback(const void *buffer
, wxUint32 size
)
1022 if (m_unread
== NULL
)
1023 m_unread
= malloc(size
);
1028 tmp
= malloc(m_unrd_size
+ size
);
1029 memcpy((char *)tmp
+ size
, m_unread
, m_unrd_size
);
1035 m_unrd_size
+= size
;
1037 memcpy(m_unread
, buffer
, size
);
1040 wxUint32
wxSocketBase::GetPushback(void *buffer
, wxUint32 size
, bool peek
)
1045 if (size
> (m_unrd_size
-m_unrd_cur
))
1046 size
= m_unrd_size
-m_unrd_cur
;
1048 memcpy(buffer
, (char *)m_unread
+ m_unrd_cur
, size
);
1053 if (m_unrd_size
== m_unrd_cur
)
1066 // ==========================================================================
1068 // ==========================================================================
1070 // --------------------------------------------------------------------------
1072 // --------------------------------------------------------------------------
1074 wxSocketServer::wxSocketServer(wxSockAddress
& addr_man
,
1075 wxSocketFlags flags
)
1076 : wxSocketBase(flags
, wxSOCKET_SERVER
)
1078 wxLogTrace( wxTRACE_Socket
, _T("Opening wxSocketServer") );
1080 m_socket
= GSocket_new();
1084 wxLogTrace( wxTRACE_Socket
, _T("*** GSocket_new failed") );
1088 // Setup the socket as server
1090 GSocket_SetLocal(m_socket
, addr_man
.GetAddress());
1091 if (GSocket_SetServer(m_socket
) != GSOCK_NOERROR
)
1093 GSocket_destroy(m_socket
);
1096 wxLogTrace( wxTRACE_Socket
, _T("*** GSocket_SetServer failed") );
1100 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
1101 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1102 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1103 wx_socket_callback
, (char *)this);
1106 // --------------------------------------------------------------------------
1108 // --------------------------------------------------------------------------
1110 bool wxSocketServer::AcceptWith(wxSocketBase
& sock
, bool wait
)
1112 GSocket
*child_socket
;
1117 // If wait == FALSE, then the call should be nonblocking.
1118 // When we are finished, we put the socket to blocking mode
1122 GSocket_SetNonBlocking(m_socket
, 1);
1124 child_socket
= GSocket_WaitConnection(m_socket
);
1127 GSocket_SetNonBlocking(m_socket
, 0);
1132 sock
.m_type
= wxSOCKET_BASE
;
1133 sock
.m_socket
= child_socket
;
1134 sock
.m_connected
= TRUE
;
1136 GSocket_SetTimeout(sock
.m_socket
, sock
.m_timeout
* 1000);
1137 GSocket_SetCallback(sock
.m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1138 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1139 wx_socket_callback
, (char *)&sock
);
1144 wxSocketBase
*wxSocketServer::Accept(bool wait
)
1146 wxSocketBase
* sock
= new wxSocketBase();
1148 sock
->SetFlags(m_flags
);
1150 if (!AcceptWith(*sock
, wait
))
1159 bool wxSocketServer::WaitForAccept(long seconds
, long milliseconds
)
1161 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
);
1164 // ==========================================================================
1166 // ==========================================================================
1168 // --------------------------------------------------------------------------
1170 // --------------------------------------------------------------------------
1172 wxSocketClient::wxSocketClient(wxSocketFlags flags
)
1173 : wxSocketBase(flags
, wxSOCKET_CLIENT
)
1177 wxSocketClient::~wxSocketClient()
1181 // --------------------------------------------------------------------------
1183 // --------------------------------------------------------------------------
1185 bool wxSocketClient::Connect(wxSockAddress
& addr_man
, bool wait
)
1191 // Shutdown and destroy the socket
1193 GSocket_destroy(m_socket
);
1196 m_socket
= GSocket_new();
1197 m_connected
= FALSE
;
1198 m_establishing
= FALSE
;
1203 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
1204 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1205 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1206 wx_socket_callback
, (char *)this);
1208 // If wait == FALSE, then the call should be nonblocking.
1209 // When we are finished, we put the socket to blocking mode
1213 GSocket_SetNonBlocking(m_socket
, 1);
1215 GSocket_SetPeer(m_socket
, addr_man
.GetAddress());
1216 err
= GSocket_Connect(m_socket
, GSOCK_STREAMED
);
1219 GSocket_SetNonBlocking(m_socket
, 0);
1221 if (err
!= GSOCK_NOERROR
)
1223 if (err
== GSOCK_WOULDBLOCK
)
1224 m_establishing
= TRUE
;
1233 bool wxSocketClient::WaitOnConnect(long seconds
, long milliseconds
)
1235 if (m_connected
) // Already connected
1238 if (!m_establishing
|| !m_socket
) // No connection in progress
1241 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
|
1245 // ==========================================================================
1247 // ==========================================================================
1249 /* NOTE: experimental stuff - might change */
1251 wxDatagramSocket::wxDatagramSocket( wxSockAddress
& addr
,
1252 wxSocketFlags flags
)
1253 : wxSocketBase( flags
, wxSOCKET_DATAGRAM
)
1255 // Create the socket
1256 m_socket
= GSocket_new();
1261 // Setup the socket as non connection oriented
1262 GSocket_SetLocal(m_socket
, addr
.GetAddress());
1263 if( GSocket_SetNonOriented(m_socket
) != GSOCK_NOERROR
)
1265 GSocket_destroy(m_socket
);
1270 // Initialize all stuff
1271 m_connected
= FALSE
;
1272 m_establishing
= FALSE
;
1273 GSocket_SetTimeout( m_socket
, m_timeout
);
1274 GSocket_SetCallback( m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1275 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1276 wx_socket_callback
, (char*)this );
1280 wxDatagramSocket
& wxDatagramSocket::RecvFrom( wxSockAddress
& addr
,
1289 wxDatagramSocket
& wxDatagramSocket::SendTo( wxSockAddress
& addr
,
1293 GSocket_SetPeer(m_socket
, addr
.GetAddress());
1298 // ==========================================================================
1300 // ==========================================================================
1302 class WXDLLEXPORT wxSocketModule
: public wxModule
1305 virtual bool OnInit()
1307 // wxSocketBase will call GSocket_Init() itself when/if needed
1311 virtual void OnExit()
1313 if ( wxSocketBase::IsInitialized() )
1314 wxSocketBase::Shutdown();
1318 DECLARE_DYNAMIC_CLASS(wxSocketModule
)
1321 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule
, wxModule
)