]>
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>
37 // -----------------------
38 // System specific headers
39 // -----------------------
42 // in order to avoid problems with our c library and double definitions
43 #define close closesocket
44 #define ioctl ioctlsocket
46 #include <wx/mac/macsock.h>
49 #if defined(__WINDOWS__)
58 #include <sys/socket.h>
60 #include <sys/ioctl.h>
66 #include <sys/filio.h>
79 #define READ_MASK wxSocketBase::REQ_READ | wxSocketBase::REQ_ACCEPT | wxSocketBase::REQ_LOST
80 #define WRITE_MASK wxSocketBase::REQ_WRITE | wxSocketBase::REQ_CONNECT
82 // --------------------------------------------------------------
83 // --------- SocketWaiter ---------------------------------------
84 // --------------------------------------------------------------
87 SocketWaiter::SocketWaiter(wxSocketBase
*socket
,
88 wxSocketInternal
*internal
)
90 m_socket(socket
), m_internal(internal
), m_fd(internal
->GetFD())
94 SocketWaiter::~SocketWaiter()
98 void SocketWaiter::ProcessReadEvent()
103 ret
= recv(m_fd
, &c
, 1, MSG_PEEK
);
105 // We are a server => emit a EVT_ACCEPT event.
106 if (ret
== -1 && m_socket
->GetType() == wxSocketBase::SOCK_SERVER
) {
107 m_socket
->OnRequest(wxSocketBase::EVT_ACCEPT
);
111 // Else, no error => there is something to be read else
112 // we've lost the connection.
114 m_socket
->OnRequest(wxSocketBase::EVT_READ
);
116 m_socket
->OnRequest(wxSocketBase::EVT_LOST
);
117 m_internal
->ReleaseData(); // In that case, we mustn't forget to unlock the mutex.
122 void SocketWaiter::ProcessWriteEvent()
124 if (m_socket
->IsConnected())
125 m_socket
->OnRequest(wxSocketBase::EVT_CONNECT
);
127 m_socket
->OnRequest(wxSocketBase::EVT_WRITE
);
130 void *SocketWaiter::Entry()
133 fd_set sockrd_set
, sockwr_set
;
142 FD_ZERO(&sockrd_set
);
143 FD_ZERO(&sockwr_set
);
145 m_internal
->AcquireData();
147 if ((m_socket
->NeededReq() & READ_MASK
) != 0)
148 FD_SET(m_fd
, &sockrd_set
);
149 if ((m_socket
->NeededReq() & WRITE_MASK
) != 0)
150 FD_SET(m_fd
, &sockwr_set
);
152 m_internal
->AcquireFD();
153 ret
= select(m_fd
+1, &sockrd_set
, &sockwr_set
, NULL
, &tv
);
154 m_internal
->ReleaseFD();
156 if (FD_ISSET(m_fd
, &sockrd_set
))
159 if (FD_ISSET(m_fd
, &sockwr_set
))
162 m_internal
->ReleaseData();
171 // If nothing happened, we wait for 100 ms.
174 // Check whether we should exit.
182 // --------------------------------------------------------------
183 // --------- SocketRequester ------------------------------------
184 // --------------------------------------------------------------
186 SocketRequester::SocketRequester(wxSocketBase
*socket
,
187 wxSocketInternal
*internal
)
189 m_socket(socket
), m_internal(internal
), m_fd(internal
->GetFD())
193 SocketRequester::~SocketRequester()
197 bool SocketRequester::WaitFor(wxSocketBase::wxRequestNotify req
, int millisec
)
201 fd_set sockrd_set
, sockwr_set
;
204 tv
.tv_sec
= millisec
/ 1000;
205 tv
.tv_usec
= (millisec
% 1000) * 1000;
207 if ((req
& READ_MASK
) != 0)
208 FD_ZERO(&sockrd_set
);
209 FD_ZERO(&sockwr_set
);
211 FD_SET(m_fd
, &sockrd_set
);
212 FD_SET(m_fd
, &sockwr_set
);
214 m_internal
->AcquireFD();
215 ret
= select(m_fd
+1, &sockrd_set
, &sockwr_set
, NULL
, &tv
);
216 m_internal
->ReleaseFD();
221 void SocketRequester::ProcessReadEvent(SockRequest
*req
)
226 // We'll wait for the first byte, in case a "timeout event" occurs it returns // immediately
227 if (!WaitFor(wxSocketBase::REQ_READ
, req
->timeout
)) {
228 m_internal
->EndRequest(req
);
232 m_internal
->AcquireFD();
233 ret
= recv(m_fd
, req
->buffer
, req
->size
,
234 (req
->type
== wxSocketBase::REQ_PEEK
) ? MSG_PEEK
: 0);
235 m_internal
->ReleaseFD();
237 // An error occured, we exit.
240 m_internal
->EndRequest(req
);
245 // If the buffer isn't full (and we want it to be full), we don't unqueue it.
246 if ((len
< req
->size
) && (m_socket
->GetFlags() & wxSocketBase::WAITALL
)) {
248 req
->io_nbytes
+= len
;
253 req
->io_nbytes
+= len
;
254 m_internal
->EndRequest(req
);
257 void SocketRequester::ProcessWriteEvent(SockRequest
*req
)
262 if (!WaitFor(wxSocketBase::REQ_WRITE
, req
->timeout
)) {
263 m_internal
->EndRequest(req
);
267 m_internal
->AcquireFD();
268 ret
= send(m_fd
, req
->buffer
, req
->size
, 0);
269 m_internal
->ReleaseFD();
272 m_internal
->EndRequest(req
);
276 if ((len
< req
->size
) && ((m_socket
->GetFlags() & wxSocketBase::WAITALL
) != 0)) {
278 req
->io_nbytes
+= len
;
282 req
->io_nbytes
+= len
;
283 m_internal
->EndRequest(req
);
286 void SocketRequester::ProcessWaitEvent(SockRequest
*req
)
288 if (WaitFor(req
->type
, req
->timeout
))
289 req
->io_nbytes
= 1; // We put 1 in the counter to tell the requester
290 // there is no timeout.
294 m_internal
->EndRequest(req
);
297 void *SocketRequester::Entry()
301 m_internal
->m_request_locker
.Lock();
303 // Wait for a new request or a destroy message.
304 req
= m_internal
->WaitForReq();
305 m_internal
->m_end_requester
.Lock();
307 m_internal
->m_invalid_requester
= TRUE
;
308 m_internal
->m_end_requester
.Unlock();
309 m_internal
->m_request_locker
.Unlock();
312 m_internal
->m_end_requester
.Unlock();
314 if ((req
->type
& wxSocketBase::REQ_WAIT
) != 0) {
315 ProcessWaitEvent(req
);
320 case wxSocketBase::REQ_READ
:
321 case wxSocketBase::REQ_PEEK
:
322 ProcessReadEvent(req
);
324 case wxSocketBase::REQ_WRITE
:
325 ProcessWriteEvent(req
);
333 // --------------------------------------------------------------
334 // --------- wxSocketInternal -----------------------------------
335 // --------------------------------------------------------------
337 wxSocketInternal::wxSocketInternal(wxSocketBase
*socket
)
341 m_thread_requester
= NULL
;
342 m_thread_waiter
= NULL
;
343 m_invalid_requester
= TRUE
;
347 wxSocketInternal::~wxSocketInternal()
351 wxASSERT(m_thread_requester
== NULL
);
353 wxASSERT(m_thread_waiter
== NULL
);
357 // ----------------------------------------------------------------------
358 // WaitForReq: it is called by SocketRequester and should return the next
359 // socket request if available
360 // ----------------------------------------------------------------------
361 SockRequest
*wxSocketInternal::WaitForReq()
366 node
= m_requests
.First();
368 m_socket_cond
.Wait(m_request_locker
, 10, 0);
370 node
= m_requests
.First();
375 return (SockRequest
*)node
->Data();
381 // ----------------------------------------------------------------------
382 // EndRequest: Should be called to finalize a request
383 // ----------------------------------------------------------------------
384 void wxSocketInternal::EndRequest(SockRequest
*req
)
390 node
= m_requests
.Member((wxObject
*)req
);
395 void wxSocketInternal::AcquireData()
398 m_socket_locker
.Lock();
402 void wxSocketInternal::ReleaseData()
405 m_socket_locker
.Unlock();
409 void wxSocketInternal::AcquireFD()
416 void wxSocketInternal::ReleaseFD()
419 m_fd_locker
.Unlock();
423 void wxSocketInternal::ResumeRequester()
428 wxASSERT(m_thread_requester
== NULL
|| m_invalid_requester
);
430 m_end_requester
.Lock();
431 if (m_invalid_requester
) {
432 if (m_thread_requester
!= NULL
)
433 delete m_thread_requester
;
434 m_invalid_requester
= FALSE
;
436 m_end_requester
.Unlock();
438 m_thread_requester
= new SocketRequester(m_socket
, this);
440 err
= m_thread_requester
->Create();
441 wxASSERT(err
== wxTHREAD_NO_ERROR
);
443 err
= m_thread_requester
->Run();
444 wxASSERT(err
== wxTHREAD_NO_ERROR
);
449 void wxSocketInternal::StopRequester()
452 m_end_requester
.Lock();
453 if (m_invalid_requester
) {
454 m_end_requester
.Unlock();
455 if (m_thread_requester
) {
456 delete m_thread_requester
;
457 m_thread_requester
= NULL
;
459 m_invalid_requester
= TRUE
;
462 m_end_requester
.Unlock();
464 wxASSERT(m_thread_requester
!= NULL
);
466 m_request_locker
.Lock();
468 // Send a signal to the requester.
469 m_socket_cond
.Signal();
471 m_request_locker
.Unlock();
473 // Finish the destruction of the requester.
474 m_thread_requester
->Delete();
476 delete m_thread_requester
;
477 m_thread_requester
= NULL
;
481 void wxSocketInternal::ResumeWaiter()
486 if (m_thread_waiter
!= NULL
)
489 m_thread_waiter
= new SocketWaiter(m_socket
, this);
491 err
= m_thread_waiter
->Create();
492 wxASSERT(err
== wxTHREAD_NO_ERROR
);
494 err
= m_thread_waiter
->Run();
495 wxASSERT(err
== wxTHREAD_NO_ERROR
);
499 void wxSocketInternal::StopWaiter()
502 if (m_thread_waiter
== NULL
)
505 m_thread_waiter
->Delete();
507 delete m_thread_waiter
;
508 m_thread_waiter
= NULL
;
512 // ----------------------------------------------------------------------
514 // ----------------------------------------------------------------------
515 void wxSocketInternal::QueueRequest(SockRequest
*request
, bool async
)
518 if (m_invalid_requester
)
524 m_request_locker
.Lock();
525 request
->done
= FALSE
;
526 m_requests
.Append((wxObject
*)request
);
527 m_socket_cond
.Signal();
528 m_request_locker
.Unlock();
533 if (wxThread::IsMain())
534 while (!request
->done
) {
538 while (!request
->done
) {
543 m_request_locker
.Lock();
545 if ((request
->type
& wxSocketBase::REQ_WAIT
) != 0) {
546 m_thread_requester
->ProcessWaitEvent(request
);
549 request
->done
= FALSE
;
551 switch (request
->type
) {
552 case wxSocketBase::REQ_PEEK
:
553 case wxSocketBase::REQ_READ
:
554 m_thread_requester
->ProcessReadEvent(request
);
556 case wxSocketBase::REQ_WRITE
:
557 m_thread_requester
->ProcessWriteEvent(request
);
561 request
->done
= TRUE
;
562 m_request_locker
.Unlock();
567 void wxSocketInternal::WaitForEnd(SockRequest
*request
)