]>
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.
170 // Check whether we should exit.
178 // --------------------------------------------------------------
179 // --------- SocketRequester ------------------------------------
180 // --------------------------------------------------------------
182 SocketRequester::SocketRequester(wxSocketBase
*socket
,
183 wxSocketInternal
*internal
)
184 : m_socket(socket
), m_internal(internal
), m_fd(internal
->GetFD())
188 SocketRequester::~SocketRequester()
192 bool SocketRequester::WaitFor(wxSocketBase::wxRequestNotify req
, int millisec
)
196 fd_set sockrd_set
, sockwr_set
;
199 tv
.tv_sec
= millisec
/ 1000;
200 tv
.tv_usec
= (millisec
% 1000) * 1000;
202 if ((req
& READ_MASK
) != 0)
203 FD_ZERO(&sockrd_set
);
204 FD_ZERO(&sockwr_set
);
206 FD_SET(m_fd
, &sockrd_set
);
207 FD_SET(m_fd
, &sockwr_set
);
209 m_internal
->AcquireFD();
210 ret
= select(FD_SETSIZE
, &sockrd_set
, &sockwr_set
, NULL
, &tv
);
211 m_internal
->ReleaseFD();
216 void SocketRequester::ProcessReadEvent(SockRequest
*req
)
221 // We'll wait for the first byte, in case a "timeout event" occurs it returns // immediately
222 if (!WaitFor(wxSocketBase::REQ_READ
, req
->timeout
)) {
223 m_internal
->EndRequest(req
);
227 m_internal
->AcquireFD();
228 ret
= recv(m_fd
, req
->buffer
, req
->size
,
229 (req
->type
== wxSocketBase::REQ_PEEK
) ? MSG_PEEK
: 0);
230 m_internal
->ReleaseFD();
232 // An error occured, we exit.
235 m_internal
->EndRequest(req
);
240 // If the buffer isn't full (and we want it to be full), we don't unqueue it.
241 if ((len
< req
->size
) && (m_socket
->GetFlags() & wxSocketBase::WAITALL
)) {
243 req
->io_nbytes
+= len
;
248 req
->io_nbytes
+= len
;
249 m_internal
->EndRequest(req
);
252 void SocketRequester::ProcessWriteEvent(SockRequest
*req
)
257 m_internal
->AcquireFD();
258 ret
= send(m_fd
, req
->buffer
, req
->size
, 0);
259 m_internal
->ReleaseFD();
262 m_internal
->EndRequest(req
);
266 if ((len
< req
->size
) && ((m_socket
->GetFlags() & wxSocketBase::WAITALL
) != 0)) {
268 req
->io_nbytes
+= len
;
272 req
->io_nbytes
+= len
;
273 m_internal
->EndRequest(req
);
276 void SocketRequester::ProcessWaitEvent(SockRequest
*req
)
278 if (WaitFor(req
->type
, req
->timeout
))
279 req
->io_nbytes
= 1; // We put 1 in the counter to tell the requester
280 // there is no timeout.
284 m_internal
->EndRequest(req
);
287 void *SocketRequester::Entry()
292 // Wait for a new request or a destroy message.
293 req
= m_internal
->WaitForReq();
294 if (TestDestroy() || req
== NULL
)
297 if ((req
->type
& wxSocketBase::REQ_WAIT
) != 0) {
298 ProcessWaitEvent(req
);
303 case wxSocketBase::REQ_READ
:
304 case wxSocketBase::REQ_PEEK
:
305 ProcessReadEvent(req
);
307 case wxSocketBase::REQ_WRITE
:
308 ProcessWriteEvent(req
);
315 // --------------------------------------------------------------
316 // --------- wxSocketInternal -----------------------------------
317 // --------------------------------------------------------------
319 wxSocketInternal::wxSocketInternal(wxSocketBase
*socket
)
322 m_thread_requester
= NULL
;
323 m_thread_waiter
= NULL
;
324 m_request_locker
.Lock();
327 wxSocketInternal::~wxSocketInternal()
329 wxASSERT(m_thread_requester
== NULL
);
330 wxASSERT(m_thread_waiter
== NULL
);
331 m_request_locker
.Unlock();
334 // ----------------------------------------------------------------------
335 // WaitForReq: it is called by SocketRequester and should return the next
336 // socket request if available
337 // ----------------------------------------------------------------------
338 SockRequest
*wxSocketInternal::WaitForReq()
342 node
= m_requests
.First();
344 m_socket_cond
.Wait(m_request_locker
);
346 node
= m_requests
.First();
351 return (SockRequest
*)node
->Data();
354 // ----------------------------------------------------------------------
355 // EndRequest: Should be called to finalize a request
356 // ----------------------------------------------------------------------
357 void wxSocketInternal::EndRequest(SockRequest
*req
)
363 node
= m_requests
.Member((wxObject
*)req
);
368 void wxSocketInternal::AcquireFD()
373 void wxSocketInternal::ReleaseFD()
375 m_fd_locker
.Unlock();
378 void wxSocketInternal::ResumeRequester()
382 wxASSERT(m_thread_requester
== NULL
);
384 m_thread_requester
= new SocketRequester(m_socket
, this);
385 m_thread_requester
->m_fd
= m_socket
->m_fd
;
387 err
= m_thread_requester
->Create();
388 wxASSERT(err
== wxTHREAD_NO_ERROR
);
390 err
= m_thread_requester
->Run();
391 wxASSERT(err
== wxTHREAD_NO_ERROR
);
394 void wxSocketInternal::StopRequester()
396 wxASSERT(m_thread_requester
!= NULL
);
398 m_socket_locker
.Lock();
400 // Send a signal to the requester.
401 if (m_requests
.Number() == 0)
402 m_socket_cond
.Signal();
404 m_socket_locker
.Unlock();
406 // Finish the destruction of the requester.
407 m_thread_requester
->Delete();
409 delete m_thread_requester
;
410 m_thread_requester
= NULL
;
413 void wxSocketInternal::ResumeWaiter()
417 if (m_thread_waiter
!= NULL
)
420 m_thread_waiter
= new SocketWaiter(m_socket
, this);
421 m_thread_waiter
->m_fd
= m_socket
->m_fd
;
423 err
= m_thread_waiter
->Create();
424 wxASSERT(err
== wxTHREAD_NO_ERROR
);
426 err
= m_thread_waiter
->Run();
427 wxASSERT(err
== wxTHREAD_NO_ERROR
);
430 void wxSocketInternal::StopWaiter()
433 if (m_thread_waiter == NULL)
436 m_thread_waiter->Delete();
438 delete m_thread_waiter;
439 m_thread_waiter = NULL;
443 // ----------------------------------------------------------------------
445 // ----------------------------------------------------------------------
446 void wxSocketInternal::QueueRequest(SockRequest
*request
, bool async
)
449 if (m_thread_requester
== NULL
)
452 m_request_locker
.Lock();
453 request
->done
= FALSE
;
454 m_requests
.Append((wxObject
*)request
);
455 m_socket_cond
.Signal();
456 m_request_locker
.Unlock();
461 if (wxThread::IsMain())
462 while (!request
->done
) {
466 while (!request
->done
) {
471 m_request_locker
.Lock();
473 if ((request
->type
& wxSocketBase::REQ_WAIT
) != 0) {
474 m_thread_requester
->ProcessWaitEvent(request
);
477 request
->done
= FALSE
;
479 switch (request
->type
) {
480 case wxSocketBase::REQ_PEEK
:
481 case wxSocketBase::REQ_READ
:
482 m_thread_requester
->ProcessReadEvent(request
);
484 case wxSocketBase::REQ_WRITE
:
485 m_thread_requester
->ProcessWriteEvent(request
);
489 request
->done
= TRUE
;
490 m_request_locker
.Unlock();
494 void wxSocketInternal::WaitForEnd(SockRequest
*request
)