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 /* Needed on Mac OS/X */
45 #define SOL_TCP IPPROTO_TCP
48 // Common socket operations
52 class ISocket
: public PreventCopy
56 virtual ~ISocket(void) {}
58 virtual bool Open(uint64_t iTimeoutMs
= 0) = 0;
59 virtual void Close(void) = 0;
60 virtual void Shutdown(void) = 0;
61 virtual bool IsOpen(void) = 0;
62 virtual ssize_t
Write(void* data
, size_t len
) = 0;
63 virtual ssize_t
Read(void* data
, size_t len
, uint64_t iTimeoutMs
= 0) = 0;
64 virtual CStdString
GetError(void) = 0;
65 virtual int GetErrorNumber(void) = 0;
66 virtual CStdString
GetName(void) = 0;
69 template <typename _SType
>
70 class CCommonSocket
: public ISocket
73 CCommonSocket(_SType initialSocketValue
, const CStdString
&strName
) :
74 m_socket(initialSocketValue
),
78 virtual ~CCommonSocket(void) {}
80 virtual CStdString
GetError(void)
83 strError
= m_strError
.IsEmpty() && m_iError
!= 0 ? strerror(m_iError
) : m_strError
;
87 virtual int GetErrorNumber(void)
92 virtual CStdString
GetName(void)
101 CStdString m_strError
;
102 CStdString m_strName
;
107 template <typename _Socket
>
108 class CProtectedSocket
: public ISocket
111 CProtectedSocket(_Socket
*socket
) :
115 virtual ~CProtectedSocket(void)
121 virtual bool Open(uint64_t iTimeoutMs
= 0)
124 if (m_socket
&& WaitReady())
126 bReturn
= m_socket
->Open(iTimeoutMs
);
132 virtual void Close(void)
134 if (m_socket
&& WaitReady())
141 virtual void Shutdown(void)
143 if (m_socket
&& WaitReady())
145 m_socket
->Shutdown();
150 virtual bool IsOpen(void)
152 CLockObject
lock(m_mutex
);
153 return m_socket
&& m_socket
->IsOpen();
156 virtual bool IsBusy(void)
158 CLockObject
lock(m_mutex
);
159 return m_socket
&& m_iUseCount
> 0;
162 virtual int GetUseCount(void)
164 CLockObject
lock(m_mutex
);
168 virtual ssize_t
Write(void* data
, size_t len
)
170 if (!m_socket
|| !WaitReady())
173 ssize_t iReturn
= m_socket
->Write(data
, len
);
179 virtual ssize_t
Read(void* data
, size_t len
, uint64_t iTimeoutMs
= 0)
181 if (!m_socket
|| !WaitReady())
184 ssize_t iReturn
= m_socket
->Read(data
, len
, iTimeoutMs
);
190 virtual CStdString
GetError(void)
193 CLockObject
lock(m_mutex
);
194 strError
= m_socket
? m_socket
->GetError() : "";
198 virtual int GetErrorNumber(void)
200 CLockObject
lock(m_mutex
);
201 return m_socket
? m_socket
->GetErrorNumber() : EINVAL
;
204 virtual CStdString
GetName(void)
207 CLockObject
lock(m_mutex
);
208 strName
= m_socket
? m_socket
->GetName() : "";
215 CLockObject
lock(m_mutex
);
217 m_condition
.Wait(m_mutex
);
228 CLockObject
lock(m_mutex
);
231 m_condition
.Broadcast();
236 CCondition m_condition
;