]>
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
))
159 // If nothing happened, we wait for 100 ms.
164 // Check whether we should exit.
171 // --------------------------------------------------------------
172 // --------- SocketRequester ------------------------------------
173 // --------------------------------------------------------------
175 SocketRequester::SocketRequester(wxSocketBase
*socket
,
176 wxSocketInternal
*internal
)
177 : m_socket(socket
), m_internal(internal
), m_fd(internal
->GetFD())
181 SocketRequester::~SocketRequester()
185 bool SocketRequester::WaitFor(wxSocketBase::wxRequestNotify req
, int millisec
)
189 fd_set sockrd_set
, sockwr_set
;
192 tv
.tv_sec
= millisec
/ 1000;
193 tv
.tv_usec
= (millisec
% 1000) * 1000;
195 if ((req
& READ_MASK
) != 0)
196 FD_ZERO(&sockrd_set
);
197 FD_ZERO(&sockwr_set
);
199 FD_SET(m_fd
, &sockrd_set
);
200 FD_SET(m_fd
, &sockwr_set
);
202 m_internal
->AcquireFD();
203 ret
= select(FD_SETSIZE
, &sockrd_set
, &sockwr_set
, NULL
, &tv
);
204 m_internal
->ReleaseFD();
209 void SocketRequester::ProcessReadEvent(SockRequest
*req
)
214 // We'll wait for the first byte, in case a "timeout event" occurs it returns // immediately
215 if (!WaitFor(wxSocketBase::REQ_READ
, req
->timeout
)) {
216 m_internal
->EndRequest(req
);
220 m_internal
->AcquireFD();
221 ret
= recv(m_fd
, req
->buffer
, req
->size
,
222 (req
->type
== wxSocketBase::REQ_PEEK
) ? MSG_PEEK
: 0);
223 m_internal
->ReleaseFD();
225 // An error occured, we exit.
228 m_internal
->EndRequest(req
);
233 // If the buffer isn't full (and we want it to be full), we don't unqueue it.
234 if ((len
< req
->size
) && (m_socket
->GetFlags() & wxSocketBase::WAITALL
)) {
236 req
->io_nbytes
+= len
;
241 req
->io_nbytes
+= len
;
242 m_internal
->EndRequest(req
);
245 void SocketRequester::ProcessWriteEvent(SockRequest
*req
)
250 m_internal
->AcquireFD();
251 ret
= send(m_fd
, req
->buffer
, req
->size
, 0);
252 m_internal
->ReleaseFD();
255 m_internal
->EndRequest(req
);
259 if ((len
< req
->size
) && ((m_socket
->GetFlags() & wxSocketBase::WAITALL
) != 0)) {
261 req
->io_nbytes
+= len
;
265 req
->io_nbytes
+= len
;
266 m_internal
->EndRequest(req
);
269 void SocketRequester::ProcessWaitEvent(SockRequest
*req
)
271 if (WaitFor(req
->type
, req
->timeout
))
272 req
->io_nbytes
= 1; // We put 1 in the counter to tell the requester
273 // there is no timeout.
277 m_internal
->EndRequest(req
);
280 void *SocketRequester::Entry()
285 // Wait for a new request or a destroy message.
286 req
= m_internal
->WaitForReq();
287 if (TestDestroy() || req
== NULL
)
290 if ((req
->type
& wxSocketBase::REQ_WAIT
) != 0) {
291 ProcessWaitEvent(req
);
296 case wxSocketBase::REQ_READ
:
297 case wxSocketBase::REQ_PEEK
:
298 ProcessReadEvent(req
);
300 case wxSocketBase::REQ_WRITE
:
301 ProcessWriteEvent(req
);
308 // --------------------------------------------------------------
309 // --------- wxSocketInternal -----------------------------------
310 // --------------------------------------------------------------
312 wxSocketInternal::wxSocketInternal(wxSocketBase
*socket
)
315 m_thread_waiter
= new SocketWaiter(socket
, this);
316 m_thread_requester
= new SocketRequester(socket
, this);
317 m_request_locker
.Lock();
320 wxSocketInternal::~wxSocketInternal()
322 wxASSERT(!m_finalized
);
323 m_request_locker
.Unlock();
324 delete m_thread_waiter
;
325 delete m_thread_requester
;
328 // ----------------------------------------------------------------------
329 // WaitForReq: it is called by SocketRequester and should return the next
330 // socket request if available
331 // ----------------------------------------------------------------------
332 SockRequest
*wxSocketInternal::WaitForReq()
336 node
= m_requests
.First();
338 m_socket_cond
.Wait(m_request_locker
);
340 node
= m_requests
.First();
345 return (SockRequest
*)node
->Data();
348 // ----------------------------------------------------------------------
349 // EndRequest: Should be called to finalize a request
350 // ----------------------------------------------------------------------
351 void wxSocketInternal::EndRequest(SockRequest
*req
)
357 node
= m_requests
.Member((wxObject
*)req
);
362 void wxSocketInternal::AcquireFD()
367 void wxSocketInternal::ReleaseFD()
369 m_fd_locker
.Unlock();
372 // ----------------------------------------------------------------------
373 // InitializeSocket: called by wxSocketBase to initialize the daemons with
374 // a new file descriptor and to create them
375 // ----------------------------------------------------------------------
376 void wxSocketInternal::InitializeSocket()
378 wxASSERT( ((m_thread_waiter
->IsAlive() && !m_thread_waiter
->IsPaused()) ||
379 (m_thread_requester
->IsAlive() && !m_thread_requester
->IsPaused())));
381 m_thread_waiter
->m_fd
= m_socket
->m_fd
;
382 m_thread_requester
->m_fd
= m_socket
->m_fd
;
384 if (m_thread_waiter
->IsPaused())
388 if (m_thread_waiter
->Create() != wxTHREAD_NO_ERROR
) {
389 // Something should be done here.
393 if (m_thread_requester
->Create() != wxTHREAD_NO_ERROR
) {
394 // Something should be done here.
398 m_thread_waiter
->Run();
399 m_thread_requester
->Run();
404 // ----------------------------------------------------------------------
405 // InitializeSocket: called by wxSocketBase to destroy daemons
406 // ----------------------------------------------------------------------
407 void wxSocketInternal::FinalizeSocket()
409 wxASSERT( (!m_thread_waiter
->IsAlive() && !m_thread_requester
->IsAlive()) );
413 m_thread_waiter
->Delete();
414 m_socket_locker
.Lock();
415 if (m_requests
.Number() == 0)
416 m_socket_cond
.Signal();
417 m_socket_locker
.Unlock();
419 m_thread_requester
->Delete();
422 void wxSocketInternal::PauseSocket()
424 if (m_thread_waiter
!= NULL
&& !m_thread_waiter
->IsPaused())
425 m_thread_waiter
->Pause();
428 void wxSocketInternal::ResumeSocket()
430 if (m_thread_waiter
!= NULL
&& m_thread_waiter
->IsPaused())
431 m_thread_waiter
->Resume();
434 void wxSocketInternal::EnableWaiter()
436 if (m_thread_waiter
!= NULL
&& m_thread_waiter
->IsPaused())
437 m_thread_waiter
->Resume();
440 void wxSocketInternal::DisableWaiter()
442 if (m_thread_waiter
!= NULL
&& !m_thread_waiter
->IsPaused())
443 m_thread_waiter
->Pause();
446 // ----------------------------------------------------------------------
448 // ----------------------------------------------------------------------
449 void wxSocketInternal::QueueRequest(SockRequest
*request
, bool async
)
452 m_request_locker
.Lock();
453 request
->done
= FALSE
;
454 m_requests
.Append((wxObject
*)request
);
455 m_request_locker
.Unlock();
458 m_socket_cond
.Signal();
461 if (wxThread::IsMain())
462 while (!request
->done
) {
466 while (!request
->done
) {
472 m_request_locker
.Lock();
474 if ((request
->type
& wxSocketBase::REQ_WAIT
) != 0) {
475 m_thread_requester
->ProcessWaitEvent(request
);
478 request
->done
= FALSE
;
480 switch (request
->type
) {
481 case wxSocketBase::REQ_PEEK
:
482 case wxSocketBase::REQ_READ
:
483 m_thread_requester
->ProcessReadEvent(request
);
485 case wxSocketBase::REQ_WRITE
:
486 m_thread_requester
->ProcessWriteEvent(request
);
490 request
->done
= TRUE
;
491 m_request_locker
.Unlock();
495 void wxSocketInternal::WaitForEnd(SockRequest
*request
)