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 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "socket.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 // ==========================================================================
26 // Headers and constants
27 // ==========================================================================
31 #include "wx/object.h"
32 #include "wx/string.h"
35 #include "wx/module.h"
40 #include "wx/gdicmn.h" // for wxPendingDelete
43 #include "wx/sckaddr.h"
44 #include "wx/socket.h"
48 #define MAX_DISCARD_SIZE (10 * 1024)
50 // what to do within waits
51 #define PROCESS_EVENTS() wxYield()
53 // use wxPostEvent or not
54 #define USE_DELAYED_EVENTS 1
56 // --------------------------------------------------------------------------
58 // --------------------------------------------------------------------------
60 IMPLEMENT_CLASS(wxSocketBase
, wxObject
)
61 IMPLEMENT_CLASS(wxSocketServer
, wxSocketBase
)
62 IMPLEMENT_CLASS(wxSocketClient
, wxSocketBase
)
63 IMPLEMENT_CLASS(wxDatagramSocket
, wxSocketBase
)
64 IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent
, wxEvent
)
66 class wxSocketState
: public wxObject
70 wxSocketEventFlags m_neededreq
;
72 wxSocketBase::wxSockCbk m_cbk
;
76 wxSocketState() : wxObject() {}
80 // ==========================================================================
82 // ==========================================================================
84 // --------------------------------------------------------------------------
86 // --------------------------------------------------------------------------
88 wxSocketBase::wxSocketBase(wxSockFlags _flags
, wxSockType _type
) :
90 m_socket(NULL
), m_evt_handler(NULL
), m_id(-1),
91 m_flags(_flags
), m_type(_type
),
92 m_neededreq(0), m_notify_state(FALSE
),
93 m_connected(FALSE
), m_establishing(FALSE
),
94 m_reading(FALSE
), m_writing(FALSE
),
95 m_error(FALSE
), m_lcount(0), m_timeout(600), m_states(),
96 m_unread(NULL
), m_unrd_size(0), m_unrd_cur(0),
97 m_cbk(NULL
), m_cdata(NULL
)
101 wxSocketBase::wxSocketBase() :
103 m_socket(NULL
), m_evt_handler(NULL
), m_id(-1),
104 m_flags(NONE
), m_type(SOCK_UNINIT
),
105 m_neededreq(0), m_notify_state(FALSE
),
106 m_connected(FALSE
), m_establishing(FALSE
),
107 m_reading(FALSE
), m_writing(FALSE
),
108 m_error(FALSE
), m_lcount(0), m_timeout(600), m_states(),
109 m_unread(NULL
), m_unrd_size(0), m_unrd_cur(0),
110 m_cbk(NULL
), m_cdata(NULL
)
114 wxSocketBase::~wxSocketBase()
119 // Shutdown and close the socket
122 // Destroy the GSocket object
124 GSocket_destroy(m_socket
);
128 bool wxSocketBase::Destroy()
130 // Delayed destruction: the socket will be deleted during the next
131 // idle loop iteration. This ensures that all pending events have
134 m_beingDeleted = TRUE;
137 if ( !wxPendingDelete.Member(this) )
138 wxPendingDelete.Append(this);
144 // --------------------------------------------------------------------------
145 // Basic IO operations
146 // --------------------------------------------------------------------------
148 // The following IO operations update m_error and m_lcount:
149 // {Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard}
151 // TODO: Should Connect, Accept and AcceptWith update m_error?
153 bool wxSocketBase::Close()
155 // Interrupt pending waits
161 GSocket_UnsetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
162 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
);
164 // Shutdown the connection
165 GSocket_Shutdown(m_socket
);
169 m_establishing
= FALSE
;
173 wxSocketBase
& wxSocketBase::Read(char* buffer
, wxUint32 nbytes
)
178 m_lcount
= _Read(buffer
, nbytes
);
180 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
181 if (m_flags
& wxSOCKET_WAITALL
)
182 m_error
= (m_lcount
!= nbytes
);
184 m_error
= (m_lcount
== 0);
186 // Allow read events from now on
192 wxUint32
wxSocketBase::_Read(char* buffer
, wxUint32 nbytes
)
197 // Try the pushback buffer first
198 total
= GetPushback(buffer
, nbytes
, FALSE
);
202 // If the socket is invalid or we got all the data, return now
203 if (!m_socket
|| !nbytes
)
206 // Possible combinations (they are checked in this order)
208 // wxSOCKET_WAITALL | wxSOCKET_BLOCK
213 if (m_flags
& wxSOCKET_NOWAIT
)
215 GSocket_SetNonBlocking(m_socket
, TRUE
);
216 ret
= GSocket_Read(m_socket
, buffer
, nbytes
);
217 GSocket_SetNonBlocking(m_socket
, FALSE
);
222 else if (m_flags
& wxSOCKET_WAITALL
)
224 while (ret
> 0 && nbytes
> 0)
226 if (!(m_flags
& wxSOCKET_BLOCK
) && !WaitForRead())
229 ret
= GSocket_Read(m_socket
, buffer
, nbytes
);
241 if ((m_flags
& wxSOCKET_BLOCK
) || WaitForRead())
243 ret
= GSocket_Read(m_socket
, buffer
, nbytes
);
253 wxSocketBase
& wxSocketBase::ReadMsg(char* buffer
, wxUint32 nbytes
)
255 wxUint32 len
, len2
, sig
, total
;
260 unsigned char sig
[4];
261 unsigned char len
[4];
270 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
272 if (_Read((char *)&msg
, sizeof(msg
)) != sizeof(msg
))
275 sig
= (wxUint32
)msg
.sig
[0];
276 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
277 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
278 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
280 if (sig
!= 0xfeeddead)
282 wxLogWarning( _("wxSocket: invalid signature in ReadMsg."));
286 len
= (wxUint32
)msg
.len
[0];
287 len
|= (wxUint32
)(msg
.len
[1] << 8);
288 len
|= (wxUint32
)(msg
.len
[2] << 16);
289 len
|= (wxUint32
)(msg
.len
[3] << 24);
299 // Don't attemp to read if the msg was zero bytes long.
302 total
= _Read(buffer
, len
);
309 char *discard_buffer
= new char[MAX_DISCARD_SIZE
];
312 // NOTE: discarded bytes don't add to m_lcount.
315 discard_len
= ((len2
> MAX_DISCARD_SIZE
)? MAX_DISCARD_SIZE
: len2
);
316 discard_len
= _Read(discard_buffer
, (wxUint32
)discard_len
);
317 len2
-= (wxUint32
)discard_len
;
319 while ((discard_len
> 0) && len2
);
321 delete [] discard_buffer
;
326 if (_Read((char *)&msg
, sizeof(msg
)) != sizeof(msg
))
329 sig
= (wxUint32
)msg
.sig
[0];
330 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
331 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
332 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
334 if (sig
!= 0xdeadfeed)
336 wxLogWarning( _("wxSocket: invalid signature in ReadMsg."));
352 wxSocketBase
& wxSocketBase::Peek(char* buffer
, wxUint32 nbytes
)
357 m_lcount
= _Read(buffer
, nbytes
);
358 Pushback(buffer
, m_lcount
);
360 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
361 if (m_flags
& wxSOCKET_WAITALL
)
362 m_error
= (m_lcount
!= nbytes
);
364 m_error
= (m_lcount
== 0);
366 // Allow read events again
372 wxSocketBase
& wxSocketBase::Write(const char *buffer
, wxUint32 nbytes
)
377 m_lcount
= _Write(buffer
, nbytes
);
379 // If in wxSOCKET_WAITALL mode, all bytes should have been written.
380 if (m_flags
& wxSOCKET_WAITALL
)
381 m_error
= (m_lcount
!= nbytes
);
383 m_error
= (m_lcount
== 0);
385 // Allow write events again
391 wxUint32
wxSocketBase::_Write(const char *buffer
, wxUint32 nbytes
)
396 // If the socket is invalid, return immediately
400 // Possible combinations (they are checked in this order)
402 // wxSOCKET_WAITALL | wxSOCKET_BLOCK
407 if (m_flags
& wxSOCKET_NOWAIT
)
409 GSocket_SetNonBlocking(m_socket
, TRUE
);
410 ret
= GSocket_Write(m_socket
, buffer
, nbytes
);
411 GSocket_SetNonBlocking(m_socket
, FALSE
);
416 else if (m_flags
& wxSOCKET_WAITALL
)
418 while (ret
> 0 && nbytes
> 0)
420 if (!(m_flags
& wxSOCKET_BLOCK
) && !WaitForWrite())
423 ret
= GSocket_Write(m_socket
, buffer
, nbytes
);
435 if ((m_flags
& wxSOCKET_BLOCK
) || WaitForWrite())
437 ret
= GSocket_Write(m_socket
, buffer
, nbytes
);
447 wxSocketBase
& wxSocketBase::WriteMsg(const char *buffer
, wxUint32 nbytes
)
453 unsigned char sig
[4];
454 unsigned char len
[4];
463 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
465 msg
.sig
[0] = (unsigned char) 0xad;
466 msg
.sig
[1] = (unsigned char) 0xde;
467 msg
.sig
[2] = (unsigned char) 0xed;
468 msg
.sig
[3] = (unsigned char) 0xfe;
470 msg
.len
[0] = (unsigned char) (nbytes
& 0xff);
471 msg
.len
[1] = (unsigned char) ((nbytes
>> 8) & 0xff);
472 msg
.len
[2] = (unsigned char) ((nbytes
>> 16) & 0xff);
473 msg
.len
[3] = (unsigned char) ((nbytes
>> 24) & 0xff);
475 if (_Write((char *)&msg
, sizeof(msg
)) < sizeof(msg
))
478 total
= _Write(buffer
, nbytes
);
483 msg
.sig
[0] = (unsigned char) 0xed;
484 msg
.sig
[1] = (unsigned char) 0xfe;
485 msg
.sig
[2] = (unsigned char) 0xad;
486 msg
.sig
[3] = (unsigned char) 0xde;
487 msg
.len
[0] = msg
.len
[1] = msg
.len
[2] = msg
.len
[3] = (char) 0;
489 if ((_Write((char *)&msg
, sizeof(msg
))) < sizeof(msg
))
503 wxSocketBase
& wxSocketBase::Unread(const char *buffer
, wxUint32 nbytes
)
506 Pushback(buffer
, nbytes
);
514 wxSocketBase
& wxSocketBase::Discard()
517 char *buffer
= new char[MAX_DISCARD_SIZE
];
525 SetFlags(wxSOCKET_NOWAIT
);
529 ret
= _Read(buffer
, MAX_DISCARD_SIZE
);
532 while (ret
== MAX_DISCARD_SIZE
);
538 // Allow read events again
544 // --------------------------------------------------------------------------
546 // --------------------------------------------------------------------------
548 // All Wait functions poll the socket using GSocket_Select() to
549 // check for the specified combination of conditions, until one
550 // of these conditions become true, an error occurs, or the
551 // timeout elapses. The polling loop calls PROCESS_EVENTS(), so
552 // this won't block the GUI.
556 class _wxSocketInternalTimer
: public wxTimer
560 unsigned long m_new_val
;
564 *m_state
= (int)m_new_val
; // Change the value
570 bool wxSocketBase::_Wait(long seconds
, long milliseconds
,
571 wxSocketEventFlags flags
)
573 GSocketEventFlags result
;
575 _wxSocketInternalTimer timer
;
576 wxTimerRunner
runTimer(timer
);
582 // Set this to TRUE to interrupt ongoing waits
585 // Check for valid socket
589 // Check for valid timeout value
591 timeout
= seconds
* 1000 + milliseconds
;
593 timeout
= m_timeout
* 1000;
599 timer
.m_state
= &state
;
601 runTimer
.Start((int)timeout
, TRUE
);
605 // Active polling (without using events)
607 // NOTE: this duplicates some of the code in OnRequest (lost
608 // connection and connection establishment handling) but
609 // this doesn't hurt. It has to be here because the event
610 // might be a bit delayed, and it has to be in OnRequest
611 // as well because maybe the Wait functions are not being
614 // Do this at least once (important if timeout == 0, when
615 // we are just polling). Also, if just polling, do not yield.
619 result
= GSocket_Select(m_socket
, flags
| GSOCK_LOST_FLAG
);
621 // Incoming connection (server) or connection established (client)
622 if (result
& GSOCK_CONNECTION_FLAG
)
625 m_establishing
= FALSE
;
629 // Data available or output buffer ready
630 if ((result
& GSOCK_INPUT_FLAG
) || (result
& GSOCK_OUTPUT_FLAG
))
636 if (result
& GSOCK_LOST_FLAG
)
639 m_establishing
= FALSE
;
640 return (flags
& GSOCK_LOST_FLAG
);
644 if ((timeout
== 0) || (m_interrupt
))
653 bool wxSocketBase::Wait(long seconds
, long milliseconds
)
655 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
657 GSOCK_CONNECTION_FLAG
|
661 bool wxSocketBase::WaitForRead(long seconds
, long milliseconds
)
663 // Check pushback buffer before entering _Wait
667 // Note that GSOCK_INPUT_LOST has to be explicitly passed to
668 // _Wait becuase of the semantics of WaitForRead: a return
669 // value of TRUE means that a GSocket_Read call will return
670 // immediately, not that there is actually data to read.
672 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
676 bool wxSocketBase::WaitForWrite(long seconds
, long milliseconds
)
678 return _Wait(seconds
, milliseconds
, GSOCK_OUTPUT_FLAG
);
681 bool wxSocketBase::WaitForLost(long seconds
, long milliseconds
)
683 return _Wait(seconds
, milliseconds
, GSOCK_LOST_FLAG
);
686 // --------------------------------------------------------------------------
688 // --------------------------------------------------------------------------
691 // Get local or peer address
694 bool wxSocketBase::GetPeer(wxSockAddress
& addr_man
) const
701 peer
= GSocket_GetPeer(m_socket
);
702 addr_man
.SetAddress(peer
);
703 GAddress_destroy(peer
);
708 bool wxSocketBase::GetLocal(wxSockAddress
& addr_man
) const
715 local
= GSocket_GetLocal(m_socket
);
716 addr_man
.SetAddress(local
);
717 GAddress_destroy(local
);
723 // Save and restore socket state
726 void wxSocketBase::SaveState()
728 wxSocketState
*state
;
730 state
= new wxSocketState();
732 state
->m_notify_state
= m_notify_state
;
733 state
->m_neededreq
= m_neededreq
;
734 state
->m_flags
= m_flags
;
735 state
->m_cbk
= m_cbk
;
736 state
->m_cdata
= m_cdata
;
738 m_states
.Append(state
);
741 void wxSocketBase::RestoreState()
744 wxSocketState
*state
;
746 node
= m_states
.Last();
750 state
= (wxSocketState
*)node
->Data();
752 SetFlags(state
->m_flags
);
753 m_cbk
= state
->m_cbk
;
754 m_cdata
= state
->m_cdata
;
755 m_neededreq
= state
->m_neededreq
;
756 Notify(state
->m_notify_state
);
766 void wxSocketBase::SetTimeout(long seconds
)
771 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
774 void wxSocketBase::SetFlags(wxSockFlags _flags
)
779 // --------------------------------------------------------------------------
780 // Callbacks (now obsolete - use events instead)
781 // --------------------------------------------------------------------------
783 wxSocketBase::wxSockCbk
wxSocketBase::Callback(wxSockCbk cbk_
)
785 wxSockCbk old_cbk
= cbk_
;
791 char *wxSocketBase::CallbackData(char *data
)
793 char *old_data
= m_cdata
;
799 // --------------------------------------------------------------------------
801 // --------------------------------------------------------------------------
803 // All events (INPUT, OUTPUT, CONNECTION, LOST) are now always
804 // internally watched; but users will only be notified of those
805 // events they are interested in.
807 static void LINKAGEMODE
wx_socket_callback(GSocket
* WXUNUSED(socket
),
811 wxSocketBase
*sckobj
= (wxSocketBase
*)cdata
;
813 sckobj
->OnRequest((wxSocketNotify
)event
);
816 wxSocketEventFlags
wxSocketBase::EventToNotify(wxSocketNotify evt
)
820 case GSOCK_INPUT
: return GSOCK_INPUT_FLAG
;
821 case GSOCK_OUTPUT
: return GSOCK_OUTPUT_FLAG
;
822 case GSOCK_CONNECTION
: return GSOCK_CONNECTION_FLAG
;
823 case GSOCK_LOST
: return GSOCK_LOST_FLAG
;
828 void wxSocketBase::SetNotify(wxSocketEventFlags flags
)
833 void wxSocketBase::Notify(bool notify
)
835 m_notify_state
= notify
;
838 void wxSocketBase::OnRequest(wxSocketNotify req_evt
)
840 wxSocketEvent
event(m_id
);
841 wxSocketEventFlags flag
= EventToNotify(req_evt
);
843 // This duplicates some code in _Wait, but this doesn't
844 // hurt. It has to be here because we don't know whether
845 // the Wait functions will be used, and it has to be in
846 // _Wait as well because the event might be a bit delayed.
850 case wxSOCKET_CONNECTION
:
851 m_establishing
= FALSE
;
855 // If we are in the middle of a R/W operation, do not
856 // propagate events to users. Also, filter 'late' events
857 // which are no longer valid.
860 if (m_reading
|| !GSocket_Select(m_socket
, GSOCK_INPUT_FLAG
))
864 case wxSOCKET_OUTPUT
:
865 if (m_writing
|| !GSocket_Select(m_socket
, GSOCK_OUTPUT_FLAG
))
871 m_establishing
= FALSE
;
878 if (((m_neededreq
& flag
) == flag
) && m_notify_state
)
880 event
.m_socket
= this;
881 event
.m_skevt
= req_evt
;
885 #if USE_DELAYED_EVENTS
886 wxPostEvent(m_evt_handler
, event
);
892 OldOnNotify(req_evt
);
894 m_cbk(*this, req_evt
, m_cdata
);
898 void wxSocketBase::OldOnNotify(wxSocketNotify
WXUNUSED(evt
))
902 void wxSocketBase::SetEventHandler(wxEvtHandler
& h_evt
, int id
)
904 m_evt_handler
= &h_evt
;
907 SetNextHandler(&h_evt
);
910 // --------------------------------------------------------------------------
912 // --------------------------------------------------------------------------
914 void wxSocketBase::Pushback(const char *buffer
, wxUint32 size
)
918 if (m_unread
== NULL
)
919 m_unread
= (char *)malloc(size
);
924 tmp
= (char *)malloc(m_unrd_size
+ size
);
925 memcpy(tmp
+size
, m_unread
, m_unrd_size
);
933 memcpy(m_unread
, buffer
, size
);
936 wxUint32
wxSocketBase::GetPushback(char *buffer
, wxUint32 size
, bool peek
)
941 if (size
> (m_unrd_size
-m_unrd_cur
))
942 size
= m_unrd_size
-m_unrd_cur
;
944 memcpy(buffer
, (m_unread
+m_unrd_cur
), size
);
949 if (m_unrd_size
== m_unrd_cur
)
961 // ==========================================================================
963 // ==========================================================================
965 // --------------------------------------------------------------------------
967 // --------------------------------------------------------------------------
969 wxSocketServer::wxSocketServer(wxSockAddress
& addr_man
,
971 : wxSocketBase(flags
, SOCK_SERVER
)
974 m_socket
= GSocket_new();
979 // Setup the socket as server
980 GSocket_SetLocal(m_socket
, addr_man
.GetAddress());
981 if (GSocket_SetServer(m_socket
) != GSOCK_NOERROR
)
983 GSocket_destroy(m_socket
);
988 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
989 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
990 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
991 wx_socket_callback
, (char *)this);
995 // --------------------------------------------------------------------------
997 // --------------------------------------------------------------------------
999 bool wxSocketServer::AcceptWith(wxSocketBase
& sock
, bool wait
)
1001 GSocket
*child_socket
;
1006 // If wait == FALSE, then the call should be nonblocking.
1007 // When we are finished, we put the socket to blocking mode
1011 GSocket_SetNonBlocking(m_socket
, TRUE
);
1013 child_socket
= GSocket_WaitConnection(m_socket
);
1016 GSocket_SetNonBlocking(m_socket
, FALSE
);
1021 sock
.m_type
= SOCK_INTERNAL
;
1022 sock
.m_socket
= child_socket
;
1023 sock
.m_connected
= TRUE
;
1025 GSocket_SetTimeout(sock
.m_socket
, sock
.m_timeout
* 1000);
1026 GSocket_SetCallback(sock
.m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1027 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1028 wx_socket_callback
, (char *)&sock
);
1033 wxSocketBase
*wxSocketServer::Accept(bool wait
)
1035 wxSocketBase
* sock
= new wxSocketBase();
1037 sock
->SetFlags((wxSockFlags
)m_flags
);
1039 if (!AcceptWith(*sock
, wait
))
1045 bool wxSocketServer::WaitForAccept(long seconds
, long milliseconds
)
1047 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
);
1050 // ==========================================================================
1052 // ==========================================================================
1054 // --------------------------------------------------------------------------
1056 // --------------------------------------------------------------------------
1058 wxSocketClient::wxSocketClient(wxSockFlags _flags
)
1059 : wxSocketBase(_flags
, SOCK_CLIENT
)
1063 // XXX: What is this for ?
1064 wxSocketClient::~wxSocketClient()
1068 // --------------------------------------------------------------------------
1070 // --------------------------------------------------------------------------
1072 bool wxSocketClient::Connect(wxSockAddress
& addr_man
, bool wait
)
1078 // Shutdown and destroy the socket
1080 GSocket_destroy(m_socket
);
1083 m_socket
= GSocket_new();
1084 m_connected
= FALSE
;
1085 m_establishing
= FALSE
;
1090 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
1091 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1092 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1093 wx_socket_callback
, (char *)this);
1095 // If wait == FALSE, then the call should be nonblocking.
1096 // When we are finished, we put the socket to blocking mode
1100 GSocket_SetNonBlocking(m_socket
, TRUE
);
1102 GSocket_SetPeer(m_socket
, addr_man
.GetAddress());
1103 err
= GSocket_Connect(m_socket
, GSOCK_STREAMED
);
1106 GSocket_SetNonBlocking(m_socket
, FALSE
);
1108 if (err
!= GSOCK_NOERROR
)
1110 if (err
== GSOCK_WOULDBLOCK
)
1111 m_establishing
= TRUE
;
1120 bool wxSocketClient::WaitOnConnect(long seconds
, long milliseconds
)
1122 if (m_connected
) // Already connected
1125 if (!m_establishing
|| !m_socket
) // No connection in progress
1128 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
);
1131 // ==========================================================================
1133 // ==========================================================================
1135 /* NOTE: experimental stuff - might change */
1137 wxDatagramSocket::wxDatagramSocket( wxSockAddress
& addr
, wxSockFlags flags
)
1138 : wxSocketBase( flags
, SOCK_DATAGRAM
)
1140 // Create the socket
1141 m_socket
= GSocket_new();
1146 // Setup the socket as non connection oriented
1147 GSocket_SetLocal(m_socket
, addr
.GetAddress());
1148 if( GSocket_SetNonOriented(m_socket
) != GSOCK_NOERROR
)
1150 GSocket_destroy(m_socket
);
1155 // Initialize all stuff
1156 m_connected
= FALSE
;
1157 m_establishing
= FALSE
;
1158 GSocket_SetTimeout( m_socket
, m_timeout
);
1159 GSocket_SetCallback( m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1160 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1161 wx_socket_callback
, (char*)this );
1165 wxDatagramSocket
& wxDatagramSocket::RecvFrom( wxSockAddress
& addr
,
1174 wxDatagramSocket
& wxDatagramSocket::SendTo( wxSockAddress
& addr
,
1178 GSocket_SetPeer(m_socket
, addr
.GetAddress());
1183 // ==========================================================================
1185 // ==========================================================================
1187 // XXX: Should be moved to event.cpp ?
1189 wxSocketEvent::wxSocketEvent(int id
)
1192 wxEventType type
= (wxEventType
)wxEVT_SOCKET
;
1197 void wxSocketEvent::CopyObject(wxObject
& obj_d
) const
1199 wxSocketEvent
*event
= (wxSocketEvent
*)&obj_d
;
1201 wxEvent::CopyObject(obj_d
);
1203 event
->m_skevt
= m_skevt
;
1204 event
->m_socket
= m_socket
;
1207 // ==========================================================================
1209 // ==========================================================================
1211 class WXDLLEXPORT wxSocketModule
: public wxModule
1213 DECLARE_DYNAMIC_CLASS(wxSocketModule
)
1216 bool OnInit() { return GSocket_Init(); }
1217 void OnExit() { GSocket_Cleanup(); }
1220 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule
, wxModule
)