From: Lars Op den Kamp Date: Wed, 11 Apr 2012 08:59:04 +0000 (+0200) Subject: win32: serial socket timeouts. bugzid: 654 X-Git-Tag: upstream/2.2.0~1^2~31^2~13 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=251eb3e8fb9955b8219b0d16d0196d724b31b3cc;p=deb_libcec.git win32: serial socket timeouts. bugzid: 654 --- diff --git a/src/lib/adapter/USBCECAdapterCommunication.cpp b/src/lib/adapter/USBCECAdapterCommunication.cpp index 527292a..8269d13 100644 --- a/src/lib/adapter/USBCECAdapterCommunication.cpp +++ b/src/lib/adapter/USBCECAdapterCommunication.cpp @@ -240,10 +240,7 @@ void *CUSBCECAdapterCommunication::Process(void) } if (!IsStopped()) - { - Sleep(5); WriteNextCommand(); - } } /* notify all threads that are waiting on messages to be sent */ diff --git a/src/lib/platform/sockets/serialport.h b/src/lib/platform/sockets/serialport.h index bdd05b9..eed05a0 100644 --- a/src/lib/platform/sockets/serialport.h +++ b/src/lib/platform/sockets/serialport.h @@ -71,6 +71,9 @@ namespace PLATFORM public: CSerialSocket(const CStdString &strName, uint32_t iBaudrate, SerialDataBits iDatabits = SERIAL_DATA_BITS_EIGHT, SerialStopBits iStopbits = SERIAL_STOP_BITS_ONE, SerialParity iParity = SERIAL_PARITY_NONE) : CCommonSocket(INVALID_SERIAL_SOCKET_VALUE, strName), + #ifdef __WINDOWS__ + m_iCurrentReadTimeout(MAXDWORD), + #endif m_bIsOpen(false), m_iBaudrate(iBaudrate), m_iDatabits(iDatabits), @@ -96,6 +99,9 @@ namespace PLATFORM protected: #ifndef __WINDOWS__ struct termios m_options; + #else + bool SetTimeouts(serial_socket_t socket, int* iError, DWORD iTimeoutMs); + DWORD m_iCurrentReadTimeout; #endif bool m_bIsOpen; diff --git a/src/lib/platform/windows/serialport.cpp b/src/lib/platform/windows/serialport.cpp index c0cdd93..96e743f 100644 --- a/src/lib/platform/windows/serialport.cpp +++ b/src/lib/platform/windows/serialport.cpp @@ -48,28 +48,25 @@ void FormatWindowsError(int iErrorCode, CStdString &strMessage) } } -bool SetTimeouts(serial_socket_t socket, int* iError, bool bBlocking) +bool CSerialSocket::SetTimeouts(serial_socket_t socket, int* iError, DWORD iTimeoutMs) { if (socket == INVALID_HANDLE_VALUE) return false; - COMMTIMEOUTS cto; - if (!GetCommTimeouts(socket, &cto)) - { - *iError = GetLastError(); - return false; - } + if (iTimeoutMs == m_iCurrentReadTimeout) + return true; - if (bBlocking) + COMMTIMEOUTS cto; + if (iTimeoutMs == 0) { - cto.ReadIntervalTimeout = 0; + cto.ReadIntervalTimeout = MAXDWORD; cto.ReadTotalTimeoutConstant = 0; cto.ReadTotalTimeoutMultiplier = 0; } else { - cto.ReadIntervalTimeout = MAXDWORD; - cto.ReadTotalTimeoutConstant = 0; + cto.ReadIntervalTimeout = 0; + cto.ReadTotalTimeoutConstant = iTimeoutMs; cto.ReadTotalTimeoutMultiplier = 0; } @@ -78,6 +75,10 @@ bool SetTimeouts(serial_socket_t socket, int* iError, bool bBlocking) *iError = GetLastError(); return false; } + else + { + m_iCurrentReadTimeout = iTimeoutMs; + } return true; } @@ -103,7 +104,13 @@ ssize_t CSerialSocket::Write(void* data, size_t len) ssize_t CSerialSocket::Read(void* data, size_t len, uint64_t iTimeoutMs /* = 0 */) { - return IsOpen() ? SerialSocketRead(m_socket, &m_iError, data, len, iTimeoutMs) : -1; + DWORD dwTimeoutMs((DWORD)iTimeoutMs); + if (iTimeoutMs != (uint64_t)iTimeoutMs) + dwTimeoutMs = MAXDWORD; + + return IsOpen() && SetTimeouts(m_socket, &m_iError, dwTimeoutMs) ? + SerialSocketRead(m_socket, &m_iError, data, len, iTimeoutMs) : + -1; } bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */) @@ -153,7 +160,7 @@ bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */) return false; } - if (!SetTimeouts(m_socket, &m_iError, false)) + if (!SetTimeouts(m_socket, &m_iError, 0)) { m_strError = "unable to set timeouts"; FormatWindowsError(GetLastError(), m_strError);