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 licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ==========================================================================
14 // ==========================================================================
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "socket.h"
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
30 #include "wx/apptrait.h"
32 #include "wx/object.h"
33 #include "wx/string.h"
36 #include "wx/module.h"
41 #include "wx/sckaddr.h"
42 #include "wx/socket.h"
44 // DLL options compatibility check:
46 WX_CHECK_BUILD_OPTIONS("wxNet")
48 // --------------------------------------------------------------------------
49 // macros and constants
50 // --------------------------------------------------------------------------
53 #define MAX_DISCARD_SIZE (10 * 1024)
55 // what to do within waits: we have 2 cases: from the main thread itself we
56 // have to call wxYield() to let the events (including the GUI events and the
57 // low-level (not wxWidgets) events from GSocket) be processed. From another
58 // thread it is enough to just call wxThread::Yield() which will give away the
59 // rest of our time slice: the explanation is that the events will be processed
60 // by the main thread anyhow, without calling wxYield(), but we don't want to
61 // eat the CPU time uselessly while sitting in the loop waiting for the data
63 #define PROCESS_EVENTS() \
65 if ( wxThread::IsMain() ) \
70 #else // !wxUSE_THREADS
71 #define PROCESS_EVENTS() wxYield()
72 #endif // wxUSE_THREADS/!wxUSE_THREADS
74 #define wxTRACE_Socket _T("wxSocket")
76 // --------------------------------------------------------------------------
78 // --------------------------------------------------------------------------
80 IMPLEMENT_CLASS(wxSocketBase
, wxObject
)
81 IMPLEMENT_CLASS(wxSocketServer
, wxSocketBase
)
82 IMPLEMENT_CLASS(wxSocketClient
, wxSocketBase
)
83 IMPLEMENT_CLASS(wxDatagramSocket
, wxSocketBase
)
84 IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent
, wxEvent
)
86 // --------------------------------------------------------------------------
88 // --------------------------------------------------------------------------
90 class wxSocketState
: public wxObject
93 wxSocketFlags m_flags
;
94 wxSocketEventFlags m_eventmask
;
99 wxSocketState() : wxObject() {}
101 DECLARE_NO_COPY_CLASS(wxSocketState
)
104 // ==========================================================================
106 // ==========================================================================
108 // --------------------------------------------------------------------------
109 // Initialization and shutdown
110 // --------------------------------------------------------------------------
112 // FIXME-MT: all this is MT-unsafe, of course, we should protect all accesses
113 // to m_countInit with a crit section
114 size_t wxSocketBase::m_countInit
= 0;
116 bool wxSocketBase::IsInitialized()
118 return m_countInit
> 0;
121 bool wxSocketBase::Initialize()
123 if ( !m_countInit
++ )
125 wxAppTraits
*traits
= wxAppConsole::GetInstance() ?
126 wxAppConsole::GetInstance()->GetTraits() : NULL
;
127 GSocketGUIFunctionsTable
*functions
=
128 traits
? traits
->GetSocketGUIFunctionsTable() : NULL
;
129 GSocket_SetGUIFunctions(functions
);
131 if ( !GSocket_Init() )
142 void wxSocketBase::Shutdown()
144 // we should be initialized
145 wxASSERT_MSG( m_countInit
, _T("extra call to Shutdown()") );
146 if ( !--m_countInit
)
152 // --------------------------------------------------------------------------
154 // --------------------------------------------------------------------------
156 void wxSocketBase::Init()
159 m_type
= wxSOCKET_UNINIT
;
170 m_beingDeleted
= FALSE
;
184 if ( !IsInitialized() )
186 // this Initialize() will be undone by wxSocketModule::OnExit(), all the
187 // other calls to it should be matched by a call to Shutdown()
192 wxSocketBase::wxSocketBase()
197 wxSocketBase::wxSocketBase(wxSocketFlags flags
, wxSocketType type
)
205 wxSocketBase::~wxSocketBase()
207 // Just in case the app called Destroy() *and* then deleted
208 // the socket immediately: don't leave dangling pointers.
209 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
211 traits
->RemoveFromPendingDelete(this);
213 // Shutdown and close the socket
217 // Destroy the GSocket object
219 GSocket_destroy(m_socket
);
221 // Free the pushback buffer
226 bool wxSocketBase::Destroy()
228 // Delayed destruction: the socket will be deleted during the next
229 // idle loop iteration. This ensures that all pending events have
231 m_beingDeleted
= TRUE
;
233 // Shutdown and close the socket
236 // Supress events from now on
239 // schedule this object for deletion
240 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
243 // let the traits object decide what to do with us
244 traits
->ScheduleForDestroy(this);
246 else // no app or no traits
248 // in wxBase we might have no app object at all, don't leak memory
255 // --------------------------------------------------------------------------
257 // --------------------------------------------------------------------------
259 // The following IO operations update m_error and m_lcount:
260 // {Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard}
262 // TODO: Should Connect, Accept and AcceptWith update m_error?
264 bool wxSocketBase::Close()
266 // Interrupt pending waits
272 GSocket_UnsetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
273 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
);
275 // Shutdown the connection
276 GSocket_Shutdown(m_socket
);
280 m_establishing
= FALSE
;
284 wxSocketBase
& wxSocketBase::Read(void* buffer
, wxUint32 nbytes
)
289 m_lcount
= _Read(buffer
, nbytes
);
291 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
292 if (m_flags
& wxSOCKET_WAITALL
)
293 m_error
= (m_lcount
!= nbytes
);
295 m_error
= (m_lcount
== 0);
297 // Allow read events from now on
303 wxUint32
wxSocketBase::_Read(void* buffer
, wxUint32 nbytes
)
307 // Try the pushback buffer first
308 total
= GetPushback(buffer
, nbytes
, FALSE
);
310 buffer
= (char *)buffer
+ total
;
312 // Return now in one of the following cases:
313 // - the socket is invalid,
314 // - we got all the data,
315 // - we got *some* data and we are not using wxSOCKET_WAITALL.
318 ((total
!= 0) && !(m_flags
& wxSOCKET_WAITALL
)) )
321 // Possible combinations (they are checked in this order)
323 // wxSOCKET_WAITALL (with or without wxSOCKET_BLOCK)
328 if (m_flags
& wxSOCKET_NOWAIT
)
330 GSocket_SetNonBlocking(m_socket
, 1);
331 ret
= GSocket_Read(m_socket
, (char *)buffer
, nbytes
);
332 GSocket_SetNonBlocking(m_socket
, 0);
343 if ( !(m_flags
& wxSOCKET_BLOCK
) && !WaitForRead() )
346 ret
= GSocket_Read(m_socket
, (char *)buffer
, nbytes
);
352 buffer
= (char *)buffer
+ ret
;
355 // If we got here and wxSOCKET_WAITALL is not set, we can leave
356 // now. Otherwise, wait until we recv all the data or until there
359 more
= (ret
> 0 && nbytes
> 0 && (m_flags
& wxSOCKET_WAITALL
));
366 wxSocketBase
& wxSocketBase::ReadMsg(void* buffer
, wxUint32 nbytes
)
368 wxUint32 len
, len2
, sig
, total
;
373 unsigned char sig
[4];
374 unsigned char len
[4];
383 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
385 if (_Read(&msg
, sizeof(msg
)) != sizeof(msg
))
388 sig
= (wxUint32
)msg
.sig
[0];
389 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
390 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
391 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
393 if (sig
!= 0xfeeddead)
395 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
399 len
= (wxUint32
)msg
.len
[0];
400 len
|= (wxUint32
)(msg
.len
[1] << 8);
401 len
|= (wxUint32
)(msg
.len
[2] << 16);
402 len
|= (wxUint32
)(msg
.len
[3] << 24);
412 // Don't attemp to read if the msg was zero bytes long.
415 total
= _Read(buffer
, len
);
422 char *discard_buffer
= new char[MAX_DISCARD_SIZE
];
425 // NOTE: discarded bytes don't add to m_lcount.
428 discard_len
= ((len2
> MAX_DISCARD_SIZE
)? MAX_DISCARD_SIZE
: len2
);
429 discard_len
= _Read(discard_buffer
, (wxUint32
)discard_len
);
430 len2
-= (wxUint32
)discard_len
;
432 while ((discard_len
> 0) && len2
);
434 delete [] discard_buffer
;
439 if (_Read(&msg
, sizeof(msg
)) != sizeof(msg
))
442 sig
= (wxUint32
)msg
.sig
[0];
443 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
444 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
445 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
447 if (sig
!= 0xdeadfeed)
449 wxLogWarning(_("wxSocket: invalid signature in ReadMsg."));
465 wxSocketBase
& wxSocketBase::Peek(void* buffer
, wxUint32 nbytes
)
470 m_lcount
= _Read(buffer
, nbytes
);
471 Pushback(buffer
, m_lcount
);
473 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
474 if (m_flags
& wxSOCKET_WAITALL
)
475 m_error
= (m_lcount
!= nbytes
);
477 m_error
= (m_lcount
== 0);
479 // Allow read events again
485 wxSocketBase
& wxSocketBase::Write(const void *buffer
, wxUint32 nbytes
)
490 m_lcount
= _Write(buffer
, nbytes
);
492 // If in wxSOCKET_WAITALL mode, all bytes should have been written.
493 if (m_flags
& wxSOCKET_WAITALL
)
494 m_error
= (m_lcount
!= nbytes
);
496 m_error
= (m_lcount
== 0);
498 // Allow write events again
504 wxUint32
wxSocketBase::_Write(const void *buffer
, wxUint32 nbytes
)
508 // If the socket is invalid or parameters are ill, return immediately
509 if (!m_socket
|| !buffer
|| !nbytes
)
512 // Possible combinations (they are checked in this order)
514 // wxSOCKET_WAITALL (with or without wxSOCKET_BLOCK)
519 if (m_flags
& wxSOCKET_NOWAIT
)
521 GSocket_SetNonBlocking(m_socket
, 1);
522 ret
= GSocket_Write(m_socket
, (const char *)buffer
, nbytes
);
523 GSocket_SetNonBlocking(m_socket
, 0);
534 if ( !(m_flags
& wxSOCKET_BLOCK
) && !WaitForWrite() )
537 ret
= GSocket_Write(m_socket
, (const char *)buffer
, nbytes
);
543 buffer
= (const char *)buffer
+ ret
;
546 // If we got here and wxSOCKET_WAITALL is not set, we can leave
547 // now. Otherwise, wait until we send all the data or until there
550 more
= (ret
> 0 && nbytes
> 0 && (m_flags
& wxSOCKET_WAITALL
));
557 wxSocketBase
& wxSocketBase::WriteMsg(const void *buffer
, wxUint32 nbytes
)
563 unsigned char sig
[4];
564 unsigned char len
[4];
572 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
574 msg
.sig
[0] = (unsigned char) 0xad;
575 msg
.sig
[1] = (unsigned char) 0xde;
576 msg
.sig
[2] = (unsigned char) 0xed;
577 msg
.sig
[3] = (unsigned char) 0xfe;
579 msg
.len
[0] = (unsigned char) (nbytes
& 0xff);
580 msg
.len
[1] = (unsigned char) ((nbytes
>> 8) & 0xff);
581 msg
.len
[2] = (unsigned char) ((nbytes
>> 16) & 0xff);
582 msg
.len
[3] = (unsigned char) ((nbytes
>> 24) & 0xff);
584 if (_Write(&msg
, sizeof(msg
)) < sizeof(msg
))
587 total
= _Write(buffer
, nbytes
);
592 msg
.sig
[0] = (unsigned char) 0xed;
593 msg
.sig
[1] = (unsigned char) 0xfe;
594 msg
.sig
[2] = (unsigned char) 0xad;
595 msg
.sig
[3] = (unsigned char) 0xde;
596 msg
.len
[0] = msg
.len
[1] = msg
.len
[2] = msg
.len
[3] = (char) 0;
598 if ((_Write(&msg
, sizeof(msg
))) < sizeof(msg
))
612 wxSocketBase
& wxSocketBase::Unread(const void *buffer
, wxUint32 nbytes
)
615 Pushback(buffer
, nbytes
);
623 wxSocketBase
& wxSocketBase::Discard()
625 char *buffer
= new char[MAX_DISCARD_SIZE
];
632 SetFlags(wxSOCKET_NOWAIT
);
636 ret
= _Read(buffer
, MAX_DISCARD_SIZE
);
639 while (ret
== MAX_DISCARD_SIZE
);
645 // Allow read events again
651 // --------------------------------------------------------------------------
653 // --------------------------------------------------------------------------
655 // All Wait functions poll the socket using GSocket_Select() to
656 // check for the specified combination of conditions, until one
657 // of these conditions become true, an error occurs, or the
658 // timeout elapses. The polling loop calls PROCESS_EVENTS(), so
659 // this won't block the GUI.
661 bool wxSocketBase::_Wait(long seconds
,
663 wxSocketEventFlags flags
)
665 GSocketEventFlags result
;
668 // Set this to TRUE to interrupt ongoing waits
671 // Check for valid socket
675 // Check for valid timeout value.
677 timeout
= seconds
* 1000 + milliseconds
;
679 timeout
= m_timeout
* 1000;
681 #if !defined(wxUSE_GUI) || !wxUSE_GUI
682 GSocket_SetTimeout(m_socket
, timeout
);
685 // Wait in an active polling loop.
687 // NOTE: We duplicate some of the code in OnRequest, but this doesn't
688 // hurt. It has to be here because the (GSocket) event might arrive
689 // a bit delayed, and it has to be in OnRequest as well because we
690 // don't know whether the Wait functions are being used.
692 // Do this at least once (important if timeout == 0, when
693 // we are just polling). Also, if just polling, do not yield.
700 result
= GSocket_Select(m_socket
, flags
| GSOCK_LOST_FLAG
);
702 // Incoming connection (server) or connection established (client)
703 if (result
& GSOCK_CONNECTION_FLAG
)
706 m_establishing
= FALSE
;
710 // Data available or output buffer ready
711 if ((result
& GSOCK_INPUT_FLAG
) || (result
& GSOCK_OUTPUT_FLAG
))
717 if (result
& GSOCK_LOST_FLAG
)
720 m_establishing
= FALSE
;
721 return (flags
& GSOCK_LOST_FLAG
) != 0;
725 if ((!timeout
) || (chrono
.Time() > timeout
) || (m_interrupt
))
734 bool wxSocketBase::Wait(long seconds
, long milliseconds
)
736 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
738 GSOCK_CONNECTION_FLAG
|
742 bool wxSocketBase::WaitForRead(long seconds
, long milliseconds
)
744 // Check pushback buffer before entering _Wait
748 // Note that GSOCK_INPUT_LOST has to be explicitly passed to
749 // _Wait becuase of the semantics of WaitForRead: a return
750 // value of TRUE means that a GSocket_Read call will return
751 // immediately, not that there is actually data to read.
753 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
758 bool wxSocketBase::WaitForWrite(long seconds
, long milliseconds
)
760 return _Wait(seconds
, milliseconds
, GSOCK_OUTPUT_FLAG
);
763 bool wxSocketBase::WaitForLost(long seconds
, long milliseconds
)
765 return _Wait(seconds
, milliseconds
, GSOCK_LOST_FLAG
);
768 // --------------------------------------------------------------------------
770 // --------------------------------------------------------------------------
773 // Get local or peer address
776 bool wxSocketBase::GetPeer(wxSockAddress
& addr_man
) const
783 peer
= GSocket_GetPeer(m_socket
);
785 // copying a null address would just trigger an assert anyway
790 addr_man
.SetAddress(peer
);
791 GAddress_destroy(peer
);
796 bool wxSocketBase::GetLocal(wxSockAddress
& addr_man
) const
803 local
= GSocket_GetLocal(m_socket
);
804 addr_man
.SetAddress(local
);
805 GAddress_destroy(local
);
811 // Save and restore socket state
814 void wxSocketBase::SaveState()
816 wxSocketState
*state
;
818 state
= new wxSocketState();
820 state
->m_flags
= m_flags
;
821 state
->m_notify
= m_notify
;
822 state
->m_eventmask
= m_eventmask
;
823 state
->m_clientData
= m_clientData
;
825 m_states
.Append(state
);
828 void wxSocketBase::RestoreState()
830 wxList::compatibility_iterator node
;
831 wxSocketState
*state
;
833 node
= m_states
.GetLast();
837 state
= (wxSocketState
*)node
->GetData();
839 m_flags
= state
->m_flags
;
840 m_notify
= state
->m_notify
;
841 m_eventmask
= state
->m_eventmask
;
842 m_clientData
= state
->m_clientData
;
844 m_states
.Erase(node
);
852 void wxSocketBase::SetTimeout(long seconds
)
857 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
860 void wxSocketBase::SetFlags(wxSocketFlags flags
)
866 // --------------------------------------------------------------------------
868 // --------------------------------------------------------------------------
870 // A note on how events are processed, which is probably the most
871 // difficult thing to get working right while keeping the same API
872 // and functionality for all platforms.
874 // When GSocket detects an event, it calls wx_socket_callback, which in
875 // turn just calls wxSocketBase::OnRequest in the corresponding wxSocket
876 // object. OnRequest does some housekeeping, and if the event is to be
877 // propagated to the user, it creates a new wxSocketEvent object and
878 // posts it. The event is not processed immediately, but delayed with
879 // AddPendingEvent instead. This is necessary in order to decouple the
880 // event processing from wx_socket_callback; otherwise, subsequent IO
881 // calls made from the user event handler would fail, as gtk callbacks
882 // are not reentrant.
884 // Note that, unlike events, user callbacks (now deprecated) are _not_
885 // decoupled from wx_socket_callback and thus they suffer from a variety
886 // of problems. Avoid them where possible and use events instead.
889 void LINKAGEMODE
wx_socket_callback(GSocket
* WXUNUSED(socket
),
890 GSocketEvent notification
,
893 wxSocketBase
*sckobj
= (wxSocketBase
*)cdata
;
895 sckobj
->OnRequest((wxSocketNotify
) notification
);
898 void wxSocketBase::OnRequest(wxSocketNotify notification
)
900 // NOTE: We duplicate some of the code in _Wait, but this doesn't
901 // hurt. It has to be here because the (GSocket) event might arrive
902 // a bit delayed, and it has to be in _Wait as well because we don't
903 // know whether the Wait functions are being used.
907 case wxSOCKET_CONNECTION
:
908 m_establishing
= FALSE
;
912 // If we are in the middle of a R/W operation, do not
913 // propagate events to users. Also, filter 'late' events
914 // which are no longer valid.
917 if (m_reading
|| !GSocket_Select(m_socket
, GSOCK_INPUT_FLAG
))
921 case wxSOCKET_OUTPUT
:
922 if (m_writing
|| !GSocket_Select(m_socket
, GSOCK_OUTPUT_FLAG
))
928 m_establishing
= FALSE
;
935 // Schedule the event
937 wxSocketEventFlags flag
= 0;
939 switch (notification
)
941 case GSOCK_INPUT
: flag
= GSOCK_INPUT_FLAG
; break;
942 case GSOCK_OUTPUT
: flag
= GSOCK_OUTPUT_FLAG
; break;
943 case GSOCK_CONNECTION
: flag
= GSOCK_CONNECTION_FLAG
; break;
944 case GSOCK_LOST
: flag
= GSOCK_LOST_FLAG
; break;
946 wxLogWarning(_("wxSocket: unknown event!."));
950 if (((m_eventmask
& flag
) == flag
) && m_notify
)
954 wxSocketEvent
event(m_id
);
955 event
.m_event
= notification
;
956 event
.m_clientData
= m_clientData
;
957 event
.SetEventObject(this);
959 m_handler
->AddPendingEvent(event
);
964 void wxSocketBase::Notify(bool notify
)
969 void wxSocketBase::SetNotify(wxSocketEventFlags flags
)
974 void wxSocketBase::SetEventHandler(wxEvtHandler
& handler
, int id
)
976 m_handler
= &handler
;
980 // --------------------------------------------------------------------------
982 // --------------------------------------------------------------------------
984 void wxSocketBase::Pushback(const void *buffer
, wxUint32 size
)
988 if (m_unread
== NULL
)
989 m_unread
= malloc(size
);
994 tmp
= malloc(m_unrd_size
+ size
);
995 memcpy((char *)tmp
+ size
, m_unread
, m_unrd_size
);
1001 m_unrd_size
+= size
;
1003 memcpy(m_unread
, buffer
, size
);
1006 wxUint32
wxSocketBase::GetPushback(void *buffer
, wxUint32 size
, bool peek
)
1011 if (size
> (m_unrd_size
-m_unrd_cur
))
1012 size
= m_unrd_size
-m_unrd_cur
;
1014 memcpy(buffer
, (char *)m_unread
+ m_unrd_cur
, size
);
1019 if (m_unrd_size
== m_unrd_cur
)
1032 // ==========================================================================
1034 // ==========================================================================
1036 // --------------------------------------------------------------------------
1038 // --------------------------------------------------------------------------
1040 wxSocketServer::wxSocketServer(wxSockAddress
& addr_man
,
1041 wxSocketFlags flags
)
1042 : wxSocketBase(flags
, wxSOCKET_SERVER
)
1044 wxLogTrace( wxTRACE_Socket
, _T("Opening wxSocketServer") );
1046 m_socket
= GSocket_new();
1050 wxLogTrace( wxTRACE_Socket
, _T("*** GSocket_new failed") );
1054 // Setup the socket as server
1056 GSocket_SetLocal(m_socket
, addr_man
.GetAddress());
1057 if (GSocket_SetServer(m_socket
) != GSOCK_NOERROR
)
1059 GSocket_destroy(m_socket
);
1062 wxLogTrace( wxTRACE_Socket
, _T("*** GSocket_SetServer failed") );
1066 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
1067 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1068 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1069 wx_socket_callback
, (char *)this);
1072 // --------------------------------------------------------------------------
1074 // --------------------------------------------------------------------------
1076 bool wxSocketServer::AcceptWith(wxSocketBase
& sock
, bool wait
)
1078 GSocket
*child_socket
;
1083 // If wait == FALSE, then the call should be nonblocking.
1084 // When we are finished, we put the socket to blocking mode
1088 GSocket_SetNonBlocking(m_socket
, 1);
1090 child_socket
= GSocket_WaitConnection(m_socket
);
1093 GSocket_SetNonBlocking(m_socket
, 0);
1098 sock
.m_type
= wxSOCKET_BASE
;
1099 sock
.m_socket
= child_socket
;
1100 sock
.m_connected
= TRUE
;
1102 GSocket_SetTimeout(sock
.m_socket
, sock
.m_timeout
* 1000);
1103 GSocket_SetCallback(sock
.m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1104 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1105 wx_socket_callback
, (char *)&sock
);
1110 wxSocketBase
*wxSocketServer::Accept(bool wait
)
1112 wxSocketBase
* sock
= new wxSocketBase();
1114 sock
->SetFlags(m_flags
);
1116 if (!AcceptWith(*sock
, wait
))
1125 bool wxSocketServer::WaitForAccept(long seconds
, long milliseconds
)
1127 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
);
1130 bool wxSocketBase::GetOption(int level
, int optname
, void *optval
, int *optlen
)
1132 if (GSocket_GetSockOpt(m_socket
, level
, optname
, optval
, optlen
)
1140 bool wxSocketBase::SetOption(int level
, int optname
, const void *optval
,
1143 if (GSocket_SetSockOpt(m_socket
, level
, optname
, optval
, optlen
)
1151 // ==========================================================================
1153 // ==========================================================================
1155 // --------------------------------------------------------------------------
1157 // --------------------------------------------------------------------------
1159 wxSocketClient::wxSocketClient(wxSocketFlags flags
)
1160 : wxSocketBase(flags
, wxSOCKET_CLIENT
)
1164 wxSocketClient::~wxSocketClient()
1168 // --------------------------------------------------------------------------
1170 // --------------------------------------------------------------------------
1172 bool wxSocketClient::Connect(wxSockAddress
& addr_man
, bool wait
)
1178 // Shutdown and destroy the socket
1180 GSocket_destroy(m_socket
);
1183 m_socket
= GSocket_new();
1184 m_connected
= FALSE
;
1185 m_establishing
= FALSE
;
1190 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
1191 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1192 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1193 wx_socket_callback
, (char *)this);
1195 // If wait == FALSE, then the call should be nonblocking.
1196 // When we are finished, we put the socket to blocking mode
1200 GSocket_SetNonBlocking(m_socket
, 1);
1202 GSocket_SetPeer(m_socket
, addr_man
.GetAddress());
1203 err
= GSocket_Connect(m_socket
, GSOCK_STREAMED
);
1206 GSocket_SetNonBlocking(m_socket
, 0);
1208 if (err
!= GSOCK_NOERROR
)
1210 if (err
== GSOCK_WOULDBLOCK
)
1211 m_establishing
= TRUE
;
1220 bool wxSocketClient::WaitOnConnect(long seconds
, long milliseconds
)
1222 if (m_connected
) // Already connected
1225 if (!m_establishing
|| !m_socket
) // No connection in progress
1228 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
|
1232 // ==========================================================================
1234 // ==========================================================================
1236 /* NOTE: experimental stuff - might change */
1238 wxDatagramSocket::wxDatagramSocket( wxSockAddress
& addr
,
1239 wxSocketFlags flags
)
1240 : wxSocketBase( flags
, wxSOCKET_DATAGRAM
)
1242 // Create the socket
1243 m_socket
= GSocket_new();
1247 wxASSERT_MSG( 0, _T("datagram socket not new'd") );
1250 // Setup the socket as non connection oriented
1251 GSocket_Unstreamed(m_socket
);
1252 GSocket_SetLocal(m_socket
, addr
.GetAddress());
1253 if( GSocket_SetNonOriented(m_socket
) != GSOCK_NOERROR
)
1255 GSocket_destroy(m_socket
);
1260 // Initialize all stuff
1261 m_connected
= FALSE
;
1262 m_establishing
= FALSE
;
1263 GSocket_SetTimeout( m_socket
, m_timeout
);
1264 GSocket_SetCallback( m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1265 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1266 wx_socket_callback
, (char*)this );
1270 wxDatagramSocket
& wxDatagramSocket::RecvFrom( wxSockAddress
& addr
,
1279 wxDatagramSocket
& wxDatagramSocket::SendTo( wxSockAddress
& addr
,
1283 GSocket_SetPeer(m_socket
, addr
.GetAddress());
1288 // ==========================================================================
1290 // ==========================================================================
1292 class wxSocketModule
: public wxModule
1295 virtual bool OnInit()
1297 // wxSocketBase will call GSocket_Init() itself when/if needed
1301 virtual void OnExit()
1303 if ( wxSocketBase::IsInitialized() )
1304 wxSocketBase::Shutdown();
1308 DECLARE_DYNAMIC_CLASS(wxSocketModule
)
1311 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule
, wxModule
)