3 * This file is part of the libCEC(R) library.
5 * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
6 * libCEC(R) is an original work, containing original code.
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
10 * This program is dual-licensed; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
28 * For more information contact:
29 * Pulse-Eight Licensing <license@pulse-eight.com>
30 * http://www.pulse-eight.com/
31 * http://www.pulse-eight.net/
34 #include "lib/platform/threads/mutex.h"
36 #if defined(__WINDOWS__)
37 #include "lib/platform/windows/os-socket.h"
39 #include "lib/platform/posix/os-socket.h"
44 // Common socket operations
48 class ISocket
: public PreventCopy
52 virtual ~ISocket(void) {}
54 virtual bool Open(uint64_t iTimeoutMs
= 0) = 0;
55 virtual void Close(void) = 0;
56 virtual void Shutdown(void) = 0;
57 virtual bool IsOpen(void) = 0;
58 virtual ssize_t
Write(void* data
, size_t len
) = 0;
59 virtual ssize_t
Read(void* data
, size_t len
, uint64_t iTimeoutMs
= 0) = 0;
60 virtual std::string
GetError(void) = 0;
61 virtual int GetErrorNumber(void) = 0;
62 virtual std::string
GetName(void) = 0;
65 template <typename _SType
>
66 class CCommonSocket
: public ISocket
69 CCommonSocket(_SType initialSocketValue
, const std::string
&strName
) :
70 m_socket(initialSocketValue
),
74 virtual ~CCommonSocket(void) {}
76 virtual std::string
GetError(void)
79 strError
= m_strError
.empty() && m_iError
!= 0 ? strerror(m_iError
) : m_strError
;
83 virtual int GetErrorNumber(void)
88 virtual std::string
GetName(void)
97 std::string m_strError
;
98 std::string m_strName
;
103 template <typename _Socket
>
104 class CProtectedSocket
: public ISocket
107 CProtectedSocket(_Socket
*socket
) :
111 virtual ~CProtectedSocket(void)
117 virtual bool Open(uint64_t iTimeoutMs
= 0)
120 if (m_socket
&& WaitReady())
122 bReturn
= m_socket
->Open(iTimeoutMs
);
128 virtual void Close(void)
130 if (m_socket
&& WaitReady())
137 virtual void Shutdown(void)
139 if (m_socket
&& WaitReady())
141 m_socket
->Shutdown();
146 virtual bool IsOpen(void)
148 CLockObject
lock(m_mutex
);
149 return m_socket
&& m_socket
->IsOpen();
152 virtual bool IsBusy(void)
154 CLockObject
lock(m_mutex
);
155 return m_socket
&& !m_bIsIdle
;
158 virtual bool IsIdle(void)
160 CLockObject
lock(m_mutex
);
161 return m_socket
&& m_bIsIdle
;
164 virtual ssize_t
Write(void* data
, size_t len
)
166 if (!m_socket
|| !WaitReady())
169 ssize_t iReturn
= m_socket
->Write(data
, len
);
175 virtual ssize_t
Read(void* data
, size_t len
, uint64_t iTimeoutMs
= 0)
177 if (!m_socket
|| !WaitReady())
180 ssize_t iReturn
= m_socket
->Read(data
, len
, iTimeoutMs
);
186 virtual std::string
GetError(void)
188 std::string strError
;
189 CLockObject
lock(m_mutex
);
190 strError
= m_socket
? m_socket
->GetError() : "";
194 virtual int GetErrorNumber(void)
196 CLockObject
lock(m_mutex
);
197 return m_socket
? m_socket
->GetErrorNumber() : -EINVAL
;
200 virtual std::string
GetName(void)
203 CLockObject
lock(m_mutex
);
204 strName
= m_socket
? m_socket
->GetName() : "";
211 CLockObject
lock(m_mutex
);
212 m_condition
.Wait(m_mutex
, m_bIsIdle
);
219 CLockObject
lock(m_mutex
);
221 m_condition
.Signal();
226 CCondition
<bool> m_condition
;