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 (with or without wxSOCKET_BLOCK)
262 if (m_flags
& wxSOCKET_NOWAIT
)
264 GSocket_SetNonBlocking(m_socket
, 1);
265 ret
= GSocket_Read(m_socket
, (char *)buffer
, nbytes
);
266 GSocket_SetNonBlocking(m_socket
, 0);
277 if ( !(m_flags
& wxSOCKET_BLOCK
) && !WaitForRead() )
280 ret
= GSocket_Read(m_socket
, (char *)buffer
, nbytes
);
286 buffer
= (char *)buffer
+ ret
;
289 // If we got here and wxSOCKET_WAITALL is not set, we can leave
290 // now. Otherwise, wait until we recv all the data or until there
293 more
= (ret
> 0 && nbytes
> 0 && (m_flags
& wxSOCKET_WAITALL
));
300 wxSocketBase
& wxSocketBase::ReadMsg(void* buffer
, wxUint32 nbytes
)
302 wxUint32 len
, len2
, sig
, total
;
307 unsigned char sig
[4];
308 unsigned char len
[4];
317 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
319 if (_Read(&msg
, sizeof(msg
)) != sizeof(msg
))
322 sig
= (wxUint32
)msg
.sig
[0];
323 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
324 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
325 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
327 if (sig
!= 0xfeeddead)
329 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
333 len
= (wxUint32
)msg
.len
[0];
334 len
|= (wxUint32
)(msg
.len
[1] << 8);
335 len
|= (wxUint32
)(msg
.len
[2] << 16);
336 len
|= (wxUint32
)(msg
.len
[3] << 24);
346 // Don't attemp to read if the msg was zero bytes long.
349 total
= _Read(buffer
, len
);
356 char *discard_buffer
= new char[MAX_DISCARD_SIZE
];
359 // NOTE: discarded bytes don't add to m_lcount.
362 discard_len
= ((len2
> MAX_DISCARD_SIZE
)? MAX_DISCARD_SIZE
: len2
);
363 discard_len
= _Read(discard_buffer
, (wxUint32
)discard_len
);
364 len2
-= (wxUint32
)discard_len
;
366 while ((discard_len
> 0) && len2
);
368 delete [] discard_buffer
;
373 if (_Read(&msg
, sizeof(msg
)) != sizeof(msg
))
376 sig
= (wxUint32
)msg
.sig
[0];
377 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
378 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
379 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
381 if (sig
!= 0xdeadfeed)
383 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
399 wxSocketBase
& wxSocketBase::Peek(void* buffer
, wxUint32 nbytes
)
404 m_lcount
= _Read(buffer
, nbytes
);
405 Pushback(buffer
, m_lcount
);
407 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
408 if (m_flags
& wxSOCKET_WAITALL
)
409 m_error
= (m_lcount
!= nbytes
);
411 m_error
= (m_lcount
== 0);
413 // Allow read events again
419 wxSocketBase
& wxSocketBase::Write(const void *buffer
, wxUint32 nbytes
)
424 m_lcount
= _Write(buffer
, nbytes
);
426 // If in wxSOCKET_WAITALL mode, all bytes should have been written.
427 if (m_flags
& wxSOCKET_WAITALL
)
428 m_error
= (m_lcount
!= nbytes
);
430 m_error
= (m_lcount
== 0);
432 // Allow write events again
438 wxUint32
wxSocketBase::_Write(const void *buffer
, wxUint32 nbytes
)
443 // If the socket is invalid or parameters are ill, return immediately
444 if (!m_socket
|| !buffer
|| !nbytes
)
447 // Possible combinations (they are checked in this order)
449 // wxSOCKET_WAITALL (with or without wxSOCKET_BLOCK)
453 if (m_flags
& wxSOCKET_NOWAIT
)
455 GSocket_SetNonBlocking(m_socket
, 1);
456 ret
= GSocket_Write(m_socket
, (const char *)buffer
, nbytes
);
457 GSocket_SetNonBlocking(m_socket
, 0);
468 if ( !(m_flags
& wxSOCKET_BLOCK
) && !WaitForWrite() )
471 ret
= GSocket_Write(m_socket
, (const char *)buffer
, nbytes
);
477 buffer
= (const char *)buffer
+ ret
;
480 // If we got here and wxSOCKET_WAITALL is not set, we can leave
481 // now. Otherwise, wait until we send all the data or until there
484 more
= (ret
> 0 && nbytes
> 0 && (m_flags
& wxSOCKET_WAITALL
));
491 wxSocketBase
& wxSocketBase::WriteMsg(const void *buffer
, wxUint32 nbytes
)
498 unsigned char sig
[4];
499 unsigned char len
[4];
508 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
510 msg
.sig
[0] = (unsigned char) 0xad;
511 msg
.sig
[1] = (unsigned char) 0xde;
512 msg
.sig
[2] = (unsigned char) 0xed;
513 msg
.sig
[3] = (unsigned char) 0xfe;
515 msg
.len
[0] = (unsigned char) (nbytes
& 0xff);
516 msg
.len
[1] = (unsigned char) ((nbytes
>> 8) & 0xff);
517 msg
.len
[2] = (unsigned char) ((nbytes
>> 16) & 0xff);
518 msg
.len
[3] = (unsigned char) ((nbytes
>> 24) & 0xff);
520 if (_Write(&msg
, sizeof(msg
)) < sizeof(msg
))
523 total
= _Write(buffer
, nbytes
);
528 msg
.sig
[0] = (unsigned char) 0xed;
529 msg
.sig
[1] = (unsigned char) 0xfe;
530 msg
.sig
[2] = (unsigned char) 0xad;
531 msg
.sig
[3] = (unsigned char) 0xde;
532 msg
.len
[0] = msg
.len
[1] = msg
.len
[2] = msg
.len
[3] = (char) 0;
534 if ((_Write(&msg
, sizeof(msg
))) < sizeof(msg
))
548 wxSocketBase
& wxSocketBase::Unread(const void *buffer
, wxUint32 nbytes
)
551 Pushback(buffer
, nbytes
);
559 wxSocketBase
& wxSocketBase::Discard()
562 char *buffer
= new char[MAX_DISCARD_SIZE
];
570 SetFlags(wxSOCKET_NOWAIT
);
574 ret
= _Read(buffer
, MAX_DISCARD_SIZE
);
577 while (ret
== MAX_DISCARD_SIZE
);
583 // Allow read events again
589 // --------------------------------------------------------------------------
591 // --------------------------------------------------------------------------
593 // All Wait functions poll the socket using GSocket_Select() to
594 // check for the specified combination of conditions, until one
595 // of these conditions become true, an error occurs, or the
596 // timeout elapses. The polling loop calls PROCESS_EVENTS(), so
597 // this won't block the GUI.
599 bool wxSocketBase::_Wait(long seconds
,
601 wxSocketEventFlags flags
)
603 GSocketEventFlags result
;
606 // Set this to TRUE to interrupt ongoing waits
609 // Check for valid socket
613 // Check for valid timeout value.
615 timeout
= seconds
* 1000 + milliseconds
;
617 timeout
= m_timeout
* 1000;
619 // Wait in an active polling loop.
621 // NOTE: We duplicate some of the code in OnRequest, but this doesn't
622 // hurt. It has to be here because the (GSocket) event might arrive
623 // a bit delayed, and it has to be in OnRequest as well because we
624 // don't know whether the Wait functions are being used.
626 // Do this at least once (important if timeout == 0, when
627 // we are just polling). Also, if just polling, do not yield.
634 result
= GSocket_Select(m_socket
, flags
| GSOCK_LOST_FLAG
);
636 // Incoming connection (server) or connection established (client)
637 if (result
& GSOCK_CONNECTION_FLAG
)
640 m_establishing
= FALSE
;
644 // Data available or output buffer ready
645 if ((result
& GSOCK_INPUT_FLAG
) || (result
& GSOCK_OUTPUT_FLAG
))
651 if (result
& GSOCK_LOST_FLAG
)
654 m_establishing
= FALSE
;
655 return (flags
& GSOCK_LOST_FLAG
) != 0;
659 if ((!timeout
) || (chrono
.Time() > timeout
) || (m_interrupt
))
668 bool wxSocketBase::Wait(long seconds
, long milliseconds
)
670 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
672 GSOCK_CONNECTION_FLAG
|
676 bool wxSocketBase::WaitForRead(long seconds
, long milliseconds
)
678 // Check pushback buffer before entering _Wait
682 // Note that GSOCK_INPUT_LOST has to be explicitly passed to
683 // _Wait becuase of the semantics of WaitForRead: a return
684 // value of TRUE means that a GSocket_Read call will return
685 // immediately, not that there is actually data to read.
687 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
691 bool wxSocketBase::WaitForWrite(long seconds
, long milliseconds
)
693 return _Wait(seconds
, milliseconds
, GSOCK_OUTPUT_FLAG
);
696 bool wxSocketBase::WaitForLost(long seconds
, long milliseconds
)
698 return _Wait(seconds
, milliseconds
, GSOCK_LOST_FLAG
);
701 // --------------------------------------------------------------------------
703 // --------------------------------------------------------------------------
706 // Get local or peer address
709 bool wxSocketBase::GetPeer(wxSockAddress
& addr_man
) const
716 peer
= GSocket_GetPeer(m_socket
);
717 addr_man
.SetAddress(peer
);
718 GAddress_destroy(peer
);
723 bool wxSocketBase::GetLocal(wxSockAddress
& addr_man
) const
730 local
= GSocket_GetLocal(m_socket
);
731 addr_man
.SetAddress(local
);
732 GAddress_destroy(local
);
738 // Save and restore socket state
741 void wxSocketBase::SaveState()
743 wxSocketState
*state
;
745 state
= new wxSocketState();
747 state
->m_flags
= m_flags
;
748 state
->m_notify
= m_notify
;
749 state
->m_eventmask
= m_eventmask
;
750 state
->m_clientData
= m_clientData
;
751 #if WXWIN_COMPATIBILITY
752 state
->m_cbk
= m_cbk
;
753 state
->m_cdata
= m_cdata
;
754 #endif // WXWIN_COMPATIBILITY
756 m_states
.Append(state
);
759 void wxSocketBase::RestoreState()
762 wxSocketState
*state
;
764 node
= m_states
.Last();
768 state
= (wxSocketState
*)node
->Data();
770 m_flags
= state
->m_flags
;
771 m_notify
= state
->m_notify
;
772 m_eventmask
= state
->m_eventmask
;
773 m_clientData
= state
->m_clientData
;
774 #if WXWIN_COMPATIBILITY
775 m_cbk
= state
->m_cbk
;
776 m_cdata
= state
->m_cdata
;
777 #endif // WXWIN_COMPATIBILITY
787 void wxSocketBase::SetTimeout(long seconds
)
792 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
795 void wxSocketBase::SetFlags(wxSocketFlags flags
)
801 // --------------------------------------------------------------------------
802 // Callbacks (now obsolete - use events instead)
803 // --------------------------------------------------------------------------
805 #if WXWIN_COMPATIBILITY
807 wxSocketBase::wxSockCbk
wxSocketBase::Callback(wxSockCbk cbk_
)
809 wxSockCbk old_cbk
= cbk_
;
815 char *wxSocketBase::CallbackData(char *data
)
817 char *old_data
= m_cdata
;
823 #endif // WXWIN_COMPATIBILITY
825 // --------------------------------------------------------------------------
827 // --------------------------------------------------------------------------
829 // A note on how events are processed, which is probably the most
830 // difficult thing to get working right while keeping the same API
831 // and functionality for all platforms.
833 // When GSocket detects an event, it calls wx_socket_callback, which in
834 // turn just calls wxSocketBase::OnRequest in the corresponding wxSocket
835 // object. OnRequest does some housekeeping, and if the event is to be
836 // propagated to the user, it creates a new wxSocketEvent object and
837 // posts it. The event is not processed immediately, but delayed with
838 // AddPendingEvent instead. This is necessary in order to decouple the
839 // event processing from wx_socket_callback; otherwise, subsequent IO
840 // calls made from the user event handler would fail, as gtk callbacks
841 // are not reentrant.
843 // Note that, unlike events, user callbacks (now deprecated) are _not_
844 // decoupled from wx_socket_callback and thus they suffer from a variety
845 // of problems. Avoid them where possible and use events instead.
847 static void LINKAGEMODE
wx_socket_callback(GSocket
* WXUNUSED(socket
),
848 GSocketEvent notification
,
851 wxSocketBase
*sckobj
= (wxSocketBase
*)cdata
;
853 sckobj
->OnRequest((wxSocketNotify
) notification
);
856 void wxSocketBase::OnRequest(wxSocketNotify notification
)
858 // NOTE: We duplicate some of the code in _Wait, but this doesn't
859 // hurt. It has to be here because the (GSocket) event might arrive
860 // a bit delayed, and it has to be in _Wait as well because we don't
861 // know whether the Wait functions are being used.
865 case wxSOCKET_CONNECTION
:
866 m_establishing
= FALSE
;
870 // If we are in the middle of a R/W operation, do not
871 // propagate events to users. Also, filter 'late' events
872 // which are no longer valid.
875 if (m_reading
|| !GSocket_Select(m_socket
, GSOCK_INPUT_FLAG
))
879 case wxSOCKET_OUTPUT
:
880 if (m_writing
|| !GSocket_Select(m_socket
, GSOCK_OUTPUT_FLAG
))
886 m_establishing
= FALSE
;
893 // Schedule the event
895 wxSocketEventFlags flag
= 0;
896 switch (notification
)
898 case GSOCK_INPUT
: flag
= GSOCK_INPUT_FLAG
; break;
899 case GSOCK_OUTPUT
: flag
= GSOCK_OUTPUT_FLAG
; break;
900 case GSOCK_CONNECTION
: flag
= GSOCK_CONNECTION_FLAG
; break;
901 case GSOCK_LOST
: flag
= GSOCK_LOST_FLAG
; break;
903 wxLogWarning(_("wxSocket: unknown event!."));
907 if (((m_eventmask
& flag
) == flag
) && m_notify
)
911 wxSocketEvent
event(m_id
);
912 event
.m_event
= notification
;
913 event
.m_clientData
= m_clientData
;
914 event
.SetEventObject(this);
916 m_handler
->AddPendingEvent(event
);
919 #if WXWIN_COMPATIBILITY
921 m_cbk(*this, notification
, m_cdata
);
922 #endif // WXWIN_COMPATIBILITY
926 void wxSocketBase::Notify(bool notify
)
931 void wxSocketBase::SetNotify(wxSocketEventFlags flags
)
936 void wxSocketBase::SetEventHandler(wxEvtHandler
& handler
, int id
)
938 m_handler
= &handler
;
942 // --------------------------------------------------------------------------
944 // --------------------------------------------------------------------------
946 void wxSocketBase::Pushback(const void *buffer
, wxUint32 size
)
950 if (m_unread
== NULL
)
951 m_unread
= malloc(size
);
956 tmp
= malloc(m_unrd_size
+ size
);
957 memcpy((char *)tmp
+ size
, m_unread
, m_unrd_size
);
965 memcpy(m_unread
, buffer
, size
);
968 wxUint32
wxSocketBase::GetPushback(void *buffer
, wxUint32 size
, bool peek
)
973 if (size
> (m_unrd_size
-m_unrd_cur
))
974 size
= m_unrd_size
-m_unrd_cur
;
976 memcpy(buffer
, (char *)m_unread
+ m_unrd_cur
, size
);
981 if (m_unrd_size
== m_unrd_cur
)
994 // ==========================================================================
996 // ==========================================================================
998 // --------------------------------------------------------------------------
1000 // --------------------------------------------------------------------------
1002 wxSocketServer::wxSocketServer(wxSockAddress
& addr_man
,
1003 wxSocketFlags flags
)
1004 : wxSocketBase(flags
, wxSOCKET_SERVER
)
1006 // Create the socket
1007 m_socket
= GSocket_new();
1012 // Setup the socket as server
1013 GSocket_SetLocal(m_socket
, addr_man
.GetAddress());
1014 if (GSocket_SetServer(m_socket
) != GSOCK_NOERROR
)
1016 GSocket_destroy(m_socket
);
1021 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
1022 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1023 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1024 wx_socket_callback
, (char *)this);
1028 // --------------------------------------------------------------------------
1030 // --------------------------------------------------------------------------
1032 bool wxSocketServer::AcceptWith(wxSocketBase
& sock
, bool wait
)
1034 GSocket
*child_socket
;
1039 // If wait == FALSE, then the call should be nonblocking.
1040 // When we are finished, we put the socket to blocking mode
1044 GSocket_SetNonBlocking(m_socket
, 1);
1046 child_socket
= GSocket_WaitConnection(m_socket
);
1049 GSocket_SetNonBlocking(m_socket
, 0);
1054 sock
.m_type
= wxSOCKET_BASE
;
1055 sock
.m_socket
= child_socket
;
1056 sock
.m_connected
= TRUE
;
1058 GSocket_SetTimeout(sock
.m_socket
, sock
.m_timeout
* 1000);
1059 GSocket_SetCallback(sock
.m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1060 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1061 wx_socket_callback
, (char *)&sock
);
1066 wxSocketBase
*wxSocketServer::Accept(bool wait
)
1068 wxSocketBase
* sock
= new wxSocketBase();
1070 sock
->SetFlags(m_flags
);
1072 if (!AcceptWith(*sock
, wait
))
1078 bool wxSocketServer::WaitForAccept(long seconds
, long milliseconds
)
1080 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
);
1083 // ==========================================================================
1085 // ==========================================================================
1087 // --------------------------------------------------------------------------
1089 // --------------------------------------------------------------------------
1091 wxSocketClient::wxSocketClient(wxSocketFlags flags
)
1092 : wxSocketBase(flags
, wxSOCKET_CLIENT
)
1096 wxSocketClient::~wxSocketClient()
1100 // --------------------------------------------------------------------------
1102 // --------------------------------------------------------------------------
1104 bool wxSocketClient::Connect(wxSockAddress
& addr_man
, bool wait
)
1110 // Shutdown and destroy the socket
1112 GSocket_destroy(m_socket
);
1115 m_socket
= GSocket_new();
1116 m_connected
= FALSE
;
1117 m_establishing
= FALSE
;
1122 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
1123 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1124 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1125 wx_socket_callback
, (char *)this);
1127 // If wait == FALSE, then the call should be nonblocking.
1128 // When we are finished, we put the socket to blocking mode
1132 GSocket_SetNonBlocking(m_socket
, 1);
1134 GSocket_SetPeer(m_socket
, addr_man
.GetAddress());
1135 err
= GSocket_Connect(m_socket
, GSOCK_STREAMED
);
1138 GSocket_SetNonBlocking(m_socket
, 0);
1140 if (err
!= GSOCK_NOERROR
)
1142 if (err
== GSOCK_WOULDBLOCK
)
1143 m_establishing
= TRUE
;
1152 bool wxSocketClient::WaitOnConnect(long seconds
, long milliseconds
)
1154 if (m_connected
) // Already connected
1157 if (!m_establishing
|| !m_socket
) // No connection in progress
1160 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
|
1164 // ==========================================================================
1166 // ==========================================================================
1168 /* NOTE: experimental stuff - might change */
1170 wxDatagramSocket::wxDatagramSocket( wxSockAddress
& addr
,
1171 wxSocketFlags flags
)
1172 : wxSocketBase( flags
, wxSOCKET_DATAGRAM
)
1174 // Create the socket
1175 m_socket
= GSocket_new();
1180 // Setup the socket as non connection oriented
1181 GSocket_SetLocal(m_socket
, addr
.GetAddress());
1182 if( GSocket_SetNonOriented(m_socket
) != GSOCK_NOERROR
)
1184 GSocket_destroy(m_socket
);
1189 // Initialize all stuff
1190 m_connected
= FALSE
;
1191 m_establishing
= FALSE
;
1192 GSocket_SetTimeout( m_socket
, m_timeout
);
1193 GSocket_SetCallback( m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1194 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1195 wx_socket_callback
, (char*)this );
1199 wxDatagramSocket
& wxDatagramSocket::RecvFrom( wxSockAddress
& addr
,
1208 wxDatagramSocket
& wxDatagramSocket::SendTo( wxSockAddress
& addr
,
1212 GSocket_SetPeer(m_socket
, addr
.GetAddress());
1217 // ==========================================================================
1219 // ==========================================================================
1221 wxSocketEvent::wxSocketEvent(int id
) : wxEvent(id
)
1223 SetEventType( (wxEventType
)wxEVT_SOCKET
);
1226 void wxSocketEvent::CopyObject(wxObject
& object_dest
) const
1228 wxSocketEvent
*event
= (wxSocketEvent
*)&object_dest
;
1230 wxEvent::CopyObject(object_dest
);
1232 event
->m_event
= m_event
;
1233 event
->m_clientData
= m_clientData
;
1236 // ==========================================================================
1238 // ==========================================================================
1240 class WXDLLEXPORT wxSocketModule
: public wxModule
1242 DECLARE_DYNAMIC_CLASS(wxSocketModule
)
1245 bool OnInit() { return GSocket_Init() != 0; }
1246 void OnExit() { GSocket_Cleanup(); }
1249 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule
, wxModule
)