1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Socket handler classes
4 // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia
6 // Updated: September 1999
7 // Copyright: (C) 1999, 1998, 1997, Guilhem Lavaux
8 // (C) 1999, Guillermo Rodriguez Garcia
10 // License: see wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
14 #pragma implementation "socket.h"
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
26 /////////////////////////////////////////////////////////////////////////////
28 /////////////////////////////////////////////////////////////////////////////
30 #include "wx/object.h"
31 #include "wx/string.h"
34 #include "wx/module.h"
41 /////////////////////////////////////////////////////////////////////////////
43 /////////////////////////////////////////////////////////////////////////////
44 #include "wx/sckaddr.h"
45 #include "wx/socket.h"
48 #if defined(__WXMSW__) || defined(__WXPM__)
49 #define PROCESS_EVENTS() wxYield()
50 #elif defined(__WXGTK__)
52 #define PROCESS_EVENTS() gtk_main_iteration()
56 // --------------------------------------------------------------
58 // --------------------------------------------------------------
59 #if !USE_SHARED_LIBRARY
60 IMPLEMENT_CLASS(wxSocketBase
, wxObject
)
61 IMPLEMENT_CLASS(wxSocketServer
, wxSocketBase
)
62 IMPLEMENT_CLASS(wxSocketClient
, wxSocketBase
)
63 IMPLEMENT_DYNAMIC_CLASS(wxSocketEvent
, wxEvent
)
66 class wxSocketState
: public wxObject
70 GSocketEventFlags evt_notify_state
;
71 wxSocketBase::wxSockFlags socket_flags
;
72 wxSocketBase::wxSockCbk c_callback
;
73 char *c_callback_data
;
76 wxSocketState() : wxObject() {}
79 // --------------------------------------------------------------
80 // wxSocketBase ctor and dtor
81 // --------------------------------------------------------------
83 wxSocketBase::wxSocketBase(wxSocketBase::wxSockFlags _flags
,
84 wxSocketBase::wxSockType _type
) :
86 m_socket(NULL
), m_id(-1),
87 m_flags(_flags
), m_type(_type
),
88 m_neededreq(0), m_notify_state(FALSE
),
89 m_connected(FALSE
), m_establishing(FALSE
),
90 m_reading(FALSE
), m_writing(FALSE
),
91 m_error(FALSE
), m_lcount(0), m_timeout(600), m_states(),
92 m_unread(NULL
), m_unrd_size(0), m_unrd_cur(0),
93 m_defering(NO_DEFER
), m_defer_buffer(NULL
), m_defer_timer(NULL
),
94 m_cbk(NULL
), m_cdata(NULL
)
98 wxSocketBase::wxSocketBase() :
100 m_socket(NULL
), m_id(-1),
101 m_flags(NONE
), m_type(SOCK_UNINIT
),
102 m_neededreq(0), m_notify_state(FALSE
),
103 m_connected(FALSE
), m_establishing(FALSE
),
104 m_reading(FALSE
), m_writing(FALSE
),
105 m_error(FALSE
), m_lcount(0), m_timeout(600), m_states(),
106 m_unread(NULL
), m_unrd_size(0), m_unrd_cur(0),
107 m_defering(NO_DEFER
), m_defer_buffer(NULL
), m_defer_timer(NULL
),
108 m_cbk(NULL
), m_cdata(NULL
)
112 wxSocketBase::~wxSocketBase()
117 // Shutdown and close the socket
120 // Destroy the GSocket object
122 GSocket_destroy(m_socket
);
125 bool wxSocketBase::Close()
130 GSocket_UnsetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
131 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
);
133 // Shutdown the connection
134 GSocket_Shutdown(m_socket
);
136 m_establishing
= FALSE
;
142 // --------------------------------------------------------------
143 // wxSocketBase basic IO operations
144 // --------------------------------------------------------------
146 // All IO operations {Read, Write, ReadMsg, WriteMsg, Peek,
147 // Unread, Discard} update m_error and m_lcount.
149 // TODO: Should Connect, Accept and AcceptWith update m_error?
151 class _wxSocketInternalTimer
: public wxTimer
155 unsigned long m_new_val
;
159 *m_state
= m_new_val
; // Change the value
163 wxUint32
wxSocketBase::DeferRead(char *buffer
, wxUint32 nbytes
)
166 _wxSocketInternalTimer timer
;
168 //wxLogMessage("Entrando a DeferRead, nbytes = %d", nbytes);
169 wxASSERT(m_defering
== NO_DEFER
);
171 // Set the defering mode to READ.
172 m_defering
= DEFER_READ
;
174 // Set the current buffer.
175 m_defer_buffer
= buffer
;
176 m_defer_nbytes
= nbytes
;
177 m_defer_timer
= &timer
;
179 timer
.m_state
= (int *)&m_defer_buffer
;
182 timer
.Start(m_timeout
* 1000, FALSE
);
184 // If the socket is readable, call DoDefer for the first time
185 if (GSocket_Select(m_socket
, GSOCK_INPUT_FLAG
))
187 //wxLogMessage("Llamando al primer DoDefer");
191 // Wait for buffer completion.
192 while (m_defer_buffer
!= NULL
)
197 // Disable defering mode.
198 m_defering
= NO_DEFER
;
199 m_defer_timer
= NULL
;
201 // Return the number of bytes read from the socket.
202 //wxLogMessage("Saliendo de DeferRead: total: %d bytes", nbytes - m_defer_nbytes);
203 return nbytes
-m_defer_nbytes
;
206 wxSocketBase
& wxSocketBase::Read(char* buffer
, wxUint32 nbytes
)
211 m_lcount
= _Read(buffer
, nbytes
);
213 // If in WAITALL mode, all bytes should have been read.
214 if (m_flags
& WAITALL
)
215 m_error
= (m_lcount
!= nbytes
);
217 m_error
= (m_lcount
== 0);
219 // Trigger another read event if there is still data available.
226 wxUint32
wxSocketBase::_Read(char* buffer
, wxUint32 nbytes
)
231 // we try this even if the connection has already been closed.
232 total
= GetPushback(buffer
, nbytes
, FALSE
);
236 // If the socket is not connected, or we have got the whole
237 // needed buffer, return immedately
238 if (!m_connected
|| !m_socket
|| !nbytes
)
241 // Possible combinations (they are checked in this order)
248 if (m_flags
& NOWAIT
) // NOWAIT
250 GSocket_SetNonBlocking(m_socket
, TRUE
);
251 ret
= GSocket_Read(m_socket
, buffer
, nbytes
);
252 GSocket_SetNonBlocking(m_socket
, FALSE
);
257 else if ((m_flags
& SPEED
) && (m_flags
& WAITALL
)) // SPEED, WAITALL
259 while (ret
> 0 && nbytes
> 0)
261 ret
= GSocket_Read(m_socket
, buffer
, nbytes
);
266 // In case the last call was an error ...
270 else if (m_flags
& SPEED
) // SPEED, !WAITALL
272 ret
= GSocket_Read(m_socket
, buffer
, nbytes
);
277 else // NONE or WAITALL
279 ret
= DeferRead(buffer
, nbytes
);
288 wxSocketBase
& wxSocketBase::ReadMsg(char* buffer
, wxUint32 nbytes
)
290 #define MAX_DISCARD_SIZE (10 * 1024)
292 wxUint32 len
, len2
, sig
, total
;
297 unsigned char sig
[4];
298 unsigned char len
[4];
307 SetFlags((m_flags
& SPEED
) | WAITALL
);
309 if (_Read((char *)&msg
, sizeof(msg
)) != sizeof(msg
))
312 sig
= (wxUint32
)msg
.sig
[0];
313 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
314 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
315 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
317 if (sig
!= 0xfeeddead)
319 wxLogMessage(wxT("Warning: invalid signature returned to ReadMsg"));
323 len
= (wxUint32
)msg
.len
[0];
324 len
|= (wxUint32
)(msg
.len
[1] << 8);
325 len
|= (wxUint32
)(msg
.len
[2] << 16);
326 len
|= (wxUint32
)(msg
.len
[3] << 24);
328 //wxLogMessage("Readmsg: %d %d %d %d -> len == %d",
329 // msg.len[0], msg.len[1], msg.len[2], msg.len[3], len);
339 // This check is necessary so that we don't attemp to read if
340 // the msg was zero bytes long.
343 total
= _Read(buffer
, len
);
350 char *discard_buffer
= new char[MAX_DISCARD_SIZE
];
353 // NOTE: discarded bytes don't add to m_lcount.
356 discard_len
= ((len2
> MAX_DISCARD_SIZE
)? MAX_DISCARD_SIZE
: len2
);
357 discard_len
= _Read(discard_buffer
, discard_len
);
360 while ((discard_len
> 0) && len2
);
362 delete [] discard_buffer
;
367 if (_Read((char *)&msg
, sizeof(msg
)) != sizeof(msg
))
370 sig
= (wxUint32
)msg
.sig
[0];
371 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
372 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
373 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
375 if (sig
!= 0xdeadfeed)
377 //wxLogMessage(wxT("Warning: invalid signature returned to ReadMsg"));
393 #undef MAX_DISCARD_SIZE
396 wxSocketBase
& wxSocketBase::Peek(char* buffer
, wxUint32 nbytes
)
401 m_lcount
= _Read(buffer
, nbytes
);
402 Pushback(buffer
, nbytes
);
404 // If in WAITALL mode, all bytes should have been read.
405 if (m_flags
& WAITALL
)
406 m_error
= (m_lcount
!= nbytes
);
408 m_error
= (m_lcount
== 0);
410 // Trigger another read event if there is still data available.
417 wxUint32
wxSocketBase::DeferWrite(const char *buffer
, wxUint32 nbytes
)
420 _wxSocketInternalTimer timer
;
422 wxASSERT(m_defering
== NO_DEFER
);
423 //wxLogMessage("Entrando a DeferWrite");
425 m_defering
= DEFER_WRITE
;
427 // Set the current buffer
428 m_defer_buffer
= (char *)buffer
;
429 m_defer_nbytes
= nbytes
;
430 m_defer_timer
= &timer
;
433 timer
.m_state
= (int *)&m_defer_buffer
;
436 timer
.Start(m_timeout
* 1000, FALSE
);
438 // If the socket is writable, call DoDefer for the first time
439 if (GSocket_Select(m_socket
, GSOCK_OUTPUT_FLAG
))
441 //wxLogMessage("Llamando al primer DoDefer");
445 // Wait for buffer completion.
446 while (m_defer_buffer
!= NULL
)
451 // Disable defering mode
452 m_defer_timer
= NULL
;
453 m_defering
= NO_DEFER
;
456 s
.Printf("Saliendo de DeferWrite: total %d bytes", nbytes
-m_defer_nbytes
);
459 return nbytes
-m_defer_nbytes
;
462 wxSocketBase
& wxSocketBase::Write(const char *buffer
, wxUint32 nbytes
)
467 m_lcount
= _Write(buffer
, nbytes
);
469 // If in WAITALL mode, all bytes should have been written.
470 if (m_flags
& WAITALL
)
471 m_error
= (m_lcount
!= nbytes
);
473 m_error
= (m_lcount
== 0);
475 // Trigger another write event if the socket is still writable
478 // TODO: TriggerWrite
482 wxUint32
wxSocketBase::_Write(const char *buffer
, wxUint32 nbytes
)
487 if (!m_connected
|| !m_socket
)
490 // Possible combinations (they are checked in this order)
497 if (m_flags
& NOWAIT
) // NOWAIT
499 GSocket_SetNonBlocking(m_socket
, TRUE
);
500 ret
= GSocket_Write(m_socket
, buffer
, nbytes
);
501 GSocket_SetNonBlocking(m_socket
, FALSE
);
506 else if ((m_flags
& SPEED
) && (m_flags
& WAITALL
)) // SPEED, WAITALL
508 while (ret
> 0 && nbytes
> 0)
510 ret
= GSocket_Write(m_socket
, buffer
, nbytes
);
515 // In case the last call was an error ...
519 else if (m_flags
& SPEED
) // SPEED, !WAITALL
521 ret
= GSocket_Write(m_socket
, buffer
, nbytes
);
526 else // NONE or WAITALL
528 ret
= DeferWrite(buffer
, nbytes
);
537 wxSocketBase
& wxSocketBase::WriteMsg(const char *buffer
, wxUint32 nbytes
)
543 unsigned char sig
[4];
544 unsigned char len
[4];
553 SetFlags((m_flags
& SPEED
) | WAITALL
);
555 // warning about 'cast truncates constant value'
557 # pragma warning(disable: 4310)
558 #endif // __VISUALC__
560 msg
.sig
[0] = (unsigned char) 0xad;
561 msg
.sig
[1] = (unsigned char) 0xde;
562 msg
.sig
[2] = (unsigned char) 0xed;
563 msg
.sig
[3] = (unsigned char) 0xfe;
565 msg
.len
[0] = (unsigned char) nbytes
& 0xff;
566 msg
.len
[1] = (unsigned char) (nbytes
>> 8) & 0xff;
567 msg
.len
[2] = (unsigned char) (nbytes
>> 16) & 0xff;
568 msg
.len
[3] = (unsigned char) (nbytes
>> 24) & 0xff;
570 //wxLogMessage("Writemsg: %d %d %d %d -> %d",
572 // (nbytes >> 8) & 0xff,
573 // (nbytes >> 16) & 0xff,
574 // (nbytes >> 24) & 0xff,
578 if (_Write((char *)&msg
, sizeof(msg
)) < sizeof(msg
))
581 total
= _Write(buffer
, nbytes
);
586 msg
.sig
[0] = (unsigned char) 0xed;
587 msg
.sig
[1] = (unsigned char) 0xfe;
588 msg
.sig
[2] = (unsigned char) 0xad;
589 msg
.sig
[3] = (unsigned char) 0xde;
590 msg
.len
[0] = msg
.len
[1] = msg
.len
[2] = msg
.len
[3] = (char) 0;
592 if ((_Write((char *)&msg
, sizeof(msg
))) < sizeof(msg
))
603 // TODO: TriggerWrite
607 # pragma warning(default: 4310)
608 #endif // __VISUALC__
611 wxSocketBase
& wxSocketBase::Unread(const char *buffer
, wxUint32 nbytes
)
614 Pushback(buffer
, nbytes
);
622 void wxSocketBase::DoDefer()
632 ret
= GSocket_Read(m_socket
, m_defer_buffer
, m_defer_nbytes
);
635 ret
= GSocket_Write(m_socket
, m_defer_buffer
, m_defer_nbytes
);
642 m_defer_nbytes
-= ret
;
644 // If we are waiting for all bytes to be acquired, keep the defering
646 if (!(m_flags
& WAITALL
) || !m_defer_nbytes
|| ret
< 0)
648 m_defer_buffer
= NULL
;
652 m_defer_buffer
+= ret
;
653 m_defer_timer
->Start(m_timeout
* 1000, FALSE
);
656 //wxLogMessage("DoDefer ha transferido %d bytes", ret);
659 wxSocketBase
& wxSocketBase::Discard()
661 #define MAX_BUFSIZE (10*1024)
664 char *my_data
= new char[MAX_BUFSIZE
];
665 wxUint32 recv_size
= MAX_BUFSIZE
;
674 while (recv_size
== MAX_BUFSIZE
)
676 recv_size
= _Read(my_data
, MAX_BUFSIZE
);
684 // Trigger another read event if there is still data available.
693 // --------------------------------------------------------------
694 // wxSocketBase get local or peer addresses
695 // --------------------------------------------------------------
697 bool wxSocketBase::GetPeer(wxSockAddress
& addr_man
) const
704 peer
= GSocket_GetPeer(m_socket
);
705 addr_man
.SetAddress(peer
);
706 GAddress_destroy(peer
);
711 bool wxSocketBase::GetLocal(wxSockAddress
& addr_man
) const
718 local
= GSocket_GetLocal(m_socket
);
719 addr_man
.SetAddress(local
);
720 GAddress_destroy(local
);
725 // --------------------------------------------------------------
726 // wxSocketBase save and restore socket state
727 // --------------------------------------------------------------
729 void wxSocketBase::SaveState()
731 wxSocketState
*state
;
733 state
= new wxSocketState();
735 state
->notify_state
= m_notify_state
;
736 state
->evt_notify_state
= m_neededreq
;
737 state
->socket_flags
= m_flags
;
738 state
->c_callback
= m_cbk
;
739 state
->c_callback_data
= m_cdata
;
741 m_states
.Append(state
);
744 void wxSocketBase::RestoreState()
747 wxSocketState
*state
;
749 node
= m_states
.Last();
753 state
= (wxSocketState
*)node
->Data();
755 SetFlags(state
->socket_flags
);
756 m_neededreq
= state
->evt_notify_state
;
757 m_cbk
= state
->c_callback
;
758 m_cdata
= state
->c_callback_data
;
759 Notify(state
->notify_state
);
766 // --------------------------------------------------------------
767 // wxSocketBase Wait functions
768 // --------------------------------------------------------------
770 // GRG: I have completely rewritten this family of functions
771 // so that they don't depend on event notifications; instead,
772 // they poll the socket, using GSocket_Select(), to check for
773 // the specified combination of event flags, until an event
774 // occurs or until the timeout ellapses. The polling loop
775 // calls PROCESS_EVENTS(), so this won't block the GUI.
777 bool wxSocketBase::_Wait(long seconds
, long milliseconds
, wxSocketEventFlags flags
)
779 GSocketEventFlags result
;
780 _wxSocketInternalTimer timer
;
784 // Check for valid socket
788 // If it is not a server, it must be connected or establishing connection
789 if ((m_type
!= SOCK_SERVER
) && (!m_connected
&& !m_establishing
))
792 // Check for valid timeout value
794 timeout
= seconds
* 1000 + milliseconds
;
796 timeout
= m_timeout
* 1000;
801 timer
.m_state
= &state
;
803 timer
.Start(timeout
, TRUE
);
806 // Active polling (without using events)
808 // NOTE: this duplicates some of the code in OnRequest (lost
809 // connection and connection establishment handling) but this
810 // doesn't hurt. It has to be here because the event might
811 // be a bit delayed, and it has to be in OnRequest as well
812 // because maybe the WaitXXX functions are not being used.
814 // Do this at least once (important if timeout == 0, when
815 // we are just polling)
818 result
= GSocket_Select(m_socket
, flags
| GSOCK_LOST_FLAG
);
821 if (result
& GSOCK_LOST_FLAG
)
824 m_defer_buffer
= NULL
;
829 // Incoming connection (server) or connection established (client)
830 if (result
& GSOCK_CONNECTION_FLAG
)
834 m_establishing
= FALSE
;
838 // If we are in the middle of a R/W operation, do not propagate
840 if ( ((result
& GSOCK_INPUT_FLAG
) || (result
& GSOCK_OUTPUT_FLAG
))
841 && (!m_writing
) && (!m_reading
) )
844 if (m_defer_buffer == NULL)
855 while ((state
== -1) && timeout
);
861 bool wxSocketBase::Wait(long seconds
, long milliseconds
)
863 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
865 GSOCK_CONNECTION_FLAG
|
869 bool wxSocketBase::WaitForRead(long seconds
, long milliseconds
)
871 // Check pushback buffer
875 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
);
878 bool wxSocketBase::WaitForWrite(long seconds
, long milliseconds
)
880 return _Wait(seconds
, milliseconds
, GSOCK_OUTPUT_FLAG
);
883 bool wxSocketBase::WaitForLost(long seconds
, long milliseconds
)
885 return _Wait(seconds
, milliseconds
, GSOCK_LOST_FLAG
);
888 void wxSocketBase::SetTimeout(long seconds
)
893 GSocket_SetTimeout(m_socket
, m_timeout
);
896 // --------------------------------------------------------------
897 // wxSocketBase flags
898 // --------------------------------------------------------------
900 void wxSocketBase::SetFlags(wxSockFlags _flags
)
905 // --------------------------------------------------------------
906 // wxSocketBase callback management
907 // --------------------------------------------------------------
909 wxSocketBase::wxSockCbk
wxSocketBase::Callback(wxSockCbk cbk_
)
911 wxSockCbk old_cbk
= cbk_
;
917 char *wxSocketBase::CallbackData(char *data
)
919 char *old_data
= m_cdata
;
925 // --------------------------------------------------------------
926 // wxSocketBase automatic notifier
927 // --------------------------------------------------------------
929 // All events (INPUT, OUTPUT, CONNECTION, LOST) are now always
930 // internally watched; but users will only be notified of those
931 // events they are interested in.
933 static void LINKAGEMODE
wx_socket_callback(GSocket
* WXUNUSED(socket
),
934 GSocketEvent event
, char *cdata
)
936 wxSocketBase
*sckobj
= (wxSocketBase
*)cdata
;
938 sckobj
->OnRequest((wxSocketNotify
)event
);
941 wxSocketEventFlags
wxSocketBase::EventToNotify(wxSocketNotify evt
)
945 case GSOCK_INPUT
: return GSOCK_INPUT_FLAG
;
946 case GSOCK_OUTPUT
: return GSOCK_OUTPUT_FLAG
;
947 case GSOCK_CONNECTION
: return GSOCK_CONNECTION_FLAG
;
948 case GSOCK_LOST
: return GSOCK_LOST_FLAG
;
953 void wxSocketBase::SetNotify(wxSocketEventFlags flags
)
958 void wxSocketBase::Notify(bool notify
)
960 m_notify_state
= notify
;
963 void wxSocketBase::OnRequest(wxSocketNotify req_evt
)
965 wxSocketEvent
event(m_id
);
966 wxSocketEventFlags flag
= EventToNotify(req_evt
);
968 // fprintf(stderr, "%s: Entering OnRequest (evt %d)\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt);
970 // NOTE: this duplicates some of the code in _Wait (lost
971 // connection and connection establishment handling) but
972 // this doesn't hurt. It has to be here because maybe the
973 // WaitXXX are not being used, and it has to be in _Wait
974 // as well because the event might be a bit delayed.
978 case wxSOCKET_CONNECTION
:
979 m_establishing
= FALSE
;
983 m_defer_buffer
= NULL
;
987 case wxSOCKET_OUTPUT
:
990 // fprintf(stderr, "%s: Habia buffer, evt %d skipped\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt);
992 // Do not notify to user
998 // If we are in the middle of a R/W operation, do not
999 // propagate events to users.
1000 if (((req_evt
== wxSOCKET_INPUT
) && m_reading
) ||
1001 ((req_evt
== wxSOCKET_OUTPUT
) && m_writing
))
1003 // fprintf(stderr, "%s: Swallowed evt %d\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt);
1007 if (((m_neededreq
& flag
) == flag
) && m_notify_state
)
1009 // fprintf(stderr, "%s: Evt %d delivered\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt);
1010 event
.m_socket
= this;
1011 event
.m_skevt
= req_evt
;
1012 ProcessEvent(event
);
1013 OldOnNotify(req_evt
);
1016 m_cbk(*this, req_evt
, m_cdata
);
1019 // fprintf(stderr, "%s: Exiting OnRequest (evt %d)\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt);
1022 void wxSocketBase::OldOnNotify(wxSocketNotify
WXUNUSED(evt
))
1026 // --------------------------------------------------------------
1027 // wxSocketBase set event handler
1028 // --------------------------------------------------------------
1030 void wxSocketBase::SetEventHandler(wxEvtHandler
& h_evt
, int id
)
1032 SetNextHandler(&h_evt
);
1036 // --------------------------------------------------------------
1037 // wxSocketBase pushback
1038 // --------------------------------------------------------------
1040 void wxSocketBase::Pushback(const char *buffer
, wxUint32 size
)
1042 if (m_unread
== NULL
)
1043 m_unread
= (char *)malloc(size
);
1047 tmp
= (char *)malloc(m_unrd_size
+ size
);
1048 memcpy(tmp
+size
, m_unread
, m_unrd_size
);
1054 m_unrd_size
+= size
;
1056 memcpy(m_unread
, buffer
, size
);
1059 wxUint32
wxSocketBase::GetPushback(char *buffer
, wxUint32 size
, bool peek
)
1064 if (size
> (m_unrd_size
-m_unrd_cur
))
1065 size
= m_unrd_size
-m_unrd_cur
;
1067 memcpy(buffer
, (m_unread
+m_unrd_cur
), size
);
1071 if (m_unrd_size
== m_unrd_cur
) {
1082 // --------------------------------------------------------------
1084 // --------------------------------------------------------------
1086 // --------------------------------------------------------------
1087 // wxSocketServer ctor and dtor
1088 // --------------------------------------------------------------
1090 wxSocketServer::wxSocketServer(wxSockAddress
& addr_man
,
1091 wxSockFlags flags
) :
1092 wxSocketBase(flags
, SOCK_SERVER
)
1094 // Create the socket
1095 m_socket
= GSocket_new();
1100 // Setup the socket as server
1101 GSocket_SetLocal(m_socket
, addr_man
.GetAddress());
1102 if (GSocket_SetServer(m_socket
) != GSOCK_NOERROR
)
1104 GSocket_destroy(m_socket
);
1109 GSocket_SetTimeout(m_socket
, m_timeout
);
1110 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1111 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1112 wx_socket_callback
, (char *)this);
1116 // --------------------------------------------------------------
1117 // wxSocketServer Accept
1118 // --------------------------------------------------------------
1120 bool wxSocketServer::AcceptWith(wxSocketBase
& sock
, bool wait
)
1122 GSocket
*child_socket
;
1127 // GRG: If wait == FALSE, then the call should be nonblocking.
1128 // When we are finished, we put the socket to blocking mode
1132 GSocket_SetNonBlocking(m_socket
, TRUE
);
1134 child_socket
= GSocket_WaitConnection(m_socket
);
1137 GSocket_SetNonBlocking(m_socket
, FALSE
);
1139 // GRG: this was not being handled!
1140 if (child_socket
== NULL
)
1143 sock
.m_type
= SOCK_INTERNAL
;
1144 sock
.m_socket
= child_socket
;
1145 sock
.m_connected
= TRUE
;
1147 GSocket_SetTimeout(sock
.m_socket
, sock
.m_timeout
);
1148 GSocket_SetCallback(sock
.m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1149 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1150 wx_socket_callback
, (char *)&sock
);
1155 wxSocketBase
*wxSocketServer::Accept(bool wait
)
1157 wxSocketBase
* sock
= new wxSocketBase();
1159 sock
->SetFlags((wxSockFlags
)m_flags
);
1161 if (!AcceptWith(*sock
, wait
))
1167 bool wxSocketServer::WaitForAccept(long seconds
, long milliseconds
)
1169 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
);
1172 // --------------------------------------------------------------
1174 // --------------------------------------------------------------
1176 // --------------------------------------------------------------
1177 // wxSocketClient ctor and dtor
1178 // --------------------------------------------------------------
1180 wxSocketClient::wxSocketClient(wxSockFlags _flags
) :
1181 wxSocketBase(_flags
, SOCK_CLIENT
)
1185 wxSocketClient::~wxSocketClient()
1189 // --------------------------------------------------------------
1190 // wxSocketClient Connect functions
1191 // --------------------------------------------------------------
1192 bool wxSocketClient::Connect(wxSockAddress
& addr_man
, bool wait
)
1199 // This should never happen.
1201 GSocket_destroy(m_socket
);
1203 // Initialize all socket stuff ...
1204 m_socket
= GSocket_new();
1205 m_connected
= FALSE
;
1206 m_establishing
= FALSE
;
1211 GSocket_SetTimeout(m_socket
, m_timeout
);
1212 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1213 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1214 wx_socket_callback
, (char *)this);
1216 // GRG: If wait == FALSE, then the call should be nonblocking.
1217 // When we are finished, we put the socket to blocking mode
1221 GSocket_SetNonBlocking(m_socket
, TRUE
);
1223 GSocket_SetPeer(m_socket
, addr_man
.GetAddress());
1224 err
= GSocket_Connect(m_socket
, GSOCK_STREAMED
);
1227 GSocket_SetNonBlocking(m_socket
, FALSE
);
1229 if (err
!= GSOCK_NOERROR
)
1231 if (err
== GSOCK_WOULDBLOCK
)
1232 m_establishing
= TRUE
;
1241 bool wxSocketClient::WaitOnConnect(long seconds
, long milliseconds
)
1243 if (m_connected
) // Already connected
1246 if (!m_establishing
|| !m_socket
) // No connection in progress
1249 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
);
1252 // --------------------------------------------------------------
1254 // --------------------------------------------------------------
1256 wxSocketEvent::wxSocketEvent(int id
)
1259 wxEventType type
= (wxEventType
)wxEVT_SOCKET
;
1264 void wxSocketEvent::CopyObject(wxObject
& obj_d
) const
1266 wxSocketEvent
*event
= (wxSocketEvent
*)&obj_d
;
1268 wxEvent::CopyObject(obj_d
);
1270 event
->m_skevt
= m_skevt
;
1271 event
->m_socket
= m_socket
;
1274 // --------------------------------------------------------------------------
1276 // --------------------------------------------------------------------------
1277 class WXDLLEXPORT wxSocketModule
: public wxModule
{
1278 DECLARE_DYNAMIC_CLASS(wxSocketModule
)
1281 return GSocket_Init();
1288 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule
, wxModule
)