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
56 #define PROCESS_EVENTS() wxYield()
58 #define PROCESS_EVENTS()
61 // --------------------------------------------------------------------------
63 // --------------------------------------------------------------------------
65 IMPLEMENT_CLASS(wxSocketBase
, wxObject
)
66 IMPLEMENT_CLASS(wxSocketServer
, wxSocketBase
)
67 IMPLEMENT_CLASS(wxSocketClient
, wxSocketBase
)
68 IMPLEMENT_CLASS(wxDatagramSocket
, wxSocketBase
)
69 IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent
, wxEvent
)
71 // --------------------------------------------------------------------------
73 // --------------------------------------------------------------------------
75 class wxSocketState
: public wxObject
78 wxSocketFlags m_flags
;
79 wxSocketEventFlags m_eventmask
;
82 #if WXWIN_COMPATIBILITY
83 wxSocketBase::wxSockCbk m_cbk
;
85 #endif // WXWIN_COMPATIBILITY
88 wxSocketState() : wxObject() {}
91 // ==========================================================================
93 // ==========================================================================
95 // --------------------------------------------------------------------------
97 // --------------------------------------------------------------------------
99 void wxSocketBase::Init()
102 m_type
= wxSOCKET_UNINIT
;
113 m_beingDeleted
= FALSE
;
126 #if WXWIN_COMPATIBILITY
129 #endif // WXWIN_COMPATIBILITY
132 wxSocketBase::wxSocketBase()
137 wxSocketBase::wxSocketBase(wxSocketFlags flags
, wxSocketType type
)
145 wxSocketBase::~wxSocketBase()
147 // Just in case the app called Destroy() *and* then deleted
148 // the socket immediately: don't leave dangling pointers.
150 wxPendingDelete
.DeleteObject(this);
153 // Shutdown and close the socket
157 // Destroy the GSocket object
159 GSocket_destroy(m_socket
);
161 // Free the pushback buffer
166 bool wxSocketBase::Destroy()
168 // Delayed destruction: the socket will be deleted during the next
169 // idle loop iteration. This ensures that all pending events have
171 m_beingDeleted
= TRUE
;
173 // Shutdown and close the socket
176 // Supress events from now on
180 if ( !wxPendingDelete
.Member(this) )
181 wxPendingDelete
.Append(this);
189 // --------------------------------------------------------------------------
191 // --------------------------------------------------------------------------
193 // The following IO operations update m_error and m_lcount:
194 // {Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard}
196 // TODO: Should Connect, Accept and AcceptWith update m_error?
198 bool wxSocketBase::Close()
200 // Interrupt pending waits
206 GSocket_UnsetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
207 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
);
209 // Shutdown the connection
210 GSocket_Shutdown(m_socket
);
214 m_establishing
= FALSE
;
218 wxSocketBase
& wxSocketBase::Read(void* buffer
, wxUint32 nbytes
)
223 m_lcount
= _Read(buffer
, nbytes
);
225 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
226 if (m_flags
& wxSOCKET_WAITALL
)
227 m_error
= (m_lcount
!= nbytes
);
229 m_error
= (m_lcount
== 0);
231 // Allow read events from now on
237 wxUint32
wxSocketBase::_Read(void* buffer
, wxUint32 nbytes
)
242 // Try the pushback buffer first
243 total
= GetPushback(buffer
, nbytes
, FALSE
);
245 buffer
= (char *)buffer
+ total
;
247 // If the socket is invalid or we got all the data, return now
248 if (!m_socket
|| !nbytes
)
251 // Possible combinations (they are checked in this order)
253 // wxSOCKET_WAITALL | wxSOCKET_BLOCK
258 if (m_flags
& wxSOCKET_NOWAIT
)
260 GSocket_SetNonBlocking(m_socket
, TRUE
);
261 ret
= GSocket_Read(m_socket
, (char *)buffer
, nbytes
);
262 GSocket_SetNonBlocking(m_socket
, FALSE
);
267 else if (m_flags
& wxSOCKET_WAITALL
)
269 while (ret
> 0 && nbytes
> 0)
271 if (!(m_flags
& wxSOCKET_BLOCK
) && !WaitForRead())
274 ret
= GSocket_Read(m_socket
, (char *)buffer
, nbytes
);
280 buffer
= (char *)buffer
+ ret
;
286 if ((m_flags
& wxSOCKET_BLOCK
) || WaitForRead())
288 ret
= GSocket_Read(m_socket
, (char *)buffer
, nbytes
);
298 wxSocketBase
& wxSocketBase::ReadMsg(void* buffer
, wxUint32 nbytes
)
300 wxUint32 len
, len2
, sig
, total
;
305 unsigned char sig
[4];
306 unsigned char len
[4];
315 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
317 if (_Read(&msg
, sizeof(msg
)) != sizeof(msg
))
320 sig
= (wxUint32
)msg
.sig
[0];
321 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
322 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
323 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
325 if (sig
!= 0xfeeddead)
327 wxLogWarning( _("wxSocket: invalid signature in ReadMsg."));
331 len
= (wxUint32
)msg
.len
[0];
332 len
|= (wxUint32
)(msg
.len
[1] << 8);
333 len
|= (wxUint32
)(msg
.len
[2] << 16);
334 len
|= (wxUint32
)(msg
.len
[3] << 24);
344 // Don't attemp to read if the msg was zero bytes long.
347 total
= _Read(buffer
, len
);
354 char *discard_buffer
= new char[MAX_DISCARD_SIZE
];
357 // NOTE: discarded bytes don't add to m_lcount.
360 discard_len
= ((len2
> MAX_DISCARD_SIZE
)? MAX_DISCARD_SIZE
: len2
);
361 discard_len
= _Read(discard_buffer
, (wxUint32
)discard_len
);
362 len2
-= (wxUint32
)discard_len
;
364 while ((discard_len
> 0) && len2
);
366 delete [] discard_buffer
;
371 if (_Read(&msg
, sizeof(msg
)) != sizeof(msg
))
374 sig
= (wxUint32
)msg
.sig
[0];
375 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
376 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
377 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
379 if (sig
!= 0xdeadfeed)
381 wxLogWarning( _("wxSocket: invalid signature in ReadMsg."));
397 wxSocketBase
& wxSocketBase::Peek(void* buffer
, wxUint32 nbytes
)
402 m_lcount
= _Read(buffer
, nbytes
);
403 Pushback(buffer
, m_lcount
);
405 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
406 if (m_flags
& wxSOCKET_WAITALL
)
407 m_error
= (m_lcount
!= nbytes
);
409 m_error
= (m_lcount
== 0);
411 // Allow read events again
417 wxSocketBase
& wxSocketBase::Write(const void *buffer
, wxUint32 nbytes
)
422 m_lcount
= _Write(buffer
, nbytes
);
424 // If in wxSOCKET_WAITALL mode, all bytes should have been written.
425 if (m_flags
& wxSOCKET_WAITALL
)
426 m_error
= (m_lcount
!= nbytes
);
428 m_error
= (m_lcount
== 0);
430 // Allow write events again
436 wxUint32
wxSocketBase::_Write(const void *buffer
, wxUint32 nbytes
)
441 // If the socket is invalid, return immediately
445 // Possible combinations (they are checked in this order)
447 // wxSOCKET_WAITALL | wxSOCKET_BLOCK
452 if (m_flags
& wxSOCKET_NOWAIT
)
454 GSocket_SetNonBlocking(m_socket
, TRUE
);
455 ret
= GSocket_Write(m_socket
, (const char *)buffer
, nbytes
);
456 GSocket_SetNonBlocking(m_socket
, FALSE
);
461 else if (m_flags
& wxSOCKET_WAITALL
)
463 while (ret
> 0 && nbytes
> 0)
465 if (!(m_flags
& wxSOCKET_BLOCK
) && !WaitForWrite())
468 ret
= GSocket_Write(m_socket
, (const char *)buffer
, nbytes
);
474 buffer
= (const char *)buffer
+ ret
;
480 if ((m_flags
& wxSOCKET_BLOCK
) || WaitForWrite())
482 ret
= GSocket_Write(m_socket
, (const char *)buffer
, nbytes
);
492 wxSocketBase
& wxSocketBase::WriteMsg(const void *buffer
, wxUint32 nbytes
)
499 unsigned char sig
[4];
500 unsigned char len
[4];
509 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
511 msg
.sig
[0] = (unsigned char) 0xad;
512 msg
.sig
[1] = (unsigned char) 0xde;
513 msg
.sig
[2] = (unsigned char) 0xed;
514 msg
.sig
[3] = (unsigned char) 0xfe;
516 msg
.len
[0] = (unsigned char) (nbytes
& 0xff);
517 msg
.len
[1] = (unsigned char) ((nbytes
>> 8) & 0xff);
518 msg
.len
[2] = (unsigned char) ((nbytes
>> 16) & 0xff);
519 msg
.len
[3] = (unsigned char) ((nbytes
>> 24) & 0xff);
521 if (_Write(&msg
, sizeof(msg
)) < sizeof(msg
))
524 total
= _Write(buffer
, nbytes
);
529 msg
.sig
[0] = (unsigned char) 0xed;
530 msg
.sig
[1] = (unsigned char) 0xfe;
531 msg
.sig
[2] = (unsigned char) 0xad;
532 msg
.sig
[3] = (unsigned char) 0xde;
533 msg
.len
[0] = msg
.len
[1] = msg
.len
[2] = msg
.len
[3] = (char) 0;
535 if ((_Write(&msg
, sizeof(msg
))) < sizeof(msg
))
549 wxSocketBase
& wxSocketBase::Unread(const void *buffer
, wxUint32 nbytes
)
552 Pushback(buffer
, nbytes
);
560 wxSocketBase
& wxSocketBase::Discard()
563 char *buffer
= new char[MAX_DISCARD_SIZE
];
571 SetFlags(wxSOCKET_NOWAIT
);
575 ret
= _Read(buffer
, MAX_DISCARD_SIZE
);
578 while (ret
== MAX_DISCARD_SIZE
);
584 // Allow read events again
590 // --------------------------------------------------------------------------
592 // --------------------------------------------------------------------------
594 // All Wait functions poll the socket using GSocket_Select() to
595 // check for the specified combination of conditions, until one
596 // of these conditions become true, an error occurs, or the
597 // timeout elapses. The polling loop calls PROCESS_EVENTS(), so
598 // this won't block the GUI.
600 bool wxSocketBase::_Wait(long seconds
,
602 wxSocketEventFlags flags
)
604 GSocketEventFlags result
;
607 // Set this to TRUE to interrupt ongoing waits
610 // Check for valid socket
614 // Check for valid timeout value.
616 timeout
= seconds
* 1000 + milliseconds
;
618 timeout
= m_timeout
* 1000;
620 // Wait in an active polling loop.
622 // NOTE: We duplicate some of the code in OnRequest, but this doesn't
623 // hurt. It has to be here because the (GSocket) event might arrive
624 // a bit delayed, and it has to be in OnRequest as well because we
625 // don't know whether the Wait functions are being used.
627 // Do this at least once (important if timeout == 0, when
628 // we are just polling). Also, if just polling, do not yield.
635 result
= GSocket_Select(m_socket
, flags
| GSOCK_LOST_FLAG
);
637 // Incoming connection (server) or connection established (client)
638 if (result
& GSOCK_CONNECTION_FLAG
)
641 m_establishing
= FALSE
;
645 // Data available or output buffer ready
646 if ((result
& GSOCK_INPUT_FLAG
) || (result
& GSOCK_OUTPUT_FLAG
))
652 if (result
& GSOCK_LOST_FLAG
)
655 m_establishing
= FALSE
;
656 return (flags
& GSOCK_LOST_FLAG
);
660 if ((!timeout
) || (chrono
.Time() > timeout
) || (m_interrupt
))
669 bool wxSocketBase::Wait(long seconds
, long milliseconds
)
671 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
673 GSOCK_CONNECTION_FLAG
|
677 bool wxSocketBase::WaitForRead(long seconds
, long milliseconds
)
679 // Check pushback buffer before entering _Wait
683 // Note that GSOCK_INPUT_LOST has to be explicitly passed to
684 // _Wait becuase of the semantics of WaitForRead: a return
685 // value of TRUE means that a GSocket_Read call will return
686 // immediately, not that there is actually data to read.
688 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
692 bool wxSocketBase::WaitForWrite(long seconds
, long milliseconds
)
694 return _Wait(seconds
, milliseconds
, GSOCK_OUTPUT_FLAG
);
697 bool wxSocketBase::WaitForLost(long seconds
, long milliseconds
)
699 return _Wait(seconds
, milliseconds
, GSOCK_LOST_FLAG
);
702 // --------------------------------------------------------------------------
704 // --------------------------------------------------------------------------
707 // Get local or peer address
710 bool wxSocketBase::GetPeer(wxSockAddress
& addr_man
) const
717 peer
= GSocket_GetPeer(m_socket
);
718 addr_man
.SetAddress(peer
);
719 GAddress_destroy(peer
);
724 bool wxSocketBase::GetLocal(wxSockAddress
& addr_man
) const
731 local
= GSocket_GetLocal(m_socket
);
732 addr_man
.SetAddress(local
);
733 GAddress_destroy(local
);
739 // Save and restore socket state
742 void wxSocketBase::SaveState()
744 wxSocketState
*state
;
746 state
= new wxSocketState();
748 state
->m_flags
= m_flags
;
749 state
->m_notify
= m_notify
;
750 state
->m_eventmask
= m_eventmask
;
751 state
->m_clientData
= m_clientData
;
752 #if WXWIN_COMPATIBILITY
753 state
->m_cbk
= m_cbk
;
754 state
->m_cdata
= m_cdata
;
755 #endif // WXWIN_COMPATIBILITY
757 m_states
.Append(state
);
760 void wxSocketBase::RestoreState()
763 wxSocketState
*state
;
765 node
= m_states
.Last();
769 state
= (wxSocketState
*)node
->Data();
771 m_flags
= state
->m_flags
;
772 m_notify
= state
->m_notify
;
773 m_eventmask
= state
->m_eventmask
;
774 m_clientData
= state
->m_clientData
;
775 #if WXWIN_COMPATIBILITY
776 m_cbk
= state
->m_cbk
;
777 m_cdata
= state
->m_cdata
;
778 #endif // WXWIN_COMPATIBILITY
788 void wxSocketBase::SetTimeout(long seconds
)
793 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
796 void wxSocketBase::SetFlags(wxSocketFlags flags
)
802 // --------------------------------------------------------------------------
803 // Callbacks (now obsolete - use events instead)
804 // --------------------------------------------------------------------------
806 #if WXWIN_COMPATIBILITY
808 wxSocketBase::wxSockCbk
wxSocketBase::Callback(wxSockCbk cbk_
)
810 wxSockCbk old_cbk
= cbk_
;
816 char *wxSocketBase::CallbackData(char *data
)
818 char *old_data
= m_cdata
;
824 #endif // WXWIN_COMPATIBILITY
826 // --------------------------------------------------------------------------
828 // --------------------------------------------------------------------------
830 // A note on how events are processed, which is probably the most
831 // difficult thing to get working right while keeping the same API
832 // and functionality for all platforms.
834 // When GSocket detects an event, it calls wx_socket_callback, which in
835 // turn just calls wxSocketBase::OnRequest in the corresponding wxSocket
836 // object. OnRequest does some housekeeping, and if the event is to be
837 // propagated to the user, it creates a new wxSocketEvent object and
838 // posts it. The event is not processed immediately, but delayed with
839 // AddPendingEvent instead. This is necessary in order to decouple the
840 // event processing from wx_socket_callback; otherwise, subsequent IO
841 // calls made from the user event handler would fail, as gtk callbacks
842 // are not reentrant.
844 // Note that, unlike events, user callbacks (now deprecated) are _not_
845 // decoupled from wx_socket_callback and thus they suffer from a variety
846 // of problems. Avoid them where possible and use events instead.
848 static void LINKAGEMODE
wx_socket_callback(GSocket
* WXUNUSED(socket
),
849 GSocketEvent notification
,
852 wxSocketBase
*sckobj
= (wxSocketBase
*)cdata
;
854 sckobj
->OnRequest((wxSocketNotify
) notification
);
857 void wxSocketBase::OnRequest(wxSocketNotify notification
)
859 // NOTE: We duplicate some of the code in _Wait, but this doesn't
860 // hurt. It has to be here because the (GSocket) event might arrive
861 // a bit delayed, and it has to be in _Wait as well because we don't
862 // know whether the Wait functions are being used.
866 case wxSOCKET_CONNECTION
:
867 m_establishing
= FALSE
;
871 // If we are in the middle of a R/W operation, do not
872 // propagate events to users. Also, filter 'late' events
873 // which are no longer valid.
876 if (m_reading
|| !GSocket_Select(m_socket
, GSOCK_INPUT_FLAG
))
880 case wxSOCKET_OUTPUT
:
881 if (m_writing
|| !GSocket_Select(m_socket
, GSOCK_OUTPUT_FLAG
))
887 m_establishing
= FALSE
;
894 // Schedule the event
896 wxSocketEventFlags flag
= -1;
897 switch (notification
)
899 case GSOCK_INPUT
: flag
= GSOCK_INPUT_FLAG
; break;
900 case GSOCK_OUTPUT
: flag
= GSOCK_OUTPUT_FLAG
; break;
901 case GSOCK_CONNECTION
: flag
= GSOCK_CONNECTION_FLAG
; break;
902 case GSOCK_LOST
: flag
= GSOCK_LOST_FLAG
; break;
905 if (((m_eventmask
& flag
) == flag
) && m_notify
)
909 wxSocketEvent
event(m_id
);
910 event
.m_event
= notification
;
911 event
.m_clientData
= m_clientData
;
912 event
.SetEventObject(this);
914 m_handler
->AddPendingEvent(event
);
917 #if WXWIN_COMPATIBILITY
919 m_cbk(*this, notification
, m_cdata
);
920 #endif // WXWIN_COMPATIBILITY
924 void wxSocketBase::Notify(bool notify
)
929 void wxSocketBase::SetNotify(wxSocketEventFlags flags
)
934 void wxSocketBase::SetEventHandler(wxEvtHandler
& handler
, int id
)
936 m_handler
= &handler
;
940 // --------------------------------------------------------------------------
942 // --------------------------------------------------------------------------
944 void wxSocketBase::Pushback(const void *buffer
, wxUint32 size
)
948 if (m_unread
== NULL
)
949 m_unread
= malloc(size
);
954 tmp
= malloc(m_unrd_size
+ size
);
955 memcpy((char *)tmp
+ size
, m_unread
, m_unrd_size
);
963 memcpy(m_unread
, buffer
, size
);
966 wxUint32
wxSocketBase::GetPushback(void *buffer
, wxUint32 size
, bool peek
)
971 if (size
> (m_unrd_size
-m_unrd_cur
))
972 size
= m_unrd_size
-m_unrd_cur
;
974 memcpy(buffer
, (char *)m_unread
+ m_unrd_cur
, size
);
979 if (m_unrd_size
== m_unrd_cur
)
992 // ==========================================================================
994 // ==========================================================================
996 // --------------------------------------------------------------------------
998 // --------------------------------------------------------------------------
1000 wxSocketServer::wxSocketServer(wxSockAddress
& addr_man
,
1001 wxSocketFlags flags
)
1002 : wxSocketBase(flags
, wxSOCKET_SERVER
)
1004 // Create the socket
1005 m_socket
= GSocket_new();
1010 // Setup the socket as server
1011 GSocket_SetLocal(m_socket
, addr_man
.GetAddress());
1012 if (GSocket_SetServer(m_socket
) != GSOCK_NOERROR
)
1014 GSocket_destroy(m_socket
);
1019 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
1020 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1021 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1022 wx_socket_callback
, (char *)this);
1026 // --------------------------------------------------------------------------
1028 // --------------------------------------------------------------------------
1030 bool wxSocketServer::AcceptWith(wxSocketBase
& sock
, bool wait
)
1032 GSocket
*child_socket
;
1037 // If wait == FALSE, then the call should be nonblocking.
1038 // When we are finished, we put the socket to blocking mode
1042 GSocket_SetNonBlocking(m_socket
, TRUE
);
1044 child_socket
= GSocket_WaitConnection(m_socket
);
1047 GSocket_SetNonBlocking(m_socket
, FALSE
);
1052 sock
.m_type
= wxSOCKET_BASE
;
1053 sock
.m_socket
= child_socket
;
1054 sock
.m_connected
= TRUE
;
1056 GSocket_SetTimeout(sock
.m_socket
, sock
.m_timeout
* 1000);
1057 GSocket_SetCallback(sock
.m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1058 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1059 wx_socket_callback
, (char *)&sock
);
1064 wxSocketBase
*wxSocketServer::Accept(bool wait
)
1066 wxSocketBase
* sock
= new wxSocketBase();
1068 sock
->SetFlags(m_flags
);
1070 if (!AcceptWith(*sock
, wait
))
1076 bool wxSocketServer::WaitForAccept(long seconds
, long milliseconds
)
1078 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
);
1081 // ==========================================================================
1083 // ==========================================================================
1085 // --------------------------------------------------------------------------
1087 // --------------------------------------------------------------------------
1089 wxSocketClient::wxSocketClient(wxSocketFlags flags
)
1090 : wxSocketBase(flags
, wxSOCKET_CLIENT
)
1094 wxSocketClient::~wxSocketClient()
1098 // --------------------------------------------------------------------------
1100 // --------------------------------------------------------------------------
1102 bool wxSocketClient::Connect(wxSockAddress
& addr_man
, bool wait
)
1108 // Shutdown and destroy the socket
1110 GSocket_destroy(m_socket
);
1113 m_socket
= GSocket_new();
1114 m_connected
= FALSE
;
1115 m_establishing
= FALSE
;
1120 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
1121 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1122 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1123 wx_socket_callback
, (char *)this);
1125 // If wait == FALSE, then the call should be nonblocking.
1126 // When we are finished, we put the socket to blocking mode
1130 GSocket_SetNonBlocking(m_socket
, TRUE
);
1132 GSocket_SetPeer(m_socket
, addr_man
.GetAddress());
1133 err
= GSocket_Connect(m_socket
, GSOCK_STREAMED
);
1136 GSocket_SetNonBlocking(m_socket
, FALSE
);
1138 if (err
!= GSOCK_NOERROR
)
1140 if (err
== GSOCK_WOULDBLOCK
)
1141 m_establishing
= TRUE
;
1150 bool wxSocketClient::WaitOnConnect(long seconds
, long milliseconds
)
1152 if (m_connected
) // Already connected
1155 if (!m_establishing
|| !m_socket
) // No connection in progress
1158 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
|
1162 // ==========================================================================
1164 // ==========================================================================
1166 /* NOTE: experimental stuff - might change */
1168 wxDatagramSocket::wxDatagramSocket( wxSockAddress
& addr
,
1169 wxSocketFlags flags
)
1170 : wxSocketBase( flags
, wxSOCKET_DATAGRAM
)
1172 // Create the socket
1173 m_socket
= GSocket_new();
1178 // Setup the socket as non connection oriented
1179 GSocket_SetLocal(m_socket
, addr
.GetAddress());
1180 if( GSocket_SetNonOriented(m_socket
) != GSOCK_NOERROR
)
1182 GSocket_destroy(m_socket
);
1187 // Initialize all stuff
1188 m_connected
= FALSE
;
1189 m_establishing
= FALSE
;
1190 GSocket_SetTimeout( m_socket
, m_timeout
);
1191 GSocket_SetCallback( m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1192 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1193 wx_socket_callback
, (char*)this );
1197 wxDatagramSocket
& wxDatagramSocket::RecvFrom( wxSockAddress
& addr
,
1206 wxDatagramSocket
& wxDatagramSocket::SendTo( wxSockAddress
& addr
,
1210 GSocket_SetPeer(m_socket
, addr
.GetAddress());
1215 // ==========================================================================
1217 // ==========================================================================
1219 wxSocketEvent::wxSocketEvent(int id
) : wxEvent(id
)
1221 SetEventType( (wxEventType
)wxEVT_SOCKET
);
1224 void wxSocketEvent::CopyObject(wxObject
& object_dest
) const
1226 wxSocketEvent
*event
= (wxSocketEvent
*)&object_dest
;
1228 wxEvent::CopyObject(object_dest
);
1230 event
->m_event
= m_event
;
1231 event
->m_clientData
= m_clientData
;
1234 // ==========================================================================
1236 // ==========================================================================
1238 class WXDLLEXPORT wxSocketModule
: public wxModule
1240 DECLARE_DYNAMIC_CLASS(wxSocketModule
)
1243 bool OnInit() { return GSocket_Init(); }
1244 void OnExit() { GSocket_Cleanup(); }
1247 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule
, wxModule
)