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
);
135 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 // we try this even if the connection has already been closed.
187 total
= GetPushback(buffer
, nbytes
, FALSE
);
191 // If the socket is not connected, or we have got the whole
192 // needed buffer, return immedately
193 if (!m_socket
|| !m_connected
|| !nbytes
)
196 // Possible combinations (they are checked in this order)
198 // wxSOCKET_WAITALL | wxSOCKET_BLOCK
203 if (m_flags
& wxSOCKET_NOWAIT
)
205 GSocket_SetNonBlocking(m_socket
, TRUE
);
206 ret
= GSocket_Read(m_socket
, buffer
, nbytes
);
207 GSocket_SetNonBlocking(m_socket
, FALSE
);
212 else if (m_flags
& wxSOCKET_WAITALL
)
214 while (ret
> 0 && nbytes
> 0)
216 if (!(m_flags
& wxSOCKET_BLOCK
) && !WaitForRead())
219 ret
= GSocket_Read(m_socket
, buffer
, nbytes
);
231 if ((m_flags
& wxSOCKET_BLOCK
) || WaitForRead())
233 ret
= GSocket_Read(m_socket
, buffer
, nbytes
);
243 wxSocketBase
& wxSocketBase::ReadMsg(char* buffer
, wxUint32 nbytes
)
245 wxUint32 len
, len2
, sig
, total
;
250 unsigned char sig
[4];
251 unsigned char len
[4];
260 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
262 if (_Read((char *)&msg
, sizeof(msg
)) != sizeof(msg
))
265 sig
= (wxUint32
)msg
.sig
[0];
266 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
267 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
268 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
270 if (sig
!= 0xfeeddead)
272 wxLogWarning( _("TCP: invalid signature returned to ReadMsg."));
276 len
= (wxUint32
)msg
.len
[0];
277 len
|= (wxUint32
)(msg
.len
[1] << 8);
278 len
|= (wxUint32
)(msg
.len
[2] << 16);
279 len
|= (wxUint32
)(msg
.len
[3] << 24);
281 //wxLogMessage("Readmsg: %d %d %d %d -> len == %d",
282 // msg.len[0], msg.len[1], msg.len[2], msg.len[3], len);
292 // This check is necessary so that we don't attemp to read if
293 // the msg was zero bytes long.
296 total
= _Read(buffer
, len
);
303 char *discard_buffer
= new char[MAX_DISCARD_SIZE
];
306 // NOTE: discarded bytes don't add to m_lcount.
309 discard_len
= ((len2
> MAX_DISCARD_SIZE
)? MAX_DISCARD_SIZE
: len2
);
310 discard_len
= _Read(discard_buffer
, (wxUint32
)discard_len
);
311 len2
-= (wxUint32
)discard_len
;
313 while ((discard_len
> 0) && len2
);
315 delete [] discard_buffer
;
320 if (_Read((char *)&msg
, sizeof(msg
)) != sizeof(msg
))
323 sig
= (wxUint32
)msg
.sig
[0];
324 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
325 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
326 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
328 if (sig
!= 0xdeadfeed)
330 //wxLogMessage(wxT("Warning: invalid signature returned to ReadMsg"));
346 wxSocketBase
& wxSocketBase::Peek(char* buffer
, wxUint32 nbytes
)
351 m_lcount
= _Read(buffer
, nbytes
);
352 Pushback(buffer
, m_lcount
);
354 // If in wxSOCKET_WAITALL mode, all bytes should have been read.
355 if (m_flags
& wxSOCKET_WAITALL
)
356 m_error
= (m_lcount
!= nbytes
);
358 m_error
= (m_lcount
== 0);
360 // Allow read events again
366 wxSocketBase
& wxSocketBase::Write(const char *buffer
, wxUint32 nbytes
)
371 m_lcount
= _Write(buffer
, nbytes
);
373 // If in wxSOCKET_WAITALL mode, all bytes should have been written.
374 if (m_flags
& wxSOCKET_WAITALL
)
375 m_error
= (m_lcount
!= nbytes
);
377 m_error
= (m_lcount
== 0);
379 // Allow write events again
385 wxUint32
wxSocketBase::_Write(const char *buffer
, wxUint32 nbytes
)
390 if (!m_connected
|| !m_socket
)
393 // Possible combinations (they are checked in this order)
395 // wxSOCKET_WAITALL | wxSOCKET_BLOCK
400 if (m_flags
& wxSOCKET_NOWAIT
)
402 GSocket_SetNonBlocking(m_socket
, TRUE
);
403 ret
= GSocket_Write(m_socket
, buffer
, nbytes
);
404 GSocket_SetNonBlocking(m_socket
, FALSE
);
409 else if (m_flags
& wxSOCKET_WAITALL
)
411 while (ret
> 0 && nbytes
> 0)
413 if (!(m_flags
& wxSOCKET_BLOCK
) && !WaitForWrite())
416 ret
= GSocket_Write(m_socket
, buffer
, nbytes
);
428 if ((m_flags
& wxSOCKET_BLOCK
) || WaitForWrite())
430 ret
= GSocket_Write(m_socket
, buffer
, nbytes
);
440 wxSocketBase
& wxSocketBase::WriteMsg(const char *buffer
, wxUint32 nbytes
)
446 unsigned char sig
[4];
447 unsigned char len
[4];
456 SetFlags((m_flags
& wxSOCKET_BLOCK
) | wxSOCKET_WAITALL
);
458 // warning about 'cast truncates constant value'
460 # pragma warning(disable: 4310)
461 #endif // __VISUALC__
463 msg
.sig
[0] = (unsigned char) 0xad;
464 msg
.sig
[1] = (unsigned char) 0xde;
465 msg
.sig
[2] = (unsigned char) 0xed;
466 msg
.sig
[3] = (unsigned char) 0xfe;
468 msg
.len
[0] = (unsigned char) nbytes
& 0xff;
469 msg
.len
[1] = (unsigned char) (nbytes
>> 8) & 0xff;
470 msg
.len
[2] = (unsigned char) (nbytes
>> 16) & 0xff;
471 msg
.len
[3] = (unsigned char) (nbytes
>> 24) & 0xff;
473 //wxLogMessage("Writemsg: %d %d %d %d -> %d",
475 // (nbytes >> 8) & 0xff,
476 // (nbytes >> 16) & 0xff,
477 // (nbytes >> 24) & 0xff,
481 if (_Write((char *)&msg
, sizeof(msg
)) < sizeof(msg
))
484 total
= _Write(buffer
, nbytes
);
489 msg
.sig
[0] = (unsigned char) 0xed;
490 msg
.sig
[1] = (unsigned char) 0xfe;
491 msg
.sig
[2] = (unsigned char) 0xad;
492 msg
.sig
[3] = (unsigned char) 0xde;
493 msg
.len
[0] = msg
.len
[1] = msg
.len
[2] = msg
.len
[3] = (char) 0;
495 if ((_Write((char *)&msg
, sizeof(msg
))) < sizeof(msg
))
509 # pragma warning(default: 4310)
510 #endif // __VISUALC__
513 wxSocketBase
& wxSocketBase::Unread(const char *buffer
, wxUint32 nbytes
)
516 Pushback(buffer
, nbytes
);
524 wxSocketBase
& wxSocketBase::Discard()
527 char *my_data
= new char[MAX_DISCARD_SIZE
];
528 wxUint32 recv_size
= MAX_DISCARD_SIZE
;
535 SetFlags(wxSOCKET_NOWAIT
);
537 while (recv_size
== MAX_DISCARD_SIZE
)
539 recv_size
= _Read(my_data
, MAX_DISCARD_SIZE
);
547 // Allow read events again
553 // --------------------------------------------------------------
554 // wxSocketBase get local or peer addresses
555 // --------------------------------------------------------------
557 bool wxSocketBase::GetPeer(wxSockAddress
& addr_man
) const
564 peer
= GSocket_GetPeer(m_socket
);
565 addr_man
.SetAddress(peer
);
566 GAddress_destroy(peer
);
571 bool wxSocketBase::GetLocal(wxSockAddress
& addr_man
) const
578 local
= GSocket_GetLocal(m_socket
);
579 addr_man
.SetAddress(local
);
580 GAddress_destroy(local
);
585 // --------------------------------------------------------------
586 // wxSocketBase save and restore socket state
587 // --------------------------------------------------------------
589 void wxSocketBase::SaveState()
591 wxSocketState
*state
;
593 state
= new wxSocketState();
595 state
->m_notify_state
= m_notify_state
;
596 state
->m_neededreq
= m_neededreq
;
597 state
->m_flags
= m_flags
;
598 state
->m_cbk
= m_cbk
;
599 state
->m_cdata
= m_cdata
;
601 m_states
.Append(state
);
604 void wxSocketBase::RestoreState()
607 wxSocketState
*state
;
609 node
= m_states
.Last();
613 state
= (wxSocketState
*)node
->Data();
615 SetFlags(state
->m_flags
);
616 m_cbk
= state
->m_cbk
;
617 m_cdata
= state
->m_cdata
;
618 m_neededreq
= state
->m_neededreq
;
619 Notify(state
->m_notify_state
);
626 // --------------------------------------------------------------
627 // wxSocketBase Wait functions
628 // --------------------------------------------------------------
630 // These WaitXXX unctions do not depend on the event system any
631 // longer; instead, they poll the socket, using GSocket_Select()
632 // to check for the specified combination of event flags, until
633 // an event occurs or until the timeout ellapses. The polling
634 // loop calls PROCESS_EVENTS(), so this won't block the GUI.
636 // XXX: Should it honour the wxSOCKET_BLOCK flag ?
638 bool wxSocketBase::_Wait(long seconds
, long milliseconds
,
639 wxSocketEventFlags flags
)
641 GSocketEventFlags result
;
642 _wxSocketInternalTimer timer
;
646 // Set this to TRUE to interrupt ongoing waits
649 // Check for valid socket
653 // If it is not a server, it must be connected or establishing connection
654 if ((m_type
!= SOCK_SERVER
) && (!m_connected
&& !m_establishing
))
657 // Check for valid timeout value
659 timeout
= seconds
* 1000 + milliseconds
;
661 timeout
= m_timeout
* 1000;
666 timer
.m_state
= &state
;
668 timer
.Start((int)timeout
, TRUE
);
671 // Active polling (without using events)
673 // NOTE: this duplicates some of the code in OnRequest (lost
674 // connection and connection establishment handling) but this
675 // doesn't hurt. It has to be here because the event might
676 // be a bit delayed, and it has to be in OnRequest as well
677 // because maybe the WaitXXX functions are not being used.
679 // Do this at least once (important if timeout == 0, when
680 // we are just polling). Also, if just polling, do not yield.
684 result
= GSocket_Select(m_socket
, flags
| GSOCK_LOST_FLAG
);
687 if (result
& GSOCK_LOST_FLAG
)
694 // Incoming connection (server) or connection established (client)
695 if (result
& GSOCK_CONNECTION_FLAG
)
699 m_establishing
= FALSE
;
703 if ((result
& GSOCK_INPUT_FLAG
) || (result
& GSOCK_OUTPUT_FLAG
))
710 if ((timeout
== 0) || (m_interrupt
))
720 bool wxSocketBase::Wait(long seconds
, long milliseconds
)
722 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
724 GSOCK_CONNECTION_FLAG
|
728 bool wxSocketBase::WaitForRead(long seconds
, long milliseconds
)
730 // Check pushback buffer
734 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
);
737 bool wxSocketBase::WaitForWrite(long seconds
, long milliseconds
)
739 return _Wait(seconds
, milliseconds
, GSOCK_OUTPUT_FLAG
);
742 bool wxSocketBase::WaitForLost(long seconds
, long milliseconds
)
744 return _Wait(seconds
, milliseconds
, GSOCK_LOST_FLAG
);
747 void wxSocketBase::SetTimeout(long seconds
)
752 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
755 // --------------------------------------------------------------
756 // wxSocketBase flags
757 // --------------------------------------------------------------
759 void wxSocketBase::SetFlags(wxSockFlags _flags
)
764 // --------------------------------------------------------------
765 // wxSocketBase callback management
766 // --------------------------------------------------------------
768 wxSocketBase::wxSockCbk
wxSocketBase::Callback(wxSockCbk cbk_
)
770 wxSockCbk old_cbk
= cbk_
;
776 char *wxSocketBase::CallbackData(char *data
)
778 char *old_data
= m_cdata
;
784 // --------------------------------------------------------------
785 // wxSocketBase automatic notifier
786 // --------------------------------------------------------------
788 // All events (INPUT, OUTPUT, CONNECTION, LOST) are now always
789 // internally watched; but users will only be notified of those
790 // events they are interested in.
792 static void LINKAGEMODE
wx_socket_callback(GSocket
* WXUNUSED(socket
),
793 GSocketEvent event
, char *cdata
)
795 wxSocketBase
*sckobj
= (wxSocketBase
*)cdata
;
797 sckobj
->OnRequest((wxSocketNotify
)event
);
800 wxSocketEventFlags
wxSocketBase::EventToNotify(wxSocketNotify evt
)
804 case GSOCK_INPUT
: return GSOCK_INPUT_FLAG
;
805 case GSOCK_OUTPUT
: return GSOCK_OUTPUT_FLAG
;
806 case GSOCK_CONNECTION
: return GSOCK_CONNECTION_FLAG
;
807 case GSOCK_LOST
: return GSOCK_LOST_FLAG
;
812 void wxSocketBase::SetNotify(wxSocketEventFlags flags
)
817 void wxSocketBase::Notify(bool notify
)
819 m_notify_state
= notify
;
822 void wxSocketBase::OnRequest(wxSocketNotify req_evt
)
824 wxSocketEvent
event(m_id
);
825 wxSocketEventFlags flag
= EventToNotify(req_evt
);
827 // dbg("Entering OnRequest (evt %d)\n", req_evt);
831 // This duplicates some code in _Wait(), but this doesn't
832 // hurt. It has to be here because we don't know whether
833 // WaitXXX will be used, and it has to be in _Wait as well
834 // because the event might be a bit delayed.
836 case wxSOCKET_CONNECTION
:
837 m_establishing
= FALSE
;
844 // If we are in the middle of a R/W operation, do not
845 // propagate events to users. Also, filter 'late' events
846 // which are no longer valid.
849 if (m_reading
|| !GSocket_Select(m_socket
, GSOCK_INPUT_FLAG
))
854 case wxSOCKET_OUTPUT
:
855 if (m_writing
|| !GSocket_Select(m_socket
, GSOCK_OUTPUT_FLAG
))
861 if (((m_neededreq
& flag
) == flag
) && m_notify_state
)
863 // dbg("Evt %d delivered\n", req_evt);
864 event
.m_socket
= this;
865 event
.m_skevt
= req_evt
;
868 #if EXPERIMENTAL_USE_POST
869 wxPostEvent(m_evt_handler
, event
);
874 OldOnNotify(req_evt
);
876 m_cbk(*this, req_evt
, m_cdata
);
880 // dbg("Exiting OnRequest (evt %d)\n", req_evt);
883 void wxSocketBase::OldOnNotify(wxSocketNotify
WXUNUSED(evt
))
887 // --------------------------------------------------------------
888 // wxSocketBase set event handler
889 // --------------------------------------------------------------
891 void wxSocketBase::SetEventHandler(wxEvtHandler
& h_evt
, int id
)
893 m_evt_handler
= &h_evt
;
896 SetNextHandler(&h_evt
);
899 // --------------------------------------------------------------
900 // wxSocketBase pushback
901 // --------------------------------------------------------------
903 void wxSocketBase::Pushback(const char *buffer
, wxUint32 size
)
907 if (m_unread
== NULL
)
908 m_unread
= (char *)malloc(size
);
913 tmp
= (char *)malloc(m_unrd_size
+ size
);
914 memcpy(tmp
+size
, m_unread
, m_unrd_size
);
922 memcpy(m_unread
, buffer
, size
);
925 wxUint32
wxSocketBase::GetPushback(char *buffer
, wxUint32 size
, bool peek
)
930 if (size
> (m_unrd_size
-m_unrd_cur
))
931 size
= m_unrd_size
-m_unrd_cur
;
933 memcpy(buffer
, (m_unread
+m_unrd_cur
), size
);
938 if (m_unrd_size
== m_unrd_cur
)
950 // --------------------------------------------------------------
952 // --------------------------------------------------------------
954 // --------------------------------------------------------------
955 // wxSocketServer ctor and dtor
956 // --------------------------------------------------------------
958 wxSocketServer::wxSocketServer(wxSockAddress
& addr_man
,
960 wxSocketBase(flags
, SOCK_SERVER
)
963 m_socket
= GSocket_new();
968 // Setup the socket as server
969 GSocket_SetLocal(m_socket
, addr_man
.GetAddress());
970 if (GSocket_SetServer(m_socket
) != GSOCK_NOERROR
)
972 GSocket_destroy(m_socket
);
977 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
978 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
979 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
980 wx_socket_callback
, (char *)this);
984 // --------------------------------------------------------------
985 // wxSocketServer Accept
986 // --------------------------------------------------------------
988 bool wxSocketServer::AcceptWith(wxSocketBase
& sock
, bool wait
)
990 GSocket
*child_socket
;
995 // GRG: If wait == FALSE, then the call should be nonblocking.
996 // When we are finished, we put the socket to blocking mode
1000 GSocket_SetNonBlocking(m_socket
, TRUE
);
1002 child_socket
= GSocket_WaitConnection(m_socket
);
1005 GSocket_SetNonBlocking(m_socket
, FALSE
);
1007 // GRG: this was not being handled!
1008 if (child_socket
== NULL
)
1011 sock
.m_type
= SOCK_INTERNAL
;
1012 sock
.m_socket
= child_socket
;
1013 sock
.m_connected
= TRUE
;
1015 GSocket_SetTimeout(sock
.m_socket
, sock
.m_timeout
* 1000);
1016 GSocket_SetCallback(sock
.m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1017 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1018 wx_socket_callback
, (char *)&sock
);
1023 wxSocketBase
*wxSocketServer::Accept(bool wait
)
1025 wxSocketBase
* sock
= new wxSocketBase();
1027 sock
->SetFlags((wxSockFlags
)m_flags
);
1029 if (!AcceptWith(*sock
, wait
))
1035 bool wxSocketServer::WaitForAccept(long seconds
, long milliseconds
)
1037 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
);
1040 // --------------------------------------------------------------
1042 // --------------------------------------------------------------
1044 // --------------------------------------------------------------
1045 // wxSocketClient ctor and dtor
1046 // --------------------------------------------------------------
1048 wxSocketClient::wxSocketClient(wxSockFlags _flags
) :
1049 wxSocketBase(_flags
, SOCK_CLIENT
)
1053 wxSocketClient::~wxSocketClient()
1057 // --------------------------------------------------------------
1058 // wxSocketClient Connect functions
1059 // --------------------------------------------------------------
1061 bool wxSocketClient::Connect(wxSockAddress
& addr_man
, bool wait
)
1068 // This should never happen.
1070 GSocket_destroy(m_socket
);
1072 // Initialize all socket stuff ...
1073 m_socket
= GSocket_new();
1074 m_connected
= FALSE
;
1075 m_establishing
= FALSE
;
1080 GSocket_SetTimeout(m_socket
, m_timeout
* 1000);
1081 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1082 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1083 wx_socket_callback
, (char *)this);
1085 // GRG: If wait == FALSE, then the call should be nonblocking.
1086 // When we are finished, we put the socket to blocking mode
1090 GSocket_SetNonBlocking(m_socket
, TRUE
);
1092 GSocket_SetPeer(m_socket
, addr_man
.GetAddress());
1093 err
= GSocket_Connect(m_socket
, GSOCK_STREAMED
);
1096 GSocket_SetNonBlocking(m_socket
, FALSE
);
1098 if (err
!= GSOCK_NOERROR
)
1100 if (err
== GSOCK_WOULDBLOCK
)
1101 m_establishing
= TRUE
;
1110 bool wxSocketClient::WaitOnConnect(long seconds
, long milliseconds
)
1112 if (m_connected
) // Already connected
1115 if (!m_establishing
|| !m_socket
) // No connection in progress
1118 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
);
1121 // --------------------------------------------------------------
1123 // --------------------------------------------------------------
1125 /* NOTE: experimental stuff - might change */
1127 wxDatagramSocket::wxDatagramSocket( wxSockAddress
& addr
, wxSockFlags flags
)
1128 : wxSocketBase( flags
, SOCK_DATAGRAM
)
1130 // Create the socket
1131 m_socket
= GSocket_new();
1136 // Setup the socket as non connection oriented
1137 GSocket_SetLocal(m_socket
, addr
.GetAddress());
1138 if( GSocket_SetNonOriented(m_socket
) != GSOCK_NOERROR
)
1140 GSocket_destroy(m_socket
);
1145 // Initialize all stuff
1146 m_connected
= FALSE
;
1147 m_establishing
= FALSE
;
1148 GSocket_SetTimeout( m_socket
, m_timeout
);
1149 GSocket_SetCallback( m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1150 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1151 wx_socket_callback
, (char*)this );
1155 wxDatagramSocket
& wxDatagramSocket::RecvFrom( wxSockAddress
& addr
,
1164 wxDatagramSocket
& wxDatagramSocket::SendTo( wxSockAddress
& addr
,
1168 GSocket_SetPeer(m_socket
, addr
.GetAddress());
1173 // --------------------------------------------------------------
1175 // --------------------------------------------------------------
1177 wxSocketEvent::wxSocketEvent(int id
)
1180 wxEventType type
= (wxEventType
)wxEVT_SOCKET
;
1185 void wxSocketEvent::CopyObject(wxObject
& obj_d
) const
1187 wxSocketEvent
*event
= (wxSocketEvent
*)&obj_d
;
1189 wxEvent::CopyObject(obj_d
);
1191 event
->m_skevt
= m_skevt
;
1192 event
->m_socket
= m_socket
;
1195 // --------------------------------------------------------------------------
1197 // --------------------------------------------------------------------------
1199 class WXDLLEXPORT wxSocketModule
: public wxModule
1201 DECLARE_DYNAMIC_CLASS(wxSocketModule
)
1205 return GSocket_Init();
1213 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule
, wxModule
)