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 // Return now in one of the following cases:
248 // - the socket is invalid,
249 // - we got all the data,
250 // - we got *some* data and we are not using wxSOCKET_WAITALL.
253 ((total
!= 0) && !(m_flags
& wxSOCKET_WAITALL
)) )
256 // Possible combinations (they are checked in this order)
258 // wxSOCKET_WAITALL | wxSOCKET_BLOCK
263 if (m_flags
& wxSOCKET_NOWAIT
)
265 GSocket_SetNonBlocking(m_socket
, TRUE
);
266 ret
= GSocket_Read(m_socket
, (char *)buffer
, nbytes
);
267 GSocket_SetNonBlocking(m_socket
, FALSE
);
272 else if (m_flags
& wxSOCKET_WAITALL
)
274 while (ret
> 0 && nbytes
> 0)
276 if (!(m_flags
& wxSOCKET_BLOCK
) && !WaitForRead())
279 ret
= GSocket_Read(m_socket
, (char *)buffer
, nbytes
);
285 buffer
= (char *)buffer
+ ret
;
291 if ((m_flags
& wxSOCKET_BLOCK
) || WaitForRead())
293 ret
= GSocket_Read(m_socket
, (char *)buffer
, nbytes
);
303 wxSocketBase
& wxSocketBase::ReadMsg(void* buffer
, wxUint32 nbytes
)
305 wxUint32 len
, len2
, sig
, total
;
310 unsigned char sig
[4];
311 unsigned char len
[4];
320 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
322 if (_Read(&msg
, sizeof(msg
)) != sizeof(msg
))
325 sig
= (wxUint32
)msg
.sig
[0];
326 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
327 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
328 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
330 if (sig
!= 0xfeeddead)
332 wxLogWarning( _("wxSocket: invalid signature in ReadMsg."));
336 len
= (wxUint32
)msg
.len
[0];
337 len
|= (wxUint32
)(msg
.len
[1] << 8);
338 len
|= (wxUint32
)(msg
.len
[2] << 16);
339 len
|= (wxUint32
)(msg
.len
[3] << 24);
349 // Don't attemp to read if the msg was zero bytes long.
352 total
= _Read(buffer
, len
);
359 char *discard_buffer
= new char[MAX_DISCARD_SIZE
];
362 // NOTE: discarded bytes don't add to m_lcount.
365 discard_len
= ((len2
> MAX_DISCARD_SIZE
)? MAX_DISCARD_SIZE
: len2
);
366 discard_len
= _Read(discard_buffer
, (wxUint32
)discard_len
);
367 len2
-= (wxUint32
)discard_len
;
369 while ((discard_len
> 0) && len2
);
371 delete [] discard_buffer
;
376 if (_Read(&msg
, sizeof(msg
)) != sizeof(msg
))
379 sig
= (wxUint32
)msg
.sig
[0];
380 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
381 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
382 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
384 if (sig
!= 0xdeadfeed)
386 wxLogWarning( _("wxSocket: invalid signature in ReadMsg."));
402 wxSocketBase
& wxSocketBase::Peek(void* buffer
, wxUint32 nbytes
)
407 m_lcount
= _Read(buffer
, nbytes
);
408 Pushback(buffer
, m_lcount
);
410 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
411 if (m_flags
& wxSOCKET_WAITALL
)
412 m_error
= (m_lcount
!= nbytes
);
414 m_error
= (m_lcount
== 0);
416 // Allow read events again
422 wxSocketBase
& wxSocketBase::Write(const void *buffer
, wxUint32 nbytes
)
427 m_lcount
= _Write(buffer
, nbytes
);
429 // If in wxSOCKET_WAITALL mode, all bytes should have been written.
430 if (m_flags
& wxSOCKET_WAITALL
)
431 m_error
= (m_lcount
!= nbytes
);
433 m_error
= (m_lcount
== 0);
435 // Allow write events again
441 wxUint32
wxSocketBase::_Write(const void *buffer
, wxUint32 nbytes
)
446 // If the socket is invalid, return immediately
450 // Possible combinations (they are checked in this order)
452 // wxSOCKET_WAITALL | wxSOCKET_BLOCK
457 if (m_flags
& wxSOCKET_NOWAIT
)
459 GSocket_SetNonBlocking(m_socket
, TRUE
);
460 ret
= GSocket_Write(m_socket
, (const char *)buffer
, nbytes
);
461 GSocket_SetNonBlocking(m_socket
, FALSE
);
466 else if (m_flags
& wxSOCKET_WAITALL
)
468 while (ret
> 0 && nbytes
> 0)
470 if (!(m_flags
& wxSOCKET_BLOCK
) && !WaitForWrite())
473 ret
= GSocket_Write(m_socket
, (const char *)buffer
, nbytes
);
479 buffer
= (const char *)buffer
+ ret
;
485 if ((m_flags
& wxSOCKET_BLOCK
) || WaitForWrite())
487 ret
= GSocket_Write(m_socket
, (const char *)buffer
, nbytes
);
497 wxSocketBase
& wxSocketBase::WriteMsg(const void *buffer
, wxUint32 nbytes
)
504 unsigned char sig
[4];
505 unsigned char len
[4];
514 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
516 msg
.sig
[0] = (unsigned char) 0xad;
517 msg
.sig
[1] = (unsigned char) 0xde;
518 msg
.sig
[2] = (unsigned char) 0xed;
519 msg
.sig
[3] = (unsigned char) 0xfe;
521 msg
.len
[0] = (unsigned char) (nbytes
& 0xff);
522 msg
.len
[1] = (unsigned char) ((nbytes
>> 8) & 0xff);
523 msg
.len
[2] = (unsigned char) ((nbytes
>> 16) & 0xff);
524 msg
.len
[3] = (unsigned char) ((nbytes
>> 24) & 0xff);
526 if (_Write(&msg
, sizeof(msg
)) < sizeof(msg
))
529 total
= _Write(buffer
, nbytes
);
534 msg
.sig
[0] = (unsigned char) 0xed;
535 msg
.sig
[1] = (unsigned char) 0xfe;
536 msg
.sig
[2] = (unsigned char) 0xad;
537 msg
.sig
[3] = (unsigned char) 0xde;
538 msg
.len
[0] = msg
.len
[1] = msg
.len
[2] = msg
.len
[3] = (char) 0;
540 if ((_Write(&msg
, sizeof(msg
))) < sizeof(msg
))
554 wxSocketBase
& wxSocketBase::Unread(const void *buffer
, wxUint32 nbytes
)
557 Pushback(buffer
, nbytes
);
565 wxSocketBase
& wxSocketBase::Discard()
568 char *buffer
= new char[MAX_DISCARD_SIZE
];
576 SetFlags(wxSOCKET_NOWAIT
);
580 ret
= _Read(buffer
, MAX_DISCARD_SIZE
);
583 while (ret
== MAX_DISCARD_SIZE
);
589 // Allow read events again
595 // --------------------------------------------------------------------------
597 // --------------------------------------------------------------------------
599 // All Wait functions poll the socket using GSocket_Select() to
600 // check for the specified combination of conditions, until one
601 // of these conditions become true, an error occurs, or the
602 // timeout elapses. The polling loop calls PROCESS_EVENTS(), so
603 // this won't block the GUI.
605 bool wxSocketBase::_Wait(long seconds
,
607 wxSocketEventFlags flags
)
609 GSocketEventFlags result
;
612 // Set this to TRUE to interrupt ongoing waits
615 // Check for valid socket
619 // Check for valid timeout value.
621 timeout
= seconds
* 1000 + milliseconds
;
623 timeout
= m_timeout
* 1000;
625 // Wait in an active polling loop.
627 // NOTE: We duplicate some of the code in OnRequest, but this doesn't
628 // hurt. It has to be here because the (GSocket) event might arrive
629 // a bit delayed, and it has to be in OnRequest as well because we
630 // don't know whether the Wait functions are being used.
632 // Do this at least once (important if timeout == 0, when
633 // we are just polling). Also, if just polling, do not yield.
640 result
= GSocket_Select(m_socket
, flags
| GSOCK_LOST_FLAG
);
642 // Incoming connection (server) or connection established (client)
643 if (result
& GSOCK_CONNECTION_FLAG
)
646 m_establishing
= FALSE
;
650 // Data available or output buffer ready
651 if ((result
& GSOCK_INPUT_FLAG
) || (result
& GSOCK_OUTPUT_FLAG
))
657 if (result
& GSOCK_LOST_FLAG
)
660 m_establishing
= FALSE
;
661 return (flags
& GSOCK_LOST_FLAG
) != 0;
665 if ((!timeout
) || (chrono
.Time() > timeout
) || (m_interrupt
))
674 bool wxSocketBase::Wait(long seconds
, long milliseconds
)
676 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
678 GSOCK_CONNECTION_FLAG
|
682 bool wxSocketBase::WaitForRead(long seconds
, long milliseconds
)
684 // Check pushback buffer before entering _Wait
688 // Note that GSOCK_INPUT_LOST has to be explicitly passed to
689 // _Wait becuase of the semantics of WaitForRead: a return
690 // value of TRUE means that a GSocket_Read call will return
691 // immediately, not that there is actually data to read.
693 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
697 bool wxSocketBase::WaitForWrite(long seconds
, long milliseconds
)
699 return _Wait(seconds
, milliseconds
, GSOCK_OUTPUT_FLAG
);
702 bool wxSocketBase::WaitForLost(long seconds
, long milliseconds
)
704 return _Wait(seconds
, milliseconds
, GSOCK_LOST_FLAG
);
707 // --------------------------------------------------------------------------
709 // --------------------------------------------------------------------------
712 // Get local or peer address
715 bool wxSocketBase::GetPeer(wxSockAddress
& addr_man
) const
722 peer
= GSocket_GetPeer(m_socket
);
723 addr_man
.SetAddress(peer
);
724 GAddress_destroy(peer
);
729 bool wxSocketBase::GetLocal(wxSockAddress
& addr_man
) const
736 local
= GSocket_GetLocal(m_socket
);
737 addr_man
.SetAddress(local
);
738 GAddress_destroy(local
);
744 // Save and restore socket state
747 void wxSocketBase::SaveState()
749 wxSocketState
*state
;
751 state
= new wxSocketState();
753 state
->m_flags
= m_flags
;
754 state
->m_notify
= m_notify
;
755 state
->m_eventmask
= m_eventmask
;
756 state
->m_clientData
= m_clientData
;
757 #if WXWIN_COMPATIBILITY
758 state
->m_cbk
= m_cbk
;
759 state
->m_cdata
= m_cdata
;
760 #endif // WXWIN_COMPATIBILITY
762 m_states
.Append(state
);
765 void wxSocketBase::RestoreState()
768 wxSocketState
*state
;
770 node
= m_states
.Last();
774 state
= (wxSocketState
*)node
->Data();
776 m_flags
= state
->m_flags
;
777 m_notify
= state
->m_notify
;
778 m_eventmask
= state
->m_eventmask
;
779 m_clientData
= state
->m_clientData
;
780 #if WXWIN_COMPATIBILITY
781 m_cbk
= state
->m_cbk
;
782 m_cdata
= state
->m_cdata
;
783 #endif // WXWIN_COMPATIBILITY
793 void wxSocketBase::SetTimeout(long seconds
)
798 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
801 void wxSocketBase::SetFlags(wxSocketFlags flags
)
807 // --------------------------------------------------------------------------
808 // Callbacks (now obsolete - use events instead)
809 // --------------------------------------------------------------------------
811 #if WXWIN_COMPATIBILITY
813 wxSocketBase::wxSockCbk
wxSocketBase::Callback(wxSockCbk cbk_
)
815 wxSockCbk old_cbk
= cbk_
;
821 char *wxSocketBase::CallbackData(char *data
)
823 char *old_data
= m_cdata
;
829 #endif // WXWIN_COMPATIBILITY
831 // --------------------------------------------------------------------------
833 // --------------------------------------------------------------------------
835 // A note on how events are processed, which is probably the most
836 // difficult thing to get working right while keeping the same API
837 // and functionality for all platforms.
839 // When GSocket detects an event, it calls wx_socket_callback, which in
840 // turn just calls wxSocketBase::OnRequest in the corresponding wxSocket
841 // object. OnRequest does some housekeeping, and if the event is to be
842 // propagated to the user, it creates a new wxSocketEvent object and
843 // posts it. The event is not processed immediately, but delayed with
844 // AddPendingEvent instead. This is necessary in order to decouple the
845 // event processing from wx_socket_callback; otherwise, subsequent IO
846 // calls made from the user event handler would fail, as gtk callbacks
847 // are not reentrant.
849 // Note that, unlike events, user callbacks (now deprecated) are _not_
850 // decoupled from wx_socket_callback and thus they suffer from a variety
851 // of problems. Avoid them where possible and use events instead.
853 static void LINKAGEMODE
wx_socket_callback(GSocket
* WXUNUSED(socket
),
854 GSocketEvent notification
,
857 wxSocketBase
*sckobj
= (wxSocketBase
*)cdata
;
859 sckobj
->OnRequest((wxSocketNotify
) notification
);
862 void wxSocketBase::OnRequest(wxSocketNotify notification
)
864 // NOTE: We duplicate some of the code in _Wait, but this doesn't
865 // hurt. It has to be here because the (GSocket) event might arrive
866 // a bit delayed, and it has to be in _Wait as well because we don't
867 // know whether the Wait functions are being used.
871 case wxSOCKET_CONNECTION
:
872 m_establishing
= FALSE
;
876 // If we are in the middle of a R/W operation, do not
877 // propagate events to users. Also, filter 'late' events
878 // which are no longer valid.
881 if (m_reading
|| !GSocket_Select(m_socket
, GSOCK_INPUT_FLAG
))
885 case wxSOCKET_OUTPUT
:
886 if (m_writing
|| !GSocket_Select(m_socket
, GSOCK_OUTPUT_FLAG
))
892 m_establishing
= FALSE
;
899 // Schedule the event
901 wxSocketEventFlags flag
= 0;
902 switch (notification
)
904 case GSOCK_INPUT
: flag
= GSOCK_INPUT_FLAG
; break;
905 case GSOCK_OUTPUT
: flag
= GSOCK_OUTPUT_FLAG
; break;
906 case GSOCK_CONNECTION
: flag
= GSOCK_CONNECTION_FLAG
; break;
907 case GSOCK_LOST
: flag
= GSOCK_LOST_FLAG
; break;
909 wxLogWarning( _("wxSocket: unknown event!."));
913 if (((m_eventmask
& flag
) == flag
) && m_notify
)
917 wxSocketEvent
event(m_id
);
918 event
.m_event
= notification
;
919 event
.m_clientData
= m_clientData
;
920 event
.SetEventObject(this);
922 m_handler
->AddPendingEvent(event
);
925 #if WXWIN_COMPATIBILITY
927 m_cbk(*this, notification
, m_cdata
);
928 #endif // WXWIN_COMPATIBILITY
932 void wxSocketBase::Notify(bool notify
)
937 void wxSocketBase::SetNotify(wxSocketEventFlags flags
)
942 void wxSocketBase::SetEventHandler(wxEvtHandler
& handler
, int id
)
944 m_handler
= &handler
;
948 // --------------------------------------------------------------------------
950 // --------------------------------------------------------------------------
952 void wxSocketBase::Pushback(const void *buffer
, wxUint32 size
)
956 if (m_unread
== NULL
)
957 m_unread
= malloc(size
);
962 tmp
= malloc(m_unrd_size
+ size
);
963 memcpy((char *)tmp
+ size
, m_unread
, m_unrd_size
);
971 memcpy(m_unread
, buffer
, size
);
974 wxUint32
wxSocketBase::GetPushback(void *buffer
, wxUint32 size
, bool peek
)
979 if (size
> (m_unrd_size
-m_unrd_cur
))
980 size
= m_unrd_size
-m_unrd_cur
;
982 memcpy(buffer
, (char *)m_unread
+ m_unrd_cur
, size
);
987 if (m_unrd_size
== m_unrd_cur
)
1000 // ==========================================================================
1002 // ==========================================================================
1004 // --------------------------------------------------------------------------
1006 // --------------------------------------------------------------------------
1008 wxSocketServer::wxSocketServer(wxSockAddress
& addr_man
,
1009 wxSocketFlags flags
)
1010 : wxSocketBase(flags
, wxSOCKET_SERVER
)
1012 // Create the socket
1013 m_socket
= GSocket_new();
1018 // Setup the socket as server
1019 GSocket_SetLocal(m_socket
, addr_man
.GetAddress());
1020 if (GSocket_SetServer(m_socket
) != GSOCK_NOERROR
)
1022 GSocket_destroy(m_socket
);
1027 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
1028 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1029 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1030 wx_socket_callback
, (char *)this);
1034 // --------------------------------------------------------------------------
1036 // --------------------------------------------------------------------------
1038 bool wxSocketServer::AcceptWith(wxSocketBase
& sock
, bool wait
)
1040 GSocket
*child_socket
;
1045 // If wait == FALSE, then the call should be nonblocking.
1046 // When we are finished, we put the socket to blocking mode
1050 GSocket_SetNonBlocking(m_socket
, TRUE
);
1052 child_socket
= GSocket_WaitConnection(m_socket
);
1055 GSocket_SetNonBlocking(m_socket
, FALSE
);
1060 sock
.m_type
= wxSOCKET_BASE
;
1061 sock
.m_socket
= child_socket
;
1062 sock
.m_connected
= TRUE
;
1064 GSocket_SetTimeout(sock
.m_socket
, sock
.m_timeout
* 1000);
1065 GSocket_SetCallback(sock
.m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1066 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1067 wx_socket_callback
, (char *)&sock
);
1072 wxSocketBase
*wxSocketServer::Accept(bool wait
)
1074 wxSocketBase
* sock
= new wxSocketBase();
1076 sock
->SetFlags(m_flags
);
1078 if (!AcceptWith(*sock
, wait
))
1084 bool wxSocketServer::WaitForAccept(long seconds
, long milliseconds
)
1086 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
);
1089 // ==========================================================================
1091 // ==========================================================================
1093 // --------------------------------------------------------------------------
1095 // --------------------------------------------------------------------------
1097 wxSocketClient::wxSocketClient(wxSocketFlags flags
)
1098 : wxSocketBase(flags
, wxSOCKET_CLIENT
)
1102 wxSocketClient::~wxSocketClient()
1106 // --------------------------------------------------------------------------
1108 // --------------------------------------------------------------------------
1110 bool wxSocketClient::Connect(wxSockAddress
& addr_man
, bool wait
)
1116 // Shutdown and destroy the socket
1118 GSocket_destroy(m_socket
);
1121 m_socket
= GSocket_new();
1122 m_connected
= FALSE
;
1123 m_establishing
= FALSE
;
1128 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
1129 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1130 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1131 wx_socket_callback
, (char *)this);
1133 // If wait == FALSE, then the call should be nonblocking.
1134 // When we are finished, we put the socket to blocking mode
1138 GSocket_SetNonBlocking(m_socket
, TRUE
);
1140 GSocket_SetPeer(m_socket
, addr_man
.GetAddress());
1141 err
= GSocket_Connect(m_socket
, GSOCK_STREAMED
);
1144 GSocket_SetNonBlocking(m_socket
, FALSE
);
1146 if (err
!= GSOCK_NOERROR
)
1148 if (err
== GSOCK_WOULDBLOCK
)
1149 m_establishing
= TRUE
;
1158 bool wxSocketClient::WaitOnConnect(long seconds
, long milliseconds
)
1160 if (m_connected
) // Already connected
1163 if (!m_establishing
|| !m_socket
) // No connection in progress
1166 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
|
1170 // ==========================================================================
1172 // ==========================================================================
1174 /* NOTE: experimental stuff - might change */
1176 wxDatagramSocket::wxDatagramSocket( wxSockAddress
& addr
,
1177 wxSocketFlags flags
)
1178 : wxSocketBase( flags
, wxSOCKET_DATAGRAM
)
1180 // Create the socket
1181 m_socket
= GSocket_new();
1186 // Setup the socket as non connection oriented
1187 GSocket_SetLocal(m_socket
, addr
.GetAddress());
1188 if( GSocket_SetNonOriented(m_socket
) != GSOCK_NOERROR
)
1190 GSocket_destroy(m_socket
);
1195 // Initialize all stuff
1196 m_connected
= FALSE
;
1197 m_establishing
= FALSE
;
1198 GSocket_SetTimeout( m_socket
, m_timeout
);
1199 GSocket_SetCallback( m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1200 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1201 wx_socket_callback
, (char*)this );
1205 wxDatagramSocket
& wxDatagramSocket::RecvFrom( wxSockAddress
& addr
,
1214 wxDatagramSocket
& wxDatagramSocket::SendTo( wxSockAddress
& addr
,
1218 GSocket_SetPeer(m_socket
, addr
.GetAddress());
1223 // ==========================================================================
1225 // ==========================================================================
1227 wxSocketEvent::wxSocketEvent(int id
) : wxEvent(id
)
1229 SetEventType( (wxEventType
)wxEVT_SOCKET
);
1232 void wxSocketEvent::CopyObject(wxObject
& object_dest
) const
1234 wxSocketEvent
*event
= (wxSocketEvent
*)&object_dest
;
1236 wxEvent::CopyObject(object_dest
);
1238 event
->m_event
= m_event
;
1239 event
->m_clientData
= m_clientData
;
1242 // ==========================================================================
1244 // ==========================================================================
1246 class WXDLLEXPORT wxSocketModule
: public wxModule
1248 DECLARE_DYNAMIC_CLASS(wxSocketModule
)
1251 bool OnInit() { return GSocket_Init(); }
1252 void OnExit() { GSocket_Cleanup(); }
1255 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule
, wxModule
)