///////////////////////////////////////////////////////////////////////////////
-// Name: socket.cpp
-// Purpose: Socket handler classes
-// Authors: Guilhem Lavaux (completely rewritten from a basic API of Andrew
-// Davidson(1995) in wxWeb)
-// Created: April 1997
-// Updated: April 1999
+// Name: sckint.cpp
+// Purpose: Socket internal classes
+// Authors: Guilhem Lavaux
+// Created: April 1999
+// Updated:
// Copyright: (C) 1999, 1998, 1997, Guilhem Lavaux
// RCS_ID: $Id$
// License: see wxWindows license
#if wxUSE_THREADS
SocketWaiter::SocketWaiter(wxSocketBase *socket,
wxSocketInternal *internal)
- : m_socket(socket), m_internal(internal), m_fd(internal->GetFD())
+ : wxThread(),
+ m_socket(socket), m_internal(internal), m_fd(internal->GetFD())
{
}
// Else, no error => there is something to be read else
// we've lost the connection.
- if (ret > 0)
+ if (ret > 0) {
m_socket->OnRequest(wxSocketBase::EVT_READ);
- else {
+ } else {
m_socket->OnRequest(wxSocketBase::EVT_LOST);
+ m_internal->ReleaseData(); // In that case, we mustn't forget to unlock the mutex.
Exit(NULL);
}
}
FD_ZERO(&sockrd_set);
FD_ZERO(&sockwr_set);
-
+
+ m_internal->AcquireData();
+
if ((m_socket->NeededReq() & READ_MASK) != 0)
FD_SET(m_fd, &sockrd_set);
if ((m_socket->NeededReq() & WRITE_MASK) != 0)
FD_SET(m_fd, &sockwr_set);
m_internal->AcquireFD();
- ret = select(FD_SETSIZE, &sockrd_set, &sockwr_set, NULL, &tv);
+ ret = select(m_fd+1, &sockrd_set, &sockwr_set, NULL, &tv);
m_internal->ReleaseFD();
if (FD_ISSET(m_fd, &sockrd_set))
if (FD_ISSET(m_fd, &sockwr_set))
ProcessWriteEvent();
+ m_internal->ReleaseData();
+
#if wxUSE_THREADS
#ifdef Yield
#undef Yield
SocketRequester::SocketRequester(wxSocketBase *socket,
wxSocketInternal *internal)
- : m_socket(socket), m_internal(internal), m_fd(internal->GetFD())
+ : wxThread(),
+ m_socket(socket), m_internal(internal), m_fd(internal->GetFD())
{
}
FD_SET(m_fd, &sockwr_set);
m_internal->AcquireFD();
- ret = select(FD_SETSIZE, &sockrd_set, &sockwr_set, NULL, &tv);
+ ret = select(m_fd+1, &sockrd_set, &sockwr_set, NULL, &tv);
m_internal->ReleaseFD();
return (ret != 0);
int ret;
size_t len;
+ if (!WaitFor(wxSocketBase::REQ_WRITE, req->timeout)) {
+ m_internal->EndRequest(req);
+ return;
+ }
+
m_internal->AcquireFD();
ret = send(m_fd, req->buffer, req->size, 0);
m_internal->ReleaseFD();
{
SockRequest *req;
+ m_internal->m_request_locker.Lock();
while (1) {
// Wait for a new request or a destroy message.
req = m_internal->WaitForReq();
m_internal->m_end_requester.Lock();
- if (TestDestroy() || req == NULL) {
+ if (req == NULL) {
m_internal->m_invalid_requester = TRUE;
m_internal->m_end_requester.Unlock();
+ m_internal->m_request_locker.Unlock();
return NULL;
}
m_internal->m_end_requester.Unlock();
m_thread_requester = NULL;
m_thread_waiter = NULL;
m_invalid_requester = TRUE;
- m_request_locker.Lock();
#endif
}
wxASSERT(m_thread_requester == NULL);
StopWaiter();
wxASSERT(m_thread_waiter == NULL);
- m_request_locker.Unlock();
#endif
}
node = m_requests.First();
if (node == NULL) {
- m_socket_cond.Wait(m_request_locker, 1, 0);
+ m_socket_cond.Wait(m_request_locker, 10, 0);
node = m_requests.First();
if (node == NULL)
delete node;
}
+void wxSocketInternal::AcquireData()
+{
+#if wxUSE_THREADS
+ m_socket_locker.Lock();
+#endif
+}
+
+void wxSocketInternal::ReleaseData()
+{
+#if wxUSE_THREADS
+ m_socket_locker.Unlock();
+#endif
+}
+
void wxSocketInternal::AcquireFD()
{
#if wxUSE_THREADS
#if wxUSE_THREADS
wxThreadError err;
- wxASSERT(m_thread_requester == NULL);
+ wxASSERT(m_thread_requester == NULL || m_invalid_requester);
m_end_requester.Lock();
if (m_invalid_requester) {
- delete m_thread_requester;
+ if (m_thread_requester != NULL)
+ delete m_thread_requester;
+ m_invalid_requester = FALSE;
+ }
+ m_end_requester.Unlock();
- m_thread_requester = new SocketRequester(m_socket, this);
- m_thread_requester->m_fd = m_socket->m_fd;
+ m_thread_requester = new SocketRequester(m_socket, this);
- err = m_thread_requester->Create();
- wxASSERT(err == wxTHREAD_NO_ERROR);
+ err = m_thread_requester->Create();
+ wxASSERT(err == wxTHREAD_NO_ERROR);
- err = m_thread_requester->Run();
- wxASSERT(err == wxTHREAD_NO_ERROR);
+ err = m_thread_requester->Run();
+ wxASSERT(err == wxTHREAD_NO_ERROR);
- m_invalid_requester = FALSE;
- }
- m_end_requester.Unlock();
#endif
}
void wxSocketInternal::StopRequester()
{
#if wxUSE_THREADS
+ m_end_requester.Lock();
if (m_invalid_requester) {
- delete m_thread_requester;
- m_thread_requester = NULL;
- m_invalid_requester = FALSE;
+ m_end_requester.Unlock();
+ if (m_thread_requester) {
+ delete m_thread_requester;
+ m_thread_requester = NULL;
+ }
+ m_invalid_requester = TRUE;
return;
}
+ m_end_requester.Unlock();
wxASSERT(m_thread_requester != NULL);
- m_socket_locker.Lock();
+ m_request_locker.Lock();
// Send a signal to the requester.
- if (m_requests.Number() == 0)
- m_socket_cond.Signal();
+ m_socket_cond.Signal();
- m_socket_locker.Unlock();
+ m_request_locker.Unlock();
// Finish the destruction of the requester.
m_thread_requester->Delete();
return;
m_thread_waiter = new SocketWaiter(m_socket, this);
- m_thread_waiter->m_fd = m_socket->m_fd;
err = m_thread_waiter->Create();
wxASSERT(err == wxTHREAD_NO_ERROR);
void wxSocketInternal::QueueRequest(SockRequest *request, bool async)
{
#if wxUSE_THREADS
+ if (m_invalid_requester)
+ ResumeRequester();
+
+ async = FALSE;
if (async) {
- if (m_thread_requester == NULL)
- ResumeRequester();
m_request_locker.Lock();
request->done = FALSE;