]>
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
))
166 // If nothing happened, we wait for 100 ms.
172 // Check whether we should exit.
179 // --------------------------------------------------------------
180 // --------- SocketRequester ------------------------------------
181 // --------------------------------------------------------------
183 SocketRequester::SocketRequester(wxSocketBase
*socket
,
184 wxSocketInternal
*internal
)
185 : m_socket(socket
), m_internal(internal
), m_fd(internal
->GetFD())
189 SocketRequester::~SocketRequester()
193 bool SocketRequester::WaitFor(wxSocketBase::wxRequestNotify req
, int millisec
)
197 fd_set sockrd_set
, sockwr_set
;
200 tv
.tv_sec
= millisec
/ 1000;
201 tv
.tv_usec
= (millisec
% 1000) * 1000;
203 if ((req
& READ_MASK
) != 0)
204 FD_ZERO(&sockrd_set
);
205 FD_ZERO(&sockwr_set
);
207 FD_SET(m_fd
, &sockrd_set
);
208 FD_SET(m_fd
, &sockwr_set
);
210 m_internal
->AcquireFD();
211 ret
= select(FD_SETSIZE
, &sockrd_set
, &sockwr_set
, NULL
, &tv
);
212 m_internal
->ReleaseFD();
217 void SocketRequester::ProcessReadEvent(SockRequest
*req
)
222 // We'll wait for the first byte, in case a "timeout event" occurs it returns // immediately
223 if (!WaitFor(wxSocketBase::REQ_READ
, req
->timeout
)) {
224 m_internal
->EndRequest(req
);
228 m_internal
->AcquireFD();
229 ret
= recv(m_fd
, req
->buffer
, req
->size
,
230 (req
->type
== wxSocketBase::REQ_PEEK
) ? MSG_PEEK
: 0);
231 m_internal
->ReleaseFD();
233 // An error occured, we exit.
236 m_internal
->EndRequest(req
);
241 // If the buffer isn't full (and we want it to be full), we don't unqueue it.
242 if ((len
< req
->size
) && (m_socket
->GetFlags() & wxSocketBase::WAITALL
)) {
244 req
->io_nbytes
+= len
;
249 req
->io_nbytes
+= len
;
250 m_internal
->EndRequest(req
);
253 void SocketRequester::ProcessWriteEvent(SockRequest
*req
)
258 m_internal
->AcquireFD();
259 ret
= send(m_fd
, req
->buffer
, req
->size
, 0);
260 m_internal
->ReleaseFD();
263 m_internal
->EndRequest(req
);
267 if ((len
< req
->size
) && ((m_socket
->GetFlags() & wxSocketBase::WAITALL
) != 0)) {
269 req
->io_nbytes
+= len
;
273 req
->io_nbytes
+= len
;
274 m_internal
->EndRequest(req
);
277 void SocketRequester::ProcessWaitEvent(SockRequest
*req
)
279 if (WaitFor(req
->type
, req
->timeout
))
280 req
->io_nbytes
= 1; // We put 1 in the counter to tell the requester
281 // there is no timeout.
285 m_internal
->EndRequest(req
);
288 void *SocketRequester::Entry()
293 // Wait for a new request or a destroy message.
294 req
= m_internal
->WaitForReq();
295 if (TestDestroy() || req
== NULL
)
298 if ((req
->type
& wxSocketBase::REQ_WAIT
) != 0) {
299 ProcessWaitEvent(req
);
304 case wxSocketBase::REQ_READ
:
305 case wxSocketBase::REQ_PEEK
:
306 ProcessReadEvent(req
);
308 case wxSocketBase::REQ_WRITE
:
309 ProcessWriteEvent(req
);
316 // --------------------------------------------------------------
317 // --------- wxSocketInternal -----------------------------------
318 // --------------------------------------------------------------
320 wxSocketInternal::wxSocketInternal(wxSocketBase
*socket
)
323 m_thread_waiter
= new SocketWaiter(socket
, this);
324 m_thread_requester
= new SocketRequester(socket
, this);
325 m_request_locker
.Lock();
328 wxSocketInternal::~wxSocketInternal()
330 m_request_locker
.Unlock();
331 delete m_thread_waiter
;
332 delete m_thread_requester
;
335 // ----------------------------------------------------------------------
336 // WaitForReq: it is called by SocketRequester and should return the next
337 // socket request if available
338 // ----------------------------------------------------------------------
339 SockRequest
*wxSocketInternal::WaitForReq()
343 node
= m_requests
.First();
345 m_socket_cond
.Wait(m_request_locker
);
347 node
= m_requests
.First();
352 return (SockRequest
*)node
->Data();
355 // ----------------------------------------------------------------------
356 // EndRequest: Should be called to finalize a request
357 // ----------------------------------------------------------------------
358 void wxSocketInternal::EndRequest(SockRequest
*req
)
364 node
= m_requests
.Member((wxObject
*)req
);
369 void wxSocketInternal::AcquireFD()
374 void wxSocketInternal::ReleaseFD()
376 m_fd_locker
.Unlock();
379 // ----------------------------------------------------------------------
380 // InitializeSocket: called by wxSocketBase to initialize the daemons with
381 // a new file descriptor and to create them
382 // ----------------------------------------------------------------------
383 void wxSocketInternal::InitializeSocket()
385 wxASSERT( ((!m_thread_waiter
->IsAlive() || m_thread_waiter
->IsPaused()) &&
386 (!m_thread_requester
->IsAlive() || m_thread_requester
->IsPaused())));
388 m_thread_waiter
->m_fd
= m_socket
->m_fd
;
389 m_thread_requester
->m_fd
= m_socket
->m_fd
;
391 if (m_thread_waiter
->IsPaused())
396 err
= m_thread_waiter
->Create();
397 wxASSERT(err
== wxTHREAD_NO_ERROR
);
398 err
= m_thread_requester
->Create();
399 wxASSERT(err
== wxTHREAD_NO_ERROR
);
401 err
= m_thread_waiter
->Run();
402 wxASSERT(err
== wxTHREAD_NO_ERROR
);
403 err
= m_thread_requester
->Run();
404 wxASSERT(err
== wxTHREAD_NO_ERROR
);
409 // ----------------------------------------------------------------------
410 // InitializeSocket: called by wxSocketBase to destroy daemons
411 // ----------------------------------------------------------------------
412 void wxSocketInternal::FinalizeSocket()
416 m_thread_waiter
->Delete();
418 // Send a signal to the thread "requester".
420 m_socket_locker
.Lock();
421 if (m_requests
.Number() == 0)
422 m_socket_cond
.Signal();
423 m_socket_locker
.Unlock();
425 // Finish the destruction of the thread "requester".
426 m_thread_requester
->Delete();
429 void wxSocketInternal::PauseSocket()
434 void wxSocketInternal::ResumeSocket()
439 void wxSocketInternal::EnableWaiter()
441 wxASSERT(m_thread_waiter
!= NULL
);
442 if (!m_thread_waiter
->IsAlive() || !m_thread_waiter
->IsPaused())
445 m_thread_waiter
->Resume();
448 void wxSocketInternal::DisableWaiter()
450 wxASSERT(m_thread_waiter
!= NULL
);
451 if (!m_thread_waiter
->IsAlive() || m_thread_waiter
->IsPaused())
454 m_thread_waiter
->Pause();
457 // ----------------------------------------------------------------------
459 // ----------------------------------------------------------------------
460 void wxSocketInternal::QueueRequest(SockRequest
*request
, bool async
)
463 m_request_locker
.Lock();
464 request
->done
= FALSE
;
465 m_requests
.Append((wxObject
*)request
);
466 m_socket_cond
.Signal();
467 m_request_locker
.Unlock();
472 if (wxThread::IsMain())
473 while (!request
->done
) {
477 while (!request
->done
) {
483 m_request_locker
.Lock();
485 if ((request
->type
& wxSocketBase::REQ_WAIT
) != 0) {
486 m_thread_requester
->ProcessWaitEvent(request
);
489 request
->done
= FALSE
;
491 switch (request
->type
) {
492 case wxSocketBase::REQ_PEEK
:
493 case wxSocketBase::REQ_READ
:
494 m_thread_requester
->ProcessReadEvent(request
);
496 case wxSocketBase::REQ_WRITE
:
497 m_thread_requester
->ProcessWriteEvent(request
);
501 request
->done
= TRUE
;
502 m_request_locker
.Unlock();
506 void wxSocketInternal::WaitForEnd(SockRequest
*request
)