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__) || defined(__WXMOTIF__)
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 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(wxSockFlags _flags
, wxSockType _type
) :
85 m_socket(NULL
), m_id(-1),
86 m_flags(_flags
), m_type(_type
),
87 m_neededreq(0), m_notify_state(FALSE
),
88 m_connected(FALSE
), m_establishing(FALSE
),
89 m_reading(FALSE
), m_writing(FALSE
),
90 m_error(FALSE
), m_lcount(0), m_timeout(600), m_states(),
91 m_unread(NULL
), m_unrd_size(0), m_unrd_cur(0),
92 m_defering(NO_DEFER
), m_defer_buffer(NULL
), m_defer_timer(NULL
),
93 m_cbk(NULL
), m_cdata(NULL
)
97 wxSocketBase::wxSocketBase() :
99 m_socket(NULL
), m_id(-1),
100 m_flags(NONE
), m_type(SOCK_UNINIT
),
101 m_neededreq(0), m_notify_state(FALSE
),
102 m_connected(FALSE
), m_establishing(FALSE
),
103 m_reading(FALSE
), m_writing(FALSE
),
104 m_error(FALSE
), m_lcount(0), m_timeout(600), m_states(),
105 m_unread(NULL
), m_unrd_size(0), m_unrd_cur(0),
106 m_defering(NO_DEFER
), m_defer_buffer(NULL
), m_defer_timer(NULL
),
107 m_cbk(NULL
), m_cdata(NULL
)
111 wxSocketBase::~wxSocketBase()
116 // Shutdown and close the socket
119 // Destroy the GSocket object
121 GSocket_destroy(m_socket
);
124 bool wxSocketBase::Close()
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
= m_new_val
; // Change the value
162 wxUint32
wxSocketBase::DeferRead(char *buffer
, wxUint32 nbytes
)
165 _wxSocketInternalTimer timer
;
167 //wxLogMessage("Entrando a DeferRead, nbytes = %d", nbytes);
168 wxASSERT(m_defering
== NO_DEFER
);
170 // Set the defering mode to READ.
171 m_defering
= DEFER_READ
;
173 // Set the current buffer.
174 m_defer_buffer
= buffer
;
175 m_defer_nbytes
= nbytes
;
176 m_defer_timer
= &timer
;
178 timer
.m_state
= (int *)&m_defer_buffer
;
181 timer
.Start(m_timeout
* 1000, FALSE
);
183 // If the socket is readable, call DoDefer for the first time
184 if (GSocket_Select(m_socket
, GSOCK_INPUT_FLAG
))
186 //wxLogMessage("Llamando al primer DoDefer");
190 // Wait for buffer completion.
191 while (m_defer_buffer
!= NULL
)
196 // Disable defering mode.
197 m_defering
= NO_DEFER
;
198 m_defer_timer
= NULL
;
200 // Return the number of bytes read from the socket.
201 //wxLogMessage("Saliendo de DeferRead: total: %d bytes", nbytes - m_defer_nbytes);
202 return nbytes
-m_defer_nbytes
;
205 wxSocketBase
& wxSocketBase::Read(char* buffer
, wxUint32 nbytes
)
210 m_lcount
= _Read(buffer
, nbytes
);
212 // If in WAITALL mode, all bytes should have been read.
213 if (m_flags
& WAITALL
)
214 m_error
= (m_lcount
!= nbytes
);
216 m_error
= (m_lcount
== 0);
218 // Trigger another read event if there is still data available.
225 wxUint32
wxSocketBase::_Read(char* buffer
, wxUint32 nbytes
)
230 // we try this even if the connection has already been closed.
231 total
= GetPushback(buffer
, nbytes
, FALSE
);
235 // If the socket is not connected, or we have got the whole
236 // needed buffer, return immedately
237 if (!m_connected
|| !m_socket
|| !nbytes
)
240 // Possible combinations (they are checked in this order)
247 if (m_flags
& NOWAIT
) // NOWAIT
249 GSocket_SetNonBlocking(m_socket
, TRUE
);
250 ret
= GSocket_Read(m_socket
, buffer
, nbytes
);
251 GSocket_SetNonBlocking(m_socket
, FALSE
);
256 else if ((m_flags
& SPEED
) && (m_flags
& WAITALL
)) // SPEED, WAITALL
258 while (ret
> 0 && nbytes
> 0)
260 ret
= GSocket_Read(m_socket
, buffer
, nbytes
);
265 // In case the last call was an error ...
269 else if (m_flags
& SPEED
) // SPEED, !WAITALL
271 ret
= GSocket_Read(m_socket
, buffer
, nbytes
);
276 else // NONE or WAITALL
278 ret
= DeferRead(buffer
, nbytes
);
287 wxSocketBase
& wxSocketBase::ReadMsg(char* buffer
, wxUint32 nbytes
)
289 #define MAX_DISCARD_SIZE (10 * 1024)
291 wxUint32 len
, len2
, sig
, total
;
296 unsigned char sig
[4];
297 unsigned char len
[4];
306 SetFlags((m_flags
& SPEED
) | WAITALL
);
308 if (_Read((char *)&msg
, sizeof(msg
)) != sizeof(msg
))
311 sig
= (wxUint32
)msg
.sig
[0];
312 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
313 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
314 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
316 if (sig
!= 0xfeeddead)
318 wxLogMessage(wxT("Warning: invalid signature returned to ReadMsg"));
322 len
= (wxUint32
)msg
.len
[0];
323 len
|= (wxUint32
)(msg
.len
[1] << 8);
324 len
|= (wxUint32
)(msg
.len
[2] << 16);
325 len
|= (wxUint32
)(msg
.len
[3] << 24);
327 //wxLogMessage("Readmsg: %d %d %d %d -> len == %d",
328 // msg.len[0], msg.len[1], msg.len[2], msg.len[3], len);
338 // This check is necessary so that we don't attemp to read if
339 // the msg was zero bytes long.
342 total
= _Read(buffer
, len
);
349 char *discard_buffer
= new char[MAX_DISCARD_SIZE
];
352 // NOTE: discarded bytes don't add to m_lcount.
355 discard_len
= ((len2
> MAX_DISCARD_SIZE
)? MAX_DISCARD_SIZE
: len2
);
356 discard_len
= _Read(discard_buffer
, discard_len
);
359 while ((discard_len
> 0) && len2
);
361 delete [] discard_buffer
;
366 if (_Read((char *)&msg
, sizeof(msg
)) != sizeof(msg
))
369 sig
= (wxUint32
)msg
.sig
[0];
370 sig
|= (wxUint32
)(msg
.sig
[1] << 8);
371 sig
|= (wxUint32
)(msg
.sig
[2] << 16);
372 sig
|= (wxUint32
)(msg
.sig
[3] << 24);
374 if (sig
!= 0xdeadfeed)
376 //wxLogMessage(wxT("Warning: invalid signature returned to ReadMsg"));
392 #undef MAX_DISCARD_SIZE
395 wxSocketBase
& wxSocketBase::Peek(char* buffer
, wxUint32 nbytes
)
400 m_lcount
= _Read(buffer
, nbytes
);
401 Pushback(buffer
, nbytes
);
403 // If in WAITALL mode, all bytes should have been read.
404 if (m_flags
& WAITALL
)
405 m_error
= (m_lcount
!= nbytes
);
407 m_error
= (m_lcount
== 0);
409 // Trigger another read event if there is still data available.
416 wxUint32
wxSocketBase::DeferWrite(const char *buffer
, wxUint32 nbytes
)
419 _wxSocketInternalTimer timer
;
421 wxASSERT(m_defering
== NO_DEFER
);
422 //wxLogMessage("Entrando a DeferWrite");
424 m_defering
= DEFER_WRITE
;
426 // Set the current buffer
427 m_defer_buffer
= (char *)buffer
;
428 m_defer_nbytes
= nbytes
;
429 m_defer_timer
= &timer
;
432 timer
.m_state
= (int *)&m_defer_buffer
;
435 timer
.Start(m_timeout
* 1000, FALSE
);
437 // If the socket is writable, call DoDefer for the first time
438 if (GSocket_Select(m_socket
, GSOCK_OUTPUT_FLAG
))
440 //wxLogMessage("Llamando al primer DoDefer");
444 // Wait for buffer completion.
445 while (m_defer_buffer
!= NULL
)
450 // Disable defering mode
451 m_defer_timer
= NULL
;
452 m_defering
= NO_DEFER
;
455 s
.Printf(wxT("Saliendo de DeferWrite: total %d bytes"), nbytes
-m_defer_nbytes
);
458 return nbytes
-m_defer_nbytes
;
461 wxSocketBase
& wxSocketBase::Write(const char *buffer
, wxUint32 nbytes
)
466 m_lcount
= _Write(buffer
, nbytes
);
468 // If in WAITALL mode, all bytes should have been written.
469 if (m_flags
& WAITALL
)
470 m_error
= (m_lcount
!= nbytes
);
472 m_error
= (m_lcount
== 0);
474 // Trigger another write event if the socket is still writable
477 // TODO: TriggerWrite
481 wxUint32
wxSocketBase::_Write(const char *buffer
, wxUint32 nbytes
)
486 if (!m_connected
|| !m_socket
)
489 // Possible combinations (they are checked in this order)
496 if (m_flags
& NOWAIT
) // NOWAIT
498 GSocket_SetNonBlocking(m_socket
, TRUE
);
499 ret
= GSocket_Write(m_socket
, buffer
, nbytes
);
500 GSocket_SetNonBlocking(m_socket
, FALSE
);
505 else if ((m_flags
& SPEED
) && (m_flags
& WAITALL
)) // SPEED, WAITALL
507 while (ret
> 0 && nbytes
> 0)
509 ret
= GSocket_Write(m_socket
, buffer
, nbytes
);
514 // In case the last call was an error ...
518 else if (m_flags
& SPEED
) // SPEED, !WAITALL
520 ret
= GSocket_Write(m_socket
, buffer
, nbytes
);
525 else // NONE or WAITALL
527 ret
= DeferWrite(buffer
, nbytes
);
536 wxSocketBase
& wxSocketBase::WriteMsg(const char *buffer
, wxUint32 nbytes
)
542 unsigned char sig
[4];
543 unsigned char len
[4];
552 SetFlags((m_flags
& SPEED
) | WAITALL
);
554 // warning about 'cast truncates constant value'
556 # pragma warning(disable: 4310)
557 #endif // __VISUALC__
559 msg
.sig
[0] = (unsigned char) 0xad;
560 msg
.sig
[1] = (unsigned char) 0xde;
561 msg
.sig
[2] = (unsigned char) 0xed;
562 msg
.sig
[3] = (unsigned char) 0xfe;
564 msg
.len
[0] = (unsigned char) nbytes
& 0xff;
565 msg
.len
[1] = (unsigned char) (nbytes
>> 8) & 0xff;
566 msg
.len
[2] = (unsigned char) (nbytes
>> 16) & 0xff;
567 msg
.len
[3] = (unsigned char) (nbytes
>> 24) & 0xff;
569 //wxLogMessage("Writemsg: %d %d %d %d -> %d",
571 // (nbytes >> 8) & 0xff,
572 // (nbytes >> 16) & 0xff,
573 // (nbytes >> 24) & 0xff,
577 if (_Write((char *)&msg
, sizeof(msg
)) < sizeof(msg
))
580 total
= _Write(buffer
, nbytes
);
585 msg
.sig
[0] = (unsigned char) 0xed;
586 msg
.sig
[1] = (unsigned char) 0xfe;
587 msg
.sig
[2] = (unsigned char) 0xad;
588 msg
.sig
[3] = (unsigned char) 0xde;
589 msg
.len
[0] = msg
.len
[1] = msg
.len
[2] = msg
.len
[3] = (char) 0;
591 if ((_Write((char *)&msg
, sizeof(msg
))) < sizeof(msg
))
602 // TODO: TriggerWrite
606 # pragma warning(default: 4310)
607 #endif // __VISUALC__
610 wxSocketBase
& wxSocketBase::Unread(const char *buffer
, wxUint32 nbytes
)
613 Pushback(buffer
, nbytes
);
621 void wxSocketBase::DoDefer()
631 ret
= GSocket_Read(m_socket
, m_defer_buffer
, m_defer_nbytes
);
634 ret
= GSocket_Write(m_socket
, m_defer_buffer
, m_defer_nbytes
);
641 m_defer_nbytes
-= ret
;
643 // If we are waiting for all bytes to be acquired, keep the defering
645 if (!(m_flags
& WAITALL
) || !m_defer_nbytes
|| ret
< 0)
647 m_defer_buffer
= NULL
;
651 m_defer_buffer
+= ret
;
652 m_defer_timer
->Start(m_timeout
* 1000, FALSE
);
655 //wxLogMessage("DoDefer ha transferido %d bytes", ret);
658 wxSocketBase
& wxSocketBase::Discard()
660 #define MAX_BUFSIZE (10*1024)
663 char *my_data
= new char[MAX_BUFSIZE
];
664 wxUint32 recv_size
= MAX_BUFSIZE
;
673 while (recv_size
== MAX_BUFSIZE
)
675 recv_size
= _Read(my_data
, MAX_BUFSIZE
);
683 // Trigger another read event if there is still data available.
692 // --------------------------------------------------------------
693 // wxSocketBase get local or peer addresses
694 // --------------------------------------------------------------
696 bool wxSocketBase::GetPeer(wxSockAddress
& addr_man
) const
703 peer
= GSocket_GetPeer(m_socket
);
704 addr_man
.SetAddress(peer
);
705 GAddress_destroy(peer
);
710 bool wxSocketBase::GetLocal(wxSockAddress
& addr_man
) const
717 local
= GSocket_GetLocal(m_socket
);
718 addr_man
.SetAddress(local
);
719 GAddress_destroy(local
);
724 // --------------------------------------------------------------
725 // wxSocketBase save and restore socket state
726 // --------------------------------------------------------------
728 void wxSocketBase::SaveState()
730 wxSocketState
*state
;
732 state
= new wxSocketState();
734 state
->notify_state
= m_notify_state
;
735 state
->evt_notify_state
= m_neededreq
;
736 state
->socket_flags
= m_flags
;
737 state
->c_callback
= m_cbk
;
738 state
->c_callback_data
= m_cdata
;
740 m_states
.Append(state
);
743 void wxSocketBase::RestoreState()
746 wxSocketState
*state
;
748 node
= m_states
.Last();
752 state
= (wxSocketState
*)node
->Data();
754 SetFlags(state
->socket_flags
);
755 m_neededreq
= state
->evt_notify_state
;
756 m_cbk
= state
->c_callback
;
757 m_cdata
= state
->c_callback_data
;
758 Notify(state
->notify_state
);
765 // --------------------------------------------------------------
766 // wxSocketBase Wait functions
767 // --------------------------------------------------------------
769 // GRG: I have completely rewritten this family of functions
770 // so that they don't depend on event notifications; instead,
771 // they poll the socket, using GSocket_Select(), to check for
772 // the specified combination of event flags, until an event
773 // occurs or until the timeout ellapses. The polling loop
774 // calls PROCESS_EVENTS(), so this won't block the GUI.
776 bool wxSocketBase::_Wait(long seconds
, long milliseconds
, wxSocketEventFlags flags
)
778 GSocketEventFlags result
;
779 _wxSocketInternalTimer timer
;
783 // Check for valid socket
787 // If it is not a server, it must be connected or establishing connection
788 if ((m_type
!= SOCK_SERVER
) && (!m_connected
&& !m_establishing
))
791 // Check for valid timeout value
793 timeout
= seconds
* 1000 + milliseconds
;
795 timeout
= m_timeout
* 1000;
800 timer
.m_state
= &state
;
802 timer
.Start(timeout
, TRUE
);
805 // Active polling (without using events)
807 // NOTE: this duplicates some of the code in OnRequest (lost
808 // connection and connection establishment handling) but this
809 // doesn't hurt. It has to be here because the event might
810 // be a bit delayed, and it has to be in OnRequest as well
811 // because maybe the WaitXXX functions are not being used.
813 // Do this at least once (important if timeout == 0, when
814 // we are just polling)
817 result
= GSocket_Select(m_socket
, flags
| GSOCK_LOST_FLAG
);
820 if (result
& GSOCK_LOST_FLAG
)
823 m_defer_buffer
= NULL
;
828 // Incoming connection (server) or connection established (client)
829 if (result
& GSOCK_CONNECTION_FLAG
)
833 m_establishing
= FALSE
;
837 // If we are in the middle of a R/W operation, do not propagate
839 if ( ((result
& GSOCK_INPUT_FLAG
) || (result
& GSOCK_OUTPUT_FLAG
))
840 && (!m_writing
) && (!m_reading
) )
843 if (m_defer_buffer == NULL)
854 while ((state
== -1) && timeout
);
860 bool wxSocketBase::Wait(long seconds
, long milliseconds
)
862 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
|
864 GSOCK_CONNECTION_FLAG
|
868 bool wxSocketBase::WaitForRead(long seconds
, long milliseconds
)
870 // Check pushback buffer
874 return _Wait(seconds
, milliseconds
, GSOCK_INPUT_FLAG
);
877 bool wxSocketBase::WaitForWrite(long seconds
, long milliseconds
)
879 return _Wait(seconds
, milliseconds
, GSOCK_OUTPUT_FLAG
);
882 bool wxSocketBase::WaitForLost(long seconds
, long milliseconds
)
884 return _Wait(seconds
, milliseconds
, GSOCK_LOST_FLAG
);
887 void wxSocketBase::SetTimeout(long seconds
)
892 GSocket_SetTimeout(m_socket
, m_timeout
);
895 // --------------------------------------------------------------
896 // wxSocketBase flags
897 // --------------------------------------------------------------
899 void wxSocketBase::SetFlags(wxSockFlags _flags
)
904 // --------------------------------------------------------------
905 // wxSocketBase callback management
906 // --------------------------------------------------------------
908 wxSocketBase::wxSockCbk
wxSocketBase::Callback(wxSockCbk cbk_
)
910 wxSockCbk old_cbk
= cbk_
;
916 char *wxSocketBase::CallbackData(char *data
)
918 char *old_data
= m_cdata
;
924 // --------------------------------------------------------------
925 // wxSocketBase automatic notifier
926 // --------------------------------------------------------------
928 // All events (INPUT, OUTPUT, CONNECTION, LOST) are now always
929 // internally watched; but users will only be notified of those
930 // events they are interested in.
932 static void LINKAGEMODE
wx_socket_callback(GSocket
* WXUNUSED(socket
),
933 GSocketEvent event
, char *cdata
)
935 wxSocketBase
*sckobj
= (wxSocketBase
*)cdata
;
937 sckobj
->OnRequest((wxSocketNotify
)event
);
940 wxSocketEventFlags
wxSocketBase::EventToNotify(wxSocketNotify evt
)
944 case GSOCK_INPUT
: return GSOCK_INPUT_FLAG
;
945 case GSOCK_OUTPUT
: return GSOCK_OUTPUT_FLAG
;
946 case GSOCK_CONNECTION
: return GSOCK_CONNECTION_FLAG
;
947 case GSOCK_LOST
: return GSOCK_LOST_FLAG
;
952 void wxSocketBase::SetNotify(wxSocketEventFlags flags
)
957 void wxSocketBase::Notify(bool notify
)
959 m_notify_state
= notify
;
962 void wxSocketBase::OnRequest(wxSocketNotify req_evt
)
964 wxSocketEvent
event(m_id
);
965 wxSocketEventFlags flag
= EventToNotify(req_evt
);
967 // fprintf(stderr, "%s: Entering OnRequest (evt %d)\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt);
969 // NOTE: this duplicates some of the code in _Wait (lost
970 // connection and connection establishment handling) but
971 // this doesn't hurt. It has to be here because maybe the
972 // WaitXXX are not being used, and it has to be in _Wait
973 // as well because the event might be a bit delayed.
977 case wxSOCKET_CONNECTION
:
978 m_establishing
= FALSE
;
982 m_defer_buffer
= NULL
;
986 case wxSOCKET_OUTPUT
:
989 // fprintf(stderr, "%s: Habia buffer, evt %d skipped\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt);
991 // Do not notify to user
997 // If we are in the middle of a R/W operation, do not
998 // propagate events to users.
999 if (((req_evt
== wxSOCKET_INPUT
) && m_reading
) ||
1000 ((req_evt
== wxSOCKET_OUTPUT
) && m_writing
))
1002 // fprintf(stderr, "%s: Swallowed evt %d\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt);
1006 if (((m_neededreq
& flag
) == flag
) && m_notify_state
)
1008 // fprintf(stderr, "%s: Evt %d delivered\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt);
1009 event
.m_socket
= this;
1010 event
.m_skevt
= req_evt
;
1011 ProcessEvent(event
);
1012 OldOnNotify(req_evt
);
1015 m_cbk(*this, req_evt
, m_cdata
);
1018 // fprintf(stderr, "%s: Exiting OnRequest (evt %d)\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt);
1021 void wxSocketBase::OldOnNotify(wxSocketNotify
WXUNUSED(evt
))
1025 // --------------------------------------------------------------
1026 // wxSocketBase set event handler
1027 // --------------------------------------------------------------
1029 void wxSocketBase::SetEventHandler(wxEvtHandler
& h_evt
, int id
)
1031 SetNextHandler(&h_evt
);
1035 // --------------------------------------------------------------
1036 // wxSocketBase pushback
1037 // --------------------------------------------------------------
1039 void wxSocketBase::Pushback(const char *buffer
, wxUint32 size
)
1041 if (m_unread
== NULL
)
1042 m_unread
= (char *)malloc(size
);
1046 tmp
= (char *)malloc(m_unrd_size
+ size
);
1047 memcpy(tmp
+size
, m_unread
, m_unrd_size
);
1053 m_unrd_size
+= size
;
1055 memcpy(m_unread
, buffer
, size
);
1058 wxUint32
wxSocketBase::GetPushback(char *buffer
, wxUint32 size
, bool peek
)
1063 if (size
> (m_unrd_size
-m_unrd_cur
))
1064 size
= m_unrd_size
-m_unrd_cur
;
1066 memcpy(buffer
, (m_unread
+m_unrd_cur
), size
);
1070 if (m_unrd_size
== m_unrd_cur
) {
1081 // --------------------------------------------------------------
1083 // --------------------------------------------------------------
1085 // --------------------------------------------------------------
1086 // wxSocketServer ctor and dtor
1087 // --------------------------------------------------------------
1089 wxSocketServer::wxSocketServer(wxSockAddress
& addr_man
,
1090 wxSockFlags flags
) :
1091 wxSocketBase(flags
, SOCK_SERVER
)
1093 // Create the socket
1094 m_socket
= GSocket_new();
1099 // Setup the socket as server
1100 GSocket_SetLocal(m_socket
, addr_man
.GetAddress());
1101 if (GSocket_SetServer(m_socket
) != GSOCK_NOERROR
)
1103 GSocket_destroy(m_socket
);
1108 GSocket_SetTimeout(m_socket
, m_timeout
);
1109 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1110 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1111 wx_socket_callback
, (char *)this);
1115 // --------------------------------------------------------------
1116 // wxSocketServer Accept
1117 // --------------------------------------------------------------
1119 bool wxSocketServer::AcceptWith(wxSocketBase
& sock
, bool wait
)
1121 GSocket
*child_socket
;
1126 // GRG: If wait == FALSE, then the call should be nonblocking.
1127 // When we are finished, we put the socket to blocking mode
1131 GSocket_SetNonBlocking(m_socket
, TRUE
);
1133 child_socket
= GSocket_WaitConnection(m_socket
);
1136 GSocket_SetNonBlocking(m_socket
, FALSE
);
1138 // GRG: this was not being handled!
1139 if (child_socket
== NULL
)
1142 sock
.m_type
= SOCK_INTERNAL
;
1143 sock
.m_socket
= child_socket
;
1144 sock
.m_connected
= TRUE
;
1146 GSocket_SetTimeout(sock
.m_socket
, sock
.m_timeout
);
1147 GSocket_SetCallback(sock
.m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1148 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1149 wx_socket_callback
, (char *)&sock
);
1154 wxSocketBase
*wxSocketServer::Accept(bool wait
)
1156 wxSocketBase
* sock
= new wxSocketBase();
1158 sock
->SetFlags((wxSockFlags
)m_flags
);
1160 if (!AcceptWith(*sock
, wait
))
1166 bool wxSocketServer::WaitForAccept(long seconds
, long milliseconds
)
1168 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
);
1171 // --------------------------------------------------------------
1173 // --------------------------------------------------------------
1175 // --------------------------------------------------------------
1176 // wxSocketClient ctor and dtor
1177 // --------------------------------------------------------------
1179 wxSocketClient::wxSocketClient(wxSockFlags _flags
) :
1180 wxSocketBase(_flags
, SOCK_CLIENT
)
1184 wxSocketClient::~wxSocketClient()
1188 // --------------------------------------------------------------
1189 // wxSocketClient Connect functions
1190 // --------------------------------------------------------------
1191 bool wxSocketClient::Connect(wxSockAddress
& addr_man
, bool wait
)
1198 // This should never happen.
1200 GSocket_destroy(m_socket
);
1202 // Initialize all socket stuff ...
1203 m_socket
= GSocket_new();
1204 m_connected
= FALSE
;
1205 m_establishing
= FALSE
;
1210 GSocket_SetTimeout(m_socket
, m_timeout
);
1211 GSocket_SetCallback(m_socket
, GSOCK_INPUT_FLAG
| GSOCK_OUTPUT_FLAG
|
1212 GSOCK_LOST_FLAG
| GSOCK_CONNECTION_FLAG
,
1213 wx_socket_callback
, (char *)this);
1215 // GRG: If wait == FALSE, then the call should be nonblocking.
1216 // When we are finished, we put the socket to blocking mode
1220 GSocket_SetNonBlocking(m_socket
, TRUE
);
1222 GSocket_SetPeer(m_socket
, addr_man
.GetAddress());
1223 err
= GSocket_Connect(m_socket
, GSOCK_STREAMED
);
1226 GSocket_SetNonBlocking(m_socket
, FALSE
);
1228 if (err
!= GSOCK_NOERROR
)
1230 if (err
== GSOCK_WOULDBLOCK
)
1231 m_establishing
= TRUE
;
1240 bool wxSocketClient::WaitOnConnect(long seconds
, long milliseconds
)
1242 if (m_connected
) // Already connected
1245 if (!m_establishing
|| !m_socket
) // No connection in progress
1248 return _Wait(seconds
, milliseconds
, GSOCK_CONNECTION_FLAG
);
1251 // --------------------------------------------------------------
1253 // --------------------------------------------------------------
1255 wxSocketEvent::wxSocketEvent(int id
)
1258 wxEventType type
= (wxEventType
)wxEVT_SOCKET
;
1263 void wxSocketEvent::CopyObject(wxObject
& obj_d
) const
1265 wxSocketEvent
*event
= (wxSocketEvent
*)&obj_d
;
1267 wxEvent::CopyObject(obj_d
);
1269 event
->m_skevt
= m_skevt
;
1270 event
->m_socket
= m_socket
;
1273 // --------------------------------------------------------------------------
1275 // --------------------------------------------------------------------------
1276 class WXDLLEXPORT wxSocketModule
: public wxModule
{
1277 DECLARE_DYNAMIC_CLASS(wxSocketModule
)
1280 return GSocket_Init();
1287 IMPLEMENT_DYNAMIC_CLASS(wxSocketModule
, wxModule
)