bump version numbers to 1.7.0 and updated changelog
[deb_libcec.git] / src / lib / platform / sockets / socket.h
1 #pragma once
2 /*
3 * This file is part of the libCEC(R) library.
4 *
5 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
6 * libCEC(R) is an original work, containing original code.
7 *
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 *
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.
14 *
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.
19 *
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.
23 *
24 *
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
27 *
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/
32 */
33
34 #include "../threads/mutex.h"
35 #include "../util/StdString.h"
36
37 #if defined(__WINDOWS__)
38 #include "../windows/os-socket.h"
39 #else
40 #include "../posix/os-socket.h"
41 #endif
42
43 // Common socket operations
44
45 namespace PLATFORM
46 {
47 class ISocket : public PreventCopy
48 {
49 public:
50 ISocket(void) {};
51 virtual ~ISocket(void) {}
52
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;
62 };
63
64 template <typename _SType>
65 class CCommonSocket : public ISocket
66 {
67 public:
68 CCommonSocket(_SType initialSocketValue, const CStdString &strName) :
69 m_socket(initialSocketValue),
70 m_strName(strName),
71 m_iError(0) {}
72
73 virtual ~CCommonSocket(void) {}
74
75 virtual CStdString GetError(void)
76 {
77 CStdString strError;
78 strError = m_strError.IsEmpty() && m_iError != 0 ? strerror(m_iError) : m_strError;
79 return strError;
80 }
81
82 virtual int GetErrorNumber(void)
83 {
84 return m_iError;
85 }
86
87 virtual CStdString GetName(void)
88 {
89 CStdString strName;
90 strName = m_strName;
91 return strName;
92 }
93
94 protected:
95 _SType m_socket;
96 CStdString m_strError;
97 CStdString m_strName;
98 int m_iError;
99 CMutex m_mutex;
100 };
101
102 template <typename _Socket>
103 class CProtectedSocket : public ISocket
104 {
105 public:
106 CProtectedSocket(_Socket *socket) :
107 m_socket(socket),
108 m_bIsIdle(true) {}
109
110 virtual ~CProtectedSocket(void)
111 {
112 Close();
113 delete m_socket;
114 }
115
116 virtual bool Open(uint64_t iTimeoutMs = 0)
117 {
118 bool bReturn(false);
119 if (m_socket && WaitReady())
120 {
121 bReturn = m_socket->Open(iTimeoutMs);
122 MarkReady();
123 }
124 return bReturn;
125 }
126
127 virtual void Close(void)
128 {
129 if (m_socket && WaitReady())
130 {
131 m_socket->Close();
132 MarkReady();
133 }
134 }
135
136 virtual void Shutdown(void)
137 {
138 if (m_socket && WaitReady())
139 {
140 m_socket->Shutdown();
141 MarkReady();
142 }
143 }
144
145 virtual bool IsOpen(void)
146 {
147 CLockObject lock(m_mutex);
148 return m_socket && m_socket->IsOpen();
149 }
150
151 virtual bool IsBusy(void)
152 {
153 CLockObject lock(m_mutex);
154 return m_socket && !m_bIsIdle;
155 }
156
157 virtual bool IsIdle(void)
158 {
159 CLockObject lock(m_mutex);
160 return m_socket && m_bIsIdle;
161 }
162
163 virtual ssize_t Write(void* data, size_t len)
164 {
165 if (!m_socket || !WaitReady())
166 return -EINVAL;
167
168 ssize_t iReturn = m_socket->Write(data, len);
169 MarkReady();
170
171 return iReturn;
172 }
173
174 virtual ssize_t Read(void* data, size_t len, uint64_t iTimeoutMs = 0)
175 {
176 if (!m_socket || !WaitReady())
177 return -EINVAL;
178
179 ssize_t iReturn = m_socket->Read(data, len, iTimeoutMs);
180 MarkReady();
181
182 return iReturn;
183 }
184
185 virtual CStdString GetError(void)
186 {
187 CStdString strError;
188 CLockObject lock(m_mutex);
189 strError = m_socket ? m_socket->GetError() : "";
190 return strError;
191 }
192
193 virtual int GetErrorNumber(void)
194 {
195 CLockObject lock(m_mutex);
196 return m_socket ? m_socket->GetErrorNumber() : -EINVAL;
197 }
198
199 virtual CStdString GetName(void)
200 {
201 CStdString strName;
202 CLockObject lock(m_mutex);
203 strName = m_socket ? m_socket->GetName() : "";
204 return strName;
205 }
206
207 private:
208 bool WaitReady(void)
209 {
210 CLockObject lock(m_mutex);
211 m_condition.Wait(m_mutex, m_bIsIdle);
212 m_bIsIdle = false;
213 return true;
214 }
215
216 void MarkReady(void)
217 {
218 CLockObject lock(m_mutex);
219 m_bIsIdle = true;
220 m_condition.Signal();
221 }
222
223 _Socket * m_socket;
224 CMutex m_mutex;
225 CCondition<bool> m_condition;
226 bool m_bIsIdle;
227 };
228 };