]>
git.saurik.com Git - wxWidgets.git/blob - src/common/sckint.cpp
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Socket handler classes
4 // Authors: Guilhem Lavaux (completely rewritten from a basic API of Andrew
5 // Davidson(1995) in wxWeb)
8 // Copyright: (C) 1999, 1998, 1997, Guilhem Lavaux
10 // License: see wxWindows license
11 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "sckint.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #define WXSOCK_INTERNAL
26 #include <wx/object.h>
28 #include <wx/socket.h>
29 #include <wx/thread.h>
30 #include <wx/sckint.h>
38 // -----------------------
39 // System specific headers
40 // -----------------------
43 // in order to avoid problems with our c library and double definitions
44 #define close closesocket
45 #define ioctl ioctlsocket
47 #include <wx/mac/macsock.h>
50 #if defined(__WINDOWS__)
59 #include <sys/socket.h>
61 #include <sys/ioctl.h>
67 #include <sys/filio.h>
80 #define READ_MASK wxSocketBase::REQ_READ | wxSocketBase::REQ_ACCEPT | wxSocketBase::REQ_LOST
81 #define WRITE_MASK wxSocketBase::REQ_WRITE | wxSocketBase::REQ_CONNECT
83 // --------------------------------------------------------------
84 // --------- SocketWaiter ---------------------------------------
85 // --------------------------------------------------------------
87 SocketWaiter::SocketWaiter(wxSocketBase
*socket
,
88 wxSocketInternal
*internal
)
89 : m_socket(socket
), m_internal(internal
), m_fd(internal
->GetFD())
93 SocketWaiter::~SocketWaiter()
97 void SocketWaiter::ProcessReadEvent()
102 ret
= recv(m_fd
, &c
, 1, MSG_PEEK
);
104 // We are a server => emit a EVT_ACCEPT event.
105 if (ret
== -1 && m_socket
->GetType() == wxSocketBase::SOCK_SERVER
) {
106 m_socket
->OnRequest(wxSocketBase::EVT_ACCEPT
);
110 // Else, no error => there is something to be read else
111 // we've lost the connection.
113 m_socket
->OnRequest(wxSocketBase::EVT_READ
);
115 m_socket
->OnRequest(wxSocketBase::EVT_LOST
);
120 void SocketWaiter::ProcessWriteEvent()
122 if (m_socket
->IsConnected())
123 m_socket
->OnRequest(wxSocketBase::EVT_CONNECT
);
125 m_socket
->OnRequest(wxSocketBase::EVT_WRITE
);
128 void *SocketWaiter::Entry()
131 fd_set sockrd_set
, sockwr_set
;
140 FD_ZERO(&sockrd_set
);
141 FD_ZERO(&sockwr_set
);
143 if ((m_socket
->NeededReq() & READ_MASK
) != 0)
144 FD_SET(m_fd
, &sockrd_set
);
145 if ((m_socket
->NeededReq() & WRITE_MASK
) != 0)
146 FD_SET(m_fd
, &sockwr_set
);
148 m_internal
->AcquireFD();
149 ret
= select(FD_SETSIZE
, &sockrd_set
, &sockwr_set
, NULL
, &tv
);
150 m_internal
->ReleaseFD();
152 if (FD_ISSET(m_fd
, &sockrd_set
))
155 if (FD_ISSET(m_fd
, &sockwr_set
))
165 // If nothing happened, we wait for 100 ms.
168 // Check whether we should exit.
176 // --------------------------------------------------------------
177 // --------- SocketRequester ------------------------------------
178 // --------------------------------------------------------------
180 SocketRequester::SocketRequester(wxSocketBase
*socket
,
181 wxSocketInternal
*internal
)
182 : m_socket(socket
), m_internal(internal
), m_fd(internal
->GetFD())
186 SocketRequester::~SocketRequester()
190 bool SocketRequester::WaitFor(wxSocketBase::wxRequestNotify req
, int millisec
)
194 fd_set sockrd_set
, sockwr_set
;
197 tv
.tv_sec
= millisec
/ 1000;
198 tv
.tv_usec
= (millisec
% 1000) * 1000;
200 if ((req
& READ_MASK
) != 0)
201 FD_ZERO(&sockrd_set
);
202 FD_ZERO(&sockwr_set
);
204 FD_SET(m_fd
, &sockrd_set
);
205 FD_SET(m_fd
, &sockwr_set
);
207 m_internal
->AcquireFD();
208 ret
= select(FD_SETSIZE
, &sockrd_set
, &sockwr_set
, NULL
, &tv
);
209 m_internal
->ReleaseFD();
214 void SocketRequester::ProcessReadEvent(SockRequest
*req
)
219 // We'll wait for the first byte, in case a "timeout event" occurs it returns // immediately
220 if (!WaitFor(wxSocketBase::REQ_READ
, req
->timeout
)) {
221 m_internal
->EndRequest(req
);
225 m_internal
->AcquireFD();
226 ret
= recv(m_fd
, req
->buffer
, req
->size
,
227 (req
->type
== wxSocketBase::REQ_PEEK
) ? MSG_PEEK
: 0);
228 m_internal
->ReleaseFD();
230 // An error occured, we exit.
233 m_internal
->EndRequest(req
);
238 // If the buffer isn't full (and we want it to be full), we don't unqueue it.
239 if ((len
< req
->size
) && (m_socket
->GetFlags() & wxSocketBase::WAITALL
)) {
241 req
->io_nbytes
+= len
;
246 req
->io_nbytes
+= len
;
247 m_internal
->EndRequest(req
);
250 void SocketRequester::ProcessWriteEvent(SockRequest
*req
)
255 m_internal
->AcquireFD();
256 ret
= send(m_fd
, req
->buffer
, req
->size
, 0);
257 m_internal
->ReleaseFD();
260 m_internal
->EndRequest(req
);
264 if ((len
< req
->size
) && ((m_socket
->GetFlags() & wxSocketBase::WAITALL
) != 0)) {
266 req
->io_nbytes
+= len
;
270 req
->io_nbytes
+= len
;
271 m_internal
->EndRequest(req
);
274 void SocketRequester::ProcessWaitEvent(SockRequest
*req
)
276 if (WaitFor(req
->type
, req
->timeout
))
277 req
->io_nbytes
= 1; // We put 1 in the counter to tell the requester
278 // there is no timeout.
282 m_internal
->EndRequest(req
);
285 void *SocketRequester::Entry()
290 // Wait for a new request or a destroy message.
291 req
= m_internal
->WaitForReq();
292 m_internal
->m_end_requester
.Lock();
293 if (TestDestroy() || req
== NULL
) {
294 m_internal
->m_invalid_requester
= TRUE
;
295 m_internal
->m_end_requester
.Unlock();
298 m_internal
->m_end_requester
.Unlock();
300 if ((req
->type
& wxSocketBase::REQ_WAIT
) != 0) {
301 ProcessWaitEvent(req
);
306 case wxSocketBase::REQ_READ
:
307 case wxSocketBase::REQ_PEEK
:
308 ProcessReadEvent(req
);
310 case wxSocketBase::REQ_WRITE
:
311 ProcessWriteEvent(req
);
318 // --------------------------------------------------------------
319 // --------- wxSocketInternal -----------------------------------
320 // --------------------------------------------------------------
322 wxSocketInternal::wxSocketInternal(wxSocketBase
*socket
)
325 m_thread_requester
= NULL
;
326 m_thread_waiter
= NULL
;
327 m_invalid_requester
= TRUE
;
328 m_request_locker
.Lock();
331 wxSocketInternal::~wxSocketInternal()
334 wxASSERT(m_thread_requester
== NULL
);
336 wxASSERT(m_thread_waiter
== NULL
);
337 m_request_locker
.Unlock();
340 // ----------------------------------------------------------------------
341 // WaitForReq: it is called by SocketRequester and should return the next
342 // socket request if available
343 // ----------------------------------------------------------------------
344 SockRequest
*wxSocketInternal::WaitForReq()
348 node
= m_requests
.First();
350 m_socket_cond
.Wait(m_request_locker
, 1, 0);
352 node
= m_requests
.First();
357 return (SockRequest
*)node
->Data();
360 // ----------------------------------------------------------------------
361 // EndRequest: Should be called to finalize a request
362 // ----------------------------------------------------------------------
363 void wxSocketInternal::EndRequest(SockRequest
*req
)
369 node
= m_requests
.Member((wxObject
*)req
);
374 void wxSocketInternal::AcquireFD()
379 void wxSocketInternal::ReleaseFD()
381 m_fd_locker
.Unlock();
384 void wxSocketInternal::ResumeRequester()
388 wxASSERT(m_thread_requester
== NULL
);
390 m_end_requester
.Lock();
391 if (m_invalid_requester
) {
392 delete m_thread_requester
;
394 m_thread_requester
= new SocketRequester(m_socket
, this);
395 m_thread_requester
->m_fd
= m_socket
->m_fd
;
397 err
= m_thread_requester
->Create();
398 wxASSERT(err
== wxTHREAD_NO_ERROR
);
400 err
= m_thread_requester
->Run();
401 wxASSERT(err
== wxTHREAD_NO_ERROR
);
403 m_invalid_requester
= FALSE
;
405 m_end_requester
.Unlock();
408 void wxSocketInternal::StopRequester()
410 if (m_invalid_requester
) {
411 delete m_thread_requester
;
412 m_thread_requester
= NULL
;
413 m_invalid_requester
= FALSE
;
417 wxASSERT(m_thread_requester
!= NULL
);
419 m_socket_locker
.Lock();
421 // Send a signal to the requester.
422 if (m_requests
.Number() == 0)
423 m_socket_cond
.Signal();
425 m_socket_locker
.Unlock();
427 // Finish the destruction of the requester.
428 m_thread_requester
->Delete();
430 delete m_thread_requester
;
431 m_thread_requester
= NULL
;
434 void wxSocketInternal::ResumeWaiter()
438 if (m_thread_waiter
!= NULL
)
441 m_thread_waiter
= new SocketWaiter(m_socket
, this);
442 m_thread_waiter
->m_fd
= m_socket
->m_fd
;
444 err
= m_thread_waiter
->Create();
445 wxASSERT(err
== wxTHREAD_NO_ERROR
);
447 err
= m_thread_waiter
->Run();
448 wxASSERT(err
== wxTHREAD_NO_ERROR
);
451 void wxSocketInternal::StopWaiter()
453 if (m_thread_waiter
== NULL
)
456 m_thread_waiter
->Delete();
458 delete m_thread_waiter
;
459 m_thread_waiter
= NULL
;
462 // ----------------------------------------------------------------------
464 // ----------------------------------------------------------------------
465 void wxSocketInternal::QueueRequest(SockRequest
*request
, bool async
)
468 if (m_thread_requester
== NULL
)
471 m_request_locker
.Lock();
472 request
->done
= FALSE
;
473 m_requests
.Append((wxObject
*)request
);
474 m_socket_cond
.Signal();
475 m_request_locker
.Unlock();
480 if (wxThread::IsMain())
481 while (!request
->done
) {
485 while (!request
->done
) {
490 m_request_locker
.Lock();
492 if ((request
->type
& wxSocketBase::REQ_WAIT
) != 0) {
493 m_thread_requester
->ProcessWaitEvent(request
);
496 request
->done
= FALSE
;
498 switch (request
->type
) {
499 case wxSocketBase::REQ_PEEK
:
500 case wxSocketBase::REQ_READ
:
501 m_thread_requester
->ProcessReadEvent(request
);
503 case wxSocketBase::REQ_WRITE
:
504 m_thread_requester
->ProcessWriteEvent(request
);
508 request
->done
= TRUE
;
509 m_request_locker
.Unlock();
513 void wxSocketInternal::WaitForEnd(SockRequest
*request
)