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 // --------------------------------------------------------------------------
81 // --------------------------------------------------------------------------
83 IMPLEMENT_CLASS(wxSocketBase
, wxObject
)
84 IMPLEMENT_CLASS(wxSocketServer
, wxSocketBase
)
85 IMPLEMENT_CLASS(wxSocketClient
, wxSocketBase
)
86 IMPLEMENT_CLASS(wxDatagramSocket
, wxSocketBase
)
87 IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent
, wxEvent
)
89 // --------------------------------------------------------------------------
91 // --------------------------------------------------------------------------
93 class wxSocketState
: public wxObject
96 wxSocketFlags m_flags
;
97 wxSocketEventFlags m_eventmask
;
100 #if WXWIN_COMPATIBILITY
101 wxSocketBase::wxSockCbk m_cbk
;
103 #endif // WXWIN_COMPATIBILITY
106 wxSocketState() : wxObject() {}
109 // ==========================================================================
111 // ==========================================================================
113 // --------------------------------------------------------------------------
114 // Initialization and shutdown
115 // --------------------------------------------------------------------------
117 // FIXME-MT: all this is MT-unsafe, of course, we should protect all accesses
118 // to m_countInit with a crit section
119 size_t wxSocketBase::m_countInit
= 0;
121 bool wxSocketBase::IsInitialized()
123 return m_countInit
> 0;
126 bool wxSocketBase::Initialize()
128 if ( !m_countInit
++ )
130 if ( !GSocket_Init() )
141 void wxSocketBase::Shutdown()
143 // we should be initialized
144 wxASSERT_MSG( m_countInit
, _T("extra call to Shutdown()") );
145 if ( !--m_countInit
)
151 // --------------------------------------------------------------------------
153 // --------------------------------------------------------------------------
155 void wxSocketBase::Init()
158 m_type
= wxSOCKET_UNINIT
;
169 m_beingDeleted
= FALSE
;
182 #if WXWIN_COMPATIBILITY
185 #endif // WXWIN_COMPATIBILITY
187 if ( !IsInitialized() )
189 // this Initialize() will be undone by wxSocketModule::OnExit(), all the
190 // other calls to it should be matched by a call to Shutdown()
195 wxSocketBase::wxSocketBase()
200 wxSocketBase::wxSocketBase(wxSocketFlags flags
, wxSocketType type
)
208 wxSocketBase::~wxSocketBase()
210 // Just in case the app called Destroy() *and* then deleted
211 // the socket immediately: don't leave dangling pointers.
213 wxPendingDelete
.DeleteObject(this);
216 // Shutdown and close the socket
220 // Destroy the GSocket object
222 GSocket_destroy(m_socket
);
224 // Free the pushback buffer
229 bool wxSocketBase::Destroy()
231 // Delayed destruction: the socket will be deleted during the next
232 // idle loop iteration. This ensures that all pending events have
234 m_beingDeleted
= TRUE
;
236 // Shutdown and close the socket
239 // Supress events from now on
243 if ( !wxPendingDelete
.Member(this) )
244 wxPendingDelete
.Append(this);
252 // --------------------------------------------------------------------------
254 // --------------------------------------------------------------------------
256 // The following IO operations update m_error and m_lcount:
257 // {Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard}
259 // TODO: Should Connect, Accept and AcceptWith update m_error?
261 bool wxSocketBase::Close()
263 // Interrupt pending waits
269 GSocket_UnsetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
270 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
);
272 // Shutdown the connection
273 GSocket_Shutdown(m_socket
);
277 m_establishing
= FALSE
;
281 wxSocketBase
& wxSocketBase::Read(void* buffer
, wxUint32 nbytes
)
286 m_lcount
= _Read(buffer
, nbytes
);
288 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
289 if (m_flags
& wxSOCKET_WAITALL
)
290 m_error
= (m_lcount
!= nbytes
);
292 m_error
= (m_lcount
== 0);
294 // Allow read events from now on
300 wxUint32
wxSocketBase::_Read(void* buffer
, wxUint32 nbytes
)
305 // Try the pushback buffer first
306 total
= GetPushback(buffer
, nbytes
, FALSE
);
308 buffer
= (char *)buffer
+ total
;
310 // Return now in one of the following cases:
311 // - the socket is invalid,
312 // - we got all the data,
313 // - we got *some* data and we are not using wxSOCKET_WAITALL.
316 ((total
!= 0) && !(m_flags
& wxSOCKET_WAITALL
)) )
319 // Possible combinations (they are checked in this order)
321 // wxSOCKET_WAITALL (with or without wxSOCKET_BLOCK)
325 if (m_flags
& wxSOCKET_NOWAIT
)
327 GSocket_SetNonBlocking(m_socket
, 1);
328 ret
= GSocket_Read(m_socket
, (char *)buffer
, nbytes
);
329 GSocket_SetNonBlocking(m_socket
, 0);
340 if ( !(m_flags
& wxSOCKET_BLOCK
) && !WaitForRead() )
343 ret
= GSocket_Read(m_socket
, (char *)buffer
, nbytes
);
349 buffer
= (char *)buffer
+ ret
;
352 // If we got here and wxSOCKET_WAITALL is not set, we can leave
353 // now. Otherwise, wait until we recv all the data or until there
356 more
= (ret
> 0 && nbytes
> 0 && (m_flags
& wxSOCKET_WAITALL
));
363 wxSocketBase
& wxSocketBase::ReadMsg(void* buffer
, wxUint32 nbytes
)
365 wxUint32 len
, len2
, sig
, total
;
370 unsigned char sig
[4];
371 unsigned char len
[4];
380 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
382 if (_Read(&msg
, sizeof(msg
)) != sizeof(msg
))
385 sig
= (wxUint32
)msg
.sig
[0];
386 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
387 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
388 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
390 if (sig
!= 0xfeeddead)
392 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
396 len
= (wxUint32
)msg
.len
[0];
397 len
|= (wxUint32
)(msg
.len
[1] << 8);
398 len
|= (wxUint32
)(msg
.len
[2] << 16);
399 len
|= (wxUint32
)(msg
.len
[3] << 24);
409 // Don't attemp to read if the msg was zero bytes long.
412 total
= _Read(buffer
, len
);
419 char *discard_buffer
= new char[MAX_DISCARD_SIZE
];
422 // NOTE: discarded bytes don't add to m_lcount.
425 discard_len
= ((len2
> MAX_DISCARD_SIZE
)? MAX_DISCARD_SIZE
: len2
);
426 discard_len
= _Read(discard_buffer
, (wxUint32
)discard_len
);
427 len2
-= (wxUint32
)discard_len
;
429 while ((discard_len
> 0) && len2
);
431 delete [] discard_buffer
;
436 if (_Read(&msg
, sizeof(msg
)) != sizeof(msg
))
439 sig
= (wxUint32
)msg
.sig
[0];
440 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
441 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
442 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
444 if (sig
!= 0xdeadfeed)
446 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
462 wxSocketBase
& wxSocketBase::Peek(void* buffer
, wxUint32 nbytes
)
467 m_lcount
= _Read(buffer
, nbytes
);
468 Pushback(buffer
, m_lcount
);
470 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
471 if (m_flags
& wxSOCKET_WAITALL
)
472 m_error
= (m_lcount
!= nbytes
);
474 m_error
= (m_lcount
== 0);
476 // Allow read events again
482 wxSocketBase
& wxSocketBase::Write(const void *buffer
, wxUint32 nbytes
)
487 m_lcount
= _Write(buffer
, nbytes
);
489 // If in wxSOCKET_WAITALL mode, all bytes should have been written.
490 if (m_flags
& wxSOCKET_WAITALL
)
491 m_error
= (m_lcount
!= nbytes
);
493 m_error
= (m_lcount
== 0);
495 // Allow write events again
501 wxUint32
wxSocketBase::_Write(const void *buffer
, wxUint32 nbytes
)
506 // If the socket is invalid or parameters are ill, return immediately
507 if (!m_socket
|| !buffer
|| !nbytes
)
510 // Possible combinations (they are checked in this order)
512 // wxSOCKET_WAITALL (with or without wxSOCKET_BLOCK)
516 if (m_flags
& wxSOCKET_NOWAIT
)
518 GSocket_SetNonBlocking(m_socket
, 1);
519 ret
= GSocket_Write(m_socket
, (const char *)buffer
, nbytes
);
520 GSocket_SetNonBlocking(m_socket
, 0);
531 if ( !(m_flags
& wxSOCKET_BLOCK
) && !WaitForWrite() )
534 ret
= GSocket_Write(m_socket
, (const char *)buffer
, nbytes
);
540 buffer
= (const char *)buffer
+ ret
;
543 // If we got here and wxSOCKET_WAITALL is not set, we can leave
544 // now. Otherwise, wait until we send all the data or until there
547 more
= (ret
> 0 && nbytes
> 0 && (m_flags
& wxSOCKET_WAITALL
));
554 wxSocketBase
& wxSocketBase::WriteMsg(const void *buffer
, wxUint32 nbytes
)
561 unsigned char sig
[4];
562 unsigned char len
[4];
571 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
573 msg
.sig
[0] = (unsigned char) 0xad;
574 msg
.sig
[1] = (unsigned char) 0xde;
575 msg
.sig
[2] = (unsigned char) 0xed;
576 msg
.sig
[3] = (unsigned char) 0xfe;
578 msg
.len
[0] = (unsigned char) (nbytes
& 0xff);
579 msg
.len
[1] = (unsigned char) ((nbytes
>> 8) & 0xff);
580 msg
.len
[2] = (unsigned char) ((nbytes
>> 16) & 0xff);
581 msg
.len
[3] = (unsigned char) ((nbytes
>> 24) & 0xff);
583 if (_Write(&msg
, sizeof(msg
)) < sizeof(msg
))
586 total
= _Write(buffer
, nbytes
);
591 msg
.sig
[0] = (unsigned char) 0xed;
592 msg
.sig
[1] = (unsigned char) 0xfe;
593 msg
.sig
[2] = (unsigned char) 0xad;
594 msg
.sig
[3] = (unsigned char) 0xde;
595 msg
.len
[0] = msg
.len
[1] = msg
.len
[2] = msg
.len
[3] = (char) 0;
597 if ((_Write(&msg
, sizeof(msg
))) < sizeof(msg
))
611 wxSocketBase
& wxSocketBase::Unread(const void *buffer
, wxUint32 nbytes
)
614 Pushback(buffer
, nbytes
);
622 wxSocketBase
& wxSocketBase::Discard()
625 char *buffer
= new char[MAX_DISCARD_SIZE
];
633 SetFlags(wxSOCKET_NOWAIT
);
637 ret
= _Read(buffer
, MAX_DISCARD_SIZE
);
640 while (ret
== MAX_DISCARD_SIZE
);
646 // Allow read events again
652 // --------------------------------------------------------------------------
654 // --------------------------------------------------------------------------
656 // All Wait functions poll the socket using GSocket_Select() to
657 // check for the specified combination of conditions, until one
658 // of these conditions become true, an error occurs, or the
659 // timeout elapses. The polling loop calls PROCESS_EVENTS(), so
660 // this won't block the GUI.
662 bool wxSocketBase::_Wait(long seconds
,
664 wxSocketEventFlags flags
)
666 GSocketEventFlags result
;
669 // Set this to TRUE to interrupt ongoing waits
672 // Check for valid socket
676 // Check for valid timeout value.
678 timeout
= seconds
* 1000 + milliseconds
;
680 timeout
= m_timeout
* 1000;
682 // Wait in an active polling loop.
684 // NOTE: We duplicate some of the code in OnRequest, but this doesn't
685 // hurt. It has to be here because the (GSocket) event might arrive
686 // a bit delayed, and it has to be in OnRequest as well because we
687 // don't know whether the Wait functions are being used.
689 // Do this at least once (important if timeout == 0, when
690 // we are just polling). Also, if just polling, do not yield.
697 result
= GSocket_Select(m_socket
, flags
| GSOCK_LOST_FLAG
);
699 // Incoming connection (server) or connection established (client)
700 if (result
& GSOCK_CONNECTION_FLAG
)
703 m_establishing
= FALSE
;
707 // Data available or output buffer ready
708 if ((result
& GSOCK_INPUT_FLAG
) || (result
& GSOCK_OUTPUT_FLAG
))
714 if (result
& GSOCK_LOST_FLAG
)
717 m_establishing
= FALSE
;
718 return (flags
& GSOCK_LOST_FLAG
) != 0;
722 if ((!timeout
) || (chrono
.Time() > timeout
) || (m_interrupt
))
731 bool wxSocketBase::Wait(long seconds
, long milliseconds
)
733 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
735 GSOCK_CONNECTION_FLAG
|
739 bool wxSocketBase::WaitForRead(long seconds
, long milliseconds
)
741 // Check pushback buffer before entering _Wait
745 // Note that GSOCK_INPUT_LOST has to be explicitly passed to
746 // _Wait becuase of the semantics of WaitForRead: a return
747 // value of TRUE means that a GSocket_Read call will return
748 // immediately, not that there is actually data to read.
750 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
754 bool wxSocketBase::WaitForWrite(long seconds
, long milliseconds
)
756 return _Wait(seconds
, milliseconds
, GSOCK_OUTPUT_FLAG
);
759 bool wxSocketBase::WaitForLost(long seconds
, long milliseconds
)
761 return _Wait(seconds
, milliseconds
, GSOCK_LOST_FLAG
);
764 // --------------------------------------------------------------------------
766 // --------------------------------------------------------------------------
769 // Get local or peer address
772 bool wxSocketBase::GetPeer(wxSockAddress
& addr_man
) const
779 peer
= GSocket_GetPeer(m_socket
);
780 addr_man
.SetAddress(peer
);
781 GAddress_destroy(peer
);
786 bool wxSocketBase::GetLocal(wxSockAddress
& addr_man
) const
793 local
= GSocket_GetLocal(m_socket
);
794 addr_man
.SetAddress(local
);
795 GAddress_destroy(local
);
801 // Save and restore socket state
804 void wxSocketBase::SaveState()
806 wxSocketState
*state
;
808 state
= new wxSocketState();
810 state
->m_flags
= m_flags
;
811 state
->m_notify
= m_notify
;
812 state
->m_eventmask
= m_eventmask
;
813 state
->m_clientData
= m_clientData
;
814 #if WXWIN_COMPATIBILITY
815 state
->m_cbk
= m_cbk
;
816 state
->m_cdata
= m_cdata
;
817 #endif // WXWIN_COMPATIBILITY
819 m_states
.Append(state
);
822 void wxSocketBase::RestoreState()
825 wxSocketState
*state
;
827 node
= m_states
.Last();
831 state
= (wxSocketState
*)node
->Data();
833 m_flags
= state
->m_flags
;
834 m_notify
= state
->m_notify
;
835 m_eventmask
= state
->m_eventmask
;
836 m_clientData
= state
->m_clientData
;
837 #if WXWIN_COMPATIBILITY
838 m_cbk
= state
->m_cbk
;
839 m_cdata
= state
->m_cdata
;
840 #endif // WXWIN_COMPATIBILITY
850 void wxSocketBase::SetTimeout(long seconds
)
855 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
858 void wxSocketBase::SetFlags(wxSocketFlags flags
)
864 // --------------------------------------------------------------------------
865 // Callbacks (now obsolete - use events instead)
866 // --------------------------------------------------------------------------
868 #if WXWIN_COMPATIBILITY
870 wxSocketBase::wxSockCbk
wxSocketBase::Callback(wxSockCbk cbk_
)
872 wxSockCbk old_cbk
= cbk_
;
878 char *wxSocketBase::CallbackData(char *data
)
880 char *old_data
= m_cdata
;
886 #endif // WXWIN_COMPATIBILITY
888 // --------------------------------------------------------------------------
890 // --------------------------------------------------------------------------
892 // A note on how events are processed, which is probably the most
893 // difficult thing to get working right while keeping the same API
894 // and functionality for all platforms.
896 // When GSocket detects an event, it calls wx_socket_callback, which in
897 // turn just calls wxSocketBase::OnRequest in the corresponding wxSocket
898 // object. OnRequest does some housekeeping, and if the event is to be
899 // propagated to the user, it creates a new wxSocketEvent object and
900 // posts it. The event is not processed immediately, but delayed with
901 // AddPendingEvent instead. This is necessary in order to decouple the
902 // event processing from wx_socket_callback; otherwise, subsequent IO
903 // calls made from the user event handler would fail, as gtk callbacks
904 // are not reentrant.
906 // Note that, unlike events, user callbacks (now deprecated) are _not_
907 // decoupled from wx_socket_callback and thus they suffer from a variety
908 // of problems. Avoid them where possible and use events instead.
911 void LINKAGEMODE
wx_socket_callback(GSocket
* WXUNUSED(socket
),
912 GSocketEvent notification
,
915 wxSocketBase
*sckobj
= (wxSocketBase
*)cdata
;
917 sckobj
->OnRequest((wxSocketNotify
) notification
);
920 void wxSocketBase::OnRequest(wxSocketNotify notification
)
922 // NOTE: We duplicate some of the code in _Wait, but this doesn't
923 // hurt. It has to be here because the (GSocket) event might arrive
924 // a bit delayed, and it has to be in _Wait as well because we don't
925 // know whether the Wait functions are being used.
929 case wxSOCKET_CONNECTION
:
930 m_establishing
= FALSE
;
934 // If we are in the middle of a R/W operation, do not
935 // propagate events to users. Also, filter 'late' events
936 // which are no longer valid.
939 if (m_reading
|| !GSocket_Select(m_socket
, GSOCK_INPUT_FLAG
))
943 case wxSOCKET_OUTPUT
:
944 if (m_writing
|| !GSocket_Select(m_socket
, GSOCK_OUTPUT_FLAG
))
950 m_establishing
= FALSE
;
957 // Schedule the event
959 wxSocketEventFlags flag
= 0;
960 switch (notification
)
962 case GSOCK_INPUT
: flag
= GSOCK_INPUT_FLAG
; break;
963 case GSOCK_OUTPUT
: flag
= GSOCK_OUTPUT_FLAG
; break;
964 case GSOCK_CONNECTION
: flag
= GSOCK_CONNECTION_FLAG
; break;
965 case GSOCK_LOST
: flag
= GSOCK_LOST_FLAG
; break;
967 wxLogWarning(_("wxSocket: unknown event!."));
971 if (((m_eventmask
& flag
) == flag
) && m_notify
)
975 wxSocketEvent
event(m_id
);
976 event
.m_event
= notification
;
977 event
.m_clientData
= m_clientData
;
978 event
.SetEventObject(this);
980 m_handler
->AddPendingEvent(event
);
983 #if WXWIN_COMPATIBILITY
985 m_cbk(*this, notification
, m_cdata
);
986 #endif // WXWIN_COMPATIBILITY
990 void wxSocketBase::Notify(bool notify
)
995 void wxSocketBase::SetNotify(wxSocketEventFlags flags
)
1000 void wxSocketBase::SetEventHandler(wxEvtHandler
& handler
, int id
)
1002 m_handler
= &handler
;
1006 // --------------------------------------------------------------------------
1008 // --------------------------------------------------------------------------
1010 void wxSocketBase::Pushback(const void *buffer
, wxUint32 size
)
1014 if (m_unread
== NULL
)
1015 m_unread
= malloc(size
);
1020 tmp
= malloc(m_unrd_size
+ size
);
1021 memcpy((char *)tmp
+ size
, m_unread
, m_unrd_size
);
1027 m_unrd_size
+= size
;
1029 memcpy(m_unread
, buffer
, size
);
1032 wxUint32
wxSocketBase::GetPushback(void *buffer
, wxUint32 size
, bool peek
)
1037 if (size
> (m_unrd_size
-m_unrd_cur
))
1038 size
= m_unrd_size
-m_unrd_cur
;
1040 memcpy(buffer
, (char *)m_unread
+ m_unrd_cur
, size
);
1045 if (m_unrd_size
== m_unrd_cur
)
1058 // ==========================================================================
1060 // ==========================================================================
1062 // --------------------------------------------------------------------------
1064 // --------------------------------------------------------------------------
1066 wxSocketServer::wxSocketServer(wxSockAddress
& addr_man
,
1067 wxSocketFlags flags
)
1068 : wxSocketBase(flags
, wxSOCKET_SERVER
)
1070 // Create the socket
1071 m_socket
= GSocket_new();
1076 // Setup the socket as server
1077 GSocket_SetLocal(m_socket
, addr_man
.GetAddress());
1078 if (GSocket_SetServer(m_socket
) != GSOCK_NOERROR
)
1080 GSocket_destroy(m_socket
);
1085 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
1086 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1087 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1088 wx_socket_callback
, (char *)this);
1092 // --------------------------------------------------------------------------
1094 // --------------------------------------------------------------------------
1096 bool wxSocketServer::AcceptWith(wxSocketBase
& sock
, bool wait
)
1098 GSocket
*child_socket
;
1103 // If wait == FALSE, then the call should be nonblocking.
1104 // When we are finished, we put the socket to blocking mode
1108 GSocket_SetNonBlocking(m_socket
, 1);
1110 child_socket
= GSocket_WaitConnection(m_socket
);
1113 GSocket_SetNonBlocking(m_socket
, 0);
1118 sock
.m_type
= wxSOCKET_BASE
;
1119 sock
.m_socket
= child_socket
;
1120 sock
.m_connected
= TRUE
;
1122 GSocket_SetTimeout(sock
.m_socket
, sock
.m_timeout
* 1000);
1123 GSocket_SetCallback(sock
.m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1124 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1125 wx_socket_callback
, (char *)&sock
);
1130 wxSocketBase
*wxSocketServer::Accept(bool wait
)
1132 wxSocketBase
* sock
= new wxSocketBase();
1134 sock
->SetFlags(m_flags
);
1136 if (!AcceptWith(*sock
, wait
))
1145 bool wxSocketServer::WaitForAccept(long seconds
, long milliseconds
)
1147 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
);
1150 // ==========================================================================
1152 // ==========================================================================
1154 // --------------------------------------------------------------------------
1156 // --------------------------------------------------------------------------
1158 wxSocketClient::wxSocketClient(wxSocketFlags flags
)
1159 : wxSocketBase(flags
, wxSOCKET_CLIENT
)
1163 wxSocketClient::~wxSocketClient()
1167 // --------------------------------------------------------------------------
1169 // --------------------------------------------------------------------------
1171 bool wxSocketClient::Connect(wxSockAddress
& addr_man
, bool wait
)
1177 // Shutdown and destroy the socket
1179 GSocket_destroy(m_socket
);
1182 m_socket
= GSocket_new();
1183 m_connected
= FALSE
;
1184 m_establishing
= FALSE
;
1189 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
1190 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1191 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1192 wx_socket_callback
, (char *)this);
1194 // If wait == FALSE, then the call should be nonblocking.
1195 // When we are finished, we put the socket to blocking mode
1199 GSocket_SetNonBlocking(m_socket
, 1);
1201 GSocket_SetPeer(m_socket
, addr_man
.GetAddress());
1202 err
= GSocket_Connect(m_socket
, GSOCK_STREAMED
);
1205 GSocket_SetNonBlocking(m_socket
, 0);
1207 if (err
!= GSOCK_NOERROR
)
1209 if (err
== GSOCK_WOULDBLOCK
)
1210 m_establishing
= TRUE
;
1219 bool wxSocketClient::WaitOnConnect(long seconds
, long milliseconds
)
1221 if (m_connected
) // Already connected
1224 if (!m_establishing
|| !m_socket
) // No connection in progress
1227 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
|
1231 // ==========================================================================
1233 // ==========================================================================
1235 /* NOTE: experimental stuff - might change */
1237 wxDatagramSocket::wxDatagramSocket( wxSockAddress
& addr
,
1238 wxSocketFlags flags
)
1239 : wxSocketBase( flags
, wxSOCKET_DATAGRAM
)
1241 // Create the socket
1242 m_socket
= GSocket_new();
1247 // Setup the socket as non connection oriented
1248 GSocket_SetLocal(m_socket
, addr
.GetAddress());
1249 if( GSocket_SetNonOriented(m_socket
) != GSOCK_NOERROR
)
1251 GSocket_destroy(m_socket
);
1256 // Initialize all stuff
1257 m_connected
= FALSE
;
1258 m_establishing
= FALSE
;
1259 GSocket_SetTimeout( m_socket
, m_timeout
);
1260 GSocket_SetCallback( m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1261 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1262 wx_socket_callback
, (char*)this );
1266 wxDatagramSocket
& wxDatagramSocket::RecvFrom( wxSockAddress
& addr
,
1275 wxDatagramSocket
& wxDatagramSocket::SendTo( wxSockAddress
& addr
,
1279 GSocket_SetPeer(m_socket
, addr
.GetAddress());
1284 // ==========================================================================
1286 // ==========================================================================
1288 class WXDLLEXPORT wxSocketModule
: public wxModule
1291 virtual bool OnInit()
1293 // wxSocketBase will call GSocket_Init() itself when/if needed
1297 virtual void OnExit()
1299 if ( wxSocketBase::IsInitialized() )
1300 wxSocketBase::Shutdown();
1304 DECLARE_DYNAMIC_CLASS(wxSocketModule
)
1307 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule
, wxModule
)