cec: and now proper predicates
[deb_libcec.git] / src / lib / platform / sockets / socket.h
index 9ef359679f1b0e48fb7634351d3f15cbbfe611d9..369fbe4c9f78101ba6cef915e1bff37e4737f8e2 100644 (file)
 #include "../posix/os-socket.h"
 #endif
 
-/* Needed on Mac OS/X */
-#ifndef SOL_TCP
-#define SOL_TCP IPPROTO_TCP
-#endif
-
 // Common socket operations
 
 namespace PLATFORM
@@ -110,7 +105,7 @@ namespace PLATFORM
   public:
     CProtectedSocket(_Socket *socket) :
       m_socket(socket),
-      m_iUseCount(0) {}
+      m_bIsIdle(true) {}
 
     virtual ~CProtectedSocket(void)
     {
@@ -156,19 +151,19 @@ namespace PLATFORM
     virtual bool IsBusy(void)
     {
       CLockObject lock(m_mutex);
-      return m_socket && m_iUseCount > 0;
+      return m_socket && !m_bIsIdle;
     }
 
-    virtual int GetUseCount(void)
+    virtual bool IsIdle(void)
     {
       CLockObject lock(m_mutex);
-      return m_iUseCount;
+      return m_socket && m_bIsIdle;
     }
 
     virtual ssize_t Write(void* data, size_t len)
     {
       if (!m_socket || !WaitReady())
-        return EINVAL;
+        return -EINVAL;
 
       ssize_t iReturn = m_socket->Write(data, len);
       MarkReady();
@@ -179,7 +174,7 @@ namespace PLATFORM
     virtual ssize_t Read(void* data, size_t len, uint64_t iTimeoutMs = 0)
     {
       if (!m_socket || !WaitReady())
-        return EINVAL;
+        return -EINVAL;
 
       ssize_t iReturn = m_socket->Read(data, len, iTimeoutMs);
       MarkReady();
@@ -198,7 +193,7 @@ namespace PLATFORM
     virtual int GetErrorNumber(void)
     {
       CLockObject lock(m_mutex);
-      return m_socket ? m_socket->GetErrorNumber() : EINVAL;
+      return m_socket ? m_socket->GetErrorNumber() : -EINVAL;
     }
 
     virtual CStdString GetName(void)
@@ -213,27 +208,21 @@ namespace PLATFORM
     bool WaitReady(void)
     {
       CLockObject lock(m_mutex);
-      if (m_iUseCount > 0)
-        m_condition.Wait(m_mutex);
-
-      if (m_iUseCount > 0)
-        return false;
-
-      ++m_iUseCount;
+      m_condition.Wait(m_mutex, m_bIsIdle);
+      m_bIsIdle = false;
       return true;
     }
 
     void MarkReady(void)
     {
       CLockObject lock(m_mutex);
-      if (m_iUseCount > 0)
-        --m_iUseCount;
-      m_condition.Broadcast();
+      m_bIsIdle = true;
+      m_condition.Signal();
     }
 
-    _Socket   *m_socket;
-    CMutex     m_mutex;
-    CCondition m_condition;
-    int        m_iUseCount;
+    _Socket *          m_socket;
+    CMutex             m_mutex;
+    CCondition<bool &> m_condition;
+    bool               m_bIsIdle;
   };
 };