win32: implemented timeouts in serial socket reads
authorLars Op den Kamp <lars@opdenkamp.eu>
Thu, 29 Mar 2012 09:53:41 +0000 (11:53 +0200)
committerLars Op den Kamp <lars@opdenkamp.eu>
Thu, 29 Mar 2012 09:53:41 +0000 (11:53 +0200)
src/lib/platform/sockets/serialport.h
src/lib/platform/windows/os-socket.h
src/lib/platform/windows/serialport.cpp

index bdd05b96f3edc0ebf1ab9946a4f69f200c9cd3e7..fb4be19d4ec650856defb3d59046510f851f942c 100644 (file)
@@ -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<serial_socket_t>(INVALID_SERIAL_SOCKET_VALUE, strName),
+#ifdef __WINDOWS__
+          m_iCurrentTimeout(0),
+#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 iTimeout);
+      DWORD           m_iCurrentTimeout;
   #endif
 
       bool            m_bIsOpen;
index 5174cba1aaa5e5db4535a345a844c10d005d47b2..86750c50b9bc4e51d5abe56d8ee8a09f253a4341 100644 (file)
@@ -95,6 +95,8 @@ namespace PLATFORM
 
   inline ssize_t SerialSocketRead(serial_socket_t socket, int *iError, void* data, size_t len, uint64_t iTimeoutMs /*= 0*/)
   {
+    *iError = 0;
+
     if (len != (DWORD)len)
     {
       *iError = EINVAL;
index c0cdd936af9e586991ac2e6b463ca2df1674abc8..fe917255d5ad2e921b4ce628077ea42d57027c49 100644 (file)
@@ -48,11 +48,14 @@ 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 iTimeout)
 {
   if (socket == INVALID_HANDLE_VALUE)
          return false;
 
+  if (iTimeout == m_iCurrentTimeout)
+    return true;
+
   COMMTIMEOUTS cto;
   if (!GetCommTimeouts(socket, &cto))
   {
@@ -60,18 +63,9 @@ bool SetTimeouts(serial_socket_t socket, int* iError, bool bBlocking)
     return false;
   }
 
-  if (bBlocking)
-  {
-    cto.ReadIntervalTimeout         = 0;
-    cto.ReadTotalTimeoutConstant    = 0;
-    cto.ReadTotalTimeoutMultiplier  = 0;
-  }
-  else
-  {
-    cto.ReadIntervalTimeout         = MAXDWORD;
-    cto.ReadTotalTimeoutConstant    = 0;
-    cto.ReadTotalTimeoutMultiplier  = 0;
-  }
+  cto.ReadIntervalTimeout         = 0;
+  cto.ReadTotalTimeoutConstant    = iTimeout;
+  cto.ReadTotalTimeoutMultiplier  = 0;
 
   if (!SetCommTimeouts(socket, &cto))
   {
@@ -79,6 +73,7 @@ bool SetTimeouts(serial_socket_t socket, int* iError, bool bBlocking)
     return false;
   }
 
+  m_iCurrentTimeout = iTimeout;
   return true;
 }
 
@@ -103,7 +98,20 @@ 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;
+  if (IsOpen())
+  {
+    DWORD iTimeout((DWORD)iTimeoutMs);
+    if (iTimeout != iTimeoutMs)
+      return -1;
+
+    int iError(0);
+    if (!SetTimeouts(m_socket, &iError, iTimeout))
+      return -1;
+
+    return SerialSocketRead(m_socket, &m_iError, data, len, iTimeoutMs);
+  }
+
+  return -1;
 }
 
 bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */)
@@ -153,7 +161,7 @@ bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */)
     return false;
   }
 
-  if (!SetTimeouts(m_socket, &m_iError, false))
+  if (!SetTimeouts(m_socket, &m_iError, MAXDWORD))
   {
     m_strError = "unable to set timeouts";
     FormatWindowsError(GetLastError(), m_strError);