3 * This file is part of the libCEC(R) library.
5 * libCEC(R) is Copyright (C) 2011-2012 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 "../threads/mutex.h"
35 #include "../util/StdString.h"
37 #if defined(__WINDOWS__)
38 #include "../windows/os-socket.h"
40 #include "../posix/os-socket.h"
43 // Common socket operations
47 class ISocket
: public PreventCopy
51 virtual ~ISocket(void) {}
53 virtual bool Open(uint64_t iTimeoutMs
= 0) = 0;
54 virtual void Close(void) = 0;
55 virtual void Shutdown(void) = 0;
56 virtual bool IsOpen(void) = 0;
57 virtual ssize_t
Write(void* data
, size_t len
) = 0;
58 virtual ssize_t
Read(void* data
, size_t len
, uint64_t iTimeoutMs
= 0) = 0;
59 virtual CStdString
GetError(void) = 0;
60 virtual int GetErrorNumber(void) = 0;
61 virtual CStdString
GetName(void) = 0;
64 template <typename _SType
>
65 class CCommonSocket
: public ISocket
68 CCommonSocket(_SType initialSocketValue
, const CStdString
&strName
) :
69 m_socket(initialSocketValue
),
73 virtual ~CCommonSocket(void) {}
75 virtual CStdString
GetError(void)
78 strError
= m_strError
.IsEmpty() && m_iError
!= 0 ? strerror(m_iError
) : m_strError
;
82 virtual int GetErrorNumber(void)
87 virtual CStdString
GetName(void)
96 CStdString m_strError
;
102 template <typename _Socket
>
103 class CProtectedSocket
: public ISocket
106 CProtectedSocket(_Socket
*socket
) :
110 virtual ~CProtectedSocket(void)
116 virtual bool Open(uint64_t iTimeoutMs
= 0)
119 if (m_socket
&& WaitReady())
121 bReturn
= m_socket
->Open(iTimeoutMs
);
127 virtual void Close(void)
129 if (m_socket
&& WaitReady())
136 virtual void Shutdown(void)
138 if (m_socket
&& WaitReady())
140 m_socket
->Shutdown();
145 virtual bool IsOpen(void)
147 CLockObject
lock(m_mutex
);
148 return m_socket
&& m_socket
->IsOpen();
151 virtual bool IsBusy(void)
153 CLockObject
lock(m_mutex
);
154 return m_socket
&& !m_bIsIdle
;
157 virtual bool IsIdle(void)
159 CLockObject
lock(m_mutex
);
160 return m_socket
&& m_bIsIdle
;
163 virtual ssize_t
Write(void* data
, size_t len
)
165 if (!m_socket
|| !WaitReady())
168 ssize_t iReturn
= m_socket
->Write(data
, len
);
174 virtual ssize_t
Read(void* data
, size_t len
, uint64_t iTimeoutMs
= 0)
176 if (!m_socket
|| !WaitReady())
179 ssize_t iReturn
= m_socket
->Read(data
, len
, iTimeoutMs
);
185 virtual CStdString
GetError(void)
188 CLockObject
lock(m_mutex
);
189 strError
= m_socket
? m_socket
->GetError() : "";
193 virtual int GetErrorNumber(void)
195 CLockObject
lock(m_mutex
);
196 return m_socket
? m_socket
->GetErrorNumber() : -EINVAL
;
199 virtual CStdString
GetName(void)
202 CLockObject
lock(m_mutex
);
203 strName
= m_socket
? m_socket
->GetName() : "";
210 CLockObject
lock(m_mutex
);
211 m_condition
.Wait(m_mutex
, m_bIsIdle
);
218 CLockObject
lock(m_mutex
);
220 m_condition
.Signal();
225 CCondition
<bool> m_condition
;