721
void ShutdownRRT(void)
723
MythSocket::m_readyread_thread.ShutdownReadyReadThread();
726
void MythSocketThread::ShutdownReadyReadThread(void)
728
m_readyread_run = false;
729
WakeReadyReadThread();
733
if (readyreadevent) {
734
::CloseHandle(readyreadevent);
735
readyreadevent = NULL;
738
::close(m_readyread_pipe[0]);
739
::close(m_readyread_pipe[1]);
743
void MythSocketThread::StartReadyReadThread(void)
745
if (m_readyread_run == false)
747
QMutexLocker locker(&m_readyread_lock);
748
if (m_readyread_run == false)
751
readyreadevent = ::CreateEvent(NULL, false, false, NULL);
752
assert(readyreadevent);
754
int ret = pipe(m_readyread_pipe);
759
m_readyread_run = true;
767
void MythSocketThread::AddToReadyRead(MythSocket *sock)
769
if (sock->socket() == -1)
771
VERBOSE(VB_SOCKET, SLOC(sock) + "attempted to insert invalid socket to ReadyRead");
774
StartReadyReadThread();
777
m_readyread_lock.lock();
778
m_readyread_addlist.append(sock);
779
m_readyread_lock.unlock();
781
WakeReadyReadThread();
784
void MythSocketThread::RemoveFromReadyRead(MythSocket *sock)
786
m_readyread_lock.lock();
787
m_readyread_dellist.append(sock);
788
m_readyread_lock.unlock();
790
WakeReadyReadThread();
793
void MythSocketThread::WakeReadyReadThread(void)
799
if (readyreadevent) ::SetEvent(readyreadevent);
801
if (m_readyread_pipe[1] >= 0)
803
char buf[1] = { '0' };
807
wret = ::write(m_readyread_pipe[1], &buf, 1);
808
if ((wret < 0) && (EAGAIN != errno) && (EINTR != errno))
810
VERBOSE(VB_IMPORTANT, "MythSocketThread, Error: "
811
"Irrecoverable WakeReadyReadThread event");
815
// dtk note: add this aft dbg for correctness..
816
//::flush(m_readyread_pipe[1]);
821
void MythSocketThread::iffound(MythSocket *sock)
823
VERBOSE(VB_SOCKET, SLOC(sock) + "socket is readable");
824
if (sock->bytesAvailable() == 0)
826
VERBOSE(VB_SOCKET, SLOC(sock) + "socket closed");
831
VERBOSE(VB_SOCKET, SLOC(sock) + "calling m_cb->connectionClosed()");
832
sock->m_cb->connectionClosed(sock);
837
sock->m_notifyread = true;
838
VERBOSE(VB_SOCKET, SLOC(sock) + "calling m_cb->readyRead()");
839
sock->m_cb->readyRead(sock);
843
bool MythSocketThread::isLocked(QMutex &mutex)
845
bool isLocked = true;
854
void MythSocketThread::run(void)
856
VERBOSE(VB_SOCKET, "MythSocketThread: readyread thread start");
863
while (m_readyread_run)
865
m_readyread_lock.lock();
866
while (m_readyread_dellist.size() > 0)
868
sock = m_readyread_dellist.takeFirst();
869
bool del = m_readyread_list.removeAll(sock);
873
m_readyread_lock.unlock();
875
m_readyread_lock.lock();
879
while (m_readyread_addlist.count() > 0)
881
sock = m_readyread_addlist.takeFirst();
882
//sock->UpRef(); Did upref in AddToReadyRead()
883
m_readyread_list.append(sock);
885
m_readyread_lock.unlock();
889
int n = m_readyread_list.count() + 1;
890
HANDLE *hEvents = new HANDLE[n];
891
memset(hEvents, 0, sizeof(HANDLE) * n);
892
unsigned *idx = new unsigned[n];
895
for (unsigned i = 0; i < (uint) m_readyread_list.count(); i++)
897
sock = m_readyread_list.at(i);
898
if (sock->state() == MythSocket::Connected
899
&& !sock->m_notifyread && !isLocked(sock->m_lock))
901
HANDLE hEvent = ::CreateEvent(NULL, false, false, NULL);
904
VERBOSE(VB_IMPORTANT, "CreateEvent failed");
908
if (SOCKET_ERROR != ::WSAEventSelect(
909
sock->socket(), hEvent,
917
VERBOSE(VB_IMPORTANT, QString(
919
"WSAEventSelect(%1, %2) failed")
920
.arg(sock->socket()));
921
::CloseHandle(hEvent);
927
hEvents[n++] = readyreadevent;
928
int rval = ::WaitForMultipleObjects(n, hEvents, false, INFINITE);
930
for (int i = 0; i < (n - 1); i++)
931
::CloseHandle(hEvents[i]);
935
if (rval == WAIT_FAILED)
937
VERBOSE(VB_IMPORTANT, "WaitForMultipleObjects returned error");
940
else if (rval >= WAIT_OBJECT_0 && rval < (WAIT_OBJECT_0 + n))
942
rval -= WAIT_OBJECT_0;
947
sock = m_readyread_list.at(rval);
948
found = (sock->state() == MythSocket::Connected)
949
&& !isLocked(sock->m_lock);
961
::ResetEvent(readyreadevent);
963
else if (rval >= WAIT_ABANDONED_0 && rval < (WAIT_ABANDONED_0 + n))
965
VERBOSE(VB_SOCKET, "abandoned");
969
VERBOSE(VB_SOCKET|VB_EXTRA, "select timeout");
972
#else /* if !USING_MINGW */
974
// add check for bad fd?
976
maxfd = m_readyread_pipe[0];
978
FD_SET(m_readyread_pipe[0], &rfds);
980
QList<MythSocket*>::const_iterator it = m_readyread_list.begin();
981
while (it != m_readyread_list.end())
984
if (sock->state() == MythSocket::Connected &&
985
sock->m_notifyread == false &&
986
!isLocked(sock->m_lock))
988
FD_SET(sock->socket(), &rfds);
989
maxfd = std::max(sock->socket(), maxfd);
994
int rval = select(maxfd + 1, &rfds, NULL, NULL, NULL);
997
VERBOSE(VB_SOCKET, "MythSocketThread: select returned error");
1002
QList<MythSocket*>::const_iterator it = m_readyread_list.begin();
1003
while (it != m_readyread_list.end())
1006
if (sock->state() == MythSocket::Connected &&
1007
FD_ISSET(sock->socket(), &rfds) &&
1008
!isLocked(sock->m_lock))
1019
if (FD_ISSET(m_readyread_pipe[0], &rfds))
1024
do rr = ::read(m_readyread_pipe[0], buf, 128);
1025
while ((rr < 0) && (EINTR == errno));
1030
VERBOSE(VB_SOCKET|VB_EXTRA, "MythSocketThread: select timeout");
1033
#endif /* !USING_MINGW */
1037
VERBOSE(VB_SOCKET, "MythSocketThread: readyread thread exit");