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 /////////////////////////////////////////////////////////////////////////////
27 /////////////////////////////////////////////////////////////////////////////
31 #include "wx/object.h"
32 #include "wx/string.h"
35 #include "wx/module.h"
39 /////////////////////////////////////////////////////////////////////////////
41 /////////////////////////////////////////////////////////////////////////////
43 #include "wx/sckaddr.h"
44 #include "wx/socket.h"
47 #define MAX_DISCARD_SIZE (10 * 1024)
49 // what to do within waits
50 #define PROCESS_EVENTS() wxYield()
52 // use wxPostEvent or not
53 #define EXPERIMENTAL_USE_POST 1
55 // --------------------------------------------------------------
57 // --------------------------------------------------------------
59 IMPLEMENT_CLASS(wxSocketBase
, wxObject
)
60 IMPLEMENT_CLASS(wxSocketServer
, wxSocketBase
)
61 IMPLEMENT_CLASS(wxSocketClient
, wxSocketBase
)
62 IMPLEMENT_CLASS(wxDatagramSocket
, wxSocketBase
)
63 IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent
, wxEvent
)
65 class wxSocketState
: public wxObject
69 GSocketEventFlags m_neededreq
;
71 wxSocketBase::wxSockCbk m_cbk
;
75 wxSocketState() : wxObject() {}
78 // --------------------------------------------------------------
79 // wxSocketBase ctor and dtor
80 // --------------------------------------------------------------
82 wxSocketBase::wxSocketBase(wxSockFlags _flags
, wxSockType _type
) :
84 m_socket(NULL
), m_evt_handler(NULL
), m_id(-1),
85 m_flags(_flags
), m_type(_type
),
86 m_neededreq(0), m_notify_state(FALSE
),
87 m_connected(FALSE
), m_establishing(FALSE
),
88 m_reading(FALSE
), m_writing(FALSE
),
89 m_error(FALSE
), m_lcount(0), m_timeout(600), m_states(),
90 m_unread(NULL
), m_unrd_size(0), m_unrd_cur(0),
91 m_cbk(NULL
), m_cdata(NULL
)
95 wxSocketBase::wxSocketBase() :
97 m_socket(NULL
), m_evt_handler(NULL
), m_id(-1),
98 m_flags(NONE
), m_type(SOCK_UNINIT
),
99 m_neededreq(0), m_notify_state(FALSE
),
100 m_connected(FALSE
), m_establishing(FALSE
),
101 m_reading(FALSE
), m_writing(FALSE
),
102 m_error(FALSE
), m_lcount(0), m_timeout(600), m_states(),
103 m_unread(NULL
), m_unrd_size(0), m_unrd_cur(0),
104 m_cbk(NULL
), m_cdata(NULL
)
108 wxSocketBase::~wxSocketBase()
113 // Shutdown and close the socket
116 // Destroy the GSocket object
118 GSocket_destroy(m_socket
);
121 bool wxSocketBase::Close()
123 // Interrupt pending waits
129 GSocket_UnsetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
130 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
);
132 // Shutdown the connection
133 GSocket_Shutdown(m_socket
);
136 m_connected
= FALSE
; // (GRG)
137 m_establishing
= FALSE
;
141 // --------------------------------------------------------------
142 // wxSocketBase basic IO operations
143 // --------------------------------------------------------------
145 // All IO operations {Read, Write, ReadMsg, WriteMsg, Peek,
146 // Unread, Discard} update m_error and m_lcount.
148 // TODO: Should Connect, Accept and AcceptWith update m_error?
150 class _wxSocketInternalTimer
: public wxTimer
154 unsigned long m_new_val
;
158 *m_state
= (int)m_new_val
; // Change the value
162 wxSocketBase
& wxSocketBase::Read(char* buffer
, wxUint32 nbytes
)
167 m_lcount
= _Read(buffer
, nbytes
);
169 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
170 if (m_flags
& wxSOCKET_WAITALL
)
171 m_error
= (m_lcount
!= nbytes
);
173 m_error
= (m_lcount
== 0);
175 // Allow read events from now on
181 wxUint32
wxSocketBase::_Read(char* buffer
, wxUint32 nbytes
)
186 // Try the pushback buffer first
187 total
= GetPushback(buffer
, nbytes
, FALSE
);
191 // If the socket is invalid or we got all the data, return now (GRG)
192 if (!m_socket
|| !nbytes
)
195 // Possible combinations (they are checked in this order)
197 // wxSOCKET_WAITALL | wxSOCKET_BLOCK
202 if (m_flags
& wxSOCKET_NOWAIT
)
204 GSocket_SetNonBlocking(m_socket
, TRUE
);
205 ret
= GSocket_Read(m_socket
, buffer
, nbytes
);
206 GSocket_SetNonBlocking(m_socket
, FALSE
);
211 else if (m_flags
& wxSOCKET_WAITALL
)
213 while (ret
> 0 && nbytes
> 0)
215 if (!(m_flags
& wxSOCKET_BLOCK
) && !WaitForRead())
218 ret
= GSocket_Read(m_socket
, buffer
, nbytes
);
230 if ((m_flags
& wxSOCKET_BLOCK
) || WaitForRead())
232 ret
= GSocket_Read(m_socket
, buffer
, nbytes
);
242 wxSocketBase
& wxSocketBase::ReadMsg(char* buffer
, wxUint32 nbytes
)
244 wxUint32 len
, len2
, sig
, total
;
249 unsigned char sig
[4];
250 unsigned char len
[4];
259 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
261 if (_Read((char *)&msg
, sizeof(msg
)) != sizeof(msg
))
264 sig
= (wxUint32
)msg
.sig
[0];
265 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
266 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
267 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
269 if (sig
!= 0xfeeddead)
271 wxLogWarning( _("wxSocket: invalid signature in ReadMsg."));
275 len
= (wxUint32
)msg
.len
[0];
276 len
|= (wxUint32
)(msg
.len
[1] << 8);
277 len
|= (wxUint32
)(msg
.len
[2] << 16);
278 len
|= (wxUint32
)(msg
.len
[3] << 24);
288 // Don't attemp to read if the msg was zero bytes long.
291 total
= _Read(buffer
, len
);
298 char *discard_buffer
= new char[MAX_DISCARD_SIZE
];
301 // NOTE: discarded bytes don't add to m_lcount.
304 discard_len
= ((len2
> MAX_DISCARD_SIZE
)? MAX_DISCARD_SIZE
: len2
);
305 discard_len
= _Read(discard_buffer
, (wxUint32
)discard_len
);
306 len2
-= (wxUint32
)discard_len
;
308 while ((discard_len
> 0) && len2
);
310 delete [] discard_buffer
;
315 if (_Read((char *)&msg
, sizeof(msg
)) != sizeof(msg
))
318 sig
= (wxUint32
)msg
.sig
[0];
319 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
320 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
321 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
323 if (sig
!= 0xdeadfeed)
325 wxLogWarning( _("wxSocket: invalid signature in ReadMsg."));
341 wxSocketBase
& wxSocketBase::Peek(char* buffer
, wxUint32 nbytes
)
346 m_lcount
= _Read(buffer
, nbytes
);
347 Pushback(buffer
, m_lcount
);
349 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
350 if (m_flags
& wxSOCKET_WAITALL
)
351 m_error
= (m_lcount
!= nbytes
);
353 m_error
= (m_lcount
== 0);
355 // Allow read events again
361 wxSocketBase
& wxSocketBase::Write(const char *buffer
, wxUint32 nbytes
)
366 m_lcount
= _Write(buffer
, nbytes
);
368 // If in wxSOCKET_WAITALL mode, all bytes should have been written.
369 if (m_flags
& wxSOCKET_WAITALL
)
370 m_error
= (m_lcount
!= nbytes
);
372 m_error
= (m_lcount
== 0);
374 // Allow write events again
380 wxUint32
wxSocketBase::_Write(const char *buffer
, wxUint32 nbytes
)
385 // If the socket is invalid, return immediately (GRG)
389 // Possible combinations (they are checked in this order)
391 // wxSOCKET_WAITALL | wxSOCKET_BLOCK
396 if (m_flags
& wxSOCKET_NOWAIT
)
398 GSocket_SetNonBlocking(m_socket
, TRUE
);
399 ret
= GSocket_Write(m_socket
, buffer
, nbytes
);
400 GSocket_SetNonBlocking(m_socket
, FALSE
);
405 else if (m_flags
& wxSOCKET_WAITALL
)
407 while (ret
> 0 && nbytes
> 0)
409 if (!(m_flags
& wxSOCKET_BLOCK
) && !WaitForWrite())
412 ret
= GSocket_Write(m_socket
, buffer
, nbytes
);
424 if ((m_flags
& wxSOCKET_BLOCK
) || WaitForWrite())
426 ret
= GSocket_Write(m_socket
, buffer
, nbytes
);
436 wxSocketBase
& wxSocketBase::WriteMsg(const char *buffer
, wxUint32 nbytes
)
442 unsigned char sig
[4];
443 unsigned char len
[4];
452 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
454 msg
.sig
[0] = (unsigned char) 0xad;
455 msg
.sig
[1] = (unsigned char) 0xde;
456 msg
.sig
[2] = (unsigned char) 0xed;
457 msg
.sig
[3] = (unsigned char) 0xfe;
459 msg
.len
[0] = (unsigned char) (nbytes
& 0xff);
460 msg
.len
[1] = (unsigned char) ((nbytes
>> 8) & 0xff);
461 msg
.len
[2] = (unsigned char) ((nbytes
>> 16) & 0xff);
462 msg
.len
[3] = (unsigned char) ((nbytes
>> 24) & 0xff);
464 if (_Write((char *)&msg
, sizeof(msg
)) < sizeof(msg
))
467 total
= _Write(buffer
, nbytes
);
472 msg
.sig
[0] = (unsigned char) 0xed;
473 msg
.sig
[1] = (unsigned char) 0xfe;
474 msg
.sig
[2] = (unsigned char) 0xad;
475 msg
.sig
[3] = (unsigned char) 0xde;
476 msg
.len
[0] = msg
.len
[1] = msg
.len
[2] = msg
.len
[3] = (char) 0;
478 if ((_Write((char *)&msg
, sizeof(msg
))) < sizeof(msg
))
492 wxSocketBase
& wxSocketBase::Unread(const char *buffer
, wxUint32 nbytes
)
495 Pushback(buffer
, nbytes
);
503 wxSocketBase
& wxSocketBase::Discard()
506 char *buffer
= new char[MAX_DISCARD_SIZE
];
514 SetFlags(wxSOCKET_NOWAIT
);
518 ret
= _Read(buffer
, MAX_DISCARD_SIZE
);
521 while (ret
== MAX_DISCARD_SIZE
);
527 // Allow read events again
533 // --------------------------------------------------------------
534 // wxSocketBase get local or peer addresses
535 // --------------------------------------------------------------
537 bool wxSocketBase::GetPeer(wxSockAddress
& addr_man
) const
544 peer
= GSocket_GetPeer(m_socket
);
545 addr_man
.SetAddress(peer
);
546 GAddress_destroy(peer
);
551 bool wxSocketBase::GetLocal(wxSockAddress
& addr_man
) const
558 local
= GSocket_GetLocal(m_socket
);
559 addr_man
.SetAddress(local
);
560 GAddress_destroy(local
);
565 // --------------------------------------------------------------
566 // wxSocketBase save and restore socket state
567 // --------------------------------------------------------------
569 void wxSocketBase::SaveState()
571 wxSocketState
*state
;
573 state
= new wxSocketState();
575 state
->m_notify_state
= m_notify_state
;
576 state
->m_neededreq
= m_neededreq
;
577 state
->m_flags
= m_flags
;
578 state
->m_cbk
= m_cbk
;
579 state
->m_cdata
= m_cdata
;
581 m_states
.Append(state
);
584 void wxSocketBase::RestoreState()
587 wxSocketState
*state
;
589 node
= m_states
.Last();
593 state
= (wxSocketState
*)node
->Data();
595 SetFlags(state
->m_flags
);
596 m_cbk
= state
->m_cbk
;
597 m_cdata
= state
->m_cdata
;
598 m_neededreq
= state
->m_neededreq
;
599 Notify(state
->m_notify_state
);
606 // --------------------------------------------------------------
607 // wxSocketBase Wait functions
608 // --------------------------------------------------------------
610 // These WaitXXX unctions do not depend on the event system any
611 // longer; instead, they poll the socket, using GSocket_Select()
612 // to check for the specified combination of event flags, until
613 // an event occurs or until the timeout ellapses. The polling
614 // loop calls PROCESS_EVENTS(), so this won't block the GUI.
616 bool wxSocketBase::_Wait(long seconds
, long milliseconds
,
617 wxSocketEventFlags flags
)
619 GSocketEventFlags result
;
620 _wxSocketInternalTimer timer
;
624 // Set this to TRUE to interrupt ongoing waits
627 // Check for valid socket (GRG)
631 // Check for valid timeout value
633 timeout
= seconds
* 1000 + milliseconds
;
635 timeout
= m_timeout
* 1000;
640 timer
.m_state
= &state
;
642 timer
.Start((int)timeout
, TRUE
);
645 // Active polling (without using events)
647 // NOTE: this duplicates some of the code in OnRequest (lost
648 // connection and connection establishment handling) but this
649 // doesn't hurt. It has to be here because the event might
650 // be a bit delayed, and it has to be in OnRequest as well
651 // because maybe the WaitXXX functions are not being used.
653 // Do this at least once (important if timeout == 0, when
654 // we are just polling). Also, if just polling, do not yield.
658 result
= GSocket_Select(m_socket
, flags
| GSOCK_LOST_FLAG
);
660 // Incoming connection (server) or connection established (client)
661 if (result
& GSOCK_CONNECTION_FLAG
)
665 m_establishing
= FALSE
;
669 // Data available or output buffer ready
670 if ((result
& GSOCK_INPUT_FLAG
) || (result
& GSOCK_OUTPUT_FLAG
))
677 if (result
& GSOCK_LOST_FLAG
)
680 m_connected
= FALSE
; // (GRG)
681 m_establishing
= FALSE
;
682 return (flags
& GSOCK_LOST_FLAG
); // (GRG) <--- Maybe here???
686 if ((timeout
== 0) || (m_interrupt
))
696 bool wxSocketBase::Wait(long seconds
, long milliseconds
)
698 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
700 GSOCK_CONNECTION_FLAG
|
704 bool wxSocketBase::WaitForRead(long seconds
, long milliseconds
)
706 // Check pushback buffer
710 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
);
713 bool wxSocketBase::WaitForWrite(long seconds
, long milliseconds
)
715 return _Wait(seconds
, milliseconds
, GSOCK_OUTPUT_FLAG
);
718 bool wxSocketBase::WaitForLost(long seconds
, long milliseconds
)
720 return _Wait(seconds
, milliseconds
, GSOCK_LOST_FLAG
);
723 void wxSocketBase::SetTimeout(long seconds
)
728 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
731 // --------------------------------------------------------------
732 // wxSocketBase flags
733 // --------------------------------------------------------------
735 void wxSocketBase::SetFlags(wxSockFlags _flags
)
740 // --------------------------------------------------------------
741 // wxSocketBase callback management
742 // --------------------------------------------------------------
744 wxSocketBase::wxSockCbk
wxSocketBase::Callback(wxSockCbk cbk_
)
746 wxSockCbk old_cbk
= cbk_
;
752 char *wxSocketBase::CallbackData(char *data
)
754 char *old_data
= m_cdata
;
760 // --------------------------------------------------------------
761 // wxSocketBase automatic notifier
762 // --------------------------------------------------------------
764 // All events (INPUT, OUTPUT, CONNECTION, LOST) are now always
765 // internally watched; but users will only be notified of those
766 // events they are interested in.
768 static void LINKAGEMODE
wx_socket_callback(GSocket
* WXUNUSED(socket
),
769 GSocketEvent event
, char *cdata
)
771 wxSocketBase
*sckobj
= (wxSocketBase
*)cdata
;
773 sckobj
->OnRequest((wxSocketNotify
)event
);
776 wxSocketEventFlags
wxSocketBase::EventToNotify(wxSocketNotify evt
)
780 case GSOCK_INPUT
: return GSOCK_INPUT_FLAG
;
781 case GSOCK_OUTPUT
: return GSOCK_OUTPUT_FLAG
;
782 case GSOCK_CONNECTION
: return GSOCK_CONNECTION_FLAG
;
783 case GSOCK_LOST
: return GSOCK_LOST_FLAG
;
788 void wxSocketBase::SetNotify(wxSocketEventFlags flags
)
793 void wxSocketBase::Notify(bool notify
)
795 m_notify_state
= notify
;
798 void wxSocketBase::OnRequest(wxSocketNotify req_evt
)
800 wxSocketEvent
event(m_id
);
801 wxSocketEventFlags flag
= EventToNotify(req_evt
);
803 // This duplicates some code in _Wait(), but this doesn't
804 // hurt. It has to be here because we don't know whether
805 // WaitXXX will be used, and it has to be in _Wait as well
806 // because the event might be a bit delayed.
810 case wxSOCKET_CONNECTION
:
811 m_establishing
= FALSE
;
815 // If we are in the middle of a R/W operation, do not
816 // propagate events to users. Also, filter 'late' events
817 // which are no longer valid.
820 if (m_reading
|| !GSocket_Select(m_socket
, GSOCK_INPUT_FLAG
))
824 case wxSOCKET_OUTPUT
:
825 if (m_writing
|| !GSocket_Select(m_socket
, GSOCK_OUTPUT_FLAG
))
831 m_establishing
= FALSE
;
838 if (((m_neededreq
& flag
) == flag
) && m_notify_state
)
840 event
.m_socket
= this;
841 event
.m_skevt
= req_evt
;
844 #if EXPERIMENTAL_USE_POST
845 wxPostEvent(m_evt_handler
, event
);
850 OldOnNotify(req_evt
);
852 m_cbk(*this, req_evt
, m_cdata
);
856 void wxSocketBase::OldOnNotify(wxSocketNotify
WXUNUSED(evt
))
860 // --------------------------------------------------------------
861 // wxSocketBase set event handler
862 // --------------------------------------------------------------
864 void wxSocketBase::SetEventHandler(wxEvtHandler
& h_evt
, int id
)
866 m_evt_handler
= &h_evt
;
869 SetNextHandler(&h_evt
);
872 // --------------------------------------------------------------
873 // wxSocketBase pushback
874 // --------------------------------------------------------------
876 void wxSocketBase::Pushback(const char *buffer
, wxUint32 size
)
880 if (m_unread
== NULL
)
881 m_unread
= (char *)malloc(size
);
886 tmp
= (char *)malloc(m_unrd_size
+ size
);
887 memcpy(tmp
+size
, m_unread
, m_unrd_size
);
895 memcpy(m_unread
, buffer
, size
);
898 wxUint32
wxSocketBase::GetPushback(char *buffer
, wxUint32 size
, bool peek
)
903 if (size
> (m_unrd_size
-m_unrd_cur
))
904 size
= m_unrd_size
-m_unrd_cur
;
906 memcpy(buffer
, (m_unread
+m_unrd_cur
), size
);
911 if (m_unrd_size
== m_unrd_cur
)
923 // --------------------------------------------------------------
925 // --------------------------------------------------------------
927 // --------------------------------------------------------------
928 // wxSocketServer ctor and dtor
929 // --------------------------------------------------------------
931 wxSocketServer::wxSocketServer(wxSockAddress
& addr_man
,
933 wxSocketBase(flags
, SOCK_SERVER
)
936 m_socket
= GSocket_new();
941 // Setup the socket as server
942 GSocket_SetLocal(m_socket
, addr_man
.GetAddress());
943 if (GSocket_SetServer(m_socket
) != GSOCK_NOERROR
)
945 GSocket_destroy(m_socket
);
950 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
951 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
952 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
953 wx_socket_callback
, (char *)this);
957 // --------------------------------------------------------------
958 // wxSocketServer Accept
959 // --------------------------------------------------------------
961 bool wxSocketServer::AcceptWith(wxSocketBase
& sock
, bool wait
)
963 GSocket
*child_socket
;
968 // If wait == FALSE, then the call should be nonblocking.
969 // When we are finished, we put the socket to blocking mode
973 GSocket_SetNonBlocking(m_socket
, TRUE
);
975 child_socket
= GSocket_WaitConnection(m_socket
);
978 GSocket_SetNonBlocking(m_socket
, FALSE
);
983 sock
.m_type
= SOCK_INTERNAL
;
984 sock
.m_socket
= child_socket
;
985 sock
.m_connected
= TRUE
;
987 GSocket_SetTimeout(sock
.m_socket
, sock
.m_timeout
* 1000);
988 GSocket_SetCallback(sock
.m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
989 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
990 wx_socket_callback
, (char *)&sock
);
995 wxSocketBase
*wxSocketServer::Accept(bool wait
)
997 wxSocketBase
* sock
= new wxSocketBase();
999 sock
->SetFlags((wxSockFlags
)m_flags
);
1001 if (!AcceptWith(*sock
, wait
))
1007 bool wxSocketServer::WaitForAccept(long seconds
, long milliseconds
)
1009 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
);
1012 // --------------------------------------------------------------
1014 // --------------------------------------------------------------
1016 // --------------------------------------------------------------
1017 // wxSocketClient ctor and dtor
1018 // --------------------------------------------------------------
1020 wxSocketClient::wxSocketClient(wxSockFlags _flags
) :
1021 wxSocketBase(_flags
, SOCK_CLIENT
)
1025 wxSocketClient::~wxSocketClient()
1029 // --------------------------------------------------------------
1030 // wxSocketClient Connect functions
1031 // --------------------------------------------------------------
1033 bool wxSocketClient::Connect(wxSockAddress
& addr_man
, bool wait
)
1037 if (m_socket
) // (GRG)
1039 // Shutdown and destroy the socket
1041 GSocket_destroy(m_socket
);
1044 m_socket
= GSocket_new();
1045 m_connected
= FALSE
;
1046 m_establishing
= FALSE
;
1051 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
1052 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1053 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1054 wx_socket_callback
, (char *)this);
1056 // If wait == FALSE, then the call should be nonblocking.
1057 // When we are finished, we put the socket to blocking mode
1061 GSocket_SetNonBlocking(m_socket
, TRUE
);
1063 GSocket_SetPeer(m_socket
, addr_man
.GetAddress());
1064 err
= GSocket_Connect(m_socket
, GSOCK_STREAMED
);
1067 GSocket_SetNonBlocking(m_socket
, FALSE
);
1069 if (err
!= GSOCK_NOERROR
)
1071 if (err
== GSOCK_WOULDBLOCK
)
1072 m_establishing
= TRUE
;
1081 bool wxSocketClient::WaitOnConnect(long seconds
, long milliseconds
)
1083 if (m_connected
) // Already connected
1086 if (!m_establishing
|| !m_socket
) // No connection in progress
1089 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
);
1092 // --------------------------------------------------------------
1094 // --------------------------------------------------------------
1096 /* NOTE: experimental stuff - might change */
1098 wxDatagramSocket::wxDatagramSocket( wxSockAddress
& addr
, wxSockFlags flags
)
1099 : wxSocketBase( flags
, SOCK_DATAGRAM
)
1101 // Create the socket
1102 m_socket
= GSocket_new();
1107 // Setup the socket as non connection oriented
1108 GSocket_SetLocal(m_socket
, addr
.GetAddress());
1109 if( GSocket_SetNonOriented(m_socket
) != GSOCK_NOERROR
)
1111 GSocket_destroy(m_socket
);
1116 // Initialize all stuff
1117 m_connected
= FALSE
;
1118 m_establishing
= FALSE
;
1119 GSocket_SetTimeout( m_socket
, m_timeout
);
1120 GSocket_SetCallback( m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1121 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1122 wx_socket_callback
, (char*)this );
1126 wxDatagramSocket
& wxDatagramSocket::RecvFrom( wxSockAddress
& addr
,
1135 wxDatagramSocket
& wxDatagramSocket::SendTo( wxSockAddress
& addr
,
1139 GSocket_SetPeer(m_socket
, addr
.GetAddress());
1144 // --------------------------------------------------------------
1146 // --------------------------------------------------------------
1148 // XXX: Should be moved to event.cpp ?
1150 wxSocketEvent::wxSocketEvent(int id
)
1153 wxEventType type
= (wxEventType
)wxEVT_SOCKET
;
1158 void wxSocketEvent::CopyObject(wxObject
& obj_d
) const
1160 wxSocketEvent
*event
= (wxSocketEvent
*)&obj_d
;
1162 wxEvent::CopyObject(obj_d
);
1164 event
->m_skevt
= m_skevt
;
1165 event
->m_socket
= m_socket
;
1168 // --------------------------------------------------------------------------
1170 // --------------------------------------------------------------------------
1172 class WXDLLEXPORT wxSocketModule
: public wxModule
1174 DECLARE_DYNAMIC_CLASS(wxSocketModule
)
1177 bool OnInit() { return GSocket_Init(); }
1178 void OnExit() { GSocket_Cleanup(); }
1181 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule
, wxModule
)