]>
git.saurik.com Git - wxWidgets.git/blob - src/common/sckint.cpp
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Socket internal classes
4 // Authors: Guilhem Lavaux
7 // Copyright: (C) 1999, 1998, 1997, Guilhem Lavaux
9 // License: see wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "sckint.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
24 #define WXSOCK_INTERNAL
25 #include "wx/object.h"
27 #include "wx/socket.h"
28 #include "wx/thread.h"
29 #include "wx/sckint.h"
30 #include "wx/sckint.h"
33 // IRIX requires bstring.h be included to use select()
44 // -----------------------
45 // System specific headers
46 // -----------------------
49 // in order to avoid problems with our c library and double definitions
50 #define close closesocket
51 #define ioctl ioctlsocket
53 #include <wx/mac/macsock.h>
56 #if defined(__WINDOWS__)
65 #include <sys/socket.h>
67 #include <sys/ioctl.h>
73 #include <sys/filio.h>
86 #define READ_MASK wxSocketBase::REQ_READ | wxSocketBase::REQ_ACCEPT | wxSocketBase::REQ_LOST
87 #define WRITE_MASK wxSocketBase::REQ_WRITE | wxSocketBase::REQ_CONNECT
89 // --------------------------------------------------------------
90 // --------- SocketWaiter ---------------------------------------
91 // --------------------------------------------------------------
94 SocketWaiter::SocketWaiter(wxSocketBase
*socket
,
95 wxSocketInternal
*internal
)
97 m_socket(socket
), m_internal(internal
), m_fd(internal
->GetFD())
101 SocketWaiter::~SocketWaiter()
105 void SocketWaiter::ProcessReadEvent()
110 m_internal
->AcquireFD();
111 ret
= recv(m_fd
, &c
, 1, MSG_PEEK
);
112 m_internal
->ReleaseFD();
114 // We are a server => emit a EVT_ACCEPT event.
115 if (ret
== -1 && m_socket
->GetType() == wxSocketBase::SOCK_SERVER
) {
116 m_socket
->OnRequest(wxSocketBase::EVT_ACCEPT
);
120 // Else, no error => there is something to be read else
121 // we've lost the connection.
123 m_socket
->OnRequest(wxSocketBase::EVT_READ
);
125 m_socket
->OnRequest(wxSocketBase::EVT_LOST
);
126 m_internal
->ReleaseData(); // In that case, we mustn't forget to unlock the mutex.
131 void SocketWaiter::ProcessWriteEvent()
133 if (m_socket
->IsConnected())
134 m_socket
->OnRequest(wxSocketBase::EVT_CONNECT
);
136 m_socket
->OnRequest(wxSocketBase::EVT_WRITE
);
139 void *SocketWaiter::Entry()
142 fd_set sockrd_set
, sockwr_set
;
151 FD_ZERO(&sockrd_set
);
152 FD_ZERO(&sockwr_set
);
154 m_internal
->AcquireData();
156 if ((m_socket
->NeededReq() & READ_MASK
) != 0)
157 FD_SET(m_fd
, &sockrd_set
);
158 if ((m_socket
->NeededReq() & WRITE_MASK
) != 0)
159 FD_SET(m_fd
, &sockwr_set
);
161 m_internal
->AcquireFD();
162 ret
= select(m_fd
+1, &sockrd_set
, &sockwr_set
, NULL
, &tv
);
163 m_internal
->ReleaseFD();
165 if (FD_ISSET(m_fd
, &sockrd_set
))
168 if (FD_ISSET(m_fd
, &sockwr_set
))
171 m_internal
->ReleaseData();
179 // We wait for 100 ms to prevent the CPU from burning.
182 // Check whether we should exit.
191 // --------------------------------------------------------------
192 // --------- SocketRequester ------------------------------------
193 // --------------------------------------------------------------
195 SocketRequester::SocketRequester(wxSocketBase
*socket
,
196 wxSocketInternal
*internal
)
201 m_socket(socket
), m_internal(internal
), m_fd(internal
->GetFD())
205 SocketRequester::~SocketRequester()
209 bool SocketRequester::WaitFor(wxSocketBase::wxRequestNotify req
, int millisec
)
213 fd_set sockrd_set
, sockwr_set
;
216 tv
.tv_sec
= millisec
/ 1000;
217 tv
.tv_usec
= (millisec
% 1000) * 1000;
219 FD_ZERO(&sockrd_set
);
220 FD_ZERO(&sockwr_set
);
221 if ((req
& READ_MASK
) != 0)
222 FD_SET(m_fd
, &sockrd_set
);
223 if ((req
& WRITE_MASK
) != 0)
224 FD_SET(m_fd
, &sockwr_set
);
226 m_internal
->AcquireFD();
227 ret
= select(m_fd
+1, &sockrd_set
, &sockwr_set
, NULL
, &tv
);
228 m_internal
->ReleaseFD();
233 void SocketRequester::ProcessReadEvent(SockRequest
*req
)
238 // We'll wait for the first byte, in case a "timeout event" occurs it returns // immediately
239 if (!WaitFor(wxSocketBase::REQ_READ
, req
->timeout
)) {
240 m_internal
->EndRequest(req
);
244 m_internal
->AcquireFD();
245 ret
= recv(m_fd
, req
->buffer
, req
->size
,
246 (req
->type
== wxSocketBase::REQ_PEEK
) ? MSG_PEEK
: 0);
247 m_internal
->ReleaseFD();
249 // An error occured, we exit.
252 m_internal
->EndRequest(req
);
257 // If the buffer isn't full (and we want it to be full), we don't unqueue it.
258 if ((len
< req
->size
) && (m_socket
->GetFlags() & wxSocketBase::WAITALL
)) {
260 req
->io_nbytes
+= len
;
264 m_internal
->EndRequest(req
);
268 req
->io_nbytes
+= len
;
269 m_internal
->EndRequest(req
);
272 void SocketRequester::ProcessWriteEvent(SockRequest
*req
)
277 if (!WaitFor(wxSocketBase::REQ_WRITE
, req
->timeout
)) {
278 m_internal
->EndRequest(req
);
282 m_internal
->AcquireFD();
283 ret
= send(m_fd
, req
->buffer
, req
->size
, 0);
284 m_internal
->ReleaseFD();
287 m_internal
->EndRequest(req
);
291 if ((len
< req
->size
) && ((m_socket
->GetFlags() & wxSocketBase::WAITALL
) != 0)) {
293 req
->io_nbytes
+= len
;
297 req
->io_nbytes
+= len
;
298 m_internal
->EndRequest(req
);
301 void SocketRequester::ProcessWaitEvent(SockRequest
*req
)
303 if (WaitFor(req
->type
, req
->timeout
))
304 req
->io_nbytes
= 1; // We put 1 in the counter to tell the requester
305 // there is no timeout.
309 m_internal
->EndRequest(req
);
314 void *SocketRequester::Entry()
318 m_internal
->m_request_locker
.Lock();
320 // Wait for a new request or a destroy message.
321 req
= m_internal
->WaitForReq();
322 m_internal
->m_end_requester
.Lock();
324 m_internal
->m_invalid_requester
= TRUE
;
325 m_internal
->m_end_requester
.Unlock();
326 m_internal
->m_request_locker
.Unlock();
329 m_internal
->m_end_requester
.Unlock();
331 if ((req
->type
& wxSocketBase::REQ_WAIT
) != 0) {
332 ProcessWaitEvent(req
);
337 case wxSocketBase::REQ_READ
:
338 case wxSocketBase::REQ_PEEK
:
339 ProcessReadEvent(req
);
341 case wxSocketBase::REQ_WRITE
:
342 ProcessWriteEvent(req
);
350 // --------------------------------------------------------------
351 // --------- wxSocketInternal -----------------------------------
352 // --------------------------------------------------------------
354 wxSocketInternal::wxSocketInternal(wxSocketBase
*socket
)
357 m_thread_requester
= NULL
;
358 m_thread_waiter
= NULL
;
359 m_invalid_requester
= TRUE
;
362 wxSocketInternal::~wxSocketInternal()
365 wxASSERT(m_thread_requester
== NULL
);
367 wxASSERT(m_thread_waiter
== NULL
);
370 // ----------------------------------------------------------------------
371 // WaitForReq: it is called by SocketRequester and should return the next
372 // socket request if available
373 // ----------------------------------------------------------------------
374 SockRequest
*wxSocketInternal::WaitForReq()
380 node
= m_requests
.First();
382 m_socket_cond
.Wait(m_request_locker
, 10, 0);
384 // Second try, if it is unsuccessul we give up.
385 node
= m_requests
.First();
390 return (SockRequest
*)node
->Data();
396 // ----------------------------------------------------------------------
397 // EndRequest: Should be called to finalize a request
398 // ----------------------------------------------------------------------
399 void wxSocketInternal::EndRequest(SockRequest
*req
)
405 node
= m_requests
.Member((wxObject
*)req
);
410 void wxSocketInternal::AcquireData()
413 m_socket_locker
.Lock();
417 void wxSocketInternal::ReleaseData()
420 m_socket_locker
.Unlock();
424 void wxSocketInternal::AcquireFD()
431 void wxSocketInternal::ReleaseFD()
434 m_fd_locker
.Unlock();
438 void wxSocketInternal::ResumeRequester()
443 wxASSERT(m_invalid_requester
);
445 m_end_requester
.Lock();
447 if (m_thread_requester
!= NULL
) {
448 m_thread_requester
->Delete(); // We must join it.
449 delete m_thread_requester
;
452 m_invalid_requester
= FALSE
;
454 m_end_requester
.Unlock();
456 m_thread_requester
= new SocketRequester(m_socket
, this);
458 err
= m_thread_requester
->Create();
459 wxASSERT(err
== wxTHREAD_NO_ERROR
);
461 err
= m_thread_requester
->Run();
462 wxASSERT(err
== wxTHREAD_NO_ERROR
);
464 if (!m_invalid_requester
)
466 m_thread_requester
= new SocketRequester(m_socket
, this);
467 m_invalid_requester
= FALSE
;
471 void wxSocketInternal::StopRequester()
474 m_end_requester
.Lock();
475 if (m_invalid_requester
) {
476 m_end_requester
.Unlock();
477 if (m_thread_requester
) {
478 m_thread_requester
->Delete();
479 delete m_thread_requester
;
480 m_thread_requester
= NULL
;
482 m_invalid_requester
= TRUE
;
485 m_end_requester
.Unlock();
487 wxASSERT(m_thread_requester
!= NULL
);
489 m_request_locker
.Lock();
491 // Send a signal to the requester.
492 m_socket_cond
.Signal();
494 m_request_locker
.Unlock();
496 // Finish the destruction of the requester.
497 m_thread_requester
->Delete();
499 delete m_thread_requester
;
500 m_thread_requester
= NULL
;
501 m_invalid_requester
= TRUE
;
503 delete m_thread_requester
;
504 m_thread_requester
= NULL
;
505 m_invalid_requester
= TRUE
;
509 void wxSocketInternal::ResumeWaiter()
514 if (m_thread_waiter
!= NULL
)
517 m_thread_waiter
= new SocketWaiter(m_socket
, this);
519 m_thread_waiter
->SetPriority(WXTHREAD_MIN_PRIORITY
);
521 err
= m_thread_waiter
->Create();
522 wxASSERT(err
== wxTHREAD_NO_ERROR
);
524 err
= m_thread_waiter
->Run();
525 wxASSERT(err
== wxTHREAD_NO_ERROR
);
529 void wxSocketInternal::StopWaiter()
532 if (m_thread_waiter
== NULL
)
535 m_thread_waiter
->Delete();
537 delete m_thread_waiter
;
538 m_thread_waiter
= NULL
;
542 // ----------------------------------------------------------------------
544 // ----------------------------------------------------------------------
545 void wxSocketInternal::QueueRequest(SockRequest
*request
, bool async
)
547 if (m_invalid_requester
)
553 m_request_locker
.Lock();
554 request
->done
= FALSE
;
555 m_requests
.Append((wxObject
*)request
);
556 m_socket_cond
.Signal();
557 m_request_locker
.Unlock();
562 if (wxThread::IsMain())
563 while (!request
->done
) {
567 while (!request
->done
) {
572 m_request_locker
.Lock();
575 if ((request
->type
& wxSocketBase::REQ_WAIT
) != 0) {
576 m_thread_requester
->ProcessWaitEvent(request
);
579 request
->done
= FALSE
;
581 switch (request
->type
) {
582 case wxSocketBase::REQ_PEEK
:
583 case wxSocketBase::REQ_READ
:
584 m_thread_requester
->ProcessReadEvent(request
);
586 case wxSocketBase::REQ_WRITE
:
587 m_thread_requester
->ProcessWriteEvent(request
);
591 request
->done
= TRUE
;
593 m_request_locker
.Unlock();
598 void wxSocketInternal::WaitForEnd(SockRequest
*request
)